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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
|
/*
io.c (02.09.09)
exFAT file system implementation library.
Free exFAT implementation.
Copyright (C) 2010-2023 Andrew Nayenko
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "exfat.h"
#include <inttypes.h>
#ifndef __UBOOT__
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#if defined(__APPLE__)
#include <sys/disk.h>
#elif defined(__OpenBSD__)
#include <sys/param.h>
#include <sys/disklabel.h>
#include <sys/dkio.h>
#include <sys/ioctl.h>
#elif defined(__NetBSD__)
#include <sys/ioctl.h>
#elif __linux__
#include <sys/mount.h>
#endif
#ifdef USE_UBLIO
#include <sys/uio.h>
#include <ublio.h>
#endif
#else
#include <fs.h>
#include <fs_internal.h>
static struct exfat_ctxt {
struct disk_partition cur_part_info;
struct blk_desc *cur_dev;
struct exfat ef;
} ctxt;
#endif
struct exfat_dev
{
int fd;
enum exfat_mode mode;
off_t size; /* in bytes */
#ifdef USE_UBLIO
off_t pos;
ublio_filehandle_t ufh;
#endif
#ifdef __UBOOT__
struct exfat_ctxt *ctxt;
#endif
};
#ifndef __UBOOT__
static bool is_open(int fd)
{
return fcntl(fd, F_GETFD) != -1;
}
static int open_ro(const char* spec)
{
return open(spec, O_RDONLY);
}
static int open_rw(const char* spec)
{
int fd = open(spec, O_RDWR);
#ifdef __linux__
int ro = 0;
/*
This ioctl is needed because after "blockdev --setro" kernel still
allows to open the device in read-write mode but fails writes.
*/
if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
{
close(fd);
errno = EROFS;
return -1;
}
#endif
return fd;
}
struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
{
struct exfat_dev* dev;
struct stat stbuf;
#ifdef USE_UBLIO
struct ublio_param up;
#endif
/* The system allocates file descriptors sequentially. If we have been
started with stdin (0), stdout (1) or stderr (2) closed, the system
will give us descriptor 0, 1 or 2 later when we open block device,
FUSE communication pipe, etc. As a result, functions using stdin,
stdout or stderr will actually work with a different thing and can
corrupt it. Protect descriptors 0, 1 and 2 from such misuse. */
while (!is_open(STDIN_FILENO)
|| !is_open(STDOUT_FILENO)
|| !is_open(STDERR_FILENO))
{
/* we don't need those descriptors, let them leak */
if (open("/dev/null", O_RDWR) == -1)
{
exfat_error("failed to open /dev/null");
return NULL;
}
}
dev = malloc(sizeof(struct exfat_dev));
if (dev == NULL)
{
exfat_error("failed to allocate memory for device structure");
return NULL;
}
switch (mode)
{
case EXFAT_MODE_RO:
dev->fd = open_ro(spec);
if (dev->fd == -1)
{
free(dev);
exfat_error("failed to open '%s' in read-only mode: %s", spec,
strerror(errno));
return NULL;
}
dev->mode = EXFAT_MODE_RO;
break;
case EXFAT_MODE_RW:
dev->fd = open_rw(spec);
if (dev->fd == -1)
{
free(dev);
exfat_error("failed to open '%s' in read-write mode: %s", spec,
strerror(errno));
return NULL;
}
dev->mode = EXFAT_MODE_RW;
break;
case EXFAT_MODE_ANY:
dev->fd = open_rw(spec);
if (dev->fd != -1)
{
dev->mode = EXFAT_MODE_RW;
break;
}
dev->fd = open_ro(spec);
if (dev->fd != -1)
{
dev->mode = EXFAT_MODE_RO;
exfat_warn("'%s' is write-protected, mounting read-only", spec);
break;
}
free(dev);
exfat_error("failed to open '%s': %s", spec, strerror(errno));
return NULL;
}
if (fstat(dev->fd, &stbuf) != 0)
{
close(dev->fd);
free(dev);
exfat_error("failed to fstat '%s'", spec);
return NULL;
}
if (!S_ISBLK(stbuf.st_mode) &&
!S_ISCHR(stbuf.st_mode) &&
!S_ISREG(stbuf.st_mode))
{
close(dev->fd);
free(dev);
exfat_error("'%s' is neither a device, nor a regular file", spec);
return NULL;
}
#if defined(__APPLE__)
if (!S_ISREG(stbuf.st_mode))
{
uint32_t block_size = 0;
uint64_t blocks = 0;
if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
{
close(dev->fd);
free(dev);
exfat_error("failed to get block size");
return NULL;
}
if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
{
close(dev->fd);
free(dev);
exfat_error("failed to get blocks count");
return NULL;
}
dev->size = blocks * block_size;
}
else
#elif defined(__OpenBSD__)
if (!S_ISREG(stbuf.st_mode))
{
struct disklabel lab;
struct partition* pp;
char* partition;
if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
{
close(dev->fd);
free(dev);
exfat_error("failed to get disklabel");
return NULL;
}
/* Don't need to check that partition letter is valid as we won't get
this far otherwise. */
partition = strchr(spec, '\0') - 1;
pp = &(lab.d_partitions[*partition - 'a']);
dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
if (pp->p_fstype != FS_NTFS)
exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
"you can fix this with fdisk(8)");
}
else
#elif defined(__NetBSD__)
if (!S_ISREG(stbuf.st_mode))
{
off_t size;
if (ioctl(dev->fd, DIOCGMEDIASIZE, &size) == -1)
{
close(dev->fd);
free(dev);
exfat_error("failed to get media size");
return NULL;
}
dev->size = size;
}
else
#endif
{
/* works for Linux, FreeBSD, Solaris */
dev->size = exfat_seek(dev, 0, SEEK_END);
if (dev->size <= 0)
{
close(dev->fd);
free(dev);
exfat_error("failed to get size of '%s'", spec);
return NULL;
}
if (exfat_seek(dev, 0, SEEK_SET) == -1)
{
close(dev->fd);
free(dev);
exfat_error("failed to seek to the beginning of '%s'", spec);
return NULL;
}
}
#ifdef USE_UBLIO
memset(&up, 0, sizeof(struct ublio_param));
up.up_blocksize = 256 * 1024;
up.up_items = 64;
up.up_grace = 32;
up.up_priv = &dev->fd;
dev->pos = 0;
dev->ufh = ublio_open(&up);
if (dev->ufh == NULL)
{
close(dev->fd);
free(dev);
exfat_error("failed to initialize ublio");
return NULL;
}
#endif
return dev;
}
int exfat_close(struct exfat_dev* dev)
{
int rc = 0;
#ifdef USE_UBLIO
if (ublio_close(dev->ufh) != 0)
{
exfat_error("failed to close ublio");
rc = -EIO;
}
#endif
if (close(dev->fd) != 0)
{
exfat_error("failed to close device: %s", strerror(errno));
rc = -EIO;
}
free(dev);
return rc;
}
int exfat_fsync(struct exfat_dev* dev)
{
int rc = 0;
#ifdef USE_UBLIO
if (ublio_fsync(dev->ufh) != 0)
{
exfat_error("ublio fsync failed");
rc = -EIO;
}
#endif
if (fsync(dev->fd) != 0)
{
exfat_error("fsync failed: %s", strerror(errno));
rc = -EIO;
}
return rc;
}
enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
{
return dev->mode;
}
off_t exfat_get_size(const struct exfat_dev* dev)
{
return dev->size;
}
off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
{
#ifdef USE_UBLIO
/* XXX SEEK_CUR will be handled incorrectly */
return dev->pos = lseek(dev->fd, offset, whence);
#else
return lseek(dev->fd, offset, whence);
#endif
}
ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
{
#ifdef USE_UBLIO
ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
if (result >= 0)
dev->pos += size;
return result;
#else
return read(dev->fd, buffer, size);
#endif
}
ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
{
#ifdef USE_UBLIO
ssize_t result = ublio_pwrite(dev->ufh, (void*) buffer, size, dev->pos);
if (result >= 0)
dev->pos += size;
return result;
#else
return write(dev->fd, buffer, size);
#endif
}
ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
off_t offset)
{
#ifdef USE_UBLIO
return ublio_pread(dev->ufh, buffer, size, offset);
#else
return pread(dev->fd, buffer, size, offset);
#endif
}
ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
off_t offset)
{
#ifdef USE_UBLIO
return ublio_pwrite(dev->ufh, (void*) buffer, size, offset);
#else
return pwrite(dev->fd, buffer, size, offset);
#endif
}
#else /* U-Boot */
struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
{
struct exfat_dev* dev;
dev = malloc(sizeof(struct exfat_dev));
if (!dev) {
exfat_error("failed to allocate memory for device structure");
return NULL;
}
dev->mode = EXFAT_MODE_RW;
dev->size = ctxt.cur_part_info.size * ctxt.cur_part_info.blksz;
dev->ctxt = &ctxt;
return dev;
}
int exfat_close(struct exfat_dev* dev)
{
free(dev);
return 0;
}
int exfat_fsync(struct exfat_dev* dev)
{
return 0;
}
enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
{
return dev->mode;
}
off_t exfat_get_size(const struct exfat_dev* dev)
{
return dev->size;
}
ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
off_t offset)
{
lbaint_t sect = offset >> ctxt.cur_dev->log2blksz;
int off = offset & (ctxt.cur_dev->blksz - 1);
if (!ctxt.cur_dev)
return -EIO;
if (fs_devread(ctxt.cur_dev, &ctxt.cur_part_info, sect,
off, size, buffer))
return 0;
return -EIO;
}
ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
off_t offset)
{
lbaint_t sect = offset >> ctxt.cur_dev->log2blksz;
int off = offset & (ctxt.cur_dev->blksz - 1);
if (!ctxt.cur_dev)
return -EIO;
if (fs_devwrite(ctxt.cur_dev, &ctxt.cur_part_info, sect,
off, size, buffer))
return 0;
return -EIO;
}
#endif
ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
void* buffer, size_t size, off_t offset)
{
uint64_t uoffset = offset;
cluster_t cluster;
char* bufp = buffer;
off_t lsize, loffset, remainder;
if (offset < 0)
return -EINVAL;
if (uoffset >= node->size)
return 0;
if (size == 0)
return 0;
if (uoffset + size > node->valid_size)
{
ssize_t bytes = 0;
if (uoffset < node->valid_size)
{
bytes = exfat_generic_pread(ef, node, buffer,
node->valid_size - uoffset, offset);
if (bytes < 0 || (size_t) bytes < node->valid_size - uoffset)
return bytes;
}
memset(buffer + bytes, 0,
MIN(size - bytes, node->size - node->valid_size));
return MIN(size, node->size - uoffset);
}
cluster = exfat_advance_cluster(ef, node, uoffset / CLUSTER_SIZE(*ef->sb));
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while reading", cluster);
return -EIO;
}
loffset = uoffset % CLUSTER_SIZE(*ef->sb);
remainder = MIN(size, node->size - uoffset);
while (remainder > 0)
{
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while reading", cluster);
return -EIO;
}
lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
if (exfat_pread(ef->dev, bufp, lsize,
exfat_c2o(ef, cluster) + loffset) < 0)
{
exfat_error("failed to read cluster %#x", cluster);
return -EIO;
}
bufp += lsize;
loffset = 0;
remainder -= lsize;
cluster = exfat_next_cluster(ef, node, cluster);
}
if (!(node->attrib & EXFAT_ATTRIB_DIR) && !ef->ro && !ef->noatime)
exfat_update_atime(node);
return MIN(size, node->size - uoffset) - remainder;
}
ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
const void* buffer, size_t size, off_t offset)
{
uint64_t uoffset = offset;
int rc;
cluster_t cluster;
const char* bufp = buffer;
off_t lsize, loffset, remainder;
if (offset < 0)
return -EINVAL;
if (uoffset > node->size)
{
rc = exfat_truncate(ef, node, uoffset, true);
if (rc != 0)
return rc;
}
if (uoffset + size > node->size)
{
rc = exfat_truncate(ef, node, uoffset + size, false);
if (rc != 0)
return rc;
}
if (size == 0)
return 0;
cluster = exfat_advance_cluster(ef, node, uoffset / CLUSTER_SIZE(*ef->sb));
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while writing", cluster);
return -EIO;
}
loffset = uoffset % CLUSTER_SIZE(*ef->sb);
remainder = size;
while (remainder > 0)
{
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while writing", cluster);
return -EIO;
}
lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
if (exfat_pwrite(ef->dev, bufp, lsize,
exfat_c2o(ef, cluster) + loffset) < 0)
{
exfat_error("failed to write cluster %#x", cluster);
return -EIO;
}
bufp += lsize;
loffset = 0;
remainder -= lsize;
node->valid_size = MAX(node->valid_size, uoffset + size - remainder);
cluster = exfat_next_cluster(ef, node, cluster);
}
if (!(node->attrib & EXFAT_ATTRIB_DIR))
/* directory's mtime should be updated by the caller only when it
creates or removes something in this directory */
exfat_update_mtime(node);
return size - remainder;
}
#ifdef __UBOOT__
#define PATH_MAX FS_DIRENT_NAME_LEN
struct exfat_dir_stream {
char dirname[PATH_MAX];
struct fs_dir_stream fs_dirs;
struct fs_dirent dirent;
int offset;
};
int exfat_fs_probe(struct blk_desc *fs_dev_desc,
struct disk_partition *fs_partition)
{
int ret;
ctxt.cur_dev = fs_dev_desc;
ctxt.cur_part_info = *fs_partition;
ret = exfat_mount(&ctxt.ef, NULL, "");
if (ret)
goto error;
return 0;
error:
ctxt.cur_dev = NULL;
return ret;
}
/* Adapted from uclibc 1.0.35 */
static char *exfat_realpath(const char *path, char got_path[])
{
char copy_path[PATH_MAX];
char *max_path, *new_path;
size_t path_len;
if (path == NULL)
return NULL;
if (*path == '\0')
return NULL;
/* Make a copy of the source path since we may need to modify it. */
path_len = strlen(path);
if (path_len >= PATH_MAX - 2)
return NULL;
/* Copy so that path is at the end of copy_path[] */
strcpy(copy_path + (PATH_MAX-1) - path_len, path);
path = copy_path + (PATH_MAX-1) - path_len;
max_path = got_path + PATH_MAX - 2; /* points to last non-NUL char */
new_path = got_path;
*new_path++ = '/';
path++;
/* Expand each slash-separated pathname component. */
while (*path != '\0') {
/* Ignore stray "/". */
if (*path == '/') {
path++;
continue;
}
if (*path == '.') {
/* Ignore ".". */
if (path[1] == '\0' || path[1] == '/') {
path++;
continue;
}
if (path[1] == '.') {
if (path[2] == '\0' || path[2] == '/') {
path += 2;
/* Ignore ".." at root. */
if (new_path == got_path + 1)
continue;
/* Handle ".." by backing up. */
while ((--new_path)[-1] != '/')
;
continue;
}
}
}
/* Safely copy the next pathname component. */
while (*path != '\0' && *path != '/') {
if (new_path > max_path)
return NULL;
*new_path++ = *path++;
}
*new_path++ = '/';
}
/* Delete trailing slash but don't whomp a lone slash. */
if (new_path != got_path + 1 && new_path[-1] == '/')
new_path--;
/* Make sure it's null terminated. */
*new_path = '\0';
return got_path;
}
int exfat_lookup_realpath(struct exfat* ef, struct exfat_node** node,
const char* path)
{
char input_path[FS_DIRENT_NAME_LEN];
char real_path[FS_DIRENT_NAME_LEN];
char *name;
/* Input is always absolute path */
snprintf(input_path, FS_DIRENT_NAME_LEN, "/%s", path);
name = exfat_realpath(input_path, real_path);
if (!name)
return -EINVAL;
return exfat_lookup(ef, node, real_path);
}
int exfat_fs_opendir(const char *filename, struct fs_dir_stream **dirsp)
{
struct exfat_dir_stream *dirs;
struct exfat_node *dnode;
int err;
if (strlen(filename) >= PATH_MAX)
return -ENAMETOOLONG;
err = exfat_lookup_realpath(&ctxt.ef, &dnode, filename);
if (err)
return err;
if (!(dnode->attrib & EXFAT_ATTRIB_DIR))
err = -ENOTDIR;
exfat_put_node(&ctxt.ef, dnode);
if (err)
return err;
dirs = calloc(1, sizeof(*dirs));
if (!dirs)
return -ENOMEM;
strncpy(dirs->dirname, filename, PATH_MAX - 1);
dirs->offset = -1;
*dirsp = &dirs->fs_dirs;
return 0;
}
int exfat_fs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
{
struct exfat_dir_stream *dirs =
container_of(fs_dirs, struct exfat_dir_stream, fs_dirs);
struct fs_dirent *dent = &dirs->dirent;
struct exfat_node *dnode, *node;
struct exfat_iterator it;
int offset = 0;
int err;
err = exfat_lookup_realpath(&ctxt.ef, &dnode, dirs->dirname);
if (err)
return err;
if (!(dnode->attrib & EXFAT_ATTRIB_DIR)) {
err = -ENOTDIR;
goto err_out;
}
/* Emulate current directory ./ */
if (dirs->offset == -1) {
dirs->offset++;
snprintf(dent->name, FS_DIRENT_NAME_LEN, ".");
dent->type = FS_DT_DIR;
*dentp = dent;
goto err_out;
}
/* Emulate parent directory ../ */
if (dirs->offset == 0) {
dirs->offset++;
snprintf(dent->name, FS_DIRENT_NAME_LEN, "..");
dent->type = FS_DT_DIR;
*dentp = dent;
goto err_out;
}
err = exfat_opendir(&ctxt.ef, dnode, &it);
if (err)
goto err_out;
*dentp = NULL;
/* Read actual directory content */
while ((node = exfat_readdir(&it))) {
if (dirs->offset != ++offset) {
exfat_put_node(&ctxt.ef, node);
continue;
}
exfat_get_name(node, dent->name);
if (node->attrib & EXFAT_ATTRIB_DIR) {
dent->type = FS_DT_DIR;
} else {
dent->type = FS_DT_REG;
dent->size = node->size;
}
exfat_put_node(&ctxt.ef, node);
*dentp = dent;
dirs->offset++;
break;
}
exfat_closedir(&ctxt.ef, &it);
err_out:
exfat_put_node(&ctxt.ef, dnode);
return err;
}
void exfat_fs_closedir(struct fs_dir_stream *fs_dirs)
{
struct exfat_dir_stream *dirs =
container_of(fs_dirs, struct exfat_dir_stream, fs_dirs);
free(dirs);
}
int exfat_fs_ls(const char *dirname)
{
struct exfat_node *dnode, *node;
char name[FS_DIRENT_NAME_LEN];
int nfiles = 0, ndirs = 2;
struct exfat_iterator it;
int err;
err = exfat_lookup_realpath(&ctxt.ef, &dnode, dirname);
if (err)
return err;
if (!(dnode->attrib & EXFAT_ATTRIB_DIR)) {
err = -ENOTDIR;
goto err_out;
}
err = exfat_opendir(&ctxt.ef, dnode, &it);
if (err)
goto err_out;
printf(" ./\n");
printf(" ../\n");
/* Read actual directory content */
while ((node = exfat_readdir(&it))) {
exfat_get_name(node, name);
if (node->attrib & EXFAT_ATTRIB_DIR) {
printf(" %s/\n", name);
ndirs++;
} else {
printf(" %8lld %s\n", node->size, name);
nfiles++;
}
exfat_put_node(&ctxt.ef, node);
}
printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
exfat_closedir(&ctxt.ef, &it);
err_out:
exfat_put_node(&ctxt.ef, dnode);
return err;
}
int exfat_fs_exists(const char *filename)
{
struct exfat_node* node;
int err;
err = exfat_lookup_realpath(&ctxt.ef, &node, filename);
if (err)
return 0;
exfat_put_node(&ctxt.ef, node);
return 1;
}
int exfat_fs_size(const char *filename, loff_t *size)
{
struct exfat_node* node;
int err;
err = exfat_lookup_realpath(&ctxt.ef, &node, filename);
if (err)
return err;
*size = node->size;
exfat_put_node(&ctxt.ef, node);
return 0;
}
int exfat_fs_read(const char *filename, void *buf, loff_t offset, loff_t len,
loff_t *actread)
{
struct exfat_node* node;
ssize_t sz;
int err;
err = exfat_lookup_realpath(&ctxt.ef, &node, filename);
if (err)
return err;
if (!len)
len = node->size;
sz = exfat_generic_pread(&ctxt.ef, node, buf, len, offset);
if (sz < 0) {
*actread = 0;
err = -EINVAL;
goto exit;
}
*actread = sz;
err = exfat_flush_node(&ctxt.ef, node);
exit:
exfat_put_node(&ctxt.ef, node);
return err;
}
int exfat_fs_unlink(const char *filename)
{
struct exfat_node* node;
int err;
err = exfat_lookup_realpath(&ctxt.ef, &node, filename);
if (err) {
printf("%s: doesn't exist (%d)\n", filename, err);
return err;
}
if (node->attrib & EXFAT_ATTRIB_DIR) {
err = exfat_rmdir(&ctxt.ef, node);
if (err == -ENOTEMPTY)
printf("Error: directory is not empty: %d\n", err);
} else {
err = exfat_unlink(&ctxt.ef, node);
}
if (err)
goto exit;
exfat_put_node(&ctxt.ef, node);
return exfat_cleanup_node(&ctxt.ef, node);
exit:
exfat_put_node(&ctxt.ef, node);
return err;
}
int exfat_fs_mkdir(const char *dirname)
{
if (!strcmp(dirname, ".") || !strcmp(dirname, ".."))
return -EINVAL;
return exfat_mkdir(&ctxt.ef, dirname);
}
int exfat_fs_write(const char *filename, void *buf, loff_t offset,
loff_t len, loff_t *actwrite)
{
struct exfat_node* node;
ssize_t sz;
int err;
/*
* Ignore -EEXIST error here, if the file exists,
* this write should act as an append to offset.
*/
err = exfat_mknod(&ctxt.ef, filename);
if (err && err != -EEXIST)
return err;
err = exfat_lookup_realpath(&ctxt.ef, &node, filename);
if (err)
return err;
/* Write into directories is not allowed. */
if (node->attrib & EXFAT_ATTRIB_DIR)
return -EISDIR;
/* Write past end of file is not allowed. */
if (offset > node->size) {
err = -EINVAL;
goto exit;
}
sz = exfat_generic_pwrite(&ctxt.ef, node, buf, len, offset);
if (sz < 0) {
*actwrite = 0;
err = -EINVAL;
goto exit;
}
err = exfat_truncate(&ctxt.ef, node, offset + sz, false);
if (err)
goto exit;
*actwrite = sz;
err = exfat_flush_node(&ctxt.ef, node);
exit:
exfat_put_node(&ctxt.ef, node);
return err;
}
int exfat_fs_rename(const char *old_path, const char *new_path)
{
return exfat_rename(&ctxt.ef, old_path, new_path);
}
void exfat_fs_close(void)
{
exfat_unmount(&ctxt.ef);
ctxt.cur_dev = NULL;
}
#endif /* __U_BOOT__ */
|