Repository navigation
Expand file tree
/
Copy pathFileFS.c
More file actions
4621 lines (4073 loc) · 120 KB
/
Copy pathFileFS.c
File metadata and controls
4621 lines (4073 loc) · 120 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
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
/*----------------------------------------------------------------------------/
/ FileFS - Implement a virtual file system within a single file R1.0 /
/-----------------------------------------------------------------------------/
/
/ Copyright (C) 2025, cyantree, all right reserved.
/
/ mail: [email protected]
/ QQ: 9234933
/
/----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "FileFS.h"
// =====================================
// platform depend start
// =====================================
// tested: winxp,win10,debian,haiku,freebsd
#define ffs_tmpfile() tmpfile()
#define ffs_fopen(filename, mode) fopen(filename, mode)
#define ffs_fwrite(ptr, size, nmemb, stream) fwrite(ptr, size, nmemb, stream)
#define ffs_fread(ptr, size, nmemb, stream) fread(ptr, size, nmemb, stream)
#define ffs_fclose(stream) fclose(stream)
#define ffs_remove(filename) remove(filename)
static void ffs_fsetpos(FILE *fp, unsigned long long pos)
{
fpos_t p;
#if defined(linux) || defined(__linux) || defined(__linux__)
p.__pos = pos;
#else
p = pos;
#endif
fsetpos(fp, &p);
}
#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
#include <io.h>
#include <errno.h>
//#include <windows.h>
static void ffs_fflush(FILE *fp)
{
fflush(fp);
_commit(_fileno(fp));
// HANDLE hFile= (HANDLE)_get_osfhandle(_fileno(fp));
// FlushFileBuffers(hFile);
}
#elif defined(__HAIKU__)
#include <unistd.h>
static void ffs_fflush(FILE *fp)
{
fflush(fp);
fsync(fileno(fp));
}
#else
#include <unistd.h>
static void ffs_fflush(FILE *fp)
{
fflush(fp);
fdatasync(fileno(fp));
}
#endif
// =====================================
// platform depend stop
// =====================================
// block尺寸
#define BLOCKSIZE 512
// 一个block中最多能容纳的文件/目录项数量
#define BLOCK_ITEM_MAXCOUNT 20
// block的开头长度:tmpindex + nextblockindex + prevblockindex
#define BLOCK_HEAD 12
// 文件/目录名最大长度
#define BLOCK_NAME_MAXSIZE 14
// .->start_blockindex在block中的位置, head 12 + state 1 + name 14
#define BLOCK_START_BLOCKINDEX 27
// .->stop_blockindex在block中的位置, head 12 + state 1 + name 14 + start 4
#define BLOCK_STOP_BLOCKINDEX 31
// .->listsize在block中的位置, head 12 + state 1 + name 14 + start 4 + stop + 4
#define BLOCK_OFFSET 35
static unsigned char magic_number[4] = {0x78, 0x11, 0x45, 0x14};
typedef struct FFS_FILE {
/*
0 - "r" (可读,不可写,必须存在)
1 - "w" (不可读,可写,无须存在,清空)
2 - "a" (不可读,可写,无须存在,追加)
3 - "r+" (可读,可写,必须存在)
4 - "w+" (可读,可写,无须存在,清空)
5 - "a+" (可读,可写,无须存在,追加)
*/
unsigned char mode;
unsigned int dir_blockindex;
unsigned short dir_offset;
unsigned int file_start_blockindex, file_stop_blockindex;
unsigned short file_offset;
unsigned int pos_blockindex;
unsigned short pos_offset;
unsigned long long pos;
} FFS_FILE;
typedef struct FFS_DIR {
unsigned int blockindex;
unsigned char block[BLOCKSIZE];
int searchindex; // 0 - BLOCK_ITEM_MAXCOUNT-1
unsigned int stop_blockindex;
unsigned short offset;
FFS_dirent dirp;
} FFS_DIR;
typedef struct TMP TMP;
typedef struct TMP {
unsigned char state; // 0-normal, 1-auto commit, 2-manu commit
// 执行事务时pwd是独立的
// 进入事务时需要将ffs->pwd复制到tmp.pwd,commit后需要再将tmp.pwd复制到ffs->pwd
char *pwd;
int pwd_size;
unsigned int pwd_blockindex;
// 4b(blockindex) + 512b(block)
FILE *fp_cp, *fp_add;
unsigned char cp_size;
unsigned int total_blocksize, unused_blockhead; // 执行fp = ffs_tmpfile()时,同步从orgfile里的block[0]读取这2个值
unsigned int new_total_blocksize, new_unused_blockhead; // 一开始和上面的值相同,会随着tmpfile的处理产生变化
} TMP;
typedef struct FileFS {
char *fn;
FILE *fp;
char *fnj;
TMP tmp;
char *pwd;
int pwd_size;
char *pwd_tmp;
int pwd_tmp_size;
unsigned int pwd_blockindex; // 当前目录所在的block
} FileFS;
// ==========================================
typedef struct BlockArray BlockArray;
typedef struct BlockArray {
unsigned char active;
unsigned char block[BLOCKSIZE];
unsigned int blockindex;
} BlockArray;
static unsigned char tmpstart(FileFS *ffs, unsigned char state);
static void tmpstop(FileFS *ffs);
static unsigned int genblockindex(FileFS *ffs);
static unsigned char readblock(FileFS *ffs, unsigned int blockindex, unsigned char *block);
static unsigned char writeblock(FileFS *ffs, unsigned int blockindex, unsigned char *block);
static unsigned char removeblock(FileFS *ffs, unsigned int blockindex);
static unsigned int findPathBlockindex(FileFS *ffs, unsigned int blockindex, char *pathname);
static void j2ffs(FileFS *ffs);
// ==========================================
static unsigned int B4toU32(unsigned char byte[4])
{
return (unsigned int)(
( byte[0] & 0xFF) |
((byte[1]<<8) & 0xFF00) |
((byte[2]<<16) & 0xFF0000) |
((byte[3]<<24) & 0xFF000000)
);
}
static void U32toB4(unsigned int v, unsigned char byte[4])
{
byte[0] = (unsigned char) ((v & 0x000000FF));
byte[1] = (unsigned char) ((v & 0x0000FF00)>>8);
byte[2] = (unsigned char) ((v & 0x00FF0000)>>16);
byte[3] = (unsigned char) ((v & 0xFF000000)>>24);
}
static unsigned short B2toU16(unsigned char byte[2])
{
return (unsigned int)(
( byte[0] & 0xFF) |
((byte[1]<<8) & 0xFF00)
);
}
static void U16toB2(unsigned short v, unsigned char byte[2])
{
byte[0] = (unsigned char) ((v & 0x00FF));
byte[1] = (unsigned char) ((v & 0xFF00)>>8);
}
// =================================
FileFS *FileFS_create()
{
FileFS *ffs = (FileFS*)malloc(sizeof(FileFS));
if ( ffs == NULL ) return NULL;
memset(ffs, 0, sizeof(FileFS));
return ffs;
}
void FileFS_destroy(FileFS *ffs)
{
if ( ffs == NULL ) return;
FileFS_umount(ffs);
free(ffs);
}
unsigned char FileFS_mkfs(const char *filename)
{
FILE *fp;
fp = fopen(filename, "wb");
if ( fp == NULL ) return 0;
unsigned char block[BLOCKSIZE];
unsigned int n;
unsigned char b4[4];
int k;
unsigned short offset;
unsigned char b2[2];
// block[0]
memset(block, 0, BLOCKSIZE);
k = 0;
// magic number
memcpy(block+k, magic_number, 4); k += 4;
// block size;
n = 2;
U32toB4(n, b4);
memcpy(block+k, b4, 4); k += 4;
// unused block head,此时为0
// other,皆为0
if ( BLOCKSIZE != ffs_fwrite(block, 1, BLOCKSIZE, fp) ) {
ffs_fclose(fp);
return 0;
}
// block[1],根目录
unsigned char state;
char name[BLOCK_NAME_MAXSIZE+1];
memset(block, 0, BLOCKSIZE);
k = 0;
// tmpindex
k += 4;
// next block, 0
k += 4;
// prev block, 0
k += 4;
// dir
// .
// state
state = 0;
memcpy(block+k, &state, 1); k += 1;
// name
memset(name, 0, BLOCK_NAME_MAXSIZE+1);
name[0] = '.';
memcpy(block+k, name, 1); k += BLOCK_NAME_MAXSIZE;
// start_blockindex, 1
n = 1;
U32toB4(n, b4);
memcpy(block+k, b4, 4); k += 4;
// stop_blockindex, 1
memcpy(block+k, b4, 4); k += 4;
// offset
offset = 4+4+4 + 25 + 25; // tmpindex+nextblockindex+prevblockindex + . + ..
U16toB2(offset, b2);
memcpy(block+k, b2, 2); k += 2;
// ..
// state
state = 0;
memcpy(block+k, &state, 1); k += 1;
// name
name[1] = '.'; // ..
memcpy(block+k, name, 2); k += BLOCK_NAME_MAXSIZE;
// start_blockindex, 0
k += 4;
// stop_blockindex, 0
k += 4;
// offset, 0
k += 2;
if ( BLOCKSIZE != ffs_fwrite(block, 1, BLOCKSIZE, fp) ) {
ffs_fclose(fp);
return 0;
}
ffs_fflush(fp);
ffs_fclose(fp);
char *fnj = (char*)malloc(strlen(filename)+1+2);
if ( fnj == NULL ) return 1;
sprintf(fnj, "%s-j", filename);
ffs_remove(fnj);
free(fnj);
return 1;
}
unsigned char FileFS_mount(FileFS *ffs, const char *filename)
{
if ( ffs == NULL ) return 0;
FILE *fp;
fp = fopen(filename, "r+b");
if ( fp == NULL ) return 0;
unsigned char block[BLOCKSIZE];
// ===== block[0]
if ( BLOCKSIZE != ffs_fread(block, 1, BLOCKSIZE, fp) ) {
ffs_fclose(fp);
return 0;
}
int k = 0;
// magic number;
unsigned char mn[4];
memcpy(mn, block+k, 4); k += 4;
//printf("%X %X %X %X\n", mn[0], mn[1], mn[2], mn[3]);
if ( memcmp(mn, magic_number, 4) != 0 ) {
ffs_fclose(fp);
return 0;
}
// block size;
unsigned int bs;
unsigned char b4[4];
memcpy(b4, block+k, 4); k+=4;
bs = B4toU32(b4);
if ( bs < 2 ) {
ffs_fclose(fp);
return 0;
}
// ===== block[1]
if ( BLOCKSIZE != ffs_fread(block, 1, BLOCKSIZE, fp) ) {
ffs_fclose(fp);
return 0;
}
unsigned char state;
char name[BLOCK_NAME_MAXSIZE+1];
memset(name, 0, BLOCK_NAME_MAXSIZE+1);
// block[1],根目录
k = 0;
// tmpindex
k += 4;
// next block
k += 4;
// prev block
k += 4;
// dir
// .
// state
memcpy(&state, block+k, 1); k += 1;
if ( state != 0 ) {
ffs_fclose(fp);
return 0;
}
// name
memcpy(name, block+k, BLOCK_NAME_MAXSIZE); k += BLOCK_NAME_MAXSIZE;
if ( strcmp(name, ".") != 0 ) {
ffs_fclose(fp);
return 0;
}
// start_blockindex, 1
k += 4;
// stop_blockindex,0
k += 4;
// offset, 0
k += 2;
// ..
// state
memcpy(&state, block+k, 1); k += 1;
if ( state != 0 ) {
ffs_fclose(fp);
return 0;
}
// name
memcpy(name, block+k, BLOCK_NAME_MAXSIZE); k += BLOCK_NAME_MAXSIZE;
if ( strcmp(name, "..") != 0 ) {
ffs_fclose(fp);
return 0;
}
// start_blockindex, 0
//k += 4;
// stop_blockindex, 0
//k += 4;
// offset, 0
//k += 2;
if ( ffs->fp != NULL ) {
ffs_fclose(ffs->fp);
ffs->fp = NULL;
}
if ( ffs->fn != NULL ) {
free(ffs->fn);
ffs->fn = NULL;
}
if ( ffs->fnj != NULL ) {
free(ffs->fnj);
ffs->fnj = NULL;
}
ffs->fp = fp;
int len = (int)strlen(filename);
ffs->fn = (char*)malloc(len+1);
if ( ffs->fn == NULL ) {
ffs_fclose(fp);
return 0;
}
strcpy(ffs->fn, filename);
ffs->fnj = (char*)malloc(len+1+2);
if ( ffs->fnj == NULL ) {
ffs_fclose(fp);
return 0;
}
sprintf(ffs->fnj, "%s-j", ffs->fn);
if ( ffs->pwd != NULL ) free(ffs->pwd);
ffs->pwd = (char*)malloc(2);
if ( ffs->pwd == NULL ) {
ffs_fclose(ffs->fp);
return 0;
}
sprintf(ffs->pwd, "/");
ffs->pwd_size = 2;
ffs->pwd_blockindex = 1;
// move data of fn-j to fn;
j2ffs(ffs);
return 1;
}
void FileFS_umount(FileFS *ffs)
{
if ( ffs == NULL ) return;
if ( ffs->fp != NULL ) {
ffs_fclose(ffs->fp);
ffs->fp = NULL;
}
if ( ffs->fn != NULL ) {
free(ffs->fn);
ffs->fn = NULL;
}
if ( ffs->fnj != NULL ) {
ffs_remove(ffs->fnj);
free(ffs->fnj);
ffs->fnj = NULL;
}
if ( ffs->tmp.fp_cp != NULL ) {
ffs_fclose(ffs->tmp.fp_cp);
ffs->tmp.fp_cp = NULL;
}
if ( ffs->tmp.fp_add != NULL ) {
ffs_fclose(ffs->tmp.fp_add);
ffs->tmp.fp_add = NULL;
}
if ( ffs->tmp.pwd != NULL ) {
free(ffs->tmp.pwd);
ffs->tmp.pwd = NULL;
}
ffs->tmp.pwd_size = 0;
if ( ffs->pwd != NULL ) {
free(ffs->pwd);
ffs->pwd = NULL;
}
ffs->pwd_size = 0;
ffs->pwd_blockindex = 0;
if ( ffs->pwd_tmp != NULL ) {
free(ffs->pwd_tmp);
ffs->pwd_tmp = NULL;
}
ffs->pwd_tmp_size = 0;
}
unsigned char FileFS_ismount(FileFS *ffs)
{
if ( ffs == NULL ) return 0;
if ( ffs->fp == NULL ) return 0;
return 1;
}
// =================================
static FFS_FILE *do_fopen_r(FileFS *ffs, char *lastname, unsigned char mode, unsigned int block_head_index)
{
/*
0 - "r" (可读,不可写,必须存在)
3 - "r+" (可读,可写,必须存在)
if not exist, return NULL;
new FFS_FILE;
pos_blockindex = start_blockindex;
pos_offset = 0;
pos = 0;
mode = bmode;
return fp;
*/
unsigned char block[BLOCKSIZE];
unsigned char state, dir_file;
unsigned char b4[4], b2[2];
unsigned int stop_blockindex;
unsigned short offset;
int k;
// block_head
if ( ! readblock(ffs, block_head_index, block) ) return NULL;
memcpy(b4, block+(12+1+14+4), 4);
stop_blockindex = B4toU32(b4);
memcpy(b2, block+(12+1+14+4+4), 2);
offset = B2toU16(b2);
// 搜索block,检查是否有名称相同的文件
unsigned char *dir_block = NULL;
unsigned int dir_blockindex = 0;
unsigned short dir_offset = 0;
int i;
unsigned char flag = 0;
unsigned int index = block_head_index;
char s[BLOCK_NAME_MAXSIZE+1];
memset(s, 0, BLOCK_NAME_MAXSIZE+1);
while (1) {
// blocksize=512,去掉前置的12byte,一共能放下20个子项目(目录或文件)
k = BLOCK_HEAD;
for (i=0; i<BLOCK_ITEM_MAXCOUNT; i++) {
state = block[k]; k++;
memcpy(s, block+k, BLOCK_NAME_MAXSIZE); k+=BLOCK_NAME_MAXSIZE;
if ( strcmp(s, lastname) != 0 ) {
k += 10; // 4+4+2
if ( index == stop_blockindex && k+1 >= offset ) { // 已搜索到最后
return NULL; // file not exist
}
continue;
}
dir_file = state & 0x01;
if ( dir_file == 0 ) {
return NULL; // same path exist;
}
dir_block = block;
dir_blockindex = index;
dir_offset = k + 10; // 位置在尾部
flag = 1;
break;
}
if ( flag ) break;
// get nextblockindex;
memcpy(b4, block+4, 4);
index = B4toU32(b4);
if ( index == 0 ) return NULL; // 到这里说明block有问题
if ( ! readblock(ffs, index, block) ) return NULL; // 到这里说明block有问题
}
if ( dir_block == NULL ) return NULL;
FFS_FILE *ff;
ff = (FFS_FILE*)malloc(sizeof(FFS_FILE));
if ( ff == NULL ) return NULL;
ff->mode = mode;
ff->dir_blockindex = dir_blockindex;
ff->dir_offset = dir_offset;
memcpy(b4, dir_block+dir_offset-10, 4);
ff->file_start_blockindex = B4toU32(b4); // 若为0,则说明尚未创建包含文件内容的block
memcpy(b4, dir_block+dir_offset-6, 4);
ff->file_stop_blockindex = B4toU32(b4);
memcpy(b2, dir_block+dir_offset-2, 2);
ff->file_offset = B2toU16(b2);
ff->pos_blockindex = ff->file_start_blockindex;
ff->pos_offset = BLOCK_HEAD;
ff->pos = 0;
return ff;
}
// 创建文件item
static unsigned char do_fopen_createfileitem(FileFS *ffs, char *lastname,
unsigned int org_start_blockindex, unsigned int org_stop_blockindex, unsigned short org_offset,
unsigned char *dir_block, unsigned int *dir_blockindex, unsigned short *dir_offset)
{
/*
if ( offset == BLOCKSIZE ) {
new_blockindex = genblock;
block_stop->nextblockindex = new_blockindex;
add lastname to block_new;
start_blockindex = stop_blockindex = offset = 0;
block_head->stop_blockindex = new_blockindex;
block_head->offset = 12 + 25;
} else {
add lastname to offset;
start_blockindex = stop_blockindex = offset = 0;
offset += 25;
}
*/
int i, k;
unsigned short new_offset;
unsigned char b4[4], b2[2];
BlockArray ba[2];
int ba_used = 0;
unsigned char *block_start, *block_stop;
unsigned int block_start_index, block_stop_index;
for (i=0; i<2; i++) ba[i].active = 0;
// block_start
if ( ! readblock(ffs, org_start_blockindex, ba[0].block) ) return 0;
ba[0].blockindex = org_start_blockindex;
ba[0].active = 1;
block_start = ba[0].block;
block_start_index = ba[0].blockindex;
ba_used = 1;
// block_stop
if ( org_stop_blockindex == org_start_blockindex ) {
block_stop = block_start;
block_stop_index = block_start_index;
} else {
if ( ! readblock(ffs, org_stop_blockindex, ba[1].block) ) return 0;
ba[1].blockindex = org_stop_blockindex;
ba[1].active = 1;
block_stop = ba[1].block;
block_stop_index = ba[1].blockindex;
ba_used++;
}
if ( ffs->tmp.state == 0 ) tmpstart(ffs, 1);
// dir_block未填满
if ( org_offset < BLOCKSIZE ) {
// == 向org_stop_block写入lastname目录项
k = org_offset;
// state
block_stop[k] = 1; // file
k++;
memset(block_stop+k, 0, BLOCK_NAME_MAXSIZE);
memcpy(block_stop+k, lastname, (int)strlen(lastname));
k += BLOCK_NAME_MAXSIZE;
k += 4; // start_blockindex
k += 4; // stop_blockindex
k += 2; // offset
new_offset = k;
U16toB2(new_offset, b2);
memcpy(block_start + BLOCK_OFFSET, b2, 2);
for (i=0; i<2; i++) {
if ( ba[i].active ) {
if ( ! writeblock(ffs, ba[i].blockindex, ba[i].block) ) {
if ( ffs->tmp.state == 1 ) tmpstop(ffs);
return 0;
}
}
}
if ( ffs->tmp.state == 1 ) {
if ( ! FileFS_commit(ffs) ) {
return 0;
}
}
memcpy(dir_block, block_stop, BLOCKSIZE);
*dir_blockindex = block_stop_index;
*dir_offset = new_offset;
return 1;
}
// 最后一个block已填满
// ======================================
unsigned int blockindex_2;
unsigned char block_2[BLOCKSIZE];
// 创建存储lastname的目录延伸块
// gen block_2 for lastname;
// block_2->prevblockindex = cur_blockindex;
// write;
blockindex_2 = genblockindex(ffs);
if ( blockindex_2 == 0 ) {
if ( ffs->tmp.state == 1 ) tmpstop(ffs);
return 0;
}
memset(block_2, 0, BLOCKSIZE);
k = 8;
// prevblockindex
U32toB4(org_stop_blockindex, b4);
memcpy(block_2+k, b4, 4);
k += 4;
// state
block_2[k] = 1; // file
k++;
memset(block_2+k, 0, BLOCK_NAME_MAXSIZE);
memcpy(block_2+k, lastname, (int)strlen(lastname));
k += BLOCK_NAME_MAXSIZE;
k += 4; // start_blockindex
k += 4; // stop_blockindex
k += 2; // offset
new_offset = k;
U16toB2(new_offset, b2);
memcpy(block_start + BLOCK_OFFSET, b2, 2);
U32toB4(blockindex_2, b4);
memcpy(block_start + BLOCK_STOP_BLOCKINDEX, b4, 4);
memcpy(block_stop + 4, b4, 4);
for (i=0; i<2; i++) {
if ( ba[i].active ) {
if ( ! writeblock(ffs, ba[i].blockindex, ba[i].block) ) {
if ( ffs->tmp.state == 1 ) tmpstop(ffs);
return 0;
}
}
}
if ( ffs->tmp.state == 1 ) {
if ( ! FileFS_commit(ffs) ) {
return 0;
}
}
memcpy(dir_block, block_2, BLOCKSIZE);
*dir_blockindex = blockindex_2;
*dir_offset = new_offset;
return 1;
}
// 删除文件内容
static unsigned char do_fopen_cleanfilecontent(FileFS *ffs, unsigned char *dir_block, unsigned int dir_blockindex, unsigned short dir_offset)
{
/*
readblock(file->start_blockindex);
readblock(file->stop_blockindex);
stop_block->nextblockindex = new_unused_blockindex;
ffs->tmp.new_unused_blockindex = start_blockindex;
file->start_blockindex = 0;
file->stop_blockindex = 0;
file->offset = 0;
*/
unsigned char b4[4];
unsigned int file_start_blockindex, file_stop_blockindex;
memcpy(b4, dir_block+dir_offset-10, 4);
file_start_blockindex = B4toU32(b4);
memcpy(b4, dir_block+dir_offset-6, 4);
file_stop_blockindex = B4toU32(b4);
if ( file_start_blockindex == 0 ) return 1; // 文件存在,但无内容
if ( ffs->tmp.state == 0 ) tmpstart(ffs, 1);
// file block_stop
unsigned char file_block_stop[BLOCKSIZE];
if ( ! readblock(ffs, file_stop_blockindex, file_block_stop) ) {
if ( ffs->tmp.state == 1 ) tmpstop(ffs);
return 0;
}
U32toB4(ffs->tmp.new_unused_blockhead, b4);
memcpy(file_block_stop + 4, b4, 4);
ffs->tmp.new_unused_blockhead = file_start_blockindex;
// set dir->start stop offset -> 0
memset(dir_block + dir_offset - 10, 0, 10);
if ( ! writeblock(ffs, dir_blockindex, dir_block) ) {
if ( ffs->tmp.state == 1 ) tmpstop(ffs);
return 0;
}
if ( ! writeblock(ffs, file_stop_blockindex, file_block_stop) ) {
if ( ffs->tmp.state == 1 ) tmpstop(ffs);
return 0;
}
if ( ffs->tmp.state == 1 ) {
if ( ! FileFS_commit(ffs) ) {
return 0;
}
}
return 1;
}
static FFS_FILE *do_fopen_w(FileFS *ffs, char *lastname, unsigned char mode, unsigned int block_head_index)
{
/*
1 - "w" (不可读,可写,无须存在,清空)
4 - "w+" (可读,可写,无须存在,清空)
if exist {
readblock(file->start_blockindex);
readblock(file->stop_blockindex);
stop_block->nextblockindex = new_unused_blockindex;
ffs->tmp.new_unused_blockindex = start_blockindex;
file->start_blockindex = 0;
file->stop_blockindex = 0;
file->offset = 0;
} else {
// new file item
if ( offset == BLOCKSIZE ) {
new_blockindex = genblock;
block_stop->nextblockindex = new_blockindex;
add lastname to block_new;
start_blockindex = stop_blockindex = offset = 0;
block_head->stop_blockindex = new_blockindex;
block_head->offset = 12 + 25;
} else {
add lastname to offset;
start_blockindex = stop_blockindex = offset = 0;
offset += 25;
}
}
new fp;
pos_blockindex = 0;
pos_offsize = 0;
fp->pos = 0;
fp->mode = bmode;
*/
unsigned char block[BLOCKSIZE];
unsigned char state, dir_file;
unsigned char b4[4], b2[2];
unsigned int stop_blockindex;
unsigned short offset;
int k;
// block_head
if ( ! readblock(ffs, block_head_index, block) ) return NULL;
memcpy(b4, block+(12+1+14+4), 4);
stop_blockindex = B4toU32(b4);
memcpy(b2, block+(12+1+14+4+4), 2);
offset = B2toU16(b2);
// 搜索block,检查是否有名称相同的文件
unsigned char *dir_block = NULL;
unsigned int dir_blockindex = 0;
unsigned short dir_offset = 0;
int i;
unsigned char flag = 0;
unsigned int index = block_head_index;
char s[BLOCK_NAME_MAXSIZE+1];
memset(s, 0, BLOCK_NAME_MAXSIZE+1);
while (1) {
// blocksize=512,去掉前置的12byte,一共能放下20个子项目(目录或文件)
k = BLOCK_HEAD;
for (i=0; i<BLOCK_ITEM_MAXCOUNT; i++) {
state = block[k]; k++;
memcpy(s, block+k, BLOCK_NAME_MAXSIZE); k+=BLOCK_NAME_MAXSIZE;
if ( strcmp(s, lastname) != 0 ) {
k += 10; // 4+4+2
if ( index == stop_blockindex && k+1 >= offset ) { // 已搜索到最后
flag = 1;
break;
}
continue;
}
dir_file = state & 0x01;
if ( dir_file == 0 ) {
return NULL; // same path exist;
}
dir_block = block;
dir_blockindex = index;
dir_offset = k + 10; // 位置在尾部
flag = 1;
break;
}
if ( flag ) break;
// get nextblockindex;
memcpy(b4, block+4, 4);
index = B4toU32(b4);
if ( index == 0 ) return NULL; // 到这里说明block有问题
if ( ! readblock(ffs, index, block) ) return NULL; // 到这里说明block有问题
}
if ( dir_block == NULL ) { // not exist
// 创建文件item
if ( ! do_fopen_createfileitem(ffs, lastname, block_head_index, stop_blockindex, offset,
block, &dir_blockindex, &dir_offset) ) {
return NULL;
}
dir_block = block;
} else {
// 删除文件内容
if ( ! do_fopen_cleanfilecontent(ffs, block, dir_blockindex, dir_offset) ) {
return NULL;
}
}
FFS_FILE *ff;
ff = (FFS_FILE*)malloc(sizeof(FFS_FILE));
if ( ff == NULL ) return NULL;
ff->mode = mode;
ff->dir_blockindex = dir_blockindex;
ff->dir_offset = dir_offset;
ff->file_start_blockindex = 0;
ff->file_stop_blockindex = 0;
ff->file_offset = 0;
ff->pos_blockindex = 0;
ff->pos_offset = 0;
ff->pos = 0;
return ff;
}
static FFS_FILE *do_fopen_a(FileFS *ffs, char *lastname, unsigned char mode, unsigned int block_head_index)
{
/*
2 - "a" (不可读,可写,无须存在,追加)
5 - "a+" (可读,可写,无须存在,追加)
if not exist {
if ( offset == BLOCKSIZE ) {
new_blockindex = genblock;
block_stop->nextblockindex = new_blockindex;
add lastname to block_new;
start_blockindex = stop_blockindex = offset = 0;
block_head->stop_blockindex = new_blockindex;
block_head->offset = 12 + 25;
} else {
add lastname to offset;
start_blockindex = stop_blockindex = offset = 0;
offset += 25;
}
new fp;
pos_blockindex = 0;
pos_offsize = 0;
fp->pos = 0;
fp->mode = bmode;
} else {
new fp;
pos_blockindex = stop_blockindex;
pos_offsize = offset;
while (1) {
from start_blockindex to stop_blockindex;
fp->pos += 512;
}
fp->pos += offset;
fp->mode = bmode;
}
*/
unsigned char block[BLOCKSIZE];
unsigned char state, dir_file;
unsigned char b4[4], b2[2];
unsigned int stop_blockindex;
unsigned short offset;
int k;
// block_head
if ( ! readblock(ffs, block_head_index, block) ) return NULL;
memcpy(b4, block+(12+1+14+4), 4);
stop_blockindex = B4toU32(b4);
memcpy(b2, block+(12+1+14+4+4), 2);
offset = B2toU16(b2);
// 搜索block,检查是否有名称相同的文件