forked from dlbeer/ufat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
766 lines (633 loc) · 16.8 KB
/
Copy pathmain.c
File metadata and controls
766 lines (633 loc) · 16.8 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/* uFAT -- small flexible VFAT implementation
Copyright (C) 2012 TracMap Holdings Ltd
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include "ufat.h"
struct command;
#define OPTION_STATISTICS 0x01
#define OPTION_RANDOMIZE 0x02
#define OPTION_MKFS 0x04
struct options {
int flags;
unsigned int log2_bs;
unsigned int seed;
ufat_block_t num_blocks;
const char *filename;
const struct command *command;
char **argv;
int argc;
};
struct command {
const char *name;
int (*func)(struct ufat *uf,
const struct options *opt);
};
struct file_device {
struct ufat_device base;
FILE *f;
int is_read_only;
};
static int file_device_read(const struct ufat_device *dev, ufat_block_t start,
ufat_block_t count, unsigned char *buffer)
{
struct file_device *f = (struct file_device *)dev;
if (fseek(f->f, start << f->base.log2_block_size, SEEK_SET) < 0) {
perror("file_device_read: fseek");
return -1;
}
if (fread(buffer, 1 << f->base.log2_block_size,
count, f->f) != count) {
if (feof(f->f)) {
memset(buffer, 0xcc,
(1 << f->base.log2_block_size) * count);
} else {
perror("file_device_read: fread");
return -1;
}
}
return 0;
}
static int file_device_write(const struct ufat_device *dev, ufat_block_t start,
ufat_block_t count, const unsigned char *buffer)
{
struct file_device *f = (struct file_device *)dev;
if (f->is_read_only) {
fprintf(stderr, "file_device_write: read-only device\n");
return -1;
}
if (fseek(f->f, start << f->base.log2_block_size, SEEK_SET) < 0) {
perror("file_device_write: fseek");
return -1;
}
if (fwrite(buffer, 1 << f->base.log2_block_size,
count, f->f) != count) {
perror("file_device_write: fwrite");
return -1;
}
return 0;
}
static int file_device_open(struct file_device *dev, const char *fname,
unsigned int log2_bs, int create)
{
dev->base.log2_block_size = log2_bs;
dev->base.read = file_device_read;
dev->base.write = file_device_write;
dev->is_read_only = 0;
if (create) {
dev->f = fopen(fname, "wb+");
} else {
dev->f = fopen(fname, "rb+");
if (!dev->f && errno == EACCES) {
dev->is_read_only = 1;
dev->f = fopen(fname, "rb");
}
}
if (!dev->f) {
perror("open");
return -1;
}
return 0;
}
static void file_device_close(struct file_device *dev)
{
fclose(dev->f);
}
static void print_date(ufat_date_t d)
{
printf("%04d-%02d-%02d",
UFAT_DATE_Y(d), UFAT_DATE_M(d), UFAT_DATE_D(d));
}
static void print_time(ufat_time_t t)
{
printf("%2d:%02d:%02d",
UFAT_TIME_H(t), UFAT_TIME_M(t), UFAT_TIME_S(t));
}
static void print_attributes(ufat_attr_t a)
{
printf("%c%c%c%c%c",
(a & UFAT_ATTR_ARCHIVE) ? 'A' : ' ',
(a & UFAT_ATTR_SYSTEM) ? 'S' : ' ',
(a & UFAT_ATTR_HIDDEN) ? 'H' : ' ',
(a & UFAT_ATTR_READONLY) ? 'R' : ' ',
(a & UFAT_ATTR_DIRECTORY) ? 'D' : ' ');
}
static int list_dir(struct ufat_directory *dir)
{
for (;;) {
struct ufat_dirent inf;
char lfn[UFAT_LFN_MAX_UTF8];
int err;
err = ufat_dir_read(dir, &inf, lfn, sizeof(lfn));
if (err < 0) {
fprintf(stderr, "list_dir: ufat_dir_read: %s\n",
ufat_strerror(err));
return err;
}
if (err)
break;
print_date(inf.modify_date);
printf(" ");
print_time(inf.modify_time);
printf(" ");
print_attributes(inf.attributes & ~UFAT_ATTR_DIRECTORY);
if (inf.attributes & UFAT_ATTR_DIRECTORY)
printf(" %9s", "<DIR>");
else
printf(" %9u", inf.file_size);
printf(" %s\n", lfn);
}
return 0;
}
static int cmd_dir(struct ufat *uf, const struct options *opt)
{
struct ufat_directory dir;
ufat_open_root(uf, &dir);
if (opt->argc) {
struct ufat_dirent ent;
int err = ufat_dir_find_path(&dir, opt->argv[0], &ent, NULL);
if (err < 0) {
fprintf(stderr, "ufat_dir_find_path: %s\n",
ufat_strerror(err));
return -1;
}
if (err) {
fprintf(stderr, "No such file or directory: %s\n",
opt->argv[0]);
return -1;
}
err = ufat_open_subdir(uf, &dir, &ent);
if (err < 0) {
fprintf(stderr, "ufat_open_subdir: %s\n",
ufat_strerror(err));
return -1;
}
}
return list_dir(&dir);
}
static int cmd_fstat(struct ufat *uf, const struct options *opt)
{
struct ufat_directory dir;
struct ufat_dirent ent;
int err;
if (!opt->argc) {
fprintf(stderr, "You must specify a file path\n");
return -1;
}
ufat_open_root(uf, &dir);
err = ufat_dir_find_path(&dir, opt->argv[0], &ent, NULL);
if (err < 0) {
fprintf(stderr, "ufat_dir_find_path: %s\n",
ufat_strerror(err));
return -1;
}
if (err) {
fprintf(stderr, "No such file or directory: %s\n",
opt->argv[0]);
return -1;
}
printf("Entry block/offset: %llu/%d\n",
ent.dirent_block, ent.dirent_pos);
if (ent.lfn_block != UFAT_BLOCK_NONE)
printf("LFN start block/offset: %llu/%d\n",
ent.lfn_block, ent.lfn_pos);
printf("Short name: %s", ent.short_name);
if (ent.short_ext[0])
printf(".%s\n", ent.short_ext);
else
printf("\n");
printf("Attributes: 0x%02x (", ent.attributes);
print_attributes(ent.attributes);
printf(")\n");
printf("Creation date/time: ");
print_date(ent.create_date);
printf(" ");
print_time(ent.create_time);
printf("\n");
printf("Modification date/time: ");
print_date(ent.modify_date);
printf(" ");
print_time(ent.modify_time);
printf("\n");
printf("Last access date: ");
print_date(ent.access_date);
printf("\n");
printf("First cluster: %u\n", ent.first_cluster);
printf("Size: %u\n", ent.file_size);
return 0;
}
static int cmd_read(struct ufat *uf, const struct options *opt)
{
struct ufat_directory dir;
struct ufat_dirent ent;
struct ufat_file file;
int err;
if (!opt->argc) {
fprintf(stderr, "You must specify a file path\n");
return -1;
}
ufat_open_root(uf, &dir);
err = ufat_dir_find_path(&dir, opt->argv[0], &ent, NULL);
if (err < 0) {
fprintf(stderr, "ufat_dir_find_path: %s\n",
ufat_strerror(err));
return -1;
}
if (err) {
fprintf(stderr, "No such file or directory: %s\n",
opt->argv[0]);
return -1;
}
err = ufat_open_file(uf, &file, &ent);
if (err < 0) {
fprintf(stderr, "ufat_open_file: %s\n", ufat_strerror(err));
return -1;
}
for (;;) {
char buf[16384];
int req_size = sizeof(buf);
int len;
if (opt->flags & OPTION_RANDOMIZE)
req_size = random() % sizeof(buf) + 1;
len = ufat_file_read(&file, buf, req_size);
if (len < 0) {
fprintf(stderr, "ufat_file_read: %s\n",
ufat_strerror(len));
return -1;
}
if (!len)
break;
if (fwrite(buf, 1, len, stdout) != len) {
perror("fwrite");
return -1;
}
}
return 0;
}
static int cmd_write(struct ufat *uf, const struct options *opt)
{
const char *basename;
struct ufat_directory dir;
struct ufat_dirent ent;
struct ufat_file file;
int err;
if (!opt->argc) {
fprintf(stderr, "You must specify a file path\n");
return -1;
}
ufat_open_root(uf, &dir);
err = ufat_dir_find_path(&dir, opt->argv[0], &ent, &basename);
if (err < 0) {
fprintf(stderr, "ufat_dir_find_path: %s\n",
ufat_strerror(err));
return -1;
}
if (err) {
time_t now = time(NULL);
struct tm *local = localtime(&now);
ent.attributes = 0;
ent.create_date = UFAT_DATE(local->tm_year + 1900,
local->tm_mon + 1,
local->tm_mday);
ent.create_time = UFAT_TIME(local->tm_hour,
local->tm_min,
local->tm_sec);
ent.modify_date = ent.create_date;
ent.modify_time = ent.create_time;
ent.access_date = ent.create_date;
err = ufat_dir_mkfile(&dir, &ent, basename);
if (err < 0) {
fprintf(stderr, "Failed to create file: %s: %s\n",
basename, ufat_strerror(err));
return -1;
}
}
err = ufat_open_file(uf, &file, &ent);
if (err < 0) {
fprintf(stderr, "ufat_open_file: %s\n", ufat_strerror(err));
return -1;
}
for (;;) {
char buf[16384];
int req_size = sizeof(buf);
int len;
if (opt->flags & OPTION_RANDOMIZE)
req_size = random() % sizeof(buf) + 1;
len = fread(buf, 1, req_size, stdin);
if (ferror(stdin)) {
perror("fread");
return -1;
}
if (!len)
break;
len = ufat_file_write(&file, buf, len);
if (len < 0) {
fprintf(stderr, "ufat_file_write: %s\n",
ufat_strerror(len));
return -1;
}
}
err = ufat_file_truncate(&file);
if (err < 0) {
fprintf(stderr, "ufat_file_truncate: %s\n",
ufat_strerror(err));
return -1;
}
return 0;
}
static int cmd_rm(struct ufat *uf, const struct options *opt)
{
struct ufat_directory dir;
struct ufat_dirent ent;
int err;
if (!opt->argc) {
fprintf(stderr, "You must specify a file path\n");
return -1;
}
ufat_open_root(uf, &dir);
err = ufat_dir_find_path(&dir, opt->argv[0], &ent, NULL);
if (err < 0) {
fprintf(stderr, "ufat_dir_find_path: %s\n",
ufat_strerror(err));
return -1;
}
if (err) {
fprintf(stderr, "No such file or directory: %s\n",
opt->argv[0]);
return -1;
}
err = ufat_dir_delete(uf, &ent);
if (err < 0) {
fprintf(stderr, "ufat_dir_delete: %s\n", ufat_strerror(err));
return -1;
}
return 0;
}
static int cmd_mkdir(struct ufat *uf, const struct options *opt)
{
const char *basename;
struct ufat_directory dir;
struct ufat_dirent ent;
struct tm *local;
time_t now = time(NULL);
int err;
if (!opt->argc) {
fprintf(stderr, "You must specify a file path\n");
return -1;
}
ufat_open_root(uf, &dir);
err = ufat_dir_find_path(&dir, opt->argv[0], &ent, &basename);
if (err < 0) {
fprintf(stderr, "ufat_dir_find_path: %s\n",
ufat_strerror(err));
return -1;
}
if (!err) {
fprintf(stderr, "Path already exists: %s\n",
opt->argv[0]);
return -1;
}
local = localtime(&now);
ent.attributes = 0;
ent.create_date = UFAT_DATE(local->tm_year + 1900, local->tm_mon + 1,
local->tm_mday);
ent.create_time = UFAT_TIME(local->tm_hour, local->tm_min,
local->tm_sec);
ent.modify_date = ent.create_date;
ent.modify_time = ent.create_time;
ent.access_date = ent.create_date;
err = ufat_dir_create(&dir, &ent, basename);
if (err < 0) {
fprintf(stderr, "ufat_dir_create: %s\n", ufat_strerror(err));
return -1;
}
return 0;
}
static void show_info(const struct ufat_bpb *bpb)
{
printf("Type: FAT%d\n", bpb->type);
printf("Blocks per cluster: %u\n",
1 << bpb->log2_blocks_per_cluster);
printf("FAT size (blocks): %llu\n", bpb->fat_size);
printf("FAT offset (block): %llu\n", bpb->fat_start);
printf("FAT count: %u\n", bpb->fat_count);
printf("Cluster starting block: %llu\n", bpb->cluster_start);
printf("Clusters: %u\n", bpb->num_clusters);
printf("Root directory block start: %llu\n", bpb->root_start);
printf("Root directory block count: %llu\n", bpb->root_size);
printf("Root directory cluster: %u\n", bpb->root_cluster);
}
static void usage(const char *progname)
{
printf(
"Usage: %s [options] <image file> [command [args]]\n"
"\n"
"Options may be any of the following:\n"
" -b block-size Set the simulated block size\n"
" -S Show performance statistics\n"
" -R seed Randomize file IO request sizes\n"
" --mkfs <num blocks> Initialize the filesystem (WARNING: all existing data\n"
" will be lost).\n"
" --help Show this text\n"
" --version Show version information\n"
"\n"
"With no command, basic information is printed. Available commands are:\n"
" dir [directory] Show a directory listing\n"
" fstat [path] Show directory entry details\n"
" read [file] Dump the contents of the given file\n"
" write [file] Write to a file\n"
" rm [path] Remove a directory or file\n"
" mkdir [directory] Create a new empty directory\n",
progname);
}
static void version_banner(void)
{
printf(
"ufat version 20120507\n"
"Copyright (C) 2012 TracMap Holdings Ltd\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
);
}
static int parse_blocksize(const char *arg, unsigned int *out)
{
unsigned int target = atoi(arg);
unsigned int c = 0;
if (!target) {
fprintf(stderr, "Block size must be greater than 0\n");
return -1;
}
while (target > 1) {
if (target & 1) {
fprintf(stderr, "Block size must be a power of 2\n");
return -1;
}
target >>= 1;
c++;
}
*out = c;
return 0;
}
static const struct command command_table[] = {
{"dir", cmd_dir},
{"fstat", cmd_fstat},
{"read", cmd_read},
{"write", cmd_write},
{"rm", cmd_rm},
{"mkdir", cmd_mkdir}
};
static const struct command *find_command(const char *name)
{
int i;
for (i = 0; i < sizeof(command_table) /
sizeof(command_table[0]); i++) {
const struct command *c = &command_table[i];
if (!strcasecmp(c->name, name))
return c;
}
return NULL;
}
static int parse_options(int argc, char **argv, struct options *opt)
{
static const struct option longopts[] = {
{"help", 0, 0, 'H'},
{"version", 0, 0, 'V'},
{"mkfs", 1, 0, 'M'},
{NULL, 0, 0, 0}
};
int o;
memset(opt, 0, sizeof(*opt));
opt->log2_bs = 9;
while ((o = getopt_long(argc, argv, "b:SR:", longopts, NULL)) >= 0)
switch (o) {
case 'M':
opt->flags |= OPTION_MKFS;
opt->num_blocks = atoll(optarg);
break;
case 'R':
opt->flags |= OPTION_RANDOMIZE;
opt->seed = atoi(optarg);
break;
case 'S':
opt->flags |= OPTION_STATISTICS;
break;
case 'H':
usage(argv[0]);
exit(0);
case 'V':
version_banner();
exit(0);
case 'b':
if (parse_blocksize(optarg, &opt->log2_bs) < 0)
return -1;
break;
case '?':
fprintf(stderr, "Try --help for usage.\n");
return -1;
}
argc -= optind;
argv += optind;
if (argc <= 0) {
fprintf(stderr, "Expected an image name\n");
return -1;
}
opt->filename = argv[0];
if (argc >= 2) {
opt->command = find_command(argv[1]);
opt->argv = argv + 2;
opt->argc = argc - 2;
if (!opt->command) {
fprintf(stderr, "Unknown command: %s\n", argv[1]);
return -1;
}
} else {
opt->command = NULL;
opt->argv = NULL;
opt->argc = 0;
}
return 0;
}
static void dump_stats(const struct ufat_stat *st)
{
fprintf(stderr, "\n");
fprintf(stderr,
"Reads: %6d comprising %6d blocks\n",
st->read, st->read_blocks);
fprintf(stderr,
"Writes: %6d comprising %6d blocks\n",
st->write, st->write_blocks);
fprintf(stderr, "Cache write/flush: %6d/%6d\n",
st->cache_write, st->cache_flush);
fprintf(stderr, "Cache hit/miss: %6d/%6d",
st->cache_hit, st->cache_miss);
if (st->cache_hit + st->cache_miss)
fprintf(stderr, " (%02d%% hit rate)\n",
st->cache_hit * 100 / (st->cache_hit + st->cache_miss));
else
fprintf(stderr, "\n");
}
int main(int argc, char **argv)
{
struct file_device dev;
struct ufat uf;
struct options opt;
int err;
if (parse_options(argc, argv, &opt) < 0)
return -1;
srandom(opt.seed);
if (file_device_open(&dev, opt.filename, opt.log2_bs,
opt.flags & OPTION_MKFS) < 0)
return -1;
if (opt.flags & OPTION_MKFS) {
err = ufat_mkfs(&dev.base, opt.num_blocks);
if (err < 0) {
fprintf(stderr, "ufat_mkfs: %s\n", ufat_strerror(err));
file_device_close(&dev);
return -1;
}
}
err = ufat_open(&uf, &dev.base);
if (err) {
fprintf(stderr, "ufat_open: %s\n", ufat_strerror(err));
file_device_close(&dev);
return -1;
}
if (!opt.command) {
show_info(&uf.bpb);
err = 0;
} else {
err = opt.command->func(&uf, &opt);
}
ufat_close(&uf);
file_device_close(&dev);
if (opt.flags & OPTION_STATISTICS)
dump_stats(&uf.stat);
return err;
}