操作系统实验用C写模拟简单的二级文件管理系统
时间: 2023-08-21 10:02:33 浏览: 148
好的,下面是一个简单的二级文件管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILENAME_LEN 20
#define MAX_FILE_NUM 50
typedef struct file {
char filename[MAX_FILENAME_LEN];
struct file *next;
} file_t;
typedef struct dir {
char dirname[MAX_FILENAME_LEN];
struct dir *parent;
struct dir *subdirs;
file_t *files;
} dir_t;
dir_t root_dir = {"/", NULL, NULL, NULL};
void print_dir(dir_t *dir) {
printf("%s:\n", dir->dirname);
printf(" Directories:\n");
dir_t *subdir = dir->subdirs;
while (subdir != NULL) {
printf(" %s\n", subdir->dirname);
subdir = subdir->next;
}
printf(" Files:\n");
file_t *file = dir->files;
while (file != NULL) {
printf(" %s\n", file->filename);
file = file->next;
}
}
dir_t *find_dir(char *path, dir_t *start_dir) {
if (*path == '/') {
start_dir = &root_dir;
++path;
}
if (*path == '\0') {
return start_dir;
}
char *next_sep = strchr(path, '/');
if (next_sep != NULL) {
*next_sep = '\0';
}
dir_t *subdir = start_dir->subdirs;
while (subdir != NULL && strcmp(subdir->dirname, path) != 0) {
subdir = subdir->next;
}
if (next_sep != NULL) {
*next_sep = '/';
return subdir == NULL ? NULL : find_dir(next_sep + 1, subdir);
} else {
return subdir;
}
}
file_t *find_file(char *filename, dir_t *start_dir) {
file_t *file = start_dir->files;
while (file != NULL && strcmp(file->filename, filename) != 0) {
file = file->next;
}
return file;
}
void create_dir(char *path) {
dir_t *parent_dir = find_dir(path, &root_dir);
if (parent_dir == NULL) {
printf("Invalid path.\n");
return;
}
char *dirname = strrchr(path, '/');
if (dirname == NULL) {
printf("Invalid path.\n");
return;
}
++dirname;
if (strlen(dirname) > MAX_FILENAME_LEN) {
printf("Directory name is too long.\n");
return;
}
if (find_dir(dirname, parent_dir) != NULL) {
printf("Directory already exists.\n");
return;
}
dir_t *new_dir = malloc(sizeof(dir_t));
strcpy(new_dir->dirname, dirname);
new_dir->parent = parent_dir;
new_dir->subdirs = NULL;
new_dir->files = NULL;
new_dir->next = parent_dir->subdirs;
parent_dir->subdirs = new_dir;
}
void create_file(char *path) {
dir_t *parent_dir = find_dir(path, &root_dir);
if (parent_dir == NULL) {
printf("Invalid path.\n");
return;
}
char *filename = strrchr(path, '/');
if (filename == NULL) {
printf("Invalid path.\n");
return;
}
++filename;
if (strlen(filename) > MAX_FILENAME_LEN) {
printf("File name is too long.\n");
return;
}
if (find_file(filename, parent_dir) != NULL) {
printf("File already exists.\n");
return;
}
file_t *new_file = malloc(sizeof(file_t));
strcpy(new_file->filename, filename);
new_file->next = parent_dir->files;
parent_dir->files = new_file;
}
void delete_dir(char *path) {
dir_t *dir = find_dir(path, &root_dir);
if (dir == NULL) {
printf("Invalid path.\n");
return;
}
if (dir->subdirs != NULL || dir->files != NULL) {
printf("Directory is not empty.\n");
return;
}
dir_t *parent_dir = dir->parent;
dir_t *prev_dir = NULL;
dir_t *curr_dir = parent_dir->subdirs;
while (curr_dir != NULL && curr_dir != dir) {
prev_dir = curr_dir;
curr_dir = curr_dir->next;
}
if (curr_dir != NULL) {
if (prev_dir == NULL) {
parent_dir->subdirs = curr_dir->next;
} else {
prev_dir->next = curr_dir->next;
}
free(curr_dir);
}
}
void delete_file(char *path) {
dir_t *parent_dir = find_dir(path, &root_dir);
if (parent_dir == NULL) {
printf("Invalid path.\n");
return;
}
char *filename = strrchr(path, '/');
if (filename == NULL) {
printf("Invalid path.\n");
return;
}
++filename;
file_t *file = parent_dir->files;
file_t *prev_file = NULL;
while (file != NULL && strcmp(file->filename, filename) != 0) {
prev_file = file;
file = file->next;
}
if (file != NULL) {
if (prev_file == NULL) {
parent_dir->files = file->next;
} else {
prev_file->next = file->next;
}
free(file);
}
}
int main() {
create_dir("/home");
create_dir("/home/user1");
create_dir("/home/user2");
create_file("/home/user1/file1.txt");
create_file("/home/user1/file2.txt");
create_file("/home/user2/file3.txt");
print_dir(&root_dir);
delete_file("/home/user1/file2.txt");
delete_dir("/home/user2");
print_dir(&root_dir);
return 0;
}
```
该代码实现了以下功能:
- 创建目录和文件
- 删除目录和文件
- 列出目录下的子目录和文件
其中,目录和文件都使用链表来存储,每个目录包含一个子目录链表和一个文件链表。文件和目录的名称使用字符串来表示,且长度不能超过 `MAX_FILENAME_LEN`。`find_dir()` 和 `find_file()` 函数用于查找目录和文件,它们都接受一个路径参数和一个起始目录参数,返回找到的目录或文件的指针,如果找不到,则返回 NULL。`create_dir()` 和 `create_file()` 函数用于创建目录和文件,它们都接受一个路径参数,如果路径中的目录不存在,则返回错误信息;如果目录或文件已经存在,则返回错误信息;否则,创建一个新的目录或文件,并将其添加到相应的链表中。`delete_dir()` 和 `delete_file()` 函数用于删除目录和文件,它们都接受一个路径参数,如果路径中的目录或文件不存在,则返回错误信息;如果目录不为空,则返回错误信息;否则,删除相应的目录或文件,并将其从相应的链表中删除。`print_dir()` 函数用于列出目录下的子目录和文件,它接受一个目录参数,遍历该目录的子目录链表和文件链表,并输出相应的信息。
请注意,此代码仅作为示例,还有很多细节需要考虑和完善,例如:
- 如何处理重名的目录或文件?
- 如何处理路径中的相对路径和绝对路径?
- 如何处理目录的嵌套层数限制?
- 如何处理文件的大小限制?
- 如何处理目录和文件的权限?
- 等等。
阅读全文