多级文件系统

2024-06-12 08:38
文章标签 文件系统 多级

本文主要是介绍多级文件系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1,模拟多级文件系统
2,利用最佳匹配算法进行空间的管理
3,实现了文件的创建,删除,查询,写入,读取,修改,共享等功能
4,可以用户登录
代码不是整洁,请见谅

#define  _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<time.h>#define Free 0 //空闲状态
#define Busy 1 //已用状态
#define OK 1    //完成
#define ERROR -1 //出错
#define MAX_length 64 //最大内存空间为25KB,一个物理块为1kb
#define MaxHang 8 //二维位图的最大值
#define MaxLie 8 //二维位图的最大值
typedef struct freearea//定义一个空闲区说明表结构
{long size;   //分区大小long address; //分区地址int state;   //状态
}ElemType;//----------  线性表的双向链表存储结构  ------------
typedef struct DuLNode //double linked list
{ElemType data;struct DuLNode *prior; //前趋指针struct DuLNode *next;  //后继指针
}DuLNode, *DuLinkList;DuLinkList block_first; //头结点
DuLinkList block_last;  //尾结点int alloc(int);//内存分配
int free_niecun(int); //内存回收
int Best_fit(int); //最佳适应算法//用于获取时间的公共变量
time_t rawtime;
struct  tm* timeinfo;int MaxMap[MaxHang][MaxLie] = { 0 }; //用于简单模拟位标志,此处一共可以存放25个文件,文件编号和行列的数字也有关系,如MaxMap[1][2]存放的文件编号是1*8+2
typedef struct filestruct
{int fileido;        //文件编号char filename[15];   //文件名char createtime[25];   //文件创建时间int isopen;         //文件状态 0 关闭  1打开int filemodeit;     //文件的权限  0,不可操作,1只读,2读写  默认是读写权限int filetype;       //文件的类型 1文件夹 2文本文件int size;           //文件的大小struct filestruct *liftbrother;//左同级的文件struct filestruct *rightbrother;//右同级的文件struct filestruct *child;      //本目录下级的文件struct filestruct *parents;    //所属的上级文件char *text;                   //用于文本文件写入int isshared;                  //用与判断是否共享 默认是0不共享,1可共享}FileStruct;
typedef struct Userstruct
{char *username;           //用户的名字char *uaercreatetime;struct Userstruct* liftbrother; //同级用户链接struct Userstruct* rightbrother; //同级用户链接struct filestruct *child;    //用户下的文件链接}UserStruct;
FileStruct *filehead = NULL;
UserStruct *userhead = NULL;
UserStruct *pwd = NULL;int Initblock()//开创带头结点的内存空间链表
{block_first = (DuLinkList)malloc(sizeof(DuLNode));block_last = (DuLinkList)malloc(sizeof(DuLNode));block_first->prior = NULL;block_first->next = block_last;block_first->data.state = 3;block_first->data.size = 0;block_last->prior = block_first;block_last->next = NULL;block_last->data.address = 0;block_last->data.size = MAX_length;block_last->data.state = Free;return OK;
}//--------------------  最佳适应算法  ------------------------
int Best_fit(int request)
{//请在此处添加为作业申请新空间且初始化的代码DuLinkList block = (DuLinkList)malloc(sizeof(DuLNode));memset(block, 0, sizeof(DuLNode));block->data.size = request;block->data.state = Busy;DuLNode *p = block_first->next;DuLNode *q = NULL; //记录最佳插入位置int i = 0;int num = 0;DuLNode *q1 = NULL;while (p){if (p->data.state == Free && p->data.size >= request){if (num == 0){q = p;i = q->data.size - request;}else if (p->data.size - request < i){q = p;i = q->data.size - request;}num++;}p = p->next;}//请在此处完成最佳适应算法的代码,重点:要查找到最小剩余空间的分区,即最佳插入位置if (q == NULL)//没有找到空闲块 return ERROR;else{//请插入找到了最佳位置并实现内存分配的代码!if ((q->data.size - request) >= 1){block->data.address = q->data.address;q->data.address = q->data.address + request;q->data.size = q->data.size - request;block->next = q;block->prior = q->prior;q->prior->next = block;q->prior = block;return (block->data.address);}else{q->data.state = Busy;free(block);return (q->data.address);}}
}//-----------------------   主 存 回 收   --------------------
int free_niecun(int ID)
{DuLNode *p = block_first->next;DuLNode *p1 = NULL;while (p){if (p->data.address== ID){p->data.state = Free;//cout << "内存块找到,准备回收!" << endl;if (p->next == NULL){if ((p->prior->data.state == Free) && (p->prior->data.address + p->prior->data.size == p->data.address)){p->prior->data.size += p->data.size;p->prior->next = NULL;free(p);}//cout << "内存块为最后一块!" << endl;break;}//请在此处添加其他情况的回收的代码,主要包括要回收的分区与前面的空闲块相连或与后面的空闲块相连,或者与前后空闲块相连等。if ((p->next->next == NULL) && (p->next->data.state == Free) && (p->data.address + p->data.size == p->next->data.address)){p->data.size += p->next->data.size;free(p->next);p->next = NULL;if ((p->prior->data.state == Free) && (p->prior->data.address + p->prior->data.size == p->data.address)){p->prior->data.size += p->data.size;p->prior->next = NULL;free(p);}break;}else if ((p->prior->data.state == Free) && (p->prior->data.address + p->prior->data.size == p->data.address)){if (p->next->data.state == Free && (p->data.address + p->data.size == p->next->data.address)){p1 = p->next;p->data.size += p->next->data.size;p->next->next->prior = p;p->next = p->next->next;free(p1);}p->prior->data.size += p->data.size;p->prior->next = p->next;p->next->prior = p->prior;free(p);break;}else if ((p->next->data.state == Free) && (p->data.address + p->data.size == p->next->data.address)){p1 = p->next;p->data.size += p->next->data.size;p->next = p->next->next;p->next->prior = p;free(p1);break;}break;}p = p->next;}//cout << "回收成功!" << endl;return OK;
}//初始化位图
void InitMap()
{int i = 0;int j = 0;for (i; i < MaxHang; i++){for(j; j < MaxLie; j++){MaxMap[i][j] = 0;}}return;
}
//初始化 
int Init(UserStruct **userhead) //初始化空的头文件
{int ret = 0;*userhead = (UserStruct*)malloc(sizeof(UserStruct));if (*userhead == NULL){ret = -1;return ret;}memset(*userhead, 0, sizeof(UserStruct));(*userhead)->liftbrother = NULL;(*userhead)->rightbrother = NULL;(*userhead)->child = NULL;return ret;
}//初始化一个默认用户root
int InitRoot(UserStruct **userhead) //初始化root用户
{int ret = 0;time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);//printf("time = %s", time);//printf("leng = %d\n", strlen(time));(*userhead)->liftbrother = (UserStruct*)malloc(sizeof(UserStruct));if ((*userhead)->liftbrother == NULL){ret = -1;return ret;}memset((*userhead)->liftbrother, 0, sizeof(UserStruct));((*userhead)->liftbrother)->username = (char*)malloc(5);memset(((*userhead)->liftbrother)->username, 0, 5);strcpy(((*userhead)->liftbrother)->username, "root");((*userhead)->liftbrother)->uaercreatetime = (char*)malloc(strlen(time));memset(((*userhead)->liftbrother)->uaercreatetime, 0, strlen(time));strcpy(((*userhead)->liftbrother)->uaercreatetime, time);((*userhead)->liftbrother)->liftbrother = NULL;((*userhead)->liftbrother)->rightbrother = *userhead;return ret;
}
//用户登录只验证用户名没有验证密码
UserStruct * Login(UserStruct *userhead)
{char uasename[15];printf("%s\n", "请输入用户名");scanf("%s", uasename);UserStruct * p = userhead->liftbrother;while (p){if (strcmp(p->username, uasename) == 0){return p;}p = p->liftbrother;}return NULL;
}//创建新用户
int CreateUser(UserStruct *userhead)
{UserStruct * p = userhead->liftbrother;UserStruct * tmp = NULL;time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);int ret = 0;char username[20];printf("%s\n", "请输入用户名");scanf("%s", username);tmp = (UserStruct *)malloc(sizeof(UserStruct));if (tmp == NULL){ret = -1;return ret;}memset(tmp, 0, sizeof(UserStruct));tmp->username = (char*)malloc(strlen(username));memset(tmp->username, 0, strlen(username));strcpy(tmp->username, username);tmp->uaercreatetime = (char*)malloc(strlen(time));memset(tmp->uaercreatetime, 0, strlen(time));strcpy(tmp->uaercreatetime, time);tmp->liftbrother = NULL;tmp->rightbrother = NULL;while (p->liftbrother != NULL){p = p->liftbrother;}p->liftbrother = tmp;tmp->rightbrother = p;return ret;
}//查找目录
FileStruct * SelectPath(FileStruct * path,char *pathname)
{FileStruct * p = path;while (p){//要满足名字相同,文件类型是文件夹以及权限是可以操作的  才可以在此目录下创建文件if ((strcmp(p->filename, pathname) == 0)){if (p->filemodeit == 0){printf("此文件夹没有执行权限\n");return NULL;}else if (p->filetype == 2){printf("此是文本文件无法创建子文件\n");return NULL;}return p;}p = p->liftbrother;}return NULL;
}
//寻找删除文件的路劲
FileStruct * SelectPath1(FileStruct * path, char *pathname)
{FileStruct * p = path;while (p){//要满足名字相同,文件类型以及权限是可以操作的  才可以在此目录下创建文件if ((strcmp(p->filename, pathname) == 0) && (p->filemodeit != 0)){return p;}p = p->liftbrother;}return NULL;
}
//设置已占位位图
void SetMap(int dress, int size)
{int i = dress / MaxHang;int j = dress%MaxHang;int sum = 0;for (i; i < MaxHang; i++){for (j; j < MaxLie; j++){MaxMap[i][j] = 1;sum++;if (sum == size)return ;}j = 0;}return;
}
//设置未占位位图
void SetMap1(int dress, int size)
{int i = dress / MaxHang;int j = dress%MaxHang;int sum = 0;for (i; i < MaxHang; i++){for (j; j < MaxLie; j++){MaxMap[i][j] = 0;sum++;if (sum == size)return;}j = 0;}return;
}
//创建文件
int CreateFile(UserStruct *pwd)
{int ret = 0;FileStruct *tmp = NULL;FileStruct *pathtmp = NULL;char path[20];char filename[25];int size = 0;int filemodeit = 2;int filetype =0 ;filehead  = pwd->child;printf("%s\n", "请输入文件的保存路径,如要保存在根目录下a文件夹下的b文件夹下  就请输入a/b  在根目录下就输入\n");scanf("%s", path);printf("请输入文件名\n");scanf("%s", filename);while(1){printf("请输入文件大小,最大为64,注意最大是64\n");scanf("%d", &size);if (size <= 64 || size>0)break;printf("输入有误,重新输入\n");}while (1){printf("请输入文件的权限  0,不可操作,1只读,2读写\n");scanf("%d", &filemodeit);if (filemodeit == 0 || filemodeit == 1 || filemodeit == 2)break;printf("输入有误,重新输入\n");}while (1){printf("请输入文件的类型  2文本文件 1文件夹 \n");scanf("%d", &filetype);if ( filetype == 1 || filetype == 2)break;printf("输入有误,重新输入\n");}if (strcmp(path,"/") == 0){while (filehead){if (strcmp(filehead->filename, filename) == 0){printf("此文件已经存在\n");return 0;}filehead = filehead->liftbrother;}ret = Best_fit(size);//利用最佳适应发分配空间if (ret == -1){printf("没有足够的空间的\n");return ERROR;}else{printf("ok\n");}tmp = (FileStruct *)malloc(sizeof(FileStruct));if (tmp == NULL){return ERROR;}memset(tmp, 0, sizeof(FileStruct));tmp->fileido = ret;/*tmp->filename = NULL;tmp->filename = (char*)malloc(strlen(filename)+1);if (tmp->filename == NULL){printf("filename内存开辟失败\n");return ERROR;}memset(tmp->filename, 0, strlen(filename)+1);*/strcpy(tmp->filename, filename);//获取创建时间time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);/*  tmp->createtime = NULL;tmp->createtime = (char*)malloc(strlen(time)+1);if (tmp->createtime == NULL){printf("createtime内存开辟失败\n");return ERROR;}memset(tmp->createtime, 0, strlen(time)+1);*/strcpy(tmp->createtime, time);tmp->filemodeit = filemodeit;tmp->filetype = filetype;tmp->isopen = 0;tmp->isshared = 0;tmp->size = size;tmp->child = NULL;if (pwd->child == NULL){pwd->child = tmp;tmp->parents = NULL;tmp->liftbrother = NULL;tmp->rightbrother = NULL;SetMap(ret, size);}else{tmp->liftbrother = pwd->child;pwd->child->rightbrother = tmp;pwd->child = tmp;tmp->parents = NULL;tmp->rightbrother = NULL;SetMap(ret, size);}}else{char*pathname = NULL;char d[] = "/";pathname = strtok(path, d);while (pathname != NULL){pathtmp =SelectPath(filehead, pathname);if (pathtmp == NULL){printf("此路径无效\n");return ERROR;}filehead = pathtmp->child;pathname = strtok(NULL, d);printf("此路径ok\n");}while (filehead){if (strcmp(filehead->filename, filename) == 0){printf("此文件已经存在\n");return 0;}filehead = filehead->liftbrother;}ret = Best_fit(size);//利用最佳适应发分配空间if (ret == -1){printf("没有足够的空间的\n");return ERROR;}tmp = (FileStruct *)malloc(sizeof(FileStruct));if (tmp == NULL){return ERROR;}memset(tmp, 0, sizeof(FileStruct));tmp->fileido = ret;/*  tmp->filename = NULL;tmp->filename = (char*)malloc(strlen(filename));if (tmp->filename == NULL){printf("filename内存开辟失败\n");return ERROR;}memset(tmp->filename, 0, strlen(filename));*/strcpy(tmp->filename, filename);//获取创建时间time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);/*tmp->createtime = NULL;tmp->createtime = (char*)malloc(strlen(time));if (tmp->createtime == NULL){printf("createtime内存开辟失败\n");return ERROR;}memset(tmp->createtime, 0, strlen(time));*/strcpy(tmp->createtime, time);tmp->filemodeit = filemodeit;tmp->filetype = filetype;tmp->isopen = 0;tmp->isshared = 0;tmp->size = size;tmp->child = NULL;if (pathtmp->child == NULL){pathtmp->child = tmp;tmp->parents = pathtmp;tmp->liftbrother = NULL;tmp->rightbrother = NULL;SetMap(ret, size);}else{tmp->liftbrother = pathtmp->child;pathtmp->child->rightbrother = tmp;pathtmp->child = tmp;tmp->rightbrother = NULL;tmp->parents = pathtmp;SetMap(ret, size);}}return OK;
}//删除函数
FileStruct * Delete(FileStruct *tmp)
{FileStruct * par = tmp->parents;if (tmp->child != NULL)Delete(tmp->child);if (tmp->liftbrother !=NULL)Delete(tmp->liftbrother);//free(tmp->filename);//free(tmp->createtime);if(tmp->text != NULL)free(tmp->text);free_niecun(tmp->fileido);SetMap1(tmp->fileido, tmp->size);if (tmp->rightbrother != NULL){par = tmp->rightbrother;}free(tmp);return par;
}//删除文件
int DeleteFile(UserStruct *pwd)
{int ret = 0;FileStruct *tmp = NULL;FileStruct *tmp1 = NULL;FileStruct *pathtmp = NULL;char path[15];char filename[20];filehead = pwd->child;printf("%s\n", "请输入要删除文件的路径,如要是在根目录下a文件夹下的b文件夹下c 文件路径请输入/a/b  名字输入c 在根目录下就输入 /  \n");scanf("%s", path);printf("请输入文件名\n");scanf("%s", filename);if (strcmp(path, "/") == 0){//删除根目录下全部的文件if (strcmp(filename, "/") == 0){tmp1 = filehead;tmp1->rightbrother = NULL;pwd->child = NULL;}//指定删除根目录中的文件else{pathtmp = SelectPath1(filehead, filename);if (pathtmp == NULL){printf("删除失败\n");return ERROR;}else if (pathtmp->filemodeit !=0) //处理文本文件{tmp1 = pathtmp;if (pathtmp->rightbrother == NULL){pwd->child = pathtmp->liftbrother;if (pathtmp->liftbrother != NULL)pathtmp->liftbrother->rightbrother = NULL;pathtmp->liftbrother = NULL;}else if (pathtmp->liftbrother == NULL){pathtmp->rightbrother->liftbrother = NULL;pathtmp->rightbrother = NULL;}else{pathtmp->rightbrother->liftbrother = pathtmp->liftbrother;pathtmp->liftbrother->rightbrother = pathtmp->rightbrother;pathtmp->liftbrother = NULL;pathtmp->rightbrother = NULL;}}}tmp1 = Delete(tmp1);if (tmp1 == NULL){printf("删除成功\n");}}else{char*pathname = NULL;char d[] = "/";pathname = strtok(path, d);while (pathname != NULL){pathtmp = SelectPath1(filehead, pathname);if (pathtmp == NULL){printf("此路径无效\n");return ERROR;}filehead = pathtmp->child;pathname = strtok(NULL, d);}if (strcmp(filename, "/") == 0){if (pathtmp->filemodeit != 0){tmp1 = pathtmp;if (pathtmp->rightbrother == NULL){pathtmp->parents->child = pathtmp->liftbrother;if (pathtmp->liftbrother != NULL){pathtmp->liftbrother->parents = pathtmp->parents;pathtmp->liftbrother->rightbrother = NULL;}pathtmp->parents = NULL;pathtmp->liftbrother = NULL;}else if (pathtmp->liftbrother == NULL){pathtmp->rightbrother->liftbrother = NULL;pathtmp->rightbrother = NULL;pathtmp->parents = NULL;}else{pathtmp->rightbrother->liftbrother = pathtmp->liftbrother;pathtmp->liftbrother->rightbrother = pathtmp->rightbrother;pathtmp->liftbrother = NULL;pathtmp->rightbrother = NULL;}tmp1 = Delete(tmp1);if (tmp1 == NULL){printf("删除成功\n");}}}else{pathtmp = SelectPath1(filehead, filename);if (pathtmp->filemodeit != 0){tmp1 = pathtmp;if (pathtmp->rightbrother == NULL){pathtmp->parents->child = pathtmp->liftbrother;if (pathtmp->liftbrother != NULL){       pathtmp->liftbrother->parents = pathtmp->parents;pathtmp->liftbrother->rightbrother = NULL;}pathtmp->parents = NULL;pathtmp->liftbrother = NULL;}else if (pathtmp->liftbrother == NULL){pathtmp->rightbrother->liftbrother = NULL;pathtmp->rightbrother = NULL;pathtmp->parents = NULL;}else{pathtmp->rightbrother->liftbrother = pathtmp->liftbrother;pathtmp->liftbrother->rightbrother = pathtmp->rightbrother;pathtmp->liftbrother = NULL;pathtmp->rightbrother = NULL;}tmp1 = Delete(tmp1);if (tmp1 == NULL){printf("删除成功\n");}}}}return OK;
}//遍历文件(只能显示可执行并且同级下的文件)
void List(UserStruct *pwd)
{int ret = 0;FileStruct *tmp = NULL;FileStruct *tmp1 = NULL;FileStruct *pathtmp = NULL;char path[30];
//  char filename[20];int size = 0;int filemodeit = 2;int filetype = 0;filehead = pwd->child;printf("%s\n", "请输入要显示文件的路径,如要是显示在根目录下a文件夹下的b文件夹下,就请输入/a/b 在根目录下就输入/  \n");scanf("%s", path);if (filehead == NULL){printf("此用户还没有文件可看\n");return ;}else if (strcmp(path, "/") == 0){tmp1 = filehead;printf("此路径有一下文件可看\n");printf("%s ,%3s,%2s,%2s,%10s\n", "名字",  "状态 ", "类型", "是否共享", "创建/修改时间");while (tmp1){if (tmp1->filemodeit != 0){printf("%s", tmp1->filename);printf("%1s", ((tmp1->isopen) == 0) ? "关闭" : "打开");printf("%1s", ((tmp1->filetype) == 1) ? "文件夹" : "文本文件");printf("%1s\n", ((tmp1->isshared) == 1) ? "共享" : "不共享");printf("%s\n", tmp1->createtime);}tmp1 = tmp1->liftbrother;}printf("\n");}else{char*pathname = NULL;char d[] = "/";pathname = strtok(path, d);while (pathname != NULL){pathtmp = SelectPath1(filehead, pathname);if (pathtmp == NULL){printf("此路径无效\n");return;}filehead = pathtmp->child;pathname = strtok(NULL, d);}if (pathtmp->child == NULL){printf("此路径还没有文件可看\n");return;}else{tmp1 = pathtmp->child;printf("此路径有一下文件可看\n");printf("%s   ,%12s,%20s,%25s\n","名字" ,"创建/修改时间" ,"状态 "  ,"类型");while (tmp1){if (tmp1->filemodeit != 0){printf("%s", tmp1->filename);printf("%12s", tmp1->createtime);printf("%20s", (tmp1->isopen==0) ? "关闭":"打开");printf("%25s\n", ((tmp1->filetype) == 1) ? "文件夹" : "文本文件");}tmp1 = tmp1->liftbrother;}printf("\n");}}return ;
}
//打开文件
int OpenFile(UserStruct *pwd,char *path1,char*filename1)
{char path[15];char filename[10];FileStruct *pathtmp = NULL;filehead = pwd->child;if (strcmp(path1, "@") == 0 && strcmp(filename1, "@")==0){printf("请输入关闭文件所在的路径\n");scanf("%s", path);printf("请输入要关闭文件的名称\n");scanf("%s", filename);}else{strcpy(path, path1);strcpy(filename, filename1);}if (strcmp(path, "/") == 0){while (filehead){if (strcmp(filehead->filename, filename) == 0){if (filehead->filemodeit != 0){time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);strcpy(filehead->createtime, time);filehead->isopen = 1;printf("文件以打开\n");printf("%d\n", filehead->isopen);return 1;}else{printf("文件没有执行权限,打开失败\n");return -1;}}filehead = filehead->liftbrother;}printf("没有此文件\n");return -1;}else{char*pathname = NULL;char d[] = "/";pathname = strtok(path, d);while (pathname != NULL){pathtmp = SelectPath1(filehead, pathname);if (pathtmp == NULL){printf("此路径无效\n");return ERROR;}filehead = pathtmp->child;pathname = strtok(NULL, d);}pathtmp = SelectPath1(filehead, filename);if (pathtmp == NULL){printf("没有此文件\n");return -1;}if (pathtmp->filemodeit != 0){time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);strcpy(pathtmp->createtime, time);pathtmp->isopen = 1;printf("文件以打开\n");printf("%d\n", pathtmp->isopen);return 1;}else{printf("文件没有执行权限,打开失败\n");return -1;}}
}//关闭文件
int CloseFile(UserStruct *pwd,char* path1,char* filename1)
{char path[15];char filename[10];FileStruct *pathtmp = NULL;filehead = pwd->child;if (strcmp(path1,"@") == 0 && strcmp(filename1,"@") == 0){printf("请输入关闭文件所在的路径\n");scanf("%s", path);printf("请输入要关闭文件的名称\n");scanf("%s", filename);}else{strcpy(path, path1);strcpy(filename, filename1);}if (strcmp(path, "/") == 0){while (filehead){if (strcmp(filehead->filename, filename) == 0){if (filehead->isopen == 1){time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);strcpy(filehead->createtime, time);filehead->isopen = 0;printf("文件以关闭\n");printf("%d\n", filehead->isopen);return 1;}else{printf("文件是关闭的\n");return 1;}}filehead = filehead->liftbrother;}printf("没有此文件\n");return -1;}else{char*pathname = NULL;char d[] = "/";pathname = strtok(path, d);while (pathname != NULL){pathtmp = SelectPath1(filehead, pathname);if (pathtmp == NULL){printf("此路径无效\n");return ERROR;}filehead = pathtmp->child;pathname = strtok(NULL, d);}pathtmp = SelectPath1(filehead, filename);if (pathtmp == NULL){printf("没有此文件\n");return -1;}if (pathtmp->isopen == 1){time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);strcpy(pathtmp->createtime, time);pathtmp->isopen = 0;printf("文件已关闭\n");printf("%d\n", pathtmp->isopen);return 1;}else{printf("文件是关闭的\n");return 1;}}
}
//文件重命名
int ResetName(UserStruct *pwd)
{char path[15];char filename[10];char newfilename[10];FileStruct *pathtmp = NULL;FileStruct *pathtmp1 = NULL;filehead = pwd->child;pathtmp1 = pwd->child;printf("请输入要进行重命名文件所在的路径 根目录是/ \n");scanf("%s", path);printf("请输入要进行重命名文件的名称\n");scanf("%s", filename);printf("请输入要进行重命名文件的新名称\n");scanf("%s", newfilename);if (strcmp(path, "/") == 0){while (pathtmp1){if (strcmp(pathtmp1->filename, newfilename) == 0){printf("有重名文件,重命名失败\n");return -1;}pathtmp1 = pathtmp1->liftbrother;}while (filehead){if (strcmp(filehead->filename, filename) == 0){if (filehead->filemodeit==0){printf("重命名失败,没有执行权限\n");return -1;}else if (filehead->isopen == 1){printf("重命名失败,文件是打开状态\n");return -1;}else{strcpy(filehead->filename, newfilename);time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);strcpy(filehead->createtime, time);printf("文件名称修改完毕\n");return 1;}}filehead = filehead->liftbrother;}printf("没有此文件\n");return -1;}else{char*pathname = NULL;char d[] = "/";pathname = strtok(path, d);while (pathname != NULL){pathtmp = SelectPath1(filehead, pathname);if (pathtmp == NULL){printf("此路径无效\n");return ERROR;}filehead = pathtmp->child;pathtmp1 = pathtmp->child;pathname = strtok(NULL, d);}pathtmp1 = SelectPath1(pathtmp1, newfilename);if (pathtmp1 != NULL){printf("有重名文件,重命名失败\n");return -1;}pathtmp = SelectPath1(filehead, filename);if (pathtmp == NULL){printf("没有此文件\n");return -1;}if (pathtmp->filemodeit == 0){printf("重命名失败,没有执行权限\n");return -1;}else if (pathtmp->isopen == 1){printf("重命名失败,文件是打开状态\n");return -1;}else{strcpy(pathtmp->filename, newfilename);time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);strcpy(pathtmp->createtime, time);printf("文件名称修改完毕\n");return 1;}}
}
//读文件
int WriteFile(UserStruct *pwd)
{int ret = 0;char path[15];char filename[10];char text[250];FileStruct *pathtmp = NULL;filehead = pwd->child;printf("请输入要读文件所在的路径\n");scanf("%s", path);printf("请输入要读文件的名称\n");scanf("%s", filename);if (strcmp(path, "/") == 0){while (filehead){if (strcmp(filehead->filename, filename) == 0){if (filehead->filemodeit != 2){printf("写入失败,没有执行写权限\n");return -1;}else if (filehead->filetype!=2){printf("写入失败,文件不是文本文件\n");return -1;}else{ret = OpenFile(pwd, path, filename);if (ret == -1){printf("文件打开失败无法写入\n");return -1;}printf("请输入要输入的字符\n");scanf("%s", text);filehead->text = (char*)malloc(250);memset(filehead->text, 0, 250);time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);strcpy(filehead->createtime, time);strcpy(filehead->text, text);ret = CloseFile(pwd, path, filename);if (ret == -1){printf("文件关闭失败无法写入\n");free(filehead->text);return -1;}printf("写入完毕\n");return 1;}}filehead = filehead->liftbrother;}printf("没有此文件\n");return -1;}else{char*pathname = NULL;char d[] = "/";pathname = strtok(path, d);while (pathname != NULL){pathtmp = SelectPath1(filehead, pathname);if (pathtmp == NULL){printf("此路径无效\n");return ERROR;}filehead = pathtmp->child;pathname = strtok(NULL, d);}pathtmp = SelectPath1(filehead, filename);if (pathtmp == NULL){printf("没有此文件\n");return -1;}if (pathtmp->filemodeit !=2){printf("写入失败,没有执行写权限\n");return -1;}else if (pathtmp->filetype!=2){printf("写入失败,文件不是文本文件\n");return -1;}else{ret = OpenFile(pwd, path, filename);if (ret == -1){printf("文件打开失败无法写入\n");return -1;}printf("请输入要输入的字符\n");scanf("%s", text);filehead->text = (char*)malloc(250);memset(filehead->text, 0, 250);time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);strcpy(pathtmp->createtime, time);strcpy(filehead->text, text);ret = CloseFile(pwd, path, filename);if (ret == -1){printf("文件关闭失败无法写入\n");free(filehead->text);return -1;}printf("写入完毕\n");return 1;}}
}
//写文件
int ReadFile(UserStruct *pwd)
{int ret = 0;char path[15];char filename[10];char text[250];FileStruct *pathtmp = NULL;filehead = pwd->child;printf("请输入要读文件所在的路径\n");scanf("%s", path);printf("请输入要读文件的名称\n");scanf("%s", filename);if (strcmp(path, "/") == 0){while (filehead){if (strcmp(filehead->filename, filename) == 0){if (filehead->filemodeit==0){printf("读失败,没有执行读权限\n");return -1;}else if (filehead->filetype != 2){printf("读失败,文件不是文本文件\n");return -1;}else{ret = OpenFile(pwd, path, filename);if (ret == -1){printf("文件打开失败无法读出\n");return -1;}printf("读取的内容是\n");printf("%s\n", filehead->text);ret = CloseFile(pwd, path, filename);if (ret == -1){printf("文件关闭失败\n");return -1;}return 1;}}filehead = filehead->liftbrother;}printf("没有此文件\n");return -1;}else{char*pathname = NULL;char d[] = "/";pathname = strtok(path, d);while (pathname != NULL){pathtmp = SelectPath1(filehead, pathname);if (pathtmp == NULL){printf("此路径无效\n");return ERROR;}filehead = pathtmp->child;pathname = strtok(NULL, d);}pathtmp = SelectPath1(filehead, filename);if (pathtmp == NULL){printf("没有此文件\n");return -1;}if (pathtmp->filemodeit == 0){printf("读取失败,没有执行读权限\n");return -1;}else if (pathtmp->filetype != 2){printf("写入失败,文件不是文本文件\n");return -1;}else{ret = OpenFile(pwd, path, filename);if (ret == -1){printf("文件打开失败无法读取\n");return -1;}printf("读取的内容是\n");printf("%s\n", filehead->text);ret = CloseFile(pwd, path, filename);if (ret == -1){printf("文件关闭失败\n");return -1;}printf("写入完毕\n");return 1;}}
}
//设置共享文件
int SetShared(UserStruct *pwd)
{char path[15];int shad = 0;int mode = 0;char filename[10];FileStruct *pathtmp = NULL;filehead = pwd->child;printf("请输入要进行修改文件所在的路径 根目录是/ \n");scanf("%s", path);printf("请输入要进行修改文件的名称\n");scanf("%s", filename);while (1){printf("请输是否要共享  0不共享 1共享\n");scanf("%d", &shad);if (shad == 0 || shad == 1)break;else{printf("只能输入 0或1");}}while (1){printf("请输入权限 1:只读  2:读写\n");scanf("%d", &mode);if (mode == 1 || mode == 2)break;else{printf("只能输入1或2\n");}}if (strcmp(path, "/") == 0){while (filehead){if (strcmp(filehead->filename, filename) == 0){if (filehead->filemodeit == 0){printf("修改失败,没有执行权限\n");return -1;}else{time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);strcpy(filehead->createtime, time);filehead->isshared = shad;filehead->filemodeit = mode;printf("文件修改完毕\n");return 1;}}filehead = filehead->liftbrother;}printf("没有此文件\n");return -1;}else{char*pathname = NULL;char d[] = "/";pathname = strtok(path, d);while (pathname != NULL){pathtmp = SelectPath1(filehead, pathname);if (pathtmp == NULL){printf("此路径无效\n");return ERROR;}filehead = pathtmp->child;pathname = strtok(NULL, d);}pathtmp = SelectPath1(filehead, filename);if (pathtmp == NULL){printf("没有此文件\n");return -1;}if (pathtmp->filemodeit == 0){printf("修改失败,没有执行权限\n");return -1;}else{time(&rawtime);timeinfo = localtime(&rawtime);char *time = asctime(timeinfo);strcpy(pathtmp->createtime, time);pathtmp->isshared = shad;pathtmp->filemodeit = mode;printf("文件修改完毕\n");return 1;}}
}
int user()
{int ret = 0;int type = 0;while (1){printf("-----------------------------------------------------------\n");printf("------1:是登陆(默认用户 root)        2:创建用户------------\n");printf("------0:退出-----------------------------------------------\n");printf("-----------------------------------------------------------\n");printf("-----------------------------------------------------------\n");printf("请选择\n");scanf("%d", &type);if (type != 1 && type != 2 && type != 0)continue;switch (type){case 1:pwd = Login(userhead);if (pwd == NULL){printf("登陆失败\n");}else if (pwd != NULL){printf("登陆成功\n");return 1;}break;case 2:ret = CreateUser(userhead);if (ret == 0){printf("创建用户成功\n");}break;case 0:return 0;default:printf("输入有误\n");break;}}
}
void Run()
{int type = 0;int ret = 0;while (1){printf("-----------------------------------------------------------\n");printf("------1:创建文件                     2:删除文件------------\n");printf("------3:显示文件                     4:重命名--------------\n");printf("------5读文件                        6:写文件--------------\n");printf("------7打开文件                      8:关闭文件------------\n");printf("------0:退出                        9:设置共享属性--------\n");printf("-----------------------------------------------------------\n");printf("请选择\n");scanf("%d", &type);if (type !=1 && type != 2 && type !=3 && type != 4 && type != 0 && type !=5 && type !=6 && type !=7 && type != 8 && type != 9)continue;switch (type){case 1:ret = CreateFile(pwd);if (ret==-1){printf("创建失败\n");}else if (ret == 1){printf("创建成功\n");printf("位图使用 0未使用 1已使用\n");int i = 0;int j = 0;for (i; i < MaxHang; i++){for (j=0; j < MaxLie; j++){printf("%d ", MaxMap[i][j]);}printf("\n");}}break;case 2:ret = DeleteFile(pwd);if (ret == -1){printf("删除失败\n");}else if (ret == 1){printf("删除成功\n");printf("位图使用 0未使用 1已使用\n");int i = 0;int j = 0;for (i; i < MaxHang; i++){for (j=0; j < MaxLie; j++){printf("%d ", MaxMap[i][j]);}printf("\n");}}break;case 3:List(pwd);break;case 4:ret = ResetName(pwd);break;case 5:ReadFile(pwd);break;case 6:WriteFile(pwd);break;case 7:ret = OpenFile(pwd,"@","@");break;case 8:ret = CloseFile(pwd,"@","@");break;case 9:ret = SetShared(pwd);break;case 0:return ;default:printf("输入有误\n");break;}}
}//打开文件int main(void)
{int ret = 0;int i = 0;int j = 0;ret = Init(&userhead);ret = InitRoot(&userhead);ret = user();if (ret == 1){ret = Initblock();InitMap();printf("位图使用 0未使用 1已使用\n");for (i; i < 8; i++){for (j = 0; j < 8; j++){printf("%d ", MaxMap[i][j]);}printf("\n");}Run();}system("pause");return 0;
}

