-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartitionfs.c
406 lines (336 loc) · 10.1 KB
/
partitionfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <fuse.h>
#include <parted/parted.h>
typedef struct Partition_s
{
off_t start;
off_t size;
uint8_t boot_indicator;
uint8_t type;
} Partition;
static time_t timestamp = 0;
static PedDevice *device = NULL;
static PedDisk *disk = NULL;
static uint8_t *sector_buffer = NULL;
static PedPartition *partition_from_path(const char *path)
{
if (path[0] == '/')
{
char *endptr;
long int n;
n = strtol(path+1, &endptr, 10);
if ((endptr != (path+1)) && (*endptr == '\0'))
{
PedPartition *p = ped_disk_get_partition(disk, n);
/* Include primary and logical partitions only */
return ((p->type & ~PED_PARTITION_LOGICAL) == 0) ? p : NULL;
}
}
// Invalid path
return NULL;
}
static int getattr_impl(const char *path, struct stat *stbuf)
{
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0)
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
stbuf->st_atime = timestamp;
stbuf->st_mtime = timestamp;
stbuf->st_ctime = timestamp;
return 0;
}
else
{
PedPartition *p = partition_from_path(path);
if (p != NULL)
{
stbuf->st_mode = S_IFREG | (device->read_only ? 0444 : 0644);
stbuf->st_nlink = 1;
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
stbuf->st_size = p->geom.length * device->sector_size;
stbuf->st_blocks = (stbuf->st_size + 511) / 512;
stbuf->st_atime = timestamp;
stbuf->st_mtime = timestamp;
stbuf->st_ctime = timestamp;
return 0;
}
}
return -ENOENT;
}
static int readdir_impl(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
char szBuffer[10];
int i;
if (strcmp(path, "/") != 0)
{
return -ENOENT;
}
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
PedPartition *p = NULL;
while ((p = ped_disk_next_partition(disk, p)) != NULL)
{
/* Include only primary and logical partitions in listing. */
if ((p->type & ~PED_PARTITION_LOGICAL) == 0)
{
snprintf(szBuffer, sizeof(szBuffer), "%u", p->num);
filler(buf, szBuffer, NULL, 0);
}
}
return 0;
}
static int open_impl(const char *path, struct fuse_file_info *fi)
{
PedPartition *p = partition_from_path(path);
if (p)
{
fi->fh = (uint64_t)((uintptr_t)p);
if (((fi->flags & O_WRONLY) != 0) || ((fi->flags & O_RDWR) != 0))
{
return device->read_only ? -EACCES : 0;
}
else
{
return 0;
}
}
else
{
return -ENOENT;
}
}
static int read_impl(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
const PedPartition *p = (const PedPartition*)((uintptr_t)fi->fh);
if (p)
{
PedSector sector_start = p->geom.start + (offset / device->sector_size);
long long sector_offset = offset % device->sector_size;
size_t size_left = size;
if (sector_start > p->geom.end)
return 0;
if (sector_offset != 0)
{
if (!ped_device_read(device, (void*)sector_buffer, sector_start, 1))
{
return -EIO;
}
long long read_amount = device->sector_size - sector_offset;
if (read_amount > size_left)
read_amount = size_left;
memcpy(buf, sector_buffer + sector_offset, read_amount);
buf += read_amount;
size_left -= read_amount;
sector_start++;
}
if ((size_left >= device->sector_size) && (sector_start <= p->geom.end))
{
PedSector sectors_left = size_left / device->sector_size;
if ((sector_start + sectors_left) > p->geom.end)
{
sectors_left = p->geom.end - sector_start + 1;
}
if (!ped_device_read(device, (void*)buf, sector_start, sectors_left))
{
return -EIO;
}
sector_start += sectors_left;
long long read_amount = sectors_left * device->sector_size;
buf += read_amount;
size_left -= read_amount;
}
if ((size_left != 0) && (sector_start <= p->geom.end))
{
if (!ped_device_read(device, (void*)sector_buffer, sector_start, 1))
{
return -EIO;
}
memcpy(buf, sector_buffer, size_left);
size_left = 0;
}
return size - size_left;
}
else
{
return -ENOENT;
}
}
static int write_impl(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
const PedPartition *p = (const PedPartition*)((uintptr_t)fi->fh);
if (p)
{
PedSector sector_start = p->geom.start + (offset / device->sector_size);
long long sector_offset = offset % device->sector_size;
size_t size_left = size;
if (sector_start > p->geom.end)
return 0;
if (sector_offset != 0)
{
long long write_amount = device->sector_size - sector_offset;
if (write_amount > size_left)
write_amount = size_left;
if (!ped_device_read(device, (void*)sector_buffer, sector_start, 1))
{
return -EIO;
}
memcpy(sector_buffer + sector_offset, buf, write_amount);
if (!ped_device_write(device, (const void*)sector_buffer, sector_start, 1))
{
return -EIO;
}
buf += write_amount;
size_left -= write_amount;
sector_start++;
}
if ((size_left >= device->sector_size) && (sector_start <= p->geom.end))
{
PedSector sectors_left = size_left / device->sector_size;
if ((sector_start + sectors_left) > p->geom.end)
{
sectors_left = p->geom.end - sector_start + 1;
}
if (!ped_device_write(device, (const void*)buf, sector_start, sectors_left))
{
return -EIO;
}
sector_start += sectors_left;
long long write_amount = sectors_left * device->sector_size;
buf += write_amount;
size_left -= write_amount;
}
if ((size_left != 0) && (sector_start <= p->geom.end))
{
if (!ped_device_read(device, (void*)sector_buffer, sector_start, 1))
{
return -EIO;
}
memcpy(sector_buffer, buf, size_left);
if (!ped_device_write(device, (const void*)sector_buffer, sector_start, 1))
{
return -EIO;
}
size_left = 0;
}
return size - size_left;
}
else
{
return -ENOENT;
}
}
int truncate_impl(const char *path, off_t new_size)
{
/* Fixed size; ignore truncate requests */
return 0;
}
static struct fuse_operations callbacks = {
.getattr = getattr_impl,
.readdir = readdir_impl,
.open = open_impl,
.read = read_impl,
.write = write_impl,
.truncate = truncate_impl,
};
static PedExceptionOption ped_handler_impl(PedException *ex)
{
if (ex->options & PED_EXCEPTION_IGNORE)
{
/* Suppress anything that can be ignored. */
return PED_EXCEPTION_IGNORE;
}
else
{
return PED_EXCEPTION_UNHANDLED;
}
}
static void usage()
{
fprintf(stderr, "usage: partitionfs source mount_point [options]\n\n");
fprintf(stderr, "general options:\n");
fprintf(stderr, " -o opt[,opt...] mount options\n");
fprintf(stderr, " -h --help print help\n");
fprintf(stderr, " -V --version print version\n\n");
fprintf(stderr, "FUSE options:\n");
fprintf(stderr, " -d -o debug enable debug output (implies -f)\n");
fprintf(stderr, " -f foreground operation\n");
fprintf(stderr, " -s disable multi-threaded operation\n\n");
fprintf(stderr, " -o allow_other allow access to other users\n");
fprintf(stderr, " -o allow_root allow access to root\n");
fprintf(stderr, " -o nonempty allow mounts over non-empty file/dir\n");
fprintf(stderr, " -o default_permissions enable permission check by kernel\n");
fprintf(stderr, " -o max_read set maximum size of read requests\n");
fprintf(stderr, " -o max_write set maximum size of write requests\n");
fprintf(stderr, " -o async_read perform reads asynchronously (default)\n");
fprintf(stderr, " -o sync_read perform reads synchronously\n");
}
static int opt_proc_impl(void *data, const char *arg, int key,
struct fuse_args *outargs)
{
if ((strcmp(arg, "--help") == 0) || (strcmp(arg, "-h") == 0))
{
usage();
exit(0);
}
if ((strcmp(arg, "--version") == 0) || (strcmp(arg, "-V") == 0))
{
fprintf(stderr, "partitionfs version 0.5\n");
exit(0);
}
if (arg[0] != '-' && (device == NULL))
{
device = ped_device_get(arg);
return (device == NULL) ? -1 : 0;
}
return 1;
}
int main(int argc, char *argv[])
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
ped_exception_set_handler(ped_handler_impl);
atexit(ped_device_free_all);
if (fuse_opt_parse(&args, NULL, NULL, opt_proc_impl) != 0)
{
usage();
return 1;
}
if (device == NULL || !ped_device_open(device))
{
fprintf(stderr, "Failed to open input file.\n");
return 2;
}
disk = ped_disk_new(device);
if (disk == NULL)
{
fprintf(stderr, "Input file does not contain a partition table.\n");
return 3;
}
sector_buffer = (uint8_t*)malloc(device->sector_size);
if (sector_buffer == NULL)
{
fprintf(stderr, "Out of memory.\n");
return 4;
}
umask(0);
timestamp = time(NULL);
return fuse_main(args.argc, args.argv, &callbacks);
}