Debian Codegraph @

vmdk/vmdk-fuse.c

raw symbols SCIP
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <[email protected]>
4 Copyright (C) 2014 Broadcom
5
6 This program can be distributed under the terms of the GNU GPLv2.
7 See the file COPYING.
8 */
9
10 /** @file
11 *
12 * This "filesystem" provides only a single file. The mountpoint
13 * needs to be a file rather than a directory.
14 */
15
16
17 #define FUSE_USE_VERSION 31
18
19 #include <fuse.h>
20 #include <fuse_lowlevel.h>
21
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <errno.h>
29
30 #include "diskinfo.h"
31
32 char *toolsVersion = "2147483647";
33
34 struct options {
35 char *vmdk_path;
36 };
37
38 #define OPTION(t, p) { t, offsetof(struct options, p), 1 }
39 static const struct fuse_opt option_spec[] = {
40 OPTION("--file=%s", vmdk_path),
41 FUSE_OPT_END
42 };
43
44 struct vmdk_data {
45 struct options options;
46 off_t capacity;
47 };
48
49 static int vmdk_getattr(const char *path, struct stat *stbuf,
50 struct fuse_file_info *fi)
51 {
52 (void) fi;
53 struct stat st;
54 struct vmdk_data *data = (struct vmdk_data *)fuse_get_context()->private_data;
55 char *vmdk_path = data->options.vmdk_path;
56
57 if(strcmp(path, "/") != 0)
58 return -ENOENT;
59
60 if (stat(vmdk_path, &st) != 0) {
61 return -errno;
62 }
63
64 stbuf->st_mode = st.st_mode;
65 stbuf->st_nlink = 1;
66 stbuf->st_uid = getuid();
67 stbuf->st_gid = getgid();
68 stbuf->st_size = data->capacity;
69 stbuf->st_blocks = 0;
70 stbuf->st_atime = st.st_atime;
71 stbuf->st_mtime = st.st_mtime;
72 stbuf->st_ctime = st.st_ctime;
73
74 return 0;
75 }
76
77 static int vmdk_truncate(const char *path, off_t size,
78 struct fuse_file_info *fi)
79 {
80 (void) size;
81 (void) fi;
82
83 if(strcmp(path, "/") != 0)
84 return -ENOENT;
85
86 return -EROFS;
87 }
88
89 static int vmdk_open(const char *path, struct fuse_file_info *fi)
90 {
91 (void) fi;
92
93 if(strcmp(path, "/") != 0)
94 return -ENOENT;
95
96 struct vmdk_data *data = (struct vmdk_data *)fuse_get_context()->private_data;
97 if (data == NULL) {
98 return -EINVAL; // Invalid configuration
99 }
100
101 DiskInfo *di = Sparse_Open(data->options.vmdk_path);
102 if (di == NULL) {
103 fprintf(stderr, "could not read %s\n", data->options.vmdk_path);
104 return -EINVAL; // File exists but invalid VMDK format
105 }
106
107 fi->fh = (uint64_t)di;
108
109 return 0;
110 }
111
112 static int vmdk_release(const char *path, struct fuse_file_info *fi)
113 {
114 if(strcmp(path, "/") != 0)
115 return -ENOENT;
116
117 DiskInfo *di = (DiskInfo *)(fi->fh);
118 if (di == NULL)
119 return -EINVAL; // Invalid file handle
120
121 di->vmt->close(di);
122
123 return 0;
124 }
125
126 static int vmdk_read(const char *path, char *buf, size_t size,
127 off_t offset, struct fuse_file_info *fi)
128 {
129 if(strcmp(path, "/") != 0)
130 return -ENOENT;
131
132 DiskInfo *di = (DiskInfo *)(fi->fh);
133 if (di == NULL)
134 return -EINVAL; // Invalid file handle
135
136 if (buf == NULL)
137 return -EINVAL; // Invalid buffer
138
139 ssize_t ret;
140 if ((ret = di->vmt->pread(di, (void *)buf, size, offset)) < 0) {
141 fprintf(stderr, "pread failed: %d (%s)\n", errno, strerror(errno));
142 return -errno; // Return the actual error from pread
143 }
144
145 return ret;
146 }
147
148 static int vmdk_write(const char *path, const char *buf, size_t size,
149 off_t offset, struct fuse_file_info *fi)
150 {
151 (void) buf;
152 (void) offset;
153 (void) fi;
154 (void) size;
155
156 if(strcmp(path, "/") != 0)
157 return -ENOENT;
158
159 return -EROFS;
160 }
161
162 static const struct fuse_operations vmdk_oper = {
163 .getattr = vmdk_getattr,
164 .truncate = vmdk_truncate,
165 .open = vmdk_open,
166 .release = vmdk_release,
167 .read = vmdk_read,
168 .write = vmdk_write,
169 };
170
171 int vmdk_init(struct vmdk_data *data)
172 {
173 DiskInfo *di;
174
175 if (data == NULL || data->options.vmdk_path == NULL) {
176 fprintf(stderr, "Invalid data or vmdk_path\n");
177 return -EINVAL;
178 }
179
180 di = Sparse_Open(data->options.vmdk_path);
181 if (di == NULL) {
182 fprintf(stderr, "could not read %s\n", data->options.vmdk_path);
183 return -EINVAL; // File exists but invalid VMDK format
184 }
185 data->capacity = di->vmt->getCapacity(di);
186 di->vmt->close(di);
187
188 return 0;
189 }
190
191 static
192 int vmdk_opt_proc(void *data, const char *arg,
193 int key, struct fuse_args *outargs)
194 {
195 (void) outargs;
196 struct vmdk_data *vmdk_data = (struct vmdk_data *)data;
197
198 switch (key) {
199 case FUSE_OPT_KEY_NONOPT:
200 if (vmdk_data->options.vmdk_path == NULL) {
201 vmdk_data->options.vmdk_path = strdup(arg);
202 if (vmdk_data->options.vmdk_path == NULL) {
203 fprintf(stderr, "Memory allocation failed for vmdk_path\n");
204 return -ENOMEM; // Out of memory
205 }
206 return 0;
207 }
208 return 1;
209 }
210 return 1;
211 }
212
213 int main(int argc, char *argv[])
214 {
215 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
216 struct stat stbuf;
217 struct vmdk_data data = {0};
218 int argc_saved;
219 char **argv_saved;
220
221 if (fuse_opt_parse(&args, &data, option_spec, vmdk_opt_proc) == -1) {
222 fuse_opt_free_args(&args);
223 if (data.options.vmdk_path) {
224 free(data.options.vmdk_path);
225 }
226 return 1;
227 }
228
229 if (!data.options.vmdk_path) {
230 fprintf(stderr, "missing vmdk file parameter (file=)\n");
231 return 1;
232 } else {
233 char *tmp = data.options.vmdk_path;
234 data.options.vmdk_path = realpath(data.options.vmdk_path, NULL);
235 if (data.options.vmdk_path == NULL) {
236 fprintf(stderr, "failed to resolve vmdk file path: %s\n", strerror(errno));
237 free(tmp);
238 return 1;
239 }
240 free(tmp);
241 }
242 if (stat(data.options.vmdk_path, &stbuf) == -1) {
243 fprintf(stderr ,"failed to access vmdk file %s: %s\n",
244 data.options.vmdk_path, strerror(errno));
245 free(data.options.vmdk_path);
246 return 1;
247 }
248 if (!S_ISREG(stbuf.st_mode)) {
249 fprintf(stderr, "vmdk file %s is not a regular file\n", data.options.vmdk_path);
250 free(data.options.vmdk_path);
251 return 1;
252 }
253
254 argc_saved = args.argc;
255 argv_saved = args.argv;
256
257 if (vmdk_init(&data) != 0) {
258 free(data.options.vmdk_path);
259 return 1;
260 }
261
262 int ret = fuse_main(argc_saved, argv_saved, &vmdk_oper, (void *)&data);
263
264 // Clean up resources
265 free(data.options.vmdk_path);
266 fuse_opt_free_args(&args);
267 return ret;
268 }