初次写,高手飘过。

这篇关于多级文件系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1053723

相关文章

使用jetty和mongodb做个简易文件系统

使用jetty和mongodb做个简易文件系统 - ciaos 时间 2014-03-09 21:21:00   博客园-所有随笔区 原文   http://www.cnblogs.com/ciaos/p/3590662.html 主题  MongoDB  Jetty  文件系统 依赖库: 1,jetty(提供http方式接口) 2,mongodb的java驱动(访问mo

Spring Boot + Vue 多级目录的构建详解

1. 背景介绍 1.1 为何选择 Spring Boot + Vue? 在现代 Web 开发中,前后端分离已成为一种标准实践。Spring Boot 提供了强大的后端开发能力,尤其在构建企业级应用时,其轻量级、高效性和丰富的生态系统让开发者如虎添翼。而 Vue.js 则以其简单易学的语法和灵活的组件系统,成为前端开发的热门选择。结合这两个技术栈,我们可以轻松实现复杂的业务逻辑与优秀的用户体验。

Kubernetes集群安装、配置glusterfs文件系统

环境介绍: 3台Centos 7.4系统节点,已经部署好Kubernetes,同时复用这3台机器作为gluster存储节点: hostIPK8s roleg1-nasp12.12.10.11master + nodeg3-nasp12.12.10.13nodeg4-nasp12.12.10.14node 安装、配置glusterfs: 在物理主机上采用yum安装的方式,步骤如下: 1

