模拟一个简单二级文件管理系统.docx
《模拟一个简单二级文件管理系统.docx》由会员分享,可在线阅读,更多相关《模拟一个简单二级文件管理系统.docx(28页珍藏版)》请在冰点文库上搜索。
模拟一个简单二级文件管理系统
模拟一个简单二级文件管理系统
设计目的:
通过具体的文件存储空间的管理、文件的物理结构、目录结构和文件操作的实现,加深对文件系统内部功能和实现过程的理解。
设计内容:
模拟一个简单二级文件管理系统
一、实验内容描述
1实验目标
本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现.
2实验要求
为DOS系统设计一个简单的二级文件系统.要求做到以下几点:
①可以实现下列命令:
login用户登录
dir列文件目录
create创建文件
delete删除文件
open打开文件
close关闭文件
read读文件
write写文件
②列目录时要列出文件名、物理地址、保护码和文件长度.
③源文件可以进行读写保护.
二、程序主要内容
1设计思路
程序中要求每个用户在登陆后才可对其拥有的文件进行操作,用户对于其他用户的文件无操作权.文件操作包括浏览、创建、删除、打开、关闭、阅读、写入、修改模式.其他操作包括新建用户、帮助、用户登入、用户登出、退出系统.
在程序文件夹下有个名为“file”的系统根目录,此目录下包括:
一个名为“mfd”的文件,记录所有注册过的帐号及密码;用户文件,以用户名作为文件名,内容为其拥有的文件名及属性;一个名为“keiji”的文件夹.“keiji”文件夹中包括:
“file.p”指针文件,记录所有已用的物理地址;一些以物理地址为名的文件,内容为文件内容.
2数据结构
file结构体系统文件数据结构:
fpaddrint,文件的物理地址、flengthint,文件长度、fmodeint,文件模式0.只读;1.可写;2.可读写;3.保护、fname[]char,文件名;
filemode结构体文件状态数据结构:
isopenint,文件当前状态,0.关闭;1.打开、modeint,文件模式0.只读;1.可写;2.可读写;3.初始化;
user结构体用户信息数据结构:
uname[]char,用户名、upassword[]char,用户密码;
userfile结构体用户文件数据结构:
uname[]char,用户名、ufile[]file,用户拥有的文件数组.
.......
[cpp]viewplaincopyprint?
1.#include
2.#include
3.#include
4.#include
5.#include
6.#defineMaxUser100//定义最大MDF主目录文件
7.#defineMaxDisk512*1024//模拟最大磁盘空间
8.#definecommandAmount12//对文件操作的指令数
9.//存储空间管理有关结构体和变量
10.chardisk[MaxDisk];//模拟512K的磁盘存储空间
11.typedefstructdistTable//磁盘块结构体
12.{
13.intmaxlength;
14.intstart;
15.intuseFlag;
16.distTable*next;
17.}diskNode;
18.diskNode*diskHead;
19.structfileTable//文件块结构体
20.{
21.charfileName[10];
22.intstrat;//文件在磁盘存储空间的起始地址
23.intlength;//文件内容长度
24.intmaxlength;//文件的最大长度
25.charfileKind[3];//文件的属性——读写方式
26.structtm*timeinfo;
27.boolopenFlag;//判断是否有进程打开了该文件
28.//fileTable*next;
29.};
30.
31.//两级目录结构体
32.typedefstructuser_file_directory//用户文件目录文件UFD
33.{
34.//charfileName[10];
35.fileTable*file;
36.user_file_directory*next;
37.}UFD;
38.//UFD*headFile;
39.typedefstructmaster_file_directory//主文件目录MFD
40.{
41.charuserName[10];
42.charpassword[10];
43.UFD*user;
44.}MFD;
45.MFDuserTable[MaxUser];
46.intused=0;//定义MFD目录中用已有的用户数
47.
48.//文件管理
49.voidfileCreate(charfileName[],intlength,charfileKind[]);//创建文件
50.voidfileWrite(charfileName[]);//写文件
51.voidfileCat(charfileName[]);//读文件
52.voidfileRen(charfileName[],charrename[]);//重命名文件
53.voidfileFine(charfileName[]);//查询文件
54.voidfileDir(charUserName[]);//显示某一用户的所有文件
55.voidfileClose(charfileName[]);//关闭已打开的文件
56.voidfileDel(charfileName[]);//删除文件
57.voidchmod(charfileName[],charkind[]);//修改文件的读写方式
58.intrequestDist(int&startPostion,intmaxLength);//磁盘分配查询
59.voidinitDisk();//初始化磁盘
60.voidfreeDisk(intstartPostion);//磁盘空间释放
61.voiddiskShow();//显示磁盘使用情况
62.
63.//用户管理
64.voiduserCreate();
65.intlogin();
66.intuserID=-1;//用户登录的ID号,值为-1时表示没有用户登录
67.
68.intmain()
69.{
70.charorder[commandAmount][10];
71.strcpy(order[0],"create");
72.strcpy(order[1],"rm");
73.strcpy(order[2],"cat");
74.strcpy(order[3],"write");
75.strcpy(order[4],"fine");
76.strcpy(order[5],"chmod");
77.strcpy(order[6],"ren");
78.strcpy(order[7],"dir");
79.strcpy(order[8],"close");
80.strcpy(order[9],"return");
81.strcpy(order[10],"exit");
82.strcpy(order[11],"df");
83.charcommand[50],command_str1[10],command_str2[10],command_str3[5],command_str4[3];
84.inti,k,j;
85.intlength;
86.initDisk();//初始化磁盘
87.for(i=0;i88.{
89.userTable[i].user=(UFD*)malloc(sizeof(UFD));
90.userTable[i].user->next=NULL;
91.}
92.while
(1)
93.{
94.printf("********************************************/n");
95.printf("1、Creatuser/n");
96.printf("2、login/n");
97.printf("********************************************/n");
98.printf("Pleasechoocethefunctionkey:
>");
99.intchoice;
100.scanf("%d",&choice);
101.if(choice==1)userCreate();
102.elseif(choice==2)userID=login();
103.elseprintf("您的输入有误,请重新选择/n");
104.while(userID!
=-1)
105.{
106.fflush(stdin);
107.printf("———————————————————————————————————————/n");
108.printf("create-创建格式:
createa11000rw,将创建名为a1,长度为1000字节可读可写的文件/n");
109.printf("rm-删除格式:
rma1,将删除名为a1的文件/n");
110.printf("cat-查看文件内容格式:
cata1,显示a1的内容/n");
111.printf("write-写入格式:
writea1/n");
112.printf("fine-查询格式:
finea1,将显示文件a1的属性/n");
113.printf("chmod-修改格式:
chmoda1r,将文件a1的权限改为只读方式/n");
114.printf("ren-重命名格式:
rena1b1,将a1改名为b1/n");
115.printf("dir-显示文件格式:
diraaa,将显示aaa用户的所有文件/n");
116.printf("df-显示磁盘空间使用情况格式:
df/n");
117.printf("close-关闭文件格式:
closea1,将关闭文件a1/n");
118.printf("return-退出用户,返回登录界面/n");
119.printf("exit-退出程序/n");
120.printf("————————————————————————————————————————/n");
121.printf("pleaseimputyourcommand:
>");
122.gets(command);
123.intselect;
124.for(i=0;command[i]!
=''&&command[i]!
='/0';i++)//command_str1字符串存储命令的操作类型
mand_str1[i]=command[i];
126.k=i;
mand_str1[k]='/0';
128.for(i=0;i129.{
130.if(!
strcmp(command_str1,order[i]))
131.{
132.select=i;
133.break;
134.}
135.}
136.if(i==commandAmount)
137.{
138.printf("您输入的命令有误,请重新输入/n");
139.continue;
140.}
141.for(i=k+1,k=0;command[i]!
=''&&command[i]!
='/0';i++,k++)//commmand_str2字符串存储文件名或用户名
mand_str2[k]=command[i];
mand_str2[k]='/0';
144.k=i;
145.switch(select)
146.{
147.case0:
for(i=k+1,k=0;command[i]!
='';i++,k++)
mand_str3[k]=command[i];
mand_str3[k]='/0';
150.k=i;
151.j=1;
152.length=0;//初始化文件长度
153.for(i=strlen(command_str3)-1;i>=0;i--)//把字符串转换为十进制
154.{
155.length+=(command_str3[i]-48)*j;
156.j*=10;
157.}
158.for(i=k+1,k=0;command[i]!
=''&&command[i]!
='/0';i++,k++)
mand_str4[k]=command[i];
mand_str4[k]='/0';
161.fileCreate(command_str2,length,command_str4);break;
162.case1:
fileDel(command_str2);break;
163.case2:
fileCat(command_str2);break;
164.case3:
165.fileWrite(command_str2);break;
166.case4:
fileFine(command_str2);break;
167.case5:
for(i=k+1,k=0;command[i]!
=''&&command[i]!
='/0';i++,k++)
mand_str3[k]=command[i];
mand_str3[k]='/0';
170.chmod(command_str2,command_str3);break;
171.case6:
for(i=k+1,k=0;command[i]!
='/0';i++,k++)
mand_str3[k]=command[i];
mand_str3[k]='/0';
174.fileRen(command_str2,command_str3);break;
175.case7:
fileDir(command_str2);break;
176.case8:
fileClose(command_str2);break;
177.case9:
UFD*p;
178.for(p=userTable[userID].user->next;p!
=NULL;p=p->next)//退出用户之前关闭所有打的文件
179.if(p->file->openFlag)
180.p->file->openFlag=false;
181.system("cls");
182.userID=-1;break;
183.case10:
exit(0);break;
184.case11:
diskShow();break;
185.}
186.}
187.}
188.return0;
189.}
190.
191.voiduserCreate()
192.{
193.charc;
194.charuserName[10];
195.inti;
196.if(used197.{
198.printf("请输入用户名:
");
199.for(i=0;c=getch();i++)
200.{
201.if(c==13)break;
202.else
203.userName[i]=c;
204.printf("%c",c);
205.}
206.userName[i]='/0';
207.for(i=0;i208.{
209.if(!
strcmp(userTable[i].userName,userName))
210.{
211.printf("/n");
212.printf("该用户名已存在,创建用户失败/n");
213.system("pause");
214.return;
215.}
216.}
217.strcpy(userTable[used].userName,userName);
218.printf("/n");
219.printf("请输入密码:
");
220.for(i=0;c=getch();i++)
221.{
222.if(c==13)break;
223.else
224.userTable[used].password[i]=c;
225.printf("*");
226.}
227.userTable[userID].password[i]='/0';
228.printf("/n");
229.printf("创建用户成功/n");
230.used++;
231.system("pause");
232.}
233.else
234.{
235.printf("创建用户失败,用户已达到上限/n");
236.system("pause");
237.}
238.fflush(stdin);
239.}
240.
241.intlogin()
242.{
243.charname[10],psw[10];
244.charc;
245.inti,times;
246.printf("请输入用户名:
");
247.for(i=0;c=getch();i++)
248.{
249.if(c==13)break;
250.else
251.name[i]=c;
252.printf("%c",c);
253.}
254.name[i]='/0';
255.for(i=0;i256.{
257.if(!
strcmp(userTable[i].userName,name))
258.break;
259.}
260.if(i==used)
261.{
262.printf("/n您输入的用户名不存在/n");
263.system("pause");
264.return-1;
265.}
266.for(times=0;times<3;times++)
267.{
268.memset(psw,'/0',sizeof(psw));
269.printf("/n请输入密码:
");
270.for(i=0;c=getch();i++)
271.{
272.if(c==13)break;
273.else
274.psw[i]=c;
275.printf("*");
276.}
277.printf("/n");
278.for(i=0;i279.{
280.if(!
strcmp(psw,userTable[i].password))
281.{
282.printf("用户登录成功/n");
283.system("pause");
284.break;
285.}
286.}
287.if(i==used)
288.{
289.printf("您输入的密码错误,您还有%d次输入机会/n",2-times);
290.if(times==2)exit(0);
291.}
292.elsebreak;
293.}
294.fflush(stdin);
295.returni;
296.}
297.
298.voidinitDisk()
299.{
300.diskHead=(diskNode*)malloc(sizeof(diskNode));
301.diskHead->maxlength=MaxDisk;
302.diskHead->useFlag=0;
303.diskHead->start=0;
304.diskHead->next=NULL;
305.}
306.intrequestDist(int&startPostion,intmaxLength)
307.{
308.intflag=0;//标记是否分配成功
309.diskNode*p,*q,*temp;
310.p=diskHead;
311.while(p)
312.{
313.if(p->useFlag==0&&p->maxlength>maxLength)
314.{
315.startPostion=p->start;
316.q=(diskNode*)malloc(sizeof(diskNode));
317.q->start=p->start;
318.q->maxlength=maxLength;
319.q->useFlag=1;
320.q->next=NULL;
321.diskHead->start=p->start+maxLength;
322.diskHead->maxlength=p->maxlength-maxLength;
323.flag=1;
324.temp=p;
325.if(diskHead->next==NULL)diskHead->next=q;
326.else
327.{
328.while(temp->next)temp=temp->next;
329.temp->next=q;
330.}
331.break;
332.}
333.p=p->next;
334.}
335.returnflag;
336.}
337.
338.voidfileCreate(charfileName[],intlength,charfileKind[])
339.{
340.//inti,j;
341.time_trawtime;
342.intstartPos;
343.UFD*fileNode,*p;
344.for(p=userTable[userID].user->next;p!
=NULL;p=p->next)