📄 main.cpp
字号:
#include"base.h"
Stack<int> sinode; /*******I 节点栈****写在超级块文件****/
Stack<int> sdisk; /*******盘块号栈****写在超级块文件****/
Stack<int> sdirid; /*******目录编号栈**写在超级块文件****/
Stack<inode> stinod;
Stack<dir> stdir;
string dirf="dir.txt";
dir rootdir;
string cmd,str,cur,str1;
int node;
int disk;
int filesize;
int addr[10];
/////////////////////////////////////////////////////////////////
void searchdir(int i);
bool irecall(int it);
bool drecall(int it);
void mkdir(string fdirn,string sondir);
void rmdir(string rmdir);
void ls(string curdir);
void cd(string cddir);
void cp(string file,string dname);
void rm(string file,string curdir);
void cat(string cfname,string curdir);
void logout();
/////////////////////////////////////////////////////////////////
int main()
{
/****************************登 陆*****************************/
int i;
cout<<"/*************************欢迎使用unix文件模拟系统*****************************/"<<endl;
for(i=0;i<3;i++)
{
if(i==2)
return 0;
else
{
if(login("usr.txt"))
{
cout<<"/***************************登陆成功,系统正在初始化***************************/"<<endl;
load("supbk.txt",sinode,sdisk,sdirid);
break;
}
else
cout<<"/************************用户名或密码不正确,请重新输入************************/"<<endl;
}
}
cout<<"/********************************系统初始化完毕********************************/"<<endl;
rootdir.dirname="root";cur="root";
cout<<"root:/";
while(1)
{
cin>>cmd;
if(cmd=="ls")
{
ls(cur);
cout<<"root:/";
}
if("cat"==cmd)
{
cin>>str;
cat(str,cur);
cout<<"root:/";
}
if(cmd=="mkdir")
{
cin>>str;
mkdir(cur,str);
cout<<"root:/";
}
if(cmd=="rmdir")
{
cin>>str;
rmdir(str);
cout<<"root:/";
}
if(cmd=="cp")
{
cin>>str1;
cp(str,str1);
cout<<"root:/";
}
if(cmd=="rm")
{
cin>>str;
rm(str,cur);
cout<<"root:/";
}
if(cmd=="cd")
{
cin>>str;
cd(str);
}
if(cmd=="logout")
{
cin>>str;
logout();
return 0;
}
}
}
bool irecall(int it)
{
int t;
t=rlockFIBStackff();
if(t==1)
{
cout<<"超级块上锁,不能访问!"<<endl;
return false;
}
else
{
wlockFIBStacktf(1);//zhi chao ji kuai suo
sinode.push(it);
//winodeid(1,it);
wlockFIBStacktf(0);
return true;
}
}
bool drecall(int it)
{
int t;
t=rlockFBStackff();
if(t==1)
{
cout<<"超级块上锁,不能访问!"<<endl;
return false;
}
else
{
wlockFIBStacktf(1);//zhi chao ji kuai suo
sdisk.push(it);
//wdiskid(1,it);
wlockFIBStacktf(0);
return true;
}
}
void mkdir(string fdirn,string sondir)
{
dir newdir,father;
inode newinode;
father=finddir(fdirn);
newdir.dirname=sondir;
newdir.id=sdirid.pop();
newdir.fatherid=father.id;
if(ialloc(sinode,node,addr))
{
winodeid(0,node);//0 --分配,1--回收
wdirid(0,newdir.id);// xie cao ji kuai
newdir.nid=node;
writedir(newdir);
}
else
cout<<"资源不足,无法创建!"<<endl;
}
void rmdir(string rmdir)
{// hai you shan chu jie dian ,hui shou jie dian pan kuai jie dian ,xie cao ji kuai
dir newdir1,newdir2;
newdir1=finddir(rmdir);
stdir.push(newdir1);
searchdir(newdir1.id);
while(!stdir.StackEmpty())
{
newdir1=stdir.pop();
sdirid.push(newdir1.id);
deletedir(newdir1.dirname);//删除要删除的目录,但还有他的子目录也要删除
deleteinode(newdir1.nid);//删除该与目录相对应的节点
irecall(newdir1.nid); //回收被删除目录所占用的节点
}
}
void ls(string curdir)
{
stdir.ClearStack();
dir newdir=finddir(curdir);
int t;
t=newdir.id;
inode ind;
ifstream in;
in.open(dirf.c_str());
while(!in.eof())
{
in>>newdir.fatherid>>newdir.dirname>>newdir.id>>newdir.nid;
if(newdir.fatherid==t)
stdir.push(newdir);
}
stdir.pop();
in.close();
while(!stdir.StackEmpty())
{
ind=findinode(stdir.Peek().nid);
cout<<ind.mode<<" "<<stdir.Peek().dirname<<" "<<ind.ctime<<" "<<ind.uid<<" "<<ind.lctime<<endl;
stdir.pop();
}
}
void searchdir(int i)
{
dir d;
ifstream in;
in.open(dirf.c_str());
while(!in.eof())
{
in>>d.fatherid>>d.dirname>>d.id>>d.nid;
if(d.fatherid==i)
{
if(in.eof())
break;
stdir.push(d);
searchdir(stdir.Peek().id);
}
}
in.close();
}
void cd(string cddir)
{
cout<<cddir<<":/";
cur=cddir;
}
void cp(string file,string dname)
{
dir d,d1;
d=finddir(file);
if(d.dirname==file)
{
cout<<"源文件不存在"<<endl;
exit(1);
}else
{
d1=finddir(dname);
d.fatherid=d1.id;
cout<<"d1.id:"<<d1.id<<endl;
writedir(d);
}
}
void rm(string file,string curdir)
{
dir d;
d=finddir(file);
if(d.dirname!=file)
{
cout<<"源文件不存在"<<endl;
exit(1);
}
else
{
char ch;
cout<<"你确实要删除此文件吗?,请键入y或n"<<endl;
cin>>ch;
if(ch=='n')
return ;
else if(ch=='y')
{
inode ind;
ind=findinode(d.nid);
sdirid.push(d.id);
irecall(d.nid);//回收节点
//hui shou pan kuai
for(int i=0;i<10;i++)
{
if(ind.di_addr[i]!=0)
drecall(ind.di_addr[i]);//回收盘块
}
deletedir(file);//删除该文件的目录
deleteinode(d.nid);//删除该文件的节点记录
cout<<"成功删除文件"<<endl;
}
}
}
void cat(string cfname,string curdir)
{
int i,t;
dir d;
//d.id=-1;
d=finddir(cfname);
if(d.dirname==cfname)
{
string s[10],s1;
inode ind;
ind=findinode(d.nid);
for(i=0;i<10;i++)
{
s[i]=cfname;
if(ind.di_addr[i]!=0)
{
s[i]+=inttochar(ind.di_addr[i]);
s[i]+=".txt";
ifstream in;
in.open(s[i].c_str());
while(!in.eof())
{
in>>s1;
cout<<s1<<endl;
}
in.close();
}
}
}
else
{
cout<<"请输入文件大小"<<endl;
cin>>filesize;
t=calculate(filesize,512);//盘块数
for(i=0;i<10;i++)
{
if(i<t)
{
diskalloc(sdisk,disk);
addr[i]=disk;
}
else
addr[i]=0;
}
//建一节点
ialloc(sinode,node,addr);
//建一目录
dir newd;
newd.dirname=cfname;
newd.fatherid=finddir(curdir).id;
newd.id=sdirid.pop();
newd.nid=node;
writedir(newd);
string *s;
s=new string[t];
for(i=0;i<t;i++)
{
ofstream out;
s[i]=cfname;
s[i]+=inttochar(addr[i]);
s[i]+=".txt";
out.open(s[i].c_str());
out.close();
}
cout<<"你可以打开文件对文件进行写操作"<<endl;
}
}
void logout()
{
cout<<"系统即将退出,正在保存系统"<<endl;
wfreeIBlockNtf(sinode.recount());
wfreeBlockNtf(sdisk.recount());
while(!sinode.StackEmpty())
{
winodeid(1,sinode.pop());
}
while(!sdisk.StackEmpty())
{
wdiskid(1,sdisk.pop());
}
while(!sdirid.StackEmpty())
{
wdirid(1,sdirid.pop());
}
sinode.ClearStack();
sdisk.ClearStack();
sdirid.ClearStack();
cout<<"系统已退出"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -