-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile.c
More file actions
1460 lines (1173 loc) · 36.9 KB
/
Copy pathfile.c
File metadata and controls
1460 lines (1173 loc) · 36.9 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
#include <dolphinfs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
extern char generic_io_block[BLOCK_SIZE];
extern char allocator_io_block[BLOCK_SIZE];
struct ofile open_files[MAX_OPEN_FILE];
long free_file_block(struct file *f, unsigned long off);
long walk_file_block(struct file *f, unsigned long off);
long alloc_file_num(struct super_block *sb)
{
/* walk all man block */
unsigned long start, end, next;
unsigned long file_num;
start = sb->file_bitmap_start;
end = sb->file_bitmap_end;
next = start;
for (; next < end; next++) {
memset(allocator_io_block, 0, sizeof(allocator_io_block));
cached_read_block(sb->blkdev, next, allocator_io_block);
file_num = scan_free_bits(allocator_io_block, sb->block_size);
/* 扫描成功 */
if (sb->block_size * 8 != file_num) {
// printf("---> get block id:%ld\n", data_block_id);
/* 如果不是第一个块,就乘以块偏移 */
if ((next - start) != 0)
file_num += (next - start) * (sb->block_size * 8); /* 一个块标记4096*8个位 */
cached_write_block(sb->blkdev, next, allocator_io_block);
// printf("---> alloc block:%ld\n", data_block_id + sb->block_off[BLOCK_AREA_DATA]);
if (file_num < sb->file_count) {
return file_num;
} else {
return -1;
}
}
}
printf("!!!WARN no free file\n");
return -1;
}
int free_file_num(struct super_block *sb, long file_num)
{
unsigned long bmap_block, byte_off, bits_off;;
unsigned long data_block_id, data_block_byte;
// printf("---> free block:%ld\n", blk);
if (file_num < 0 || file_num >= sb->file_count)
return -1;
/* 获取位偏移 */
bits_off = file_num % 8;
data_block_byte = file_num / 8;
/* 获取块内字节偏移 */
byte_off = data_block_byte % sb->block_size;
/* 获取块偏移 */
bmap_block = data_block_byte / sb->block_size;
/* 转换出逻辑块 */
bmap_block += sb->file_bitmap_start;
memset(allocator_io_block, 0, sizeof(allocator_io_block));
cached_read_block(sb->blkdev, bmap_block, allocator_io_block);
allocator_io_block[byte_off] &= ~(1 << bits_off);
/* 修改块 */
cached_write_block(sb->blkdev, bmap_block, allocator_io_block);
return 0;
}
int load_file_info(struct super_block *sb, struct file *file, long num)
{
struct file *file_table;
unsigned long file_info_block, block_off;
unsigned long file_count_in_block;
file_count_in_block = sb->block_size / sizeof(struct file);
block_off = num % file_count_in_block;
file_info_block = num / file_count_in_block;
file_info_block += sb->file_info_start;
/* 通过file_info_block读取文件所在的块 */
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, file_info_block, generic_io_block);
/* 通过block_off定位某个文件 */
file_table = (struct file *)generic_io_block;
*file = file_table[block_off];
return 0;
}
static int sync_file_name(struct super_block *sb, unsigned long num, char *name)
{
struct file_name *file_name;
unsigned long file_name_block, block_off;
unsigned long file_count_in_block;
file_count_in_block = sb->block_size / sizeof(struct file_name);
block_off = num % file_count_in_block;
file_name_block = num / file_count_in_block;
file_name_block += sb->file_name_start;
/* 通过file_info_block读取文件所在的块 */
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, file_name_block, generic_io_block);
/* 通过block_off定位某个文件 */
file_name = (struct file_name *)generic_io_block;
strncpy(file_name[block_off].buf, name, FILE_NAME_LEN);
cached_write_block(sb->blkdev, file_name_block, generic_io_block);
}
static int load_file_name(struct super_block *sb, unsigned long num, char *name, int len)
{
struct file_name *file_name;
unsigned long file_name_block, block_off;
unsigned long file_count_in_block;
file_count_in_block = sb->block_size / sizeof(struct file_name);
block_off = num % file_count_in_block;
file_name_block = num / file_count_in_block;
file_name_block += sb->file_name_start;
/* 通过file_info_block读取文件所在的块 */
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, file_name_block, generic_io_block);
/* 通过block_off定位某个文件 */
file_name = (struct file_name *)generic_io_block;
strncpy(name, file_name[block_off].buf, min(len, FILE_NAME_LEN));
return 0;
}
long walk_file_name(struct super_block *sb, long file_num, void *buf, long len)
{
/* 遍历到最大值或者最小值则返回-1,表示遍历完成 */
if (file_num < 0 || file_num >= MAX_FILES)
return -1;
/* 检查file_num是否已经被分配 */
load_file_name(sb, file_num, buf, len);
/* 返回下一个可以检查的文件号 */
return file_num + 1;
}
int sync_file_info(struct super_block *sb, struct file *file, long num)
{
struct file *file_table;
unsigned long file_info_block, block_off;
unsigned long file_count_in_block;
file_count_in_block = sb->block_size / sizeof(struct file);
block_off = num % file_count_in_block;
file_info_block = num / file_count_in_block;
file_info_block += sb->file_info_start;
/* 通过file_info_block读取文件所在的块 */
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, file_info_block, generic_io_block);
/* 通过block_off定位某个文件 */
file_table = (struct file *)generic_io_block;
file_table[block_off] = *file;
cached_write_block(sb->blkdev, file_info_block, generic_io_block);
return 0;
}
/**
* 分配一个文件号,并分配内存保存数据
*/
struct file *alloc_file(struct super_block *sb)
{
long num = alloc_file_num(sb);
if (num < 0) {
return NULL;
}
/* load file num from disk */
struct file *f = malloc(sizeof(struct file));
f->num = num;
f->sb = sb;
return f;
}
/**
* 释放文件号,并释放内存
*/
void free_file(struct file *file, int sync)
{
if (sync) {
struct file empty_file;
memset(&empty_file, 0, sizeof(struct file));
sync_file_info(file->sb, &empty_file, file->num);
/* clear name */
struct file_name file_name = {0};
sync_file_name(file->sb, file->num, file_name.buf);
}
free_file_num(file->sb, file->num);
free(file);
}
void dump_all_file(struct super_block *sb)
{
int i;
struct file *f;
printf("==== dump files ====\n");
struct file tmp_file;
for (i = 0; i < sb->file_count; i++) {
load_file_info(sb, &tmp_file, i);
f = &tmp_file;
if (f->hash != 0) {
printf(" [%d] hash:%p, table:%lld, size:%lld, ref:%d, mode:%x\n",
i, f->hash, f->data_table, f->file_size, f->ref, f->mode);
}
}
}
struct ofile *alloc_ofile(void)
{
int i;
struct ofile *of;
for (i = 0; i < MAX_OPEN_FILE; i++) {
of = &open_files[i];
if (of->f == NULL) {
return of;
}
}
return NULL;
}
#define ofile_idx(of) ((of) - &open_files[0])
#define idx_ofile(idx) (&open_files[(idx)])
/**
* 1. path -> file
* 2. file -> block table
* 3. block table -> block
*/
unsigned long str2hash(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
struct file *create_file(struct super_block *sb, char *path, int mode)
{
if (!path || !strlen(path) || !mode)
return NULL;
struct file *f = alloc_file(sb);
if (!f)
return NULL;
time_t current_time = time(NULL);
f->hash = str2hash(path);
f->data_table = alloc_data_block(sb);
f->ref = 0;
f->mode = mode;
f->file_size = 0;
f->type = (mode & FF_DIR) ? FILE_TYPE_DIR : FILE_TYPE_REGULAR;
f->permissions = FILE_PERM_ALL;
f->owner = 0;
f->lock_count = 0;
f->create_time = current_time;
f->modify_time = current_time;
f->access_time = current_time;
if (!f->data_table) {
free_file(f, 0);
return NULL;
}
/* NOTICE: clear data table */
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_write_block(sb->blkdev, f->data_table, generic_io_block);
/* sync file to disk */
sync_file_info(sb, f, f->num);
/* sync file name to disk */
sync_file_name(sb, f->num, path);
return f;
}
static struct file *search_file(struct super_block *sb, char *path)
{
if (!path || !strlen(path))
return NULL;
int i;
struct file *f = malloc(sizeof(struct file));
if (!f) {
return NULL;
}
for (i = 0; i < sb->file_count; i++) {
load_file_info(sb, f, i);
if (f->hash == str2hash(path)) {
f->sb = sb;
return f;
}
}
free(f);
return NULL;
}
static int check_empty_u32(unsigned int *table, unsigned long size)
{
int i;
for (i = 0; i < size; i++) {
if (table[i] != 0) {
return 0;
}
}
return 1;
}
int delete_file(struct super_block *sb, char *path)
{
if (!path || !strlen(path))
return -1;
struct file *f = search_file(sb, path);
if (!f) {
printf("delete: err: file %s not found\n", path);
return -1;
}
/* 1. 删除文件数据 */
if (f->file_size > 0) {
unsigned long blocks;
blocks = DIV_ROUND_UP(f->file_size, sb->block_size);
int i;
for (i = 0; i < blocks; i++) {
long ret = walk_file_block(f, i * sb->block_size);
assert(ret > 0);
}
for (i = 0; i < blocks; i++) {
long ret = free_file_block(f, i * sb->block_size);
if (ret != 0) {
printf("[ERR] delete_file: free block %d failed with %d\n", i, ret);
}
}
}
assert(f->data_table > 0);
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, f->data_table, generic_io_block);
assert(check_empty_u32(generic_io_block, sizeof(generic_io_block) / sizeof(unsigned int)));
/* free data table */
free_block(sb, f->data_table);
/* 2. 删除文件结构 */
free_file(f, 1);
return 0;
}
int open_file(struct super_block *sb, char *path, int flags)
{
if (!path || !strlen(path) || !flags)
return -1;
struct file *f = search_file(sb, path);
if (!f) {
/* file not found, create new */
if (flags & FF_CRATE) {
f = create_file(sb, path, flags & (~FF_CRATE));
if (!f) {
return -1;
}
} else {
return -1;
}
}
struct ofile *of = alloc_ofile();
if (!of)
return -1;
of->f = f;
of->oflags = flags;
of->off = 0;
of->sb = sb;
f->ref++;
sync_file_info(sb, f, f->num);
return ofile_idx(of);
}
int close_file(int file)
{
if (file < 0 || file >= MAX_OPEN_FILE)
return -1;
struct ofile *of = idx_ofile(file);
if (!of->f)
return -1;
of->f->ref--;
of->oflags = 0;
of->off = 0;
sync_file_info(of->f->sb, of->f, of->f->num);
/* 释放文件信息内存 */
free(of->f);
of->f = NULL;
return 0;
}
long get_file_block(struct file *f, unsigned long off)
{
unsigned int *l1, *l2;
unsigned int l1_val, l2_val;
struct super_block *sb = f->sb;
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, f->data_table, generic_io_block);
l1 = (unsigned int *)generic_io_block;
assert(l1 != NULL);
l1_val = l1[GET_BGD_OFF(off)];
if (!l1_val) { // alloc BMD block
l1_val = alloc_data_block(sb);
if (!l1_val) {
return -1;
}
l1[GET_BGD_OFF(off)] = l1_val;
// sync block
cached_write_block(sb->blkdev, f->data_table, generic_io_block);
// clear new block
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_write_block(sb->blkdev, l1_val, generic_io_block);
}
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, l1_val, generic_io_block);
l2 = (unsigned int *)generic_io_block;
l2_val = l2[GET_BMD_OFF(off)];
if (!l2_val) { // alloc data block
l2_val = alloc_data_block(sb);
if (!l2_val) {
return -1;
}
l2[GET_BMD_OFF(off)] = l2_val;
// sync block
cached_write_block(sb->blkdev, l1_val, generic_io_block);
// clear new block
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_write_block(sb->blkdev, l2_val, generic_io_block);
}
return l2_val;
}
long walk_file_block(struct file *f, unsigned long off)
{
unsigned int *l1, *l2;
unsigned int l1_val, l2_val;
struct super_block *sb = f->sb;
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, f->data_table, generic_io_block);
l1 = (unsigned int *)generic_io_block;
assert(l1 != NULL);
l1_val = l1[GET_BGD_OFF(off)];
if (!l1_val) { // alloc BMD block
printf("ERR off:%d, l1_val:%d\n", off, l1_val);
return 0;
}
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, l1_val, generic_io_block);
l2 = (unsigned int *)generic_io_block;
l2_val = l2[GET_BMD_OFF(off)];
if (!l2_val) { // alloc data block
printf("ERR off:%d->%d, l2_val:%d\n", off, GET_BMD_OFF(off), l2_val);
return 0;
}
return l2_val;
}
long free_file_block(struct file *f, unsigned long off)
{
unsigned int *l1, *l2;
unsigned int l1_val, l2_val;
int i;
struct super_block *sb = f->sb;
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, f->data_table, generic_io_block);
l1 = (unsigned int *)generic_io_block;
if (l1 == NULL) {
return -1;
}
l1_val = l1[GET_BGD_OFF(off)];
if (!l1_val) { // none BMD block
return -2;
}
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, l1_val, generic_io_block);
l2 = (unsigned int *)generic_io_block;
l2_val = l2[GET_BMD_OFF(off)];
if (!l2_val) { // none data block
return -3;
}
/* free data block */
free_data_block(sb, l2_val);
l2[GET_BMD_OFF(off)] = 0; /* clear data block in l2 */
// sync block
cached_write_block(sb->blkdev, l1_val, generic_io_block);
/* check l2 empty? */
if (!check_empty_u32(generic_io_block, sizeof(generic_io_block) / sizeof(unsigned int))) {
return 0;
}
/* l2 empty !*/
memset(generic_io_block, 0, sizeof(generic_io_block));
cached_read_block(sb->blkdev, f->data_table, generic_io_block);
l1 = (unsigned int *)generic_io_block;
/* free l2 block */
free_block(sb, l1_val);
// printf("%s:%d: free l2 block: %d at %d\n", __func__, __LINE__, l1_val, l2_val);
l1[GET_BGD_OFF(off)] = 0; /* clear l2 block in l1 */
// sync block
cached_write_block(sb->blkdev, f->data_table, generic_io_block);
return 0;
}
/**
* 按照页面的方式寻址,并写入数据。
*/
long do_write_file(struct file *f, long off, void *buf, long len)
{
long vir_blk, phy_blk, blk_off, chunk;
long next_off;
long left_len; // 必须是有符号数
long write_len;
char *pbuf;
long ret;
struct super_block *sb = f->sb;
next_off = off;
left_len = len;
write_len = 0;
pbuf = buf;
while (left_len > 0) {
/* 计算出需要访问的位置,以及大小 */
vir_blk = next_off / BLOCK_SIZE;
blk_off = next_off % BLOCK_SIZE;
chunk = min(left_len, BLOCK_SIZE - blk_off);
/* 通过文件表获取物理块位置 */
ret = get_file_block(f, vir_blk * BLOCK_SIZE);
if (ret < 0) { // 没有可用块
return -1;
}
phy_blk = ret;
/* 将数据读取块 */
memset(generic_io_block, 0, sizeof(generic_io_block));
ret = cached_read_block(sb->blkdev, phy_blk, generic_io_block);
if (ret != sizeof(generic_io_block)) { // 写IO失败
return -1;
}
/* 拷贝数据 */
memcpy(generic_io_block + blk_off, pbuf, chunk);
/* 将数据写入块 */
ret = cached_write_block(sb->blkdev, phy_blk, generic_io_block);
if (ret != sizeof(generic_io_block)) { // 写IO失败
return -1;
}
/* 只有在偏移+chunk大小超过文件大小的时候,才去更新文件大小 */
if (next_off + chunk > f->file_size) {
f->file_size = next_off + chunk;
sync_file_info(sb, f, f->num);
}
write_len += chunk;
pbuf += chunk;
next_off += chunk;
left_len -= chunk;
}
return write_len;
}
long write_file(int file, void *buf, long len)
{
long ret;
if (file < 0 || file >= MAX_OPEN_FILE)
return -1;
struct ofile *of = idx_ofile(file);
if (!of->f)
return -1;
/* 检查写权限 */
if (!check_permission(of->f, FILE_PERM_WRITE, of->f->owner)) {
printf("[WRITE] Permission denied for file %d\n", of->f->num);
return -1;
}
ret = do_write_file(of->f, of->off, buf, len);
if (ret > 0) {
of->off += ret;
update_modify_time(of->f);
sync_file_info(of->f->sb, of->f, of->f->num);
}
return ret;
}
long do_read_file(struct file *f, long off, void *buf, long len)
{
long vir_blk, phy_blk, blk_off, chunk;
long next_off;
long left_len; // 必须是有符号数
long file_left; // 必须是有符号数
long read_len;
char *pbuf;
long ret;
long file_size;
struct super_block *sb = f->sb;
file_size = f->file_size;
next_off = off;
left_len = len;
read_len = 0;
pbuf = buf;
if (off >= file_size)
return 0;
file_left = file_size - next_off;
while (left_len > 0 && (file_left) > 0) {
/* 计算出需要访问的位置,以及大小 */
vir_blk = next_off / BLOCK_SIZE;
blk_off = next_off % BLOCK_SIZE;
/* 读取buf大小,直到文件结束 */
chunk = min(left_len, BLOCK_SIZE - blk_off);
chunk = min(chunk, file_left);
/* 通过文件表获取物理块位置 */
ret = get_file_block(f, vir_blk * BLOCK_SIZE);
if (ret < 0) { // 没有可用块
return -1;
}
phy_blk = ret;
/* 将数据读取块 */
memset(generic_io_block, 0, sizeof(generic_io_block));
ret = cached_read_block(sb->blkdev, phy_blk, generic_io_block);
if (ret != sizeof(generic_io_block)) { // 写IO失败
return -1;
}
/* 拷贝数据 */
memcpy(pbuf, generic_io_block + blk_off, chunk);
read_len += chunk;
pbuf += chunk;
next_off += chunk;
left_len -= chunk;
file_left = file_size - next_off;
}
return read_len;
}
long read_file(int file, void *buf, long len)
{
long ret;
if (file < 0 || file >= MAX_OPEN_FILE)
return -1;
struct ofile *of = idx_ofile(file);
if (!of->f)
return -1;
/* 检查读权限 */
if (!check_permission(of->f, FILE_PERM_READ, of->f->owner)) {
printf("[READ] Permission denied for file %d\n", of->f->num);
return -1;
}
ret = do_read_file(of->f, of->off, buf, len);
if (ret > 0) {
of->off += ret;
update_access_time(of->f);
sync_file_info(of->f->sb, of->f, of->f->num);
}
return ret;
}
int seek_file(int file, int off, int pos)
{
long ret;
if (file < 0 || file >= MAX_OPEN_FILE)
return -1;
struct ofile *of = idx_ofile(file);
struct file *f;
if (!of->f)
return -1;
ret = of->off;
switch (pos) {
case FP_SET:
of->off = off;
break;
case FP_CUR:
of->off += off;
break;
case FP_END:
f = of->f;
of->off = f->file_size + off;
if (of->off > f->file_size)
of->off = f->file_size;
break;
default:
break;
}
return of->off;
}
long tell_file(int file)
{
long ret;
if (file < 0 || file >= MAX_OPEN_FILE)
return -1;
struct ofile *of = idx_ofile(file);
struct file *f;
if (!of->f)
return -1;
return of->off;
}
void list_files(struct super_block *sb)
{
long idx, next;
char name[FILE_NAME_LEN];
idx = 0;
while (idx >= 0) {
memset(name, 0, sizeof(name));
next = walk_file_name(sb, idx, name, FILE_NAME_LEN);
if (name[0] != 0) {
struct file_stat stat;
stat_file(sb, name, &stat);
printf("[%d] name:%s num: %d size: %d mode:%x\n", idx, name, stat.num, stat.file_size, stat.mode);
}
idx = next;
}
}
int rename_file(struct super_block *sb, char *src_name, char *dest_name)
{
struct file *f;
if (!src_name || !dest_name) {
return -1;
}
f = search_file(sb, src_name);
if (!f) {
return -1;
}
/* 修改名字 */
sync_file_name(f->sb, f->num, dest_name);
/* 修改名字hash */
f->hash = str2hash(dest_name);
sync_file_info(sb, f, f->num);
return 0;
}
int stat_file(struct super_block *sb, char *path, struct file_stat *stat)
{
struct file *f;
if (!path) {
return -1;
}
f = search_file(sb, path);
if (!f) {
return -1;
}
stat->file_size = f->file_size;
stat->num = f->num;
stat->mode = f->mode;
stat->type = f->type;
stat->permissions = f->permissions;
stat->owner = f->owner;
stat->create_time = f->create_time;
stat->modify_time = f->modify_time;
stat->access_time = f->access_time;
return 0;
}
/* ==================== 符号链接功能 ==================== */
int create_symlink(struct super_block *sb, const char *target, const char *link_path)
{
if (!sb || !target || !link_path) {
return -1;
}
/* 检查目标路径是否存在 */
struct file_stat target_stat;
if (stat_file(sb, target, &target_stat) != 0) {
printf("[SYMLINK] Target %s does not exist\n", target);
return -1;
}
/* 创建符号链接文件 */
struct file *link_file = create_file(sb, (char *)link_path, FF_SYMLINK | FF_READ);
if (!link_file) {
printf("[SYMLINK] Failed to create symlink %s\n", link_path);
return -1;
}
/* 设置文件类型为符号链接 */
link_file->type = FILE_TYPE_SYMLINK;
/* 将目标路径写入文件内容 */
long target_len = strlen(target) + 1;
if (target_len > BLOCK_SIZE) {
printf("[SYMLINK] Target path too long\n");
free_file(link_file, 1);
return -1;
}
/* 写入目标路径 */
long written = do_write_file(link_file, 0, (char *)target, target_len);
if (written != target_len) {
printf("[SYMLINK] Failed to write target path\n");
free_file(link_file, 1);
return -1;
}
link_file->file_size = target_len;
sync_file_info(sb, link_file, link_file->num);
printf("[SYMLINK] Created symlink %s -> %s\n", link_path, target);
return 0;
}
int read_symlink(struct super_block *sb, const char *link_path, char *target, size_t size)
{
if (!sb || !link_path || !target || size == 0) {
return -1;
}
/* 查找符号链接文件 */
struct file *link_file = search_file(sb, (char *)link_path);
if (!link_file) {
printf("[SYMLINK] Symlink %s not found\n", link_path);
return -1;
}
/* 检查是否为符号链接 */
if (link_file->type != FILE_TYPE_SYMLINK) {
printf("[SYMLINK] %s is not a symlink\n", link_path);
free(link_file);
return -1;
}
/* 读取目标路径 */
size_t read_size = (link_file->file_size < size) ? link_file->file_size : size - 1;
long read_len = do_read_file(link_file, 0, target, read_size);
free(link_file);
if (read_len < 0) {
printf("[SYMLINK] Failed to read symlink target\n");
return -1;
}
target[read_len] = '\0';
return 0;
}
int is_symlink(struct super_block *sb, const char *path)
{
if (!sb || !path) {
return 0;
}
struct file *f = search_file(sb, (char *)path);
if (!f) {
return 0;
}
int result = (f->type == FILE_TYPE_SYMLINK);
free(f);
return result;
}
/* ==================== 文件锁定功能 ==================== */
#define MAX_FILE_LOCKS 32
static struct file_lock file_locks[MAX_FILE_LOCKS];
int lock_file(int file, int lock_type)
{
if (file < 0 || file >= MAX_OPEN_FILE) {
return -1;
}
struct ofile *of = idx_ofile(file);
if (!of->f) {
return -1;
}
/* 检查是否已被锁定 */
for (int i = 0; i < MAX_FILE_LOCKS; i++) {
if (file_locks[i].file_num == of->f->num && file_locks[i].file_num != 0) {
/* 检查锁类型兼容性 */
if (lock_type == 1 && file_locks[i].lock_type == 1) {
/* 写锁冲突 */
printf("[LOCK] File %d already has write lock\n", of->f->num);
return -1;
}