Ubuntu构建只读文件系统

本文介绍Ubuntu构建只读文件系统。 嵌入式系统使用过程中,有时会涉及到非法关机(比如直接关机,或意外断电),这可能造成文件系统损坏,为了提高系统的可靠性,通常将根文件系统设置为只读,将其他需要读写的数据放置在另外开辟的一个磁盘分区,本文针对嵌入式系统常用的Ubuntu操作系统构建只读文件系统。 1.基本原理 1)OverlayFS简介 OverlayFS(Overlay File Sy

linux 文件系统与磁盘

总结 文件系统(1)磁道(2)扇区:一个磁道上分多个扇区,目前有 512bytes 和 4k 两种格式(3)分区:把多个扇区组合 做分区,每种分区类型下的分区表决定最大支持的磁盘容量,如MBR,GPT分区(4)文件系统:对每个分区格式化成不同的文件系统,如EXT2(5)inode /block/superblock:文件系统通常将文件数据和文件属性 这两部分数据放置到不同的区块,权限与属性放置到

服务器数据恢复—OneFS文件系统下数据被删除的数据恢复案例

服务器数据恢复环境&故障: EMC NAS(Isilon S200),共3个节点,每个节点配置12块STAT硬盘。数据分两部分:一部分数据为vmware虚拟机(WEB服务器),通过NFS协议共享到ESX主机;另一部分数据为视频教学文件,通过CIFS协议共享给虚拟机(WEB服务器)。 外部入侵导致视重要数据被删除,其中包括MSSQL数据库,MP4、ASF和TS类型的视频教学文件。主要是删除了NFS共

U盘 文件系统

Linux系统 U盘异常情况。 异常 描述 将文件(独立文件、文件夹、压缩包)从计算机复制(命令 或 图形界面)到U盘后,出现下述情形(1)复制到U盘的文件删除不掉,提示只读文件系统(2)从U盘复制回计算机,提示只读文件系统,输入/输出错误,文件损坏等。 解决方案 原因之一是U盘使用的文件系统有问题,可以通过格式化U盘为其他文件系统来解决(如ntfs)。

【Linux】深入理解Linux文件系统中的inode

文章目录 深入理解Linux文件系统中的inode1. 什么是inode?2. inode的结构示例: 3. inode的工作原理4. inode与文件系统的关系5. inode的实际应用6. 总结 深入理解Linux文件系统中的inode 在Linux文件系统中,inode是一个核心概念,它在文件的管理和操作中起着至关重要的作用。如果你是Linux系统的使用者或管理员,

Android14音频进阶之定制ramdisk文件系统init服务(八十三)

简介: CSDN博客专家、《Android系统多媒体进阶实战》一书作者 新书发布:《Android系统多媒体进阶实战》🚀 优质专栏: Audio工程师进阶系列【原创干货持续更新中……】🚀 优质专栏: 多媒体系统工程师系列【原创干货持续更新中……】🚀 优质视频课程:AAOS车载系统+AOSP14系统攻城狮入门视频实战课 🚀 人生格言: 人生从来没有捷径,只有行动才是治疗恐惧

Linux MTD设备文件系统

1. 文件系统简介 文件系统是一种存储和组织计算机数据的方法,它使得对其访问和查找变得容易,文件系统使用文件和树形目录的抽象逻辑概念代替了硬盘和光盘等物理设备使用数据块的概念,用户使用文件系统来保存数据不必关心数据实际保存在硬盘(或者光盘)的地址为多少的数据块上,只需要记住这个文件的所属目录和文件名。在写入新数据之前,用户不必关心硬盘上的那个块地址没有被使用,硬盘上的存储空间管理(分配和释放)功能