Skip to content

Commit f27d817

Browse files
committed
Fix existed bugs & Finish unlink
1 parent 79a5f70 commit f27d817

File tree

9 files changed

+347
-5
lines changed

9 files changed

+347
-5
lines changed

Diff for: .vscode/launch.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
// 使用 IntelliSense 了解相关属性。
3+
// 悬停以查看现有属性的描述。
4+
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
8+
{
9+
"preLaunchTask": "ninja build",
10+
"postDebugTask": "umount Krxk",
11+
"name": "(gdb) 启动",
12+
"type": "cppdbg",
13+
"request": "launch",
14+
"program": "${workspaceFolder}/build/example/sampleFS",
15+
"args": ["-d", "Krxk"],
16+
"stopAtEntry": false,
17+
"cwd": "${workspaceFolder}/build/example/",
18+
"environment": [],
19+
"externalConsole": false,
20+
"MIMode": "gdb",
21+
"setupCommands": [
22+
{
23+
"description": "为 gdb 启用整齐打印",
24+
"text": "-enable-pretty-printing",
25+
"ignoreFailures": true
26+
},
27+
{
28+
"description": "将反汇编风格设置为 Intel",
29+
"text": "-gdb-set disassembly-flavor intel",
30+
"ignoreFailures": true
31+
}
32+
]
33+
}
34+
]
35+
}

Diff for: .vscode/tasks.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "ninja build",
8+
"type": "shell",
9+
"command": "cd build && ninja",
10+
"problemMatcher": [],
11+
"group": {
12+
"kind": "build",
13+
"isDefault": true
14+
}
15+
},
16+
{
17+
"label": "umount Krxk",
18+
"type": "shell",
19+
"command": "umount ${workspaceFolder}/build/example/Krxk",
20+
"problemMatcher": [],
21+
"group": {
22+
"kind": "test",
23+
"isDefault": false
24+
}
25+
}
26+
]
27+
}

Diff for: build/example/SFS_Img

0 Bytes
Binary file not shown.

Diff for: build/example/init_disk

8.39 KB
Binary file not shown.

Diff for: build/example/init_disk.p/init_disk.c.o

20 KB
Binary file not shown.

Diff for: build/example/sampleFS

25.2 KB
Binary file not shown.

Diff for: build/example/sampleFS.p/sampleFS.c.o

42.3 KB
Binary file not shown.

Diff for: example/sampleFS.c

+116-1
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ int SFS_mknod(const char *path, mode_t mode,dev_t rdev)
816816
free(ptrInode);
817817
free(ptrEntry);
818818
fclose(fp);
819-
return -ENOENT;
819+
return -EBADF; // fuse框架似乎不会对mknod命令检测该返回值,故无法提示用户超过最大子文件数量
820820
}
821821

822822
// 读取 inode 位图 与 数据位图
@@ -926,12 +926,127 @@ int SFS_mknod(const char *path, mode_t mode,dev_t rdev)
926926
int SFS_write(const char *path, const char *buf, size_t size, off_t off, struct fuse_file_info *fi)
927927
{
928928
printf("Write path: %s\n", path);
929+
printf("Content in buf: %s\n", buf);
929930
return 16;
930931
}
931932

