资源简介

通过模拟文件系统的实现,深入理解操作系统中文件系统的理论知识, 加深对教材中的重要算法的理解。同时通过编程实现这些算法,更好地掌握操作系统的原理及实现方法,提高综合运用各专业课知识的能力。 本文件系统采用多级目录,其中第一级对应于用户账号,第二级对应于用户账号下的文件,第三级对应文件夹下的文件,以此类推。另外,为了简单本文件系统未考虑文件共享、文件系统安全以及管道文件与设备文件等特殊内容。

资源截图

代码片段和文件信息


#include“FSMain.h“
//新建用户
bool createuser()
{
struct user newuser;
char name[10];
char pass[10];
int pride;
if(usernowpride!=1)
{
cout<<“当前用户没有新建用户的权限\n“;
return false;
}
FILE *fp;
if((fp = fopen(“user““ab+“))==NULL)
{
cout<<“用户表打开失败\n“;
return false;
}
else
{
cout<<“请输入用户名:“;
cin>>name;
if(strcmp(name“root“)==0)
{
printf(“Error:此为超级管理员\n“);
return false;
}
rewind(fp);
while(!feof(fp))
{
fread(&newusersizeof(struct user)1fp);
if(strcmp(newuser.namename)==0)
{
cout<<“该用户名已经存在\n“;
fclose(fp);
return false;
}
}
cout<<“请输入用户密码:“;
cin>>pass;
cout<<“请输入用户权限(0普通用户,1管理员):“;
cin>>pride;
strcpy(newuser.namename);
strcpy(newuser.passpass);
newuser.pride=pride;

// FILE fpuser;//为新建用户建立用户目录文件
if(!fopen(newuser.name“ab+“))
{
cout<<“用户创建失败\n“;
//如创建失败则把已经建立的用户目录删除
char cmd[20] = “DEL “;
strcat(cmdnewuser.name);
system(cmd);
fclose(fp);
return false;
}
if(!fwrite(&newusersizeof(struct user)1fp))
{
cout<<“创建失败\n“;
fclose(fp);
return false;
}
else
{

cout<<“用户创建成功\n“;
fclose(fp);
return true;
}
}
}

//用户登陆
bool login()
{
char name[10];
char pass[10];
cout<<“\n\t\t\t○用户名:“;
cin>>name;
cout<<“\t\t\t○密  码:“;
cin>>pass;
if((strcmp(“root“name)==0)&&(strcmp(“123456“pass)==0))//管理员
{
usernowpride = 1;
strcpy(usernowname“root“);
return true;
}

FILE *fp=NULL;
struct user actuser;
if(!(fp=fopen(“user““ab+“)))
{
cout<<“Error:用户表错误\n“;
return false;
}
rewind(fp);
while(!feof(fp))
{
fread(&actusersizeof(struct user)1fp);
if((strcmp(actuser.namename)==0)&&(strcmp(actuser.passpass)==0))
{
usernowpride = actuser.pride;//记录当前用户的权限
strcpy(usernownameactuser.name);//记录当前用户的主路径
nowlevel=-1;//记录当前目录层次
fclose(fp);
//设置路径
if(strcmp(usernowpath“\\“)!=0) //不是根目录就添加斜杠
{
strcat(usernowpath“\\“);
}
strcat(usernowpathusernowname);
return true;
}
}
cout<<“Error:用户名或密码无效,请核对后再输入\n“;
fclose(fp);
return false;
}

//初始化空闲区链表以及文件索引链
void intlist()
{
//-----------------------------清空各链表
fidx=NULL;
fblk=NULL;
//-----------------------------
int i=0;
struct dreitem dnow;
FILE *fp;
if(!(fp=fopen(usernowname“rb“)))
{
cout<<“Error:打开用户目录失败\n“;
return ;
}
else
{
int enp;
int sp;
fseek(fp02);
enp=ftell(fp);
fseek(fp00);
sp=ftell(fp);
if(sp==enp) return;

while(!feof(fp))
{
fread(&dnowsizeof(struct dreitem)1fp);
if(dnow.empty==0)
{
//把空闲区连到空闲链表中
struct freeb *fb =getb(struct freeb);
fb->number=i;
fb->next=NULL;
struct freeb *p=fblk;
if(p==NULL)
{
fblk=getb(struct freeb);
fblk->next=fb;
}
else
{
while(p->next!=NULL)
{
p=p->next;
}
p->next=fb;
}
}
else
{
//

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-04-15 22:44  FileSystem\
     目录           0  2016-04-15 17:06  FileSystem\FileSystem_01\
     文件         288  2016-04-15 16:04  FileSystem\FileSystem_01\1w
     文件          96  2016-04-15 14:27  FileSystem\FileSystem_01\2w
     目录           0  2016-03-07 13:29  FileSystem\FileSystem_01\bin\
     目录           0  2016-04-01 14:11  FileSystem\FileSystem_01\bin\Debug\
     文件     1074748  2016-04-01 14:11  FileSystem\FileSystem_01\bin\Debug\FileSystem_01.exe
     文件        1157  2016-03-11 21:20  FileSystem\FileSystem_01\FileSystem_01.cbp
     文件         367  2016-04-01 14:23  FileSystem\FileSystem_01\FileSystem_01.depend
     文件        1531  2016-04-15 17:06  FileSystem\FileSystem_01\FileSystem_01.layout
     文件       20364  2016-04-01 14:11  FileSystem\FileSystem_01\FSMain.cpp
     文件        1329  2016-03-11 16:55  FileSystem\FileSystem_01\FSMain.h
     文件        1016  2016-03-10 16:12  FileSystem\FileSystem_01\Login.cpp
     目录           0  2016-03-07 14:58  FileSystem\FileSystem_01\obj\
     目录           0  2016-04-01 14:11  FileSystem\FileSystem_01\obj\Debug\
     文件       35662  2016-04-01 14:11  FileSystem\FileSystem_01\obj\Debug\FSMain.o
     文件       17707  2016-03-10 16:35  FileSystem\FileSystem_01\obj\Debug\Login.o
     文件          56  2016-04-15 14:26  FileSystem\FileSystem_01\user
     文件      640981  2016-04-15 17:04  FileSystem\操作系统课程设计(田庆 计133-2 201358503230 文件系统).docx

评论

共有 条评论