forked from dlbeer/ufat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1183 lines (976 loc) · 25.3 KB
/
main.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
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* uFAT -- small flexible VFAT implementation
* Copyright (C) 2012 TracMap Holdings Ltd
*
* Author: Daniel Beer <dlbeer@gmail.com>, www.dlbeer.co.nz
*
* 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 <ctype.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 *in_file;
const char *out_file;
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, void *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 void *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 FILE *open_input(const char *fname)
{
FILE *in;
if (!fname)
return stdin;
in = fopen(fname, "rb");
if (!in) {
fprintf(stderr, "Failed to open %s for reading: %s\n",
fname, strerror(errno));
return NULL;
}
return in;
}
static void close_input(const char *fname, FILE *f)
{
if (fname)
fclose(f);
}
static FILE *open_output(const char *fname)
{
FILE *out;
if (!fname)
return stdout;
out = fopen(fname, "wb");
if (!out) {
fprintf(stderr, "Failed to open %s for writing: %s\n",
fname, strerror(errno));
return NULL;
}
return out;
}
static int close_output(const char *fname, FILE *f)
{
if (fname && fclose(f) < 0) {
fprintf(stderr, "Error on closing %s: %s\n",
fname, strerror(errno));
return -1;
}
return 0;
}
static void print_date(FILE *out, ufat_date_t d)
{
fprintf(out, "%04d-%02d-%02d",
UFAT_DATE_Y(d), UFAT_DATE_M(d), UFAT_DATE_D(d));
}
static void print_time(FILE *out, ufat_time_t t)
{
fprintf(out, "%2d:%02d:%02d",
UFAT_TIME_H(t), UFAT_TIME_M(t), UFAT_TIME_S(t));
}
static void print_attributes(FILE *out, ufat_attr_t a)
{
fprintf(out, "%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(FILE *out, 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(out, inf.modify_date);
fprintf(out, " ");
print_time(out, inf.modify_time);
fprintf(out, " ");
print_attributes(out, inf.attributes & ~UFAT_ATTR_DIRECTORY);
if (inf.attributes & UFAT_ATTR_DIRECTORY)
fprintf(out, " %9s", "<DIR>");
else
fprintf(out, " %9u", inf.file_size);
fprintf(out, " %s\n", lfn);
}
return 0;
}
static int find_path(struct ufat *uf,
struct ufat_directory *dir, const char *path,
struct ufat_dirent *inf, const char **path_out)
{
int err;
ufat_open_root(uf, dir);
err = ufat_dir_find_path(dir, path, inf, path_out);
if (err < 0)
fprintf(stderr, "ufat_dir_find_path: %s\n",
ufat_strerror(err));
return err;
}
static int require_file(struct ufat *uf, const char *path,
struct ufat_dirent *ent)
{
struct ufat_directory dir;
int err = find_path(uf, &dir, path, ent, NULL);
if (err < 0)
return err;
if (err) {
fprintf(stderr, "No such file or directory: %s\n", path);
return -1;
}
return 0;
}
static int cmd_dir(struct ufat *uf, const struct options *opt)
{
struct ufat_directory dir;
FILE *out;
if (opt->argc) {
struct ufat_dirent ent;
int err;
if (require_file(uf, opt->argv[0], &ent) < 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;
}
} else {
ufat_open_root(uf, &dir);
}
out = open_output(opt->out_file);
if (!out)
return -1;
if (list_dir(out, &dir) < 0) {
close_output(opt->out_file, out);
return -1;
}
return close_output(opt->out_file, out);
}
static int cmd_fstat(struct ufat *uf, const struct options *opt)
{
FILE *out;
struct ufat_dirent ent;
char can_name[UFAT_LFN_MAX_UTF8];
int err;
if (!opt->argc) {
fprintf(stderr, "You must specify a file path\n");
return -1;
}
if (require_file(uf, opt->argv[0], &ent) < 0)
return -1;
out = open_output(opt->out_file);
if (!out)
return -1;
err = ufat_get_filename(uf, &ent, can_name, sizeof(can_name));
if (err < 0)
fprintf(stderr, "ufat_get_filename: %s\n",
ufat_strerror(err));
else
fprintf(out, "Filename: %s\n", can_name);
fprintf(out, "Entry block/offset: %llu/%d\n",
ent.dirent_block, ent.dirent_pos);
if (ent.lfn_block != UFAT_BLOCK_NONE)
fprintf(out, "LFN start block/offset: %llu/%d\n",
ent.lfn_block, ent.lfn_pos);
fprintf(out, "Short name: %s", ent.short_name);
if (ent.short_ext[0])
fprintf(out, ".%s\n", ent.short_ext);
else
fprintf(out, "\n");
fprintf(out, "Attributes: 0x%02x (", ent.attributes);
print_attributes(out, ent.attributes);
fprintf(out, ")\n");
fprintf(out, "Creation date/time: ");
print_date(out, ent.create_date);
fprintf(out, " ");
print_time(out, ent.create_time);
fprintf(out, "\n");
fprintf(out, "Modification date/time: ");
print_date(out, ent.modify_date);
fprintf(out, " ");
print_time(out, ent.modify_time);
fprintf(out, "\n");
fprintf(out, "Last access date: ");
print_date(out, ent.access_date);
fprintf(out, "\n");
fprintf(out, "First cluster: %u\n", ent.first_cluster);
fprintf(out, "Size: %u\n", ent.file_size);
return close_output(opt->out_file, out);
}
static int cmd_read(struct ufat *uf, const struct options *opt)
{
FILE *out;
struct ufat_dirent ent;
struct ufat_file file;
int err;
if (!opt->argc) {
fprintf(stderr, "You must specify a file path\n");
return -1;
}
if (require_file(uf, opt->argv[0], &ent) < 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;
}
out = open_output(opt->out_file);
if (!out)
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));
close_output(opt->out_file, out);
return -1;
}
if (!len)
break;
if (fwrite(buf, 1, len, out) != (size_t)len) {
perror("fwrite");
close_output(opt->out_file, out);
return -1;
}
}
return close_output(opt->out_file, out);
}
static int cmd_write(struct ufat *uf, const struct options *opt)
{
FILE *in;
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;
}
err = find_path(uf, &dir, opt->argv[0], &ent, &basename);
if (err < 0)
return -1;
in = open_input(opt->in_file);
if (!in)
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));
close_input(opt->in_file, in);
return -1;
}
}
err = ufat_open_file(uf, &file, &ent);
if (err < 0) {
fprintf(stderr, "ufat_open_file: %s\n", ufat_strerror(err));
close_input(opt->in_file, in);
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, in);
if (ferror(stdin)) {
perror("fread");
close_input(opt->in_file, in);
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));
close_input(opt->in_file, in);
return -1;
}
}
err = ufat_file_truncate(&file);
if (err < 0) {
fprintf(stderr, "ufat_file_truncate: %s\n",
ufat_strerror(err));
close_input(opt->in_file, in);
return -1;
}
close_input(opt->in_file, in);
return 0;
}
static int cmd_rm(struct ufat *uf, const struct options *opt)
{
struct ufat_dirent ent;
int err;
if (!opt->argc) {
fprintf(stderr, "You must specify a file path\n");
return -1;
}
if (require_file(uf, opt->argv[0], &ent) < 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;
}
err = find_path(uf, &dir, opt->argv[0], &ent, &basename);
if (err < 0)
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 int parse_dosattr(const char *attr, ufat_attr_t *a)
{
ufat_attr_t out = 0;
while (*attr) {
switch (*attr) {
case 'A':
case 'a':
out |= UFAT_ATTR_ARCHIVE;
break;
case 'H':
case 'h':
out |= UFAT_ATTR_HIDDEN;
break;
case 'R':
case 'r':
out |= UFAT_ATTR_READONLY;
break;
case 'S':
case 's':
out |= UFAT_ATTR_SYSTEM;
break;
default:
fprintf(stderr, "Unrecognized DOS attribute: %c\n",
*attr);
return -1;
}
attr++;
}
*a = out;
return 0;
}
static int parse_date(const char *text, ufat_date_t *d)
{
int bits[3] = {0};
int b = 0;
int i;
if (!strcasecmp(text, "now")) {
time_t now = time(NULL);
struct tm *local = localtime(&now);
*d = UFAT_DATE(local->tm_year + 1900,
local->tm_mon + 1,
local->tm_mday);
return 0;
}
for (i = 0; text[i]; i++) {
const int c = text[i];
if (c == '-') {
b++;
if (b >= 3)
goto bad_format;
} else if (isdigit(c)) {
bits[b] = bits[b] * 10 + c - '0';
} else {
goto bad_format;
}
}
if (b < 2)
goto bad_format;
*d = UFAT_DATE(bits[0], bits[1], bits[2]);
return 0;
bad_format:
fprintf(stderr, "Date must be of the form YYYY-MM-DD: %s\n", text);
return -1;
}
static int parse_time(const char *text, ufat_time_t *t)
{
int bits[3] = {0};
int b = 0;
int i;
if (!strcasecmp(text, "now")) {
time_t now = time(NULL);
struct tm *local = localtime(&now);
*t = UFAT_TIME(local->tm_hour,
local->tm_min,
local->tm_sec);
return 0;
}
for (i = 0; text[i]; i++) {
const int c = text[i];
if (c == ':') {
b++;
if (b >= 3)
goto bad_format;
} else if (isdigit(c)) {
bits[b] = bits[b] * 10 + c - '0';
} else {
goto bad_format;
}
}
if (b < 2)
goto bad_format;
*t = UFAT_TIME(bits[0], bits[1], bits[2]);
return 0;
bad_format:
fprintf(stderr, "Time must be of the form HH:MM:SS: %s\n",
text);
return -1;
}
static int parse_attribute(const char *spec, struct ufat_dirent *ent)
{
char key[64];
char *value;
if (*spec == '+' || *spec == '~' || *spec == '=') {
ufat_attr_t a;
if (parse_dosattr(spec + 1, &a) < 0)
return -1;
if (*spec == '+')
ent->attributes |= a;
else if (*spec == '=')
ent->attributes = a;
else
ent->attributes &= ~a;
return 0;
}
/* Try to identify a key=value pair */
strncpy(key, spec, sizeof(key));
key[sizeof(key) - 1] = 0;
value = strchr(key, '=');
if (!value) {
fprintf(stderr, "Unknown attribute specification: %s\n",
spec);
return -1;
}
*(value++) = 0;
/* Deal with it */
if (!strcasecmp(key, "create_date"))
return parse_date(value, &ent->create_date);
if (!strcasecmp(key, "create_time"))
return parse_time(value, &ent->create_time);
if (!strcasecmp(key, "modify_date"))
return parse_date(value, &ent->modify_date);
if (!strcasecmp(key, "modify_time"))
return parse_time(value, &ent->modify_time);
if (!strcasecmp(key, "access_date"))
return parse_date(value, &ent->access_date);
fprintf(stderr, "Unknown attribute: %s\n", key);
return -1;
}
static int cmd_rename(struct ufat *uf, const struct options *opt)
{
struct ufat_directory dir;
struct ufat_dirent ent;
int err;
const char *src_path;
const char *dst_name;
if (opt->argc < 2) {
fprintf(stderr, "You must specified a source path and "
"a new name.\n");
return -1;
}
src_path = opt->argv[0];
dst_name = opt->argv[1];
err = find_path(uf, &dir, src_path, &ent, NULL);
if (err < 0)
return -1;
if (err) {
fprintf(stderr, "No such file or directory: %s\n",
ufat_strerror(err));
return -1;
}
err = ufat_move(&ent, &dir, dst_name);
if (err < 0) {
fprintf(stderr, "ufat_move: %s\n", ufat_strerror(err));
return -1;
}
return 0;
}
static int cmd_move(struct ufat *uf, const struct options *opt)
{
struct ufat_directory dir;
struct ufat_dirent src_ent;
struct ufat_dirent dst_ent;
int err;
const char *src_path;
const char *dst_path;
const char *new_name;
char old_name[UFAT_LFN_MAX_UTF8];
if (opt->argc < 2) {
fprintf(stderr, "You must specified a source path and "
"a destination path.\n");
return -1;
}
src_path = opt->argv[0];
dst_path = opt->argv[1];
err = find_path(uf, &dir, src_path, &src_ent, NULL);
if (err < 0)
return -1;
if (err) {
fprintf(stderr, "No such file or directory: %s\n",
opt->argv[0]);
return -1;
}
ufat_open_root(uf, &dir);
err = ufat_dir_find_path(&dir, dst_path, &dst_ent, &new_name);
if (err < 0) {
fprintf(stderr, "ufat_dir_find_path: %s\n",
ufat_strerror(err));
return -1;
}
if (!err) {
/* Is the destination a directory or an existing file? */
if (!(dst_ent.attributes & UFAT_ATTR_DIRECTORY)) {
fprintf(stderr, "File already exists: %s\n",
dst_path);
return -1;
}
/* It's a directory name. Use the old canonical filename. */
err = ufat_open_subdir(uf, &dir, &dst_ent);
if (err < 0) {
fprintf(stderr, "ufat_open_subdir: %s\n",
ufat_strerror(err));
return -1;
}
err = ufat_get_filename(uf, &src_ent,
old_name, sizeof(old_name));
if (err < 0) {
fprintf(stderr, "ufat_get_filename: %s\n",
ufat_strerror(err));
return -1;
}
new_name = old_name;
}
err = ufat_move(&src_ent, &dir, new_name);
if (err < 0) {
fprintf(stderr, "ufat_move: %s\n", ufat_strerror(err));
return -1;
}
return 0;
}
static int cmd_chattr(struct ufat *uf, const struct options *opt)
{
struct ufat_dirent ent;
int err;
int i;
if (!opt->argc) {
fprintf(stderr, "You must specify a file path\n");
return -1;
}
if (require_file(uf, opt->argv[0], &ent) < 0)
return -1;
for (i = 1; i < opt->argc; i++)
if (parse_attribute(opt->argv[i], &ent) < 0)
return -1;
err = ufat_update_attributes(uf, &ent);
if (err < 0) {
fprintf(stderr, "ufat_dir_update_attributes: %s\n",
ufat_strerror(err));
return -1;
}
return 0;
}
static void show_info(FILE *out, const struct ufat_bpb *bpb)
{
fprintf(out, "Type: FAT%d\n", bpb->type);
fprintf(out, "Blocks per cluster: %u\n",
1 << bpb->log2_blocks_per_cluster);
fprintf(out, "FAT size (blocks): %llu\n", bpb->fat_size);
fprintf(out, "FAT offset (block): %llu\n", bpb->fat_start);
fprintf(out, "FAT count: %u\n", bpb->fat_count);
fprintf(out, "Cluster starting block: %llu\n", bpb->cluster_start);
fprintf(out, "Clusters: %u\n", bpb->num_clusters);
fprintf(out, "Root directory block start: %llu\n", bpb->root_start);
fprintf(out, "Root directory block count: %llu\n", bpb->root_size);
fprintf(out, "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"
" -i filename Read input from the given file\n"
" -o filename Write output to the given file\n"
" --mkfs <num blocks> Initialize the filesystem (WARNING: all existing\n"
" data 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"
" chattr [path] [attributes]\n"
" Alter file attributes/dates/times (see below)\n"
" move [src] [dst] Move a file from one place to another\n"
" rename [src] [new-name] Rename a file without moving it\n"
"\n"
"Attributes are specified using arguments with a key=value syntax:\n"
" create_date=YYYY-MM-DD Creation date\n"
" create_time=HH:MM:SS Creation time\n"
" modify_date=YYYY-MM-DD Modification date\n"
" modify_time=HH:MM:SS Modification time\n"
" access_date=YYYY-MM-DD Access date\n"
" [+/~/=]AHRS Alter archive/hidden/read-only/system\n"
"\n"
"The special value \"now\" can be used for either dates or times.\n",
progname);
}
static void version_banner(void)
{
printf(
"ufat version 20120528\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},