932933
int SFS_unlink(const char *path)
933934
{
934935
printf("Unlink path: %s\n", path);
936+
int pathLength = sizeof(char) * (strlen(path) + 1); // 保存目录长度
937+
938+
int startInodeNum = 1;
939+
for(char* pNext = path; !IsReachPathEnd(pNext);) {
940+
startInodeNum = HelpWalkPath(pNext, startInodeNum, &pNext);
941+
if(startInodeNum == -ENOENT) {
942+
return -ENOENT;
943+
}
944+
}
945+
946+
FILE* fp = fopen(imgPath, "r+");
947+
if(fp == NULL) {
948+
perror("Failed to open imgdisk.\n");
949+
return -ENOENT;
950+
}
951+
952+
// 读Inode
953+
struct inode* ptrInode = malloc(sizeof(struct inode));
954+
struct fileObj * ptrFileObj = malloc(sizeof(struct fileObj));
955+
956+
fseek(fp, getInodeOffsetByNum(startInodeNum), SEEK_SET);
957+
fread(ptrInode, sizeof(struct inode), 1, fp);
958+
959+
fseek(fp, getDataOffsetByNum(ptrInode->addr[0]), SEEK_SET);
960+
fread(ptrFileObj, sizeof(struct fileObj), 1, fp);
961+
962+
if(HelpGenFileObjHeadChecksum(ptrFileObj) != ptrFileObj->checksum) {
963+
//非文件,为目录
964+
free(ptrInode);
965+
free(ptrFileObj);
966+
fclose(fp);
967+
return -EISDIR;
968+
}
969+
970+
// 分离得到父目录
971+
char *parent = malloc(pathLength);
972+
strcpy(parent, path);
973+
char *temp = strrchr(parent, '/');
974+
*temp = '\0';
975+
int anotherStartNum = 1;
976+
for(char* pParentNext = parent; !IsReachPathEnd(pParentNext); ) {
977+
anotherStartNum = HelpWalkPath(pParentNext, anotherStartNum, &pParentNext);
978+
if(anotherStartNum == -ENOENT) {
979+
return -ENOENT; //理论上此情况不会发生。因为文件存在必然父目录存在
980+
}
981+
} // 此后 anotherStartNum 为 父目录的Inode号
982+
983+
// 获取使用的磁盘块,不清零,故在write前需要对Data位图标记为0的块进行清零,read不应该读取Data标记为0的块
984+
struct bitmap_inode* ptrBi = malloc(sizeof(struct bitmap_inode));
985+
struct bitmap_dblock* ptrDb = malloc(sizeof(struct bitmap_dblock));
986+
987+
fseek(fp, getInodeBitmapOffset(), SEEK_SET);
988+
fread(ptrBi, sizeof(struct bitmap_inode), 1, fp);
989+
990+
fseek(fp, getDataBitmapOffset(), SEEK_SET);
991+
fread(ptrDb, sizeof(struct bitmap_dblock), 1, fp);
992+
993+
// 重新标记Inode 与 Data 位图
994+
AddrNode* addrListHead = HelpWalkInodeTable(fp, ptrInode);
995+
int curIndex = -1;
996+
for(AddrNode* addrCur = addrListHead; addrCur!=NULL;) {
997+
int res = ReadAddrList(&addrCur, &curIndex);
998+
if(res == -1) {
999+
break;
1000+
}
1001+
setBitmapValue(ptrDb, res, 0);
1002+
}
1003+
1004+
setBitmapValue(ptrBi, ptrInode->st_ino, 0);
1005+
1006+
fseek(fp, getInodeBitmapOffset(), SEEK_SET);
1007+
fwrite(ptrBi, sizeof(struct bitmap_inode), 1, fp);
1008+
1009+
fseek(fp, getDataBitmapOffset(), SEEK_SET);
1010+
fwrite(ptrDb, sizeof(struct bitmap_dblock), 1, fp);
1011+
1012+
// 重写父目录
1013+
fseek(fp, getInodeOffsetByNum(anotherStartNum), SEEK_SET);
1014+
struct inode* ptrParentInode = malloc(sizeof(struct inode));
1015+
fread(ptrParentInode, sizeof(struct inode), 1, fp);
1016+
1017+
fseek(fp, getDataOffsetByNum(ptrParentInode->addr[0]), SEEK_SET);
1018+
struct dentry* ptrParentEntry = malloc(sizeof(struct dentry));
1019+
fread(ptrParentEntry, sizeof(struct dentry), 1, fp);
1020+
1021+
for(int i=0; i < max_child_count; i++) {
1022+
if(ptrParentEntry->childInodeNo[i] == ptrInode->st_ino) {
1023+
ptrParentEntry->childInodeNo[i] = 0;
1024+
}
1025+
}
1026+
1027+
fseek(fp, getDataOffsetByNum(ptrParentInode->addr[0]), SEEK_SET);
1028+
fwrite(ptrParentEntry, sizeof(struct dentry), 1, fp);
1029+
1030+
// 清空文件 Inode 与 文件头(mknod不检测脏数据)
1031+
fseek(fp, getDataOffsetByNum(ptrInode->addr[0]), SEEK_SET);
1032+
char* empty = malloc(fs_bl_size);
1033+
memset(empty, 0, fs_bl_size);
1034+
fwrite(empty, fs_bl_size, 1 ,fp);
1035+
1036+
fseek(fp, getInodeOffsetByNum(ptrInode->st_ino), SEEK_SET);
1037+
fwrite(empty, sizeof(struct inode), 1, fp);
1038+
1039+
// 清理
1040+
free(empty);
1041+
free(ptrParentEntry);
1042+
free(ptrParentInode);
1043+
free(ptrBi);
1044+
free(ptrDb);
1045+
FreeAddrList(addrListHead);
1046+
free(parent);
1047+
free(ptrInode);
1048+
free(ptrFileObj);
1049+
fclose(fp);
9351050
return 0;
9361051
}
9371052

0 commit comments

Comments
 (0)