-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesys.c
1460 lines (1205 loc) · 29.8 KB
/
filesys.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
#include "filesys.h"
#include <string.h>
// first bitmap block must start at 0 to simlify offset calculation
#define TFS_FIRST_BITMAP_BLK 0
#define TFS_ROOT_DIR_BLK 1
typedef struct {
uint32_t prev;
uint32_t next;
uint8_t data[];
} _PACKED TFS_DATA_BLK;
#define TFS_DATA_LEN (TFS_BLOCKSIZE - sizeof(TFS_DATA_BLK))
typedef struct {
uint32_t prev;
uint32_t next;
uint32_t parent;
TFS_DIR_ITEM items[];
} _PACKED TFS_DIR_BLK;
#define TFS_DIR_BLK_ITEMS ((TFS_BLOCKSIZE - sizeof(TFS_DIR_BLK)) / sizeof(TFS_DIR_ITEM))
typedef union {
uint8_t raw[TFS_BLOCKSIZE];
TFS_DIR_BLK dir;
TFS_DATA_BLK data;
} TFS_BLK_BUFFER;
TFS_DRIVE_INFO drive_info;
uint8_t last_error;
#ifdef TFS_EXTENDED_API
typedef struct {
uint8_t usage_count;
uint32_t dir_blk;
TFS_DIR_ITEM *dir_item;
uint32_t size;
uint32_t first_blk;
uint32_t curr_blk;
uint32_t curr_pos;
} TFS_FILEHANDLE;
static TFS_FILEHANDLE handles[TFS_MAX_FDS];
#define SEEK_ERROR 0
#define SEEK_OK 1
#define SEEK_EOF 2
#define SEEK_APPEND 3
static uint8_t item_usage_count(TFS_DIR_ITEM *item);
static void init_pos(TFS_FILEHANDLE *hnd);
static void update_dir_item(TFS_FILEHANDLE *hnd);
static uint8_t seek(TFS_FILEHANDLE *hnd, uint32_t pos, uint8_t append);
#endif
#define TFS_BITMAP_BLK_INVAL 0xffffffff
#define TFS_BITMAP_BLK_COUNT (TFS_BLOCKSIZE << 3)
#define TFS_BITMAP_BLK_MASK (TFS_BITMAP_BLK_COUNT - 1)
#define TFS_BITMAP_BLK_SHIFT (TFS_BLOCKSIZE_WIDTH + 3)
#define GET_BITMAK_BLK(x) ((x) & ~TFS_BITMAP_BLK_MASK)
#ifndef TFS_FILENAME_CMP
#define TFS_FILENAME_CMP(ref, cmp) (strncmp(ref, cmp, TFS_NAME_LEN) == 0)
#endif
static uint32_t last_bitmap_blk;
static uint16_t last_bitmap_len;
static uint32_t loaded_bitmap_blk;
static uint8_t bitmap_blk[TFS_BLOCKSIZE];
static uint32_t current_dir_blk;
static uint32_t loaded_dir_blk;
static TFS_BLK_BUFFER blk_buf;
static void load_bitmap(uint32_t pos);
static uint32_t alloc_block(void);
static void free_block(uint32_t pos);
static void free_file_blocks(uint32_t pos);
static void write_dir_cleanup(void);
static TFS_DIR_ITEM *find_file(const char *name, uint8_t want_free_item);
static void load_bitmap(uint32_t pos) {
drive_read_block(pos, bitmap_blk);
if (last_error != TFS_ERR_OK) {
loaded_bitmap_blk = TFS_BITMAP_BLK_INVAL;
return;
}
loaded_bitmap_blk = pos;
}
static uint32_t alloc_block(void) {
uint32_t start, pos;
uint32_t block;
uint16_t i;
uint8_t *p;
uint8_t mask;
// no current bitmap block -> full was detected
if (loaded_bitmap_blk == TFS_BITMAP_BLK_INVAL) {
last_error = TFS_ERR_DISK_FULL;
return 0;
}
start = loaded_bitmap_blk;
pos = loaded_bitmap_blk;
while (1) {
// serach for free block in current bitmap block
for (i = 0, p = bitmap_blk, block = pos; i < TFS_BLOCKSIZE; i++, p++, block += 8) {
if (*p != 0xff) {
for (mask = 1; mask != 0; mask <<= 1, block++) {
if ((*p & mask) == 0) {
// free block found, mark as used
*p |= mask;
// write updated bitmap block
drive_write_block(loaded_bitmap_blk, bitmap_blk);
if (last_error != TFS_ERR_OK) {
return 0;
}
return block;
}
}
}
}
// no free block found, go to next one
// turn around on end of disk
if (pos == last_bitmap_blk) {
pos = TFS_FIRST_BITMAP_BLK;
} else {
pos += TFS_BITMAP_BLK_COUNT;
}
// disk is full if we're back to where we have started
if (pos == start) {
loaded_bitmap_blk = TFS_BITMAP_BLK_INVAL;
last_error = TFS_ERR_DISK_FULL;
return 0;
}
// read next block
load_bitmap(pos);
if (last_error != TFS_ERR_OK) {
return 0;
}
}
}
static void free_block(uint32_t pos) {
uint32_t tmp;
uint8_t mask;
uint16_t offset;
// load corrosponding bitmap block
tmp = GET_BITMAK_BLK(pos);
if (loaded_bitmap_blk != tmp) {
load_bitmap(tmp);
if (last_error != TFS_ERR_OK) {
return;
}
}
// mark block as unused
offset = pos & TFS_BITMAP_BLK_MASK;
mask = 1 << (offset & 0x07);
offset >>= 3;
bitmap_blk[offset] &= ~mask;
// write block
drive_write_block(loaded_bitmap_blk, bitmap_blk);
if (last_error != TFS_ERR_OK) {
return;
}
}
static void free_file_blocks(uint32_t pos) {
while (pos != 0) {
drive_read_block(pos, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return;
}
free_block(pos);
if (last_error != TFS_ERR_OK) {
return;
}
pos = blk_buf.data.next;
}
}
static void write_dir_cleanup(void) {
uint8_t i;
TFS_DIR_ITEM *p;
uint32_t prev, next;
// this block is the last one -> do normal write
if (blk_buf.dir.prev == 0 && blk_buf.dir.next == 0) {
drive_write_block(loaded_dir_blk, blk_buf.raw);
return;
}
// check for completly empty directory block
for (i = 0, p = blk_buf.dir.items; i < TFS_DIR_BLK_ITEMS; i++, p++) {
if (p->type != TFS_DIR_ITEM_FREE) {
// not empty -> do normal write
drive_write_block(loaded_dir_blk, blk_buf.raw);
return;
}
}
// remember pointers
prev = blk_buf.dir.prev;
next = blk_buf.dir.next;
if (prev == 0) {
// we are on list head, so move the next block to this position
drive_read_block(next, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return;
}
blk_buf.dir.prev = 0;
// exchange pointers
prev = loaded_dir_blk;
loaded_dir_blk = next;
next = blk_buf.dir.next;
} else {
// update prev
drive_read_block(prev, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return;
}
blk_buf.dir.next = next;
}
// write updated block
drive_write_block(prev, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return;
}
if (next != 0) {
// update next
drive_read_block(next, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return;
}
blk_buf.dir.prev = prev;
drive_write_block(next, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return;
}
}
// free unused block
free_block(loaded_dir_blk);
}
static TFS_DIR_ITEM *find_file(const char *name, uint8_t want_free_item) {
uint32_t pos = current_dir_blk;
uint8_t i;
TFS_DIR_ITEM *p;
uint32_t free_blk = 0;
int8_t free_item = -1;
// check for name
if (*name == 0) {
last_error = TFS_ERR_NO_NAME;
return NULL;
}
while (1) {
// read current directory block
drive_read_block(pos, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return NULL;
}
loaded_dir_blk = pos;
// iterrate items
for (i = 0, p = blk_buf.dir.items; i < TFS_DIR_BLK_ITEMS; i++, p++) {
if (p->type == TFS_DIR_ITEM_FREE) {
// remember free item, if found one
if (free_item < 0) {
free_blk = pos;
free_item = i;
}
} else {
// check filename
if (TFS_FILENAME_CMP(name, p->name)) {
return p;
}
}
}
// go to next block in chain
pos = blk_buf.dir.next;
if (pos == 0) {
break;
}
}
// no match found and we do not want a free die item
if (!want_free_item) {
return NULL;
}
// free item found
if (free_item >= 0) {
// reload directory block, if an other than the one with the free item is loaded
if (loaded_dir_blk != free_blk) {
drive_read_block(free_blk, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return NULL;
}
loaded_dir_blk = free_blk;
}
return &blk_buf.dir.items[free_item];
}
// now we need a new directory block, so alloc one
free_blk = alloc_block();
if (last_error != TFS_ERR_OK) {
return NULL;
}
// add pointer to new block to last one
blk_buf.dir.next = free_blk;
drive_write_block(loaded_dir_blk, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return NULL;
}
// initialize new block
// keep some fields from the last one loaded (e.g. type, patent)
blk_buf.dir.prev = loaded_dir_blk;
blk_buf.dir.next = 0;
memset(blk_buf.dir.items, 0, sizeof(TFS_DIR_ITEM) * TFS_DIR_BLK_ITEMS);
loaded_dir_blk = free_blk;
// write block
drive_write_block(free_blk, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return NULL;
}
return blk_buf.dir.items; // first item is free on new block
}
void tfs_init(void) {
#ifdef TFS_EXTENDED_API
memset(handles, 0, sizeof(handles));
#endif
last_error = TFS_ERR_OK;
drive_select();
last_bitmap_blk = drive_info.blk_count - 1;
last_bitmap_len = (last_bitmap_len & TFS_BITMAP_BLK_MASK) + 1;
last_bitmap_blk = GET_BITMAK_BLK(last_bitmap_blk);
load_bitmap(TFS_FIRST_BITMAP_BLK);
if (last_error != TFS_ERR_OK) {
goto out;
}
current_dir_blk = TFS_ROOT_DIR_BLK;
loaded_dir_blk = 0;
out:
drive_deselect();
}
#ifdef TFS_ENABLE_FORMAT
void tfs_format(void) {
uint32_t pos;
uint8_t mask, last;
uint16_t offset;
#ifdef TFS_FORMAT_STATE_CALLBACK
uint32_t prog_max = last_bitmap_blk >> TFS_BITMAP_BLK_SHIFT;
#endif
last_error = TFS_ERR_OK;
#ifdef TFS_FORMAT_STATE_CALLBACK
tfs_format_state(TFS_FORMAT_STATE_START);
#endif
drive_select();
// write the bitmap-blocks
// first block always in use (the bitmapblock itself)
memset(&bitmap_blk, 0, TFS_BLOCKSIZE);
bitmap_blk[0] = 1;
pos = TFS_FIRST_BITMAP_BLK;
last = 0;
#ifdef TFS_FORMAT_STATE_CALLBACK
tfs_format_state(TFS_FORMAT_STATE_BITMAP_START);
#endif
while(1) {
#ifdef TFS_FORMAT_STATE_CALLBACK
// print progress
tfs_format_progress(pos >> TFS_BITMAP_BLK_SHIFT, prog_max);
#endif
if (pos == last_bitmap_blk) {
last = 1;
// mark all blocks after end of disk as used
mask = 0xff << (last_bitmap_len & 0x07);
offset = last_bitmap_len >> 3;
bitmap_blk[offset] |= mask;
for (offset++; offset < TFS_BLOCKSIZE; offset++) {
bitmap_blk[offset] = 0xff;
}
}
// write block
drive_write_block(pos, bitmap_blk);
if (last_error != TFS_ERR_OK) {
goto out;
}
// break on last block
if (last) {
#ifdef TFS_FORMAT_STATE_CALLBACK
tfs_format_state(TFS_FORMAT_STATE_BITMAP_DONE);
#endif
break;
}
// skip to next block
pos += TFS_BITMAP_BLK_COUNT;
};
// read the first bitmap-block
load_bitmap(TFS_FIRST_BITMAP_BLK);
if (last_error != TFS_ERR_OK) {
goto out;
}
#ifdef TFS_FORMAT_STATE_CALLBACK
tfs_format_state(TFS_FORMAT_STATE_ROOTDIR);
#endif
// alloc root dir block (should be block 3)
pos = alloc_block();
if (last_error != TFS_ERR_OK) {
goto out;
}
// init root directory
memset(blk_buf.raw, 0, TFS_BLOCKSIZE);
// write block
drive_write_block(pos, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
goto out;
}
current_dir_blk = TFS_ROOT_DIR_BLK;
loaded_dir_blk = 0;
#ifdef TFS_FORMAT_STATE_CALLBACK
tfs_format_state(TFS_FORMAT_STATE_DONE);
#endif
out:
drive_deselect();
}
#endif
uint32_t tfs_get_used(void) {
uint32_t pos, used;
uint16_t i;
uint8_t *p;
uint8_t mask;
last_error = TFS_ERR_OK;
drive_select();
pos = TFS_FIRST_BITMAP_BLK;
used = 0;
while (1) {
// load bitmap block
load_bitmap(pos);
if (last_error != TFS_ERR_OK) {
drive_deselect();
return 0;
}
// count allocated blocks
for (i = 0, p = bitmap_blk; i < TFS_BLOCKSIZE; i++, p++) {
if (*p > 0) {
for (mask = 1; mask != 0; mask <<= 1) {
if ((*p & mask) != 0) {
used++;
}
}
}
}
// check for end of list
if (pos == last_bitmap_blk) {
load_bitmap(TFS_FIRST_BITMAP_BLK);
drive_deselect();
// blocks after dist end are marked as use, so substract them
return used - (TFS_BITMAP_BLK_COUNT - last_bitmap_len);
}
// next block
pos += TFS_BITMAP_BLK_COUNT;
}
}
#ifdef TFS_READ_DIR_USERDATA
uint8_t tfs_read_dir(TFS_READ_DIR_USERDATA data) {
#else
uint8_t tfs_read_dir(void) {
#endif
uint32_t pos = current_dir_blk;
uint8_t i;
uint8_t done = 0;
TFS_DIR_ITEM *p;
last_error = TFS_ERR_OK;
drive_select();
while (1) {
// read current directory block
drive_read_block(pos, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
goto out;
}
// iterrate items
for (i = 0, p = blk_buf.dir.items; i < TFS_DIR_BLK_ITEMS; i++, p++) {
#ifdef TFS_READ_DIR_USERDATA
if (!tfs_dir_handler(data, p)) {
#else
if (!tfs_dir_handler(p)) {
#endif
goto out;
}
}
// go to next block in chain
pos = blk_buf.dir.next;
if (pos == 0) {
done = 1;
break;
}
}
out:
drive_deselect();
return done;
}
void tfs_change_dir_root(void) {
current_dir_blk = TFS_ROOT_DIR_BLK;
}
void tfs_change_dir_parent(void) {
last_error = TFS_ERR_OK;
drive_select();
drive_read_block(current_dir_blk, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
goto out;
}
if (blk_buf.dir.parent == 0) {
last_error = TFS_ERR_NOT_EXIST;
goto out;
}
current_dir_blk = blk_buf.dir.parent;
out:
drive_deselect();
}
void tfs_change_dir(const char *name) {
TFS_DIR_ITEM *item;
last_error = TFS_ERR_OK;
drive_select();
// search for dir name
item = find_file(name, 0);
if (last_error != TFS_ERR_OK) {
goto out;
}
// directory not found?
if (item == NULL || item->type != TFS_DIR_ITEM_DIR) {
last_error = TFS_ERR_NOT_EXIST;
goto out;
}
// set to found item
current_dir_blk = item->blk;
out:
drive_deselect();
}
void tfs_create_dir(const char *name) {
TFS_DIR_ITEM *item;
uint32_t new;
last_error = TFS_ERR_OK;
drive_select();
// check for name
item = find_file(name, 1);
if (last_error != TFS_ERR_OK) {
goto out;
}
// directory already exists?
if (item->type != TFS_DIR_ITEM_FREE) {
last_error = TFS_ERR_FILE_EXIST;
goto out;
}
// alloc new dir block
new = alloc_block();
if (last_error != TFS_ERR_OK) {
goto out;
}
// update item
item->type = TFS_DIR_ITEM_DIR;
item->blk = new;
item->size = 0;
strncpy(item->name, name, TFS_NAME_LEN);
drive_write_block(loaded_dir_blk, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
goto out;
}
// init sub directory
memset(blk_buf.raw, 0, TFS_BLOCKSIZE);
blk_buf.dir.parent = current_dir_blk;
// write block
drive_write_block(new, blk_buf.raw);
out:
drive_deselect();
}
void tfs_write_file(const char *name, const uint8_t *data, uint32_t len, uint8_t overwrite) {
TFS_DIR_ITEM *item;
uint32_t pos;
uint16_t blk_len;
last_error = TFS_ERR_OK;
drive_select();
// check for name
item = find_file(name, 1);
if (last_error != TFS_ERR_OK) {
goto out;
}
// file already exists?
if (item->type != TFS_DIR_ITEM_FREE) {
if (!overwrite || item->type != TFS_DIR_ITEM_FILE) {
last_error = TFS_ERR_FILE_EXIST;
goto out;
}
#ifdef TFS_EXTENDED_API
// check if file is in use
if (item_usage_count(item) > 0) {
last_error = TFS_FILE_BUSY;
goto out;
}
#endif
// free old data blocks
free_file_blocks(item->blk);
if (last_error != TFS_ERR_OK) {
goto out;
}
}
if (len == 0) {
// clear block pointer in case of overwrite
pos = 0;
} else {
// allocate first data block
pos = alloc_block();
if (last_error != TFS_ERR_OK) {
goto out;
}
}
// update item
item->type = TFS_DIR_ITEM_FILE;
item->blk = pos;
item->size = len;
strncpy(item->name, name, TFS_NAME_LEN);
drive_write_block(loaded_dir_blk, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
goto out;
}
// write data blocks
blk_buf.data.prev = 0;
while (pos != 0) {
// calculate block length and update remaining length
if (len > TFS_DATA_LEN) {
blk_len = TFS_DATA_LEN;
len -= TFS_DATA_LEN;
// allocate next data block
blk_buf.data.next = alloc_block();
// if error -> try to write the last data block, error is handled after write
} else {
blk_len = len;
len = 0;
blk_buf.data.next = 0;
}
// copy user data
memcpy(blk_buf.data.data, data, blk_len);
data += blk_len;
// write block
drive_write_block(pos, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
goto out;
}
blk_buf.data.prev = pos;
pos = blk_buf.data.next;
}
out:
drive_deselect();
}
uint32_t tfs_read_file(const char *name, uint8_t *data, uint32_t max_len) {
TFS_DIR_ITEM *item;
uint32_t pos;
uint32_t len = 0;
uint32_t rem;
uint16_t blk_len;
last_error = TFS_ERR_OK;
drive_select();
// search for file
item = find_file(name, 0);
if (last_error != TFS_ERR_OK) {
goto out;
}
// file not found?
if (item == NULL || item->type != TFS_DIR_ITEM_FILE) {
last_error = TFS_ERR_NOT_EXIST;
goto out;
}
// initialize loop
pos = item->blk;
len = item->size;
if (len > max_len) {
len = max_len;
}
rem = len;
while (1) {
// read next data block
drive_read_block(pos, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
goto out;
}
// calculate block length and update remaining length
if (rem > TFS_DATA_LEN) {
blk_len = TFS_DATA_LEN;
rem -= TFS_DATA_LEN;
} else {
blk_len = rem;
rem = 0;
}
// copy user data
memcpy(data, blk_buf.data.data, blk_len);
data += blk_len;
// go to next data block
pos = blk_buf.data.next;
if (pos == 0) {
if (rem > 0) {
last_error = TFS_ERR_UNEXP_EOF;
goto out;
}
goto out;
}
}
out:
drive_deselect();
return len;
}
#ifdef TFS_EXTENDED_API
void tfs_delete(const char *name, uint8_t type) {
#else
void tfs_delete(const char *name) {
#endif
TFS_DIR_ITEM *item;
uint32_t pos;
uint8_t i;
TFS_DIR_ITEM *p;
last_error = TFS_ERR_OK;
drive_select();
// search for name
item = find_file(name, 0);
if (last_error != TFS_ERR_OK) {
goto out;
}
// not found?
if (item == NULL) {
last_error = TFS_ERR_NOT_EXIST;
goto out;
}
#ifdef TFS_EXTENDED_API
// check file type
if (type != 0 && item->type != type) {
last_error = TFS_ERR_NOT_EXIST;
goto out;
}
// check if file is in use
if (item_usage_count(item) > 0) {
last_error = TFS_FILE_BUSY;
goto out;
}
#endif
// remember starting block
pos = item->blk;
// delete file
if (item->type == TFS_DIR_ITEM_FILE) {
// update item
item->type = TFS_DIR_ITEM_FREE;
write_dir_cleanup();
if (last_error != TFS_ERR_OK) {
goto out;
}
// free data blocks
free_file_blocks(pos);
goto out;
}
// delete directory
if (item->type == TFS_DIR_ITEM_DIR) {
// read sub directory block
drive_read_block(pos, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
goto out;
}
// check if directory is empty
if (blk_buf.dir.next != 0) {
last_error = TFS_ERR_NOT_EMPTY;
goto out;
}
for (i = 0, p = blk_buf.dir.items; i < TFS_DIR_BLK_ITEMS; i++, p++) {
if (p->type != TFS_DIR_ITEM_FREE) {
last_error = TFS_ERR_NOT_EMPTY;
goto out;
}
}
// re-read parent directory block
drive_read_block(loaded_dir_blk, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
goto out;
}
// update item
item->type = TFS_DIR_ITEM_FREE;
write_dir_cleanup();
if (last_error != TFS_ERR_OK) {
goto out;
}
// free directory block
free_block(pos);
goto out;
}
last_error = TFS_ERR_NOT_EXIST;
out:
drive_deselect();
}
void tfs_rename(const char *from, const char *to) {
TFS_DIR_ITEM *item;
last_error = TFS_ERR_OK;
drive_select();
// check if 'to name' already exists
item = find_file(to, 0);
if (last_error != TFS_ERR_OK) {
goto out;
}
// file already exists?
if (item != NULL) {
last_error = TFS_ERR_FILE_EXIST;
goto out;
}
// find 'from name' item
item = find_file(from, 0);
if (last_error != TFS_ERR_OK) {
goto out;
}
// file not exists?
if (item == NULL) {
last_error = TFS_ERR_NOT_EXIST;
goto out;
}
// update item
strncpy(item->name, to, TFS_NAME_LEN);
drive_write_block(loaded_dir_blk, blk_buf.raw);
out:
drive_deselect();
}
#ifdef TFS_EXTENDED_API
static uint8_t item_usage_count(TFS_DIR_ITEM *item) {
TFS_FILEHANDLE *hnd;
int8_t fd;
for (fd = 0, hnd = handles; fd < TFS_MAX_FDS; fd++, hnd++) {
if (hnd->dir_blk == loaded_dir_blk && hnd->dir_item == item) {
return hnd->usage_count;
}
}
return 0;
}
static void init_pos(TFS_FILEHANDLE *hnd) {
hnd->curr_blk = hnd->first_blk;
hnd->curr_pos = 0;
}
static void update_dir_item(TFS_FILEHANDLE *hnd) {
// read file's directory block
drive_read_block(hnd->dir_blk, blk_buf.raw);
if (last_error != TFS_ERR_OK) {
return;
}
// update item
hnd->dir_item->blk = hnd->first_blk;
hnd->dir_item->size = hnd->size;
drive_write_block(hnd->dir_blk, blk_buf.raw);
}
static uint8_t seek(TFS_FILEHANDLE *hnd, uint32_t pos, uint8_t append) {
uint32_t last_blk = 0;
uint32_t last_pos = 0;
// go to start position
if (pos == 0 || hnd->curr_blk == 0) {
init_pos(hnd);
}
// seek backward, till we are in requested block
while (hnd->curr_blk != 0 && hnd->curr_pos > pos) {