feat(filesystem): 引入Umount系统调用 (#719)

* feat(filesystem): 引入Umount系统调用

* 将所有ENOSYS误用更正

* 修复了一个使同一个挂载点可以挂载2个文件系统的bug

* 统一注释,增强程序稳定性,统一接口。注意:Umount时在fatfs的路径要使用大写,此受限于当前文件系统设计。
This commit is contained in:
Samuel Dai
2024-04-15 13:02:04 +08:00
committed by GitHub
parent ceeb2e943c
commit 1074eb34e7
46 changed files with 1274 additions and 463 deletions

1
user/apps/test_ramfs/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
test_ramfs

View File

@ -0,0 +1,20 @@
ifeq ($(ARCH), x86_64)
CROSS_COMPILE=x86_64-linux-musl-
else ifeq ($(ARCH), riscv64)
CROSS_COMPILE=riscv64-linux-musl-
endif
CC=$(CROSS_COMPILE)gcc
.PHONY: all
all: main.c
$(CC) -static -o test_ramfs main.c
.PHONY: install clean
install: all
mv test_ramfs $(DADK_CURRENT_BUILD_DIR)/test_ramfs
clean:
rm test_ramfs *.o
fmt:

115
user/apps/test_ramfs/main.c Normal file
View File

@ -0,0 +1,115 @@
// #include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/mount.h>
#define MAX_PATH_LENGTH 100
#define MAX_DIR_DEPTH 4
int main(int argc, char const* argv[]) {
if (mkdir("/SOME", 0777) == -1) {
perror("Failed to create directory under /some");
return 1;
}
// Create a directory under /SOME/RAMFS
if (mkdir("/SOME/RAMFS", 0777) == -1) {
perror("Failed to create directory under /SOME/RAMFS");
return 1;
}
// Mount the first ramfs at /SOME/RAMFS
if (mount("", "/SOME/RAMFS", "ramfs", 0, NULL) == -1) {
perror("Failed to mount ramfs at /SOME/RAMFS");
return 1;
}
if (mkdir("/SOME/RAMFS/some", 0777) == -1) {
perror("Failed to create directory under /SOME/RAMFS/some");
return 1;
}
puts("Success mkdir /SOME/RAMFS/some");
// Create a directory under /SOME/RAMFS/some/another
if (mkdir("/SOME/RAMFS/some/another", 0777) == -1) {
perror("Failed to create directory under /SOME/RAMFS/some/another");
return 1;
}
puts("Success mkdir /SOME/RAMFS/some/another");
if (mount("", "/SOME/RAMFS/some/another", "ramfs", 0, NULL) == -1) {
perror("Failed to mount ramfs at /SOME/RAMFS/some/another");
return 1;
}
puts("Success mount on /SOME/RAMFS/some/another");
if (mkdir("/SOME/RAMFS/some/another/just_another", 0777) == -1) {
perror("Failed to create directory under /SOME/RAMFS/some/another");
return 1;
}
puts("Success mkdir /SOME/RAMFS/some/another/just_another");
if (mount("", "/SOME/RAMFS/some/another/just_another", "ramfs", 0, NULL) == -1) {
perror("Failed to mount ramfs at /SOME/RAMFS/some/another");
return 1;
}
puts("Success mount on /SOME/RAMFS/some/another/just_another");
// Write files under /SOME/RAMFS and /SOME/RAMFS/some/another
FILE* file1 = fopen("/SOME/RAMFS/file1.txt", "w");
if (file1 == NULL) {
perror("Failed to open /SOME/RAMFS/file1.txt");
return 1;
}
fprintf(file1, "This is file1.txt\n");
fclose(file1);
FILE* file2 = fopen("/SOME/RAMFS/some/another/file2.txt", "w");
if (file2 == NULL) {
perror("Failed to open /SOME/RAMFS/some/another/file2.txt");
return 1;
}
fprintf(file2, "This is file2.txt\n");
fclose(file2);
FILE* file3 = fopen("/SOME/RAMFS/some/another/just_another/file3.txt", "w+");
if (file3 == NULL) {
perror("Failed to open /SOME/RAMFS/some/another/just_another/file3.txt");
return 1;
}
fprintf(file3, "Multi mount behave well.\n");
// print file3.txt
char buffer[100];
fseek(file3, 0, SEEK_SET);
fread(buffer, 1, 100, file3);
printf("file3.txt content: %s\n", buffer);
fclose(file3);
// test umount with flags ( use umount2 )
if (umount("/SOME/RAMFS/some/another/just_another") == -1) {
perror("Failed to umount ramfs at /SOME/RAMFS/some/another/just_another");
return 1;
}
puts("Successful umount /SOME/RAMFS/some/another/just_another");
// delete just_another
if (rmdir("/SOME/RAMFS/some/another/just_another") == -1) {
perror("Failed to delete /SOME/RAMFS/some/another/just_another");
return 1;
}
return 0;
}