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

📄 filesystem.cpp

📁 操作系统文件管理
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// filesystem.cpp: implementation of the filesystem class.
//
//////////////////////////////////////////////////////////////////////
#define L sizeof(struct fcb)
#define CD 1
#define EOF -1

#include "filesystem.h"
#include <string.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h> 

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

filesystem::filesystem()
{
    init();
}

filesystem::~filesystem()
{

}

void filesystem::init()
{   
	root=(struct fcb*)malloc(L);
	c=(struct fcb*)malloc(L);
	d=(struct fcb*)malloc(L);
	e=(struct fcb*)malloc(L);
	f=(struct fcb*)malloc(L);
//-----------------------------------------
	GetSystemTime(&(root->createtime));
	strcpy(root->filename,"root");
	root->type=DIR;
	root->rightbrother=NULL;
	root->child=c;
//------------------------------------------
	c->rightbrother=d;
	d->rightbrother=e;
	d->rightbrother=f;
	f->rightbrother=NULL;
	c->father=c;
	d->father=d;
	e->father=e;
	f->father=f;
//------------------------------------------
	GetSystemTime(&(c->createtime));
	c->child=NULL;
	strcpy(c->filename,"C");
	c->type=DIR;
	c->size=7756464128;
//------------------------------------------
	GetSystemTime(&(d->createtime));
	d->child=NULL;
	strcpy(d->filename,"D");
	d->type=DIR;
	d->size=1853685720;
//------------------------------------------
	GetSystemTime(&(e->createtime));
	e->child=NULL;
	strcpy(e->filename,"E");
	e->type=DIR;
	e->size=1719427072;
//------------------------------------------
	GetSystemTime(&(f->createtime));
	f->child=NULL;
	strcpy(f->filename,"F");
	f->type=DIR;
	f->size=1276334080;
//===========================================
	strcpy(pd.db[0].dirname,"C");
	strcpy(pd.db[1].dirname,"D");
	strcpy(pd.db[2].dirname,"E");
	strcpy(pd.db[3].dirname,"F");
	pd.db[0].size=7756464128;
	pd.db[1].size=1853685720;
	pd.db[2].size=1719427072;
	pd.db[3].size=1276334080;
	pd.db[0].state=1;
	pd.db[1].state=0;
	pd.db[2].state=0;
	pd.db[3].state=0;
	pd.p=0;
//===========================================
	present=c;//初始化访问为C盘
	predir=c;
	strcpy(url,"C:\\");
	front=c;
	dp=0;
	dirs.i=0;
//===========================================
	for(int i=0;i<1000;i++)
	{
	    space[i]=0;
		fat[i]=-1;
	}
}

int filesystem::cd()
{
	struct fcb *p;
	int i=dirs.i-1,j;
	char turl[100];
	strcpy(turl,url);
	if(locateDir(CD)==0)
	{
		printf("系统找不到指定路径\n");
		goto over;
	}
	p=NULL;
	
	if(strcmp(dirs.dir[i],".")==0)
	{
	//	present=present;
		//url不变
		predir=present;
	}
	else if(strcmp(dirs.dir[i],"..")==0)
	{
		predir=present->father;
	}
	else if(strcmp(dirs.dir[i],"\\")==0)
	{		    
		switch(pd.p)
		{
		case 0: predir=c;  break;
		case 1: predir=d;  break;
		case 2: predir=e;  break;
		case 3: predir=f;  break;
		}
	}
	else
//	if(i>=0)
	{
	    p=present->child;
		while(p!=NULL)
		{/*
			if(p->type==F)
			{
			    printf("目录名无效\n\n");
				goto over;
			}*/
			if(p->type==F)
			{
				p=p->rightbrother;
				continue;
			}
		    if(strcmp(p->filename,dirs.dir[i])==0)
			{
				if(url[3]!='\0')
					strcat(url,"\\");
				strcat(url,p->filename);
				break;
			}
			p=p->rightbrother;
		}
		if(p!=NULL)
			present=p;
		else  //找不到路径,返回原来的url
		{
			strcpy(url,turl);
			printf("系统找不到指定路径\n");
			goto over;
		}
	    predir=present;
		if(p!=NULL&&p->type==F)
		{
	        printf("目录名无效\n\n");
		    goto over;
		}
	}
	
over:
	outputUrl();
}

int filesystem::dir()
{
	struct fcb *t,*p;
	int mulu=0,wj=0,size=0,i;
	i=dirs.i-1;
	if(locateDir(0)==0)
	{
		p=NULL;
		goto next;
	}
    if(dirs.i==0)
		p=present;   /*
	else if(dirs.i==-1)
		p=predir;*/
	else if(strcmp(dirs.dir[i],".")==0)
	{
		p=present;
		//url不变
	}
	else if(strcmp(dirs.dir[i],"..")==0)
	{
		p=present->father;
	}
	else if(strcmp(dirs.dir[i],"\\")==0)
	{		    
		switch(pd.p)
		{
		case 0: p=c;  break;
		case 1: p=d;  break;
		case 2: p=e;  break;
		case 3: p=f;  break;
		}
	}
	else
//	if(i!=0)
	{
		p=present->child;
		while(p!=NULL)
		{
		    if(strcmp(p->filename,dirs.dir[i])==0)
				break;
			p=p->rightbrother;
		}
	}

next:
	printf("%s中的目录\n",url);
	if(p==NULL)
	{
	    printf("找不到文件\n");
		return 0;
	}
	outputTime(p);
	printf("<DIR>         .\n");
	t=p->father;
	outputTime(t);
	printf("<DIR>         ..\n");
	p=p->child;
	while(p!=NULL)
	{
//	    if(p->type==DIR)
//		{
		    outputTime(p);
			if(p->type==DIR)
			{
			    printf("<DIR>         ");
				printf("%s\n",p->filename);
				mulu++;
			}
			else
			{
			    printf("%13d",p->size);
				printf(" %s\n",p->filename);
				size+=p->size;
				wj++;
			}
//		}
			p=p->rightbrother;
	}
    printf("%d 个文件    %d 字节\n",wj,size);
	printf("%d 个目录    %14.0f 可用字节\n",mulu,pd.db[pd.p].size);

	outputUrl();
	return 1;
}

int filesystem::md()
{
	struct fcb *front,*t,*p;
	int i,j;
    i=0;
	present=predir;
	if(strcmp(dirs.dir[i],".")==0)
	{
		i=1;
		present=present;
		//url不变
	}
	else if(strcmp(dirs.dir[i],"..")==0)
	{
		i=1;
		present=present->father;
	}
	else if(strcmp(dirs.dir[i],"\\")==0)
	{		 
		i=1;
		switch(pd.p)
		{
		case 0: present=c;  break;
		case 1: present=d;  break;
		case 2: present=e;  break;
		case 3: present=f;  break;
		}
	}
	else
		i=0;
	t=present;
	front=NULL;
	for(i;i<dirs.i;i++)
	{	    
		p=t->child;
		while(p!=NULL)
		{
			if(p->type==F)
			{
				front=p;
			    p=p->rightbrother;
				continue;
			}
		    if(strcmp(p->filename,dirs.dir[i])==0)
			{
				t=p;				
				break;
			}
			front=p;
			p=p->rightbrother;
		}
		if(p==NULL)//从当前起,剩下的为目录名
		{
			goto mkdir;
		}
	}
mkdir:
//-----先查找是否已有该目录------------
	if(i==dirs.i)
	{
		printf("该文件已存在\n");
		goto over;
	}
	if(front!=NULL)
		present=front->father;
	//=========过滤==============
	p=present->child;
		while(p!=NULL)
		{/*
			if(p->type==DIR)
			{
			    p=p->rightbrother;
				continue;
			}*/
		    if(strcmp(p->filename,dirs.dir[i])==0)
			{
				printf("该文件已存在\n");
				goto over;
			}
			p=p->rightbrother;
		}
	for(j=i;j<dirs.i;j++)
	{
	    t=(struct fcb*)malloc(L);
		strcpy(t->filename,dirs.dir[j]);
		t->father=present;
		t->child=NULL;
		t->rightbrother=NULL;
		GetSystemTime(&(t->createtime));
		t->createtime.wHour=(t->createtime.wHour+8)%24;
		t->type=DIR;		
		if(front!=NULL)
		{
		    front->rightbrother=t;
			front=NULL;
		}
		else
			present->child=t;
		present=t;
	//	front=t;
	}
//	front->rightbrother=NULL;
over:
	outputUrl();
	return 1;
}

int filesystem::mk()
{
    struct fcb *front,*t,*p;
	int i,j;
	char temp[3];
    i=0;
	present=predir;
	if(strcmp(dirs.dir[i],".")==0)
	{
		i=1;
		present=present;
		//url不变
	}
	else if(strcmp(dirs.dir[i],"..")==0)
	{
		i=1;
		present=present->father;
	}
	else if(strcmp(dirs.dir[i],"\\")==0)
	{		 
		i=1;
		switch(pd.p)
		{
		case 0: present=c;  break;
		case 1: present=d;  break;
		case 2: present=e;  break;
		case 3: present=f;  break;
		}
	}
	else
		i=0;
	t=present;
	front=NULL;
	for(i;i<dirs.i;i++)
	{	    
		p=t->child;
		while(p!=NULL)
		{
			if(p->type==F)
			{
				front=p;
			    p=p->rightbrother;
				continue;
			}
		    if(strcmp(p->filename,dirs.dir[i])==0)
			{
				t=p;				
				break;
			}
			front=p;
			p=p->rightbrother;
		}
		if(p==NULL)//从当前起,剩下的为目录名
		{
			goto mkfile;
		}
	}
mkfile:
	//-----先查找是否已有该文件------------
	if(i==dirs.i)
	{
		printf("该文件已存在\n");
		goto over;
	}
	if(front!=NULL)
		present=front->father;
	//=========过滤==============
	p=present->child;
		while(p!=NULL)
		{/*
			if(p->type==DIR)
			{
			    p=p->rightbrother;
				continue;
			}*/
		    if(strcmp(p->filename,dirs.dir[i])==0)
			{
				printf("该文件已存在\n");
				goto over;
			}
			p=p->rightbrother;
		}
	for(j=i;j<dirs.i;j++)
	{
	    t=(struct fcb*)malloc(L);
		strcpy(t->filename,dirs.dir[j]);
		t->father=present;
		t->child=NULL;
		t->rightbrother=NULL;
		GetSystemTime(&(t->createtime));
		t->createtime.wHour=(t->createtime.wHour+8)%24;
		t->type=F;
		if(front!=NULL)
		    front->rightbrother=t;
		else
			present->child=t;
//		front=t;
//======在此添加文件大小等其他属性==================
		printf("输入文件所需的物理块数:");
//		scanf("%d",&t->blocknums);
		gets(temp);
		t->blocknums=atoi(temp);
		t->firstblock=spaceDistribute(t->blocknums);
		t->size=t->blocknums*1024;
		pd.db[pd.p].size-=t->size;
	}
//	front->rightbrother=NULL;
over:
	outputUrl();
	return 1;
}

int filesystem::del()
{
	struct fcb *p,*f;
	int exist=0;
    if(locateDir(0)==0)
	{
	    printf("系统找不到指定文件\n");
		goto over;
	}
	f=present;/////////////////////////////////////////////////
    p=present->child;
	while(p!=NULL)
	{
		if(p->type==DIR)
		{
			p=p->rightbrother;
			continue;
		}
	    else if(strcmp(p->filename,dirs.dir[dirs.i-1])==0)
		{
			exist=1;
			if(f==present)
		        f->child=p->rightbrother;
	        else
		        f->rightbrother=p->rightbrother;
			back(p->firstblock,p->blocknums);
			pd.db[pd.p].size+=p->size;
			break;
		}
		else if(mohupipei(p->filename)==1)
		{
			exist=1;
			back(p->firstblock,p->blocknums);
			pd.db[pd.p].size+=p->size;
			if(f==present)
		        f->child=p->rightbrother;
	        else
		        f->rightbrother=p->rightbrother;
			p=p->rightbrother;			
			continue;
		}
		f=p;
		p=p->rightbrother;
	}
	if(exist==0)
		printf("找不到 %s\\%s\n",url,dirs.dir[dirs.i-1]);
over:

⌨️ 快捷键说明

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