基于Linux的模拟文件系统的设计与实现

更新时间:2024-06-19 19:13:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

中北大学

操作系统课程设计

说 明 书

学 院、系: 专 业: 学 生 姓 名:

赵巍

软件学院 软件工程

学 号: 0921010414

设 计 题 目: 基于Linux的模拟文件系统的设计与实现

起 迄 日 期: 2011年12月22日- 2012年1月7日 指 导 教 师:

康珺

2012 年 1月 7 日

1 需求分析

所要实现的功能:

(1) 设计一个10个用户的文件系统。每个用户最多可以保存10个文件,一次运行用户可打

开多个文件。

(2) 程序采用二级文件目录。(即设置主目录(MFD)和用户文件目录(UFD))。另外,可打

开文件设置指针。

(3) 为了方便实现,对文件的读写作了简化。在执行读写命令时,只需改读写指针。并不进

行实际的读写操作。

(4) 实现的基本功能主要包括:改变目录(CD),创建目录(MD),显示目录(DIR),删除目

录(RD),打开全部文件(openall),打开单个文件(open),建立一个文件(create),删除一个文件(delete),写文件(write),读文件(read),改文件的保护码(change),退出(exit)等。

考虑特殊情况如:各个命令对全路径和相对路径的支持、目录不存在时,给出错误信息、不能用cd进入文件、命令之中不能有空格(如 ex it,给出错误提示)、相对路径的解析、路径中的空格剔除、新建目录或文件时的问题、重名问题、目录或文件的名字长度限制、目录或文件的名字中包含不合法字符(注意空格)、删除目录或文件时的问题、删除不存在的文件或目录给出错误提示、删除目录时目录不为空(如果该目录为空,则可删除,否则给出是否做删除提示,删除操作将该目录下的全部文件和子目录都删除)、进入到某个目录下,却要删除本目录或上级目录、不能用delete删除目录、不能用RD删除文件等都要考虑在内。 最终获得的成果就是:

①在系统中用一个文件来模拟一个磁盘;

②此系统至少有:Create、delete、open、close、read、write等和部分文件属性的功能。 ③实现这个文件系统。

④能实际演示这个文件系统。基本上是进入一个界面(此界面就是该文件系统的界面)后,可以实现设计的操作要求。

2

1

总体设计

2.1系统活动图

注册 登陆 退出 用户管理系统 文件目录管理系统

新 删进打路登 建除入开径陆目目目

文控退录 录 录 件 制 出 目录内文件管理系统 新读写 登建文文删移陆 文件 件 除 动 退 件 出

2.2 头文件:

#include #include #include 2.3结构体:

typedef struct file{ char name[10]; struct file *next; FILE *fp; }File;

typedef struct content{ char name[10];

2

File *fileHead; int f_num;

struct content *next; }Content;

typedef struct user{ char name[10]; char psw[10]; Content *conHead; int c_num;

struct user *next; }User;

2.4 全局变量和函数: void registe() int login() void MenuUser() void FolderCreate() void FolderDelete() void FolderViewAllFiles() void FolderIntoFolder() void MenuFolder() void FileCreate() void FileRead() void FileWrite() void FileDelete() void FileMove()

3.详细设计 用户管理系统: registe():用户注册

login():用户登录

目录管理系统: FolderCreate():新建目录

FolderDelete():删除目录

3

FolderIntoFolder():进入目录

FolderViewAllFiles():显示所有目录内的所有文件

absolutePathFileController():通过绝对路径直接读文件

文件管理系统: FileCreate():新建文件 FileDelete():删除文件 FileMove():跨目录移动文件 FileRead():读文件

FileWrite():写文件

3.1 用户管理系统:

截图3-1

相关核心代码: //step0

void registe() {

if(user_num>=10) {

printf(\return; }

//allocate new memory block for new user User *add,*last;

add=(User *)malloc(sizeof(User)); add->next=NULL; if(user_head==NULL) user_head=add; else {

last=user_head;

while(last->next!=NULL) last=last->next; last->next=add;

4

}

user_num++;

//fulfill the new user char name[10],psw[10]; User *check; char buffer[2];

fgets(buffer,2,stdin);

unr:printf(%used):\\n\

scanf(\

check=user_head; while(check!=add) {

if(strcmp(name,check->name)==0) {

printf(\goto unr; }

check=check->next; }

strcpy(add->name,name);

fgets(buffer,2,stdin); printf(\fgets(psw,10,stdin); strcpy(add->psw,psw);

add->conHead=NULL; add->c_num=0;

//success

printf(\

return; }

//step0 int login()

5

{

if(user_head==NULL) {

printf(\return 0; }

char buffer[2];

fgets(buffer,2,stdin);

char name[10],psw[10]; User *check;

printf(%unl:printf(\scanf(\

if(name[0]=='#') return 0;

check=user_head; while(check!=NULL) {

if(!strcmp(check->name,name)) break; else

check=check->next; }

if(check==NULL) {

printf(\goto unl; }

//match password

fgets(buffer,2,stdin); printf(\fgets(psw,11,stdin);

if(strcmp(psw,check->psw)!=0) {

printf(\goto unl; }

printf(\cur_user=check;

6

return 1; }

void main(int argc, char* argv[]) {

user_num=0; user_head=NULL; cur_user=NULL; int choice; for(;;) {

printf(\printf(\printf(\printf(\

printf(\printf(\%d user exist\\n\printf(\

scanf(\switch(choice) {

case 1: registe();continue; case 2:if(login()) MenuUser(); continue;

case 0:printf(\default:printf(\} } }

3.2 目录管理系统:

7

截图3-2

相关核心代码: //step0-1

void MenuUser() {

void FolderCreate(); void FolderDelete();

void FolderViewAllFiles(); void FolderIntoFolder();

void absolutePathFileController();

User *current;

Content *goThrough;

current=cur_user;

goThrough=current->conHead;

for(;;) {

printf(\printf(\printf(\printf(\printf(\

printf(\printf(\

printf(\printf(\view all your folders below: %d folders total\\n\if(current->conHead==NULL)

8

printf(\|\\n no folder.\\n\else {

goThrough=current->conHead; while(goThrough!=NULL) {

printf(\|\\n %s\\n\goThrough=goThrough->next; } }

printf(\printf(\int choice;

scanf(\switch(choice) {

case 1:FolderCreate();continue; case 2:FolderIntoFolder();continue; case 3:FolderDelete();continue;

case 4:FolderViewAllFiles();continue;

case 5:absolutePathFileController();continue; case 6:printf(\

case 0:printf(\default:printf(\} } }

//step1

void FolderCreate() {

User *user; user=cur_user;

//allocate new memory block for new folder Content *add,*last;

add=(Content *)malloc(sizeof(Content)); add->next=NULL;

if(user->conHead==NULL) user->conHead=add; else

9

{

last=user->conHead; while(last->next!=NULL) last=last->next; last->next=add; }

user->c_num++;

add->fileHead=NULL; add->f_num=0;

//fulfill the new folder name char name[10]; Content *check;

char buffer[2];

fgets(buffer,2,stdin);

con_r:printf(\create a new folder:\\nfolder name(9,only alphabet and number can be used):\\n\scanf(\

check=user->conHead; while(check!=add) {

if(strcmp(name,check->name)==0) {

printf(\goto con_r; }

check=check->next; }

//check legality of the name int i=0;

for(;i<=9;i++) {

if(name[i]>=97&&name[i]<=122)//lower continue; else

if(name[i]>=48&&name[i]<=57)//num continue; else

10

if(name[i]>=65&&name[i]<=90)//upper continue; else

if(name[i]=='\\0') break; else {

printf(\goto con_r; } }

strcpy(add->name,name);

printf(\return; }

//step1

void FolderDelete() {

if(cur_user->conHead==NULL) {

printf(\return; }

Content *front,*current;

char name[10];

printf(\con_d:printf(\scanf(\

if(name[0]=='#') return;

//seek folder name

current=cur_user->conHead; while(current!=NULL) {

if(!strcmp(current->name,name)) break; else

11

current=current->next; }

if(current==NULL) {

printf(\goto con_d; }

//if has any files

while(current->fileHead!=NULL) {

char buffer[2];

fgets(buffer,2,stdin);

printf(\char judge;

scanf(\

if(judge=='n'||judge=='y'||judge=='N'||judge=='Y') if(judge=='n'||judge=='N') return; else break; else {

printf(\continue; } }

//user recognise char psw[10];

printf(\char buffer[2];

fgets(buffer,2,stdin); fgets(psw,10,stdin);

if(strcmp(cur_user->psw,psw)!=0) {

printf(\return; }

12

//execute delete operation front=cur_user->conHead; if(front==current) //if curren is the head file {

cur_user->conHead=current->next; free(current);

printf(\return; }

while(front->next!=current) front=front->next;

front->next=current->next; free(current); cur_user->c_num--;

printf(\return; }

//step1

void FolderViewAllFiles() {

printf(\

Content *con;

con=cur_user->conHead; int fi_num=0;

while(con!=NULL) {

printf(\%s-----------------\\n\

File *fi;

fi=con->fileHead; while(fi!=NULL) {

printf(\| %s\\n\

13

printf(\| |\\n\fi=fi->next; }

fi_num+=con->f_num; con=con->next; }

printf(\return; }

//step1

void FolderIntoFolder() {

void MenuFolder();

if(cur_user->conHead==NULL) {

printf(\return; }

printf(\char name[10];

char buffer[2];

fgets(buffer,2,stdin);

Content *check;

printf(\con_g:printf(\scanf(\

if(name[0]=='#') return;

check=cur_user->conHead; while(check!=NULL) {

14

if(!strcmp(check->name,name)) break; else

check=check->next; }

if(check==NULL) {

printf(\goto con_g; }

//success

cur_con=check; MenuFolder(); return; }

//step ~

void absolutePathFileController() {

printf(\

printf(\printf(\

char name[28];

printf(\absolute:printf(\scanf(\

if(name[0]=='#') return;

char folder_name[10]; char file_name[14];

//analyse the absolute path //get folder name if(name[0]=='/') {

printf(\goto absolute; }

15

int i=0,j=0;

while(name[i]!='/'&&i<=9) {

if(name[i]=='\\0') {

printf(\goto absolute; }

folder_name[i]=name[i]; i++; }

folder_name[i]='\\0'; i++;

//get file name if(name[i]=='\\0') {

printf(\goto absolute; }

while(name[i+j]!='\\0'&&j<=9) {

file_name[j]=name[i+j]; j++; }

file_name[j]='\\0';

printf(\printf(\

printf(\printf(\printf(\

//seek folder

Content *folder_checker;

folder_checker=cur_user->conHead; while(folder_checker!=NULL) {

if(!strcmp(folder_checker->name,folder_name)) break; else

16

folder_checker=folder_checker->next; }

if(folder_checker==NULL) {

printf(\goto absolute; }

//seek file

File *file_checker;

file_checker=folder_checker->fileHead; while(file_checker!=NULL) {

if(!strcmp(file_checker->name,file_name)) break; else

file_checker=file_checker->next; }

if(file_checker==NULL) {

printf(\goto absolute; }

//read the file

char fullName[20]={\strcat(fullName,file_name);

printf(\file_checker->fp=fopen(fullName,\char ch;

while(ch!=EOF) {

putchar(ch=getc(file_checker->fp)); }

printf(\fclose(file_checker->fp);

return; }

17

3.2 文件管理系统:

截图3-3

相关核心代码: //step1-2

void MenuFolder() {

void FileCreate(); void FileRead(); void FileWrite(); void FileDelete(); void FileMove();

File *goThrough; Content *current; current=cur_con;

goThrough=current->fileHead; char buffer[2]; int choice;

for(;;) {

fgets(buffer,2,stdin);

printf(\printf(\printf(\printf(\printf(\printf(\

printf(\

printf(\printf(\view all your files below: %d files

18

total\\n\if(current->fileHead==NULL) printf(\|\\n no file.\\n\else {

goThrough=current->fileHead; while(goThrough!=NULL) {

printf(\|\\n %s\\n\goThrough=goThrough->next; } }

printf(\printf(\choice=-1;

scanf(\switch(choice) {

case 1:FileCreate();continue; case 2:FileRead();continue; case 3:FileWrite();continue; case 4:FileDelete();continue; case 5:FileMove();continue;

case 6:printf(\

case 0:printf(\default:printf(\} } }

//step2

void FileCreate() {

Content *cur_content; cur_content=cur_con;

//allocate new memory block for new file's index File *add,*last;

add=(File *)malloc(sizeof(File)); add->next=NULL; add->fp=NULL;

19

if(cur_content->fileHead==NULL) cur_content->fileHead=add; else {

last=cur_content->fileHead; while(last->next!=NULL) last=last->next; last->next=add; }

cur_content->f_num++;

//fulfill the new folder name char name[10]; File *check; char buffer[2];

fgets(buffer,2,stdin);

file_r:printf(\create a new File:\\nfile name(9,only alphabet and number can be used):\\n\

scanf(\

check=cur_content->fileHead; while(check!=add) {

if(strcmp(name,check->name)==0) {

printf(\goto file_r; }

check=check->next; }

//check legality of the name int i=0;

for(;i<=9;i++) {

if(name[i]>=97&&name[i]<=122)//lower continue; else

if(name[i]>=48&&name[i]<=57)//num continue;

20

else

if(name[i]>=65&&name[i]<=90)//upper continue; else

if(name[i]=='\\0') break; else {

printf(\goto file_r; } }

strcpy(add->name,name); char fullName[20]={\strcat(fullName,name);

add->fp=fopen(fullName,\if(add->fp==NULL) {

printf(\goto file_r; }

fclose(add->fp); printf(\return; }

//step2

void FileRead() {

if(cur_con->fileHead==NULL) {

printf(\return; }

printf(\File *current;

char name[10];

printf(\

21

file_r:printf(\scanf(\

if(name[0]=='#') return;

//seek file name

current=cur_con->fileHead; while(current!=NULL) {

if(!strcmp(current->name,name)) break; else

current=current->next; }

if(current==NULL) {

printf(\goto file_r; }

char fullName[20]={\strcat(fullName,name);

printf(\current->fp=fopen(fullName,\char ch;

while(ch!=EOF) {

putchar(ch=getc(current->fp)); }

printf(\fclose(current->fp);

return; }

//step2

void FileWrite() {

if(cur_con->fileHead==NULL) {

printf(\return;

22

}

printf(\File *current;

char name[10];

printf(\file_r:printf(\scanf(\

if(name[0]=='#') return;

//seek file name

current=cur_con->fileHead; while(current!=NULL) {

if(!strcmp(current->name,name)) break; else

current=current->next; }

if(current==NULL) {

printf(\goto file_r; }

char fullName[20]={\strcat(fullName,name);

printf(\details:(tpye in \\\current->fp=fopen(fullName,\char ch;

ch=getchar(); while(ch!='#') {

putc(ch,current->fp); ch=getchar(); }

fclose(current->fp);

printf(\return;

23

}

//step2

void FileDelete() {

if(cur_con->fileHead==NULL) {

printf(\return; }

File *front,*current;

char name[10];

printf(\file_d:printf(\scanf(\

if(name[0]=='#') return;

//seek file name

current=cur_con->fileHead; while(current!=NULL) {

if(!strcmp(current->name,name)) break; else

current=current->next; }

if(current==NULL) {

printf(\goto file_d; }

//execute delete operation front=cur_con->fileHead; if(front==current) //if curren is the head file {

cur_con->fileHead=current->next;

24

本文来源:https://www.bwwdw.com/article/zd43.html

Top