⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileprocess.h

📁 文件管理系统
💻 H
📖 第 1 页 / 共 2 页
字号:
			td->d_fsize=0;
			for(int i=0;i<10;i++)
			{
				td->d_add[i]=-1;
			}
			Write(td);
			user.u_cdir->f_fsize+=1;
			SaveDirfcb(user.u_cdir);//将修之后的当前目录信息存到硬盘中去		
			block.BWrite(fdpos.blkno,tempbuf);
			delete[]tempbuf;
			return true;
		}
	}
}

Hfcb TFcb::FindFree(TUser& user)//fd.blkno=-1时说明没有找到空闲空间
{
	Hfcb fd;
	fd.blkno=-1;
	char* tempbuf=NULL;
	dir* td=NULL;
	if((tempbuf=new char[BLOCK])==NULL)
	{
		cout<<"RAM deficient!\n";
		return fd;
	}
	for(int i=0;i<10;i++)
	{
		if(user.u_cdir->f_add[i]<=0||user.u_cdir->f_add[i]>=2048)
		{
			if((user.u_cdir->f_add[i]=block.Balloc())==-1)//-1时表示没有分配到磁盘块空间
			{
				cout<<"Storage space deficient!\n";
				return fd;
			}
			else
			{

				fd.blkno=user.u_cdir->f_add[i];
				fd.offset=0;
				return fd;
			}
		}
		else//查找该块中是否还有空间空间
		{
			block.Bread(user.u_cdir->f_add[i],tempbuf);
			
			for(int j=0;j<FILENUM;j++)
			{
				td=(dir*)(tempbuf+sizeof(dir)*j);
				if(td->d_mode==NONODE)
				{
					fd.blkno=user.u_cdir->f_add[i];
					fd.offset=sizeof(dir)*j;
					return fd;
				}
			}
		}
	}
	return fd;
}

bool TFcb::IsFirst()
{
	return first;
}

void TFcb::JustWrRight(char* fname,TUser& user)//判断用户是否对该文件有写权限
{//用户权限说明请看文档中的说明
	if(fname==NULL)
	{
		cout<<"Can not find the file!\n";
		return ;
	}
	bool write=false;
	char* tempbuf=NULL;
	dir* df=NULL;
	if((tempbuf=new char[BLOCK])==NULL)
	{
		cout<<"RAM deficient!\n";
		return ;
	}
	Hfcb tf;
	tf=Namei(fname,user);//查找是否存在该文件
	if(tf.blkno!=-1)
	{
		block.Bread(tf.blkno,tempbuf);
		df=(dir*)(tempbuf+tf.offset);
		if(user.u_uid==0||user.u_uid==df->d_uid)//如果用户标识相同或是超级用户
		{
			if(user.u_uid==0)//如果是超级用户,则对任何文件有写权限
				write=true;
			if(df->d_mode!=IREAD&&df->d_mode!=IEXEC)
				write=true;
			else
				write=false;
		}
		else
		{
			if(user.u_gid==df->d_gid)//该文件为同组用户写权限
			{
				if(df->d_mode==GWRITE||df->d_mode==OWRITE||df->d_mode==OREAD)
					write=true;
				else
					write=false;
			}
			else
			{
				if(df->d_mode==OWRITE)//其它用户写权限
					write=true;
				else
					write=false;
			}
		}
	}
	else
	{
		cout<<"Can not find the file!\n";
		delete[]tempbuf;
		return ;
	}
	if(write==true)//如果有写权限,则调用写函数
	{
		Write(df);
		block.BWrite(tf.blkno,tempbuf);
		delete[]tempbuf;
	}
	else
	{
		cout<<"You have no right to write the file!\n";
		delete[]tempbuf;
	}
}


Hfcb TFcb::Namei(char* fdname,TUser& user)//of.blkno=-1时说明没有找到该名字的文件
{
	Hfcb of;
	of.blkno=-1;
	char* tempbuf=NULL;
	dir* td=NULL;
	
	if((tempbuf=new char[BLOCK])==NULL)
	{
		cout<<"RAM deficient!"<<endl;
		return of;
	}
	for(int i=0;i<10;i++)
	{
		if(user.u_cdir->f_add[i]<=0||user.u_cdir->f_add[i]>=2048)//限制不能超过模拟磁盘空间
		{
			return of;
		}
		else
		{
			block.Bread(user.u_cdir->f_add[i],tempbuf);
			for(int j=0;j<FILENUM;j++)
			{
				td=(dir*)(tempbuf+j*sizeof(dir));
				if(td->d_mode!=NONODE&&strcmp(fdname,td->d_name)==0)//确认是否是所要查找的文件目录
				{
					of.blkno=user.u_cdir->f_add[i];
					of.offset=j*sizeof(dir);
					return of;
				}
			}
		}
	}
	return of;
}

bool TFcb::OpenFile(char* fdname,TUser& user)//用于open命令,找打开文件
{
	bool open;
	if(fdname==NULL)
	{
		cout<<"Can not find the file!"<<endl;
		return false;
	}
	char* tempbuf=NULL;
	Hfcb tf=Namei(fdname,user);//查找文件
	int bno;
	if(tf.blkno==-1)
	{
		cout<<"Can not find the file!"<<endl;
		return false;
	}
	if((tempbuf=new char[BLOCK])==NULL)
	{
		cout<<"RAM deficient!"<<endl;
		return false;
	}
	block.Bread(tf.blkno,tempbuf);
	dir* fd=(dir*)(tempbuf+tf.offset);
	if(user.u_uid==fd->d_uid||user.u_uid==0)//如果是超级用户或文件主则有权限打开文件
		open=true;
	else
	{
		if(user.u_gid==fd->d_gid)//同组用户则根据文件属性来决定是否有权限打开文件
		{
			if(fd->d_mode!=IEXEC)
				open=true;
			else
			{
				if(fd->d_mode!=IREAD&&fd->d_mode!=IEXEC&&fd->d_mode!=GEXEC)//其他用户也根据文件属性来决定是否有权限打开文件
					open=true;
				else
					open=false;
			}
		}
	}
	if(open=true)
	{
		if(fd->d_mode==IFROOT||fd->d_mode==ROOT||fd->d_mode==IFDIR)
		{
			cout<<"Can not open the DIR!"<<endl;
			delete[]tempbuf;
			return false;
		}
		bno=Balfcb();//分配空的fcb
		if(bno!=-1)
		{//初始化fcb
		
			user.u_pdir=&tfcb[bno];//分配fcb
			user.u_pdir->f_count=1;
			user.u_pdir->f_flag=FCBUSE;//置fcb已经用标志
			user.u_pdir->f_blkno=tf.blkno;
			user.u_pdir->f_number=tf.offset;
			user.u_pdir->f_mode=fd->d_mode;
			user.u_pdir->f_uid=fd->d_uid;
			user.u_pdir->f_gid=fd->d_gid;
			user.u_pdir->f_fsize=fd->d_fsize;
			int na;
			for(na=0;na<8;na++)
				user.u_pdir->f_name[na]=fd->d_name[na];
			for(int c=0;c<10;c++)
				user.u_pdir->f_add[c]=fd->d_add[c];
			delete[]tempbuf;
			return true;
		}
		else
		{
			cout<<"Can not allocate the FCB!"<<endl;
			return false;
		}
	}
	else
	{
		cout<<"You have no right to open the file!"<<endl;
		delete[]tempbuf;
		return false;
	}
}

void TFcb::ReadFile(char* fname,TUser& user)//用于read命令,读出文件内容
{
	Hfcb tf=Namei(fname,user);//查找该文件是否存在
	dir* td=NULL;
	bool read=false;
	char* tempbuf=NULL;
	char* readbuf=NULL;
	if(tf.blkno!=-1)
	{
		if((tempbuf=new char[BLOCK])==NULL)
		{
			cout<<"RAM deficient!"<<endl;
			return ;
		}
		if((readbuf=new char[BLOCK])==NULL)
		{
			cout<<"RAM deficient!"<<endl;
			delete[]tempbuf;
			return;
		}
		block.Bread(tf.blkno,tempbuf);
		td=(dir*)(tempbuf+tf.offset);
		if(td->d_uid==user.u_uid||td->d_uid==0)//同一用户或是超级用户
		{//判断是否有权限读该文件
			if(td->d_uid==0)
			{
				read=true;
			}
			else
			{
				if(td->d_mode!=IEXEC)
					read=true;
				else
					read=false;					
			}
		}
		else
		{
			if(td->d_gid==user.u_gid)//如果是同组用户
			{
				if(td->d_mode==GREAD||td->d_mode==GWRITE||td->d_mode==IFREG||td->d_mode==OREAD||td->d_mode==OWRITE||td->d_mode==OEXEC)
					read=true;
				else
					read=false;
			}
			else
			{//其它用户
				if(td->d_mode==OREAD||td->d_mode==OWRITE||td->d_mode==IFREG)
					read=true;
				else
					read=false;
			}
		}
		if(read==true)
		{
			int i,j,k;
			cout<<endl;
			for(i=0,j=0;i<10;i++)
			{
				if(td->d_add[i]!=-1)//读文件
				{
					block.Bread(td->d_add[i],readbuf);

					for(k=0;j<=td->d_fsize&&k<BLOCK;j++,k++)
					{
						cout<<readbuf[k];
					}
				}
				else
				{
					cout<<endl;
					delete[]tempbuf;
					delete[]readbuf;
					break;
				}
			}
		}
		else
		{
			cout<<"You have no right to read the file!"<<endl;
			delete[]tempbuf;
			delete[]readbuf;
		}
	}
	else
	{
		cout<<"Can not find the file!"<<endl;
	}
}


void TFcb::SaveDirfcb(SFcb* tfcb)//存储fcb到磁盘
{
	char* tempbuf=NULL;
	dir* fd=NULL;
	if((tempbuf=new char[BLOCK])==NULL)
	{
		cout<<"RAM deficient!"<<endl;
		return ;
	}
	block.Bread(tfcb->f_blkno,tempbuf);
	fd=(dir*)(tempbuf+tfcb->f_number);
	fd->d_fsize=tfcb->f_fsize;
	fd->d_gid=tfcb->f_gid;
	fd->d_uid=tfcb->f_uid;
	fd->d_mode=tfcb->f_mode;
	for(int j=0;j<8;j++)
	{
		fd->d_name[j]=tfcb->f_name[j];
	}
	for(int i=0;i<10;i++)
	{
		fd->d_add[i]=tfcb->f_add[i];
	}
	block.BWrite(tfcb->f_blkno,tempbuf);
}

void TFcb::SetFile(int flag,TUser& user,char* fname)//用于set命令,设置文件属性
{
	char* tempbuf=NULL;
	Hfcb fc=Namei(fname,user);//查找该文件
	if(fc.blkno==-1)
	{
		cout<<"Can not find the file!"<<endl;
		return ;
	}
	if((tempbuf=new char[BLOCK])==NULL)
	{
		cout<<"RAM deficient!"<<endl;
		return ;
	}
	block.Bread(fc.blkno,tempbuf);
	dir* fd=(dir*)(tempbuf+fc.offset);
	if(fd->d_uid==user.u_uid||user.u_uid==0)//文件主和超级用户具有改变该文件的属性的权限
	{
		fd->d_mode=flag;
		block.BWrite(fc.blkno,tempbuf);
		delete[]tempbuf;
		return ;
	}
	else
	{
		cout<<"You have no right to modify the file's attributes!"<<endl;
		delete[]tempbuf;
		return;
	}
}



void TFcb::WFUser(USERINFO info)//将新建的用户信息写入磁盘
{
	int i,j;
	
	USERINFO* tuser;
	if(first==true)
	{
		block.FirstInit(info);
		first=false;
	}
	else
	{
		block.Bread(0,buf);//读出用于存储用户信息的磁盘块
		for(i=1;i<=USERNUM;i++)
		{
			tuser=(USERINFO*)(buf+256+sizeof(USERINFO)*i);
			if(tuser->d_flag==1)
				continue;
			else
			{//将用户信息写入buf
				tuser->d_flag=1;
				tuser->d_gid=info.d_gid;
				tuser->d_uid=info.d_uid;
				for(j=0;j<8;j++)
				{
					tuser->d_name[j]=info.d_name[j];
					tuser->password[j]=info.password[j];
				}

				block.BWrite(0,buf);
				block.SetFirst();
				break;
			}
		}
		if(i>USERNUM)
			cout<<"Sorry,can not accept new users!"<<endl;
	}
}

void TFcb::ShowUser()//用于user命令,显示已注册用户
{
	char* tempbuf=NULL;
	int i;
	if((tempbuf=new char[BLOCK])==NULL)
	{
		cout<<"RAM deficient!"<<endl;
		return ;
	}
	block.Bread(0,tempbuf);
	USERINFO* un=NULL;
	cout<<setiosflags(ios::left)
		<<setw(13)<<"[USERS]"<<setw(13)<<"[GID]"<<setw(13)<<"[UID]"<<endl;
	for(i=0;i<USERNUM;i++)
	{
		un=(USERINFO*)(tempbuf+256+i*sizeof(USERINFO));
		if(un->d_flag==-1)
			continue;
		else
		{
			cout<<setw(13)<<un->d_name;
			cout<<setw(13)<<(int)un->d_gid;
			cout<<setw(13)<<(int)un->d_uid<<endl;
		}
	}
	delete[]tempbuf;
}


bool TFcb::Write(dir* td)//写文件
{
	if(td==NULL)
	{
		cout<<"Can not find the DIR structure!"<<endl;
		return false;
	}
	char* tempbuf=NULL;
	if((tempbuf=new char[512])==NULL)
	{
		cout<<"RAM deficient!"<<endl;
		return false;
	}
	char input[BLOCK];
	//string input;
	cout<<"Please input data below(end with a '$'):"<<endl;
	//cin>>input;
	cin.getline(input,BLOCK,'$');
	int i,j,bno,ilength;
	for(i=0;i<BLOCK;i++)
		if(input[i]==0)
		{
			ilength=i;
			break;
		}
	if(td->d_fsize==0)//如果该文件为新文件
	{
		for(i=0;i<BLOCK;i++)
		{
			tempbuf[i]='\0';
		}
		for(i=0;i<ilength;i++)
		{
			tempbuf[i]=input[i];
		}
		if((bno=block.Balloc())==-1)
		{
			cout<<"Storage space deficient!"<<endl;
			return false;
		}
		td->d_add[0]=bno;
		td->d_fsize=ilength;
		block.BWrite(bno,tempbuf);
		return true;
	}
	else
	{//如果该文件为旧文件(即已经有内容
		block.Bread(td->d_add[0],tempbuf);
		for(i=td->d_fsize,j=0;j<ilength&&i<BLOCK;i++,j++)
		{
			tempbuf[i]=input[j];
		}
		td->d_fsize+=ilength;
		block.BWrite(td->d_add[0],tempbuf);
		return true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -