📄 多用户多级目录文件系统.cpp
字号:
(strcmp(username, "user6") == 0 && strcmp(password, "user6") == 0) ||
(strcmp(username, "user7") == 0 && strcmp(password, "user7") == 0) ||
(strcmp(username, "user8") == 0 && strcmp(password, "user8") == 0) ||
(strcmp(username, "root") == 0 && strcmp(password, "root") == 0))
{
if (strcmp(username, "root") == 0) // 一个管理员
{
strcpy(CS.CurrentUser.UserName, username);
CS.CurrentUser.ut = admin;
}
else
{
strcpy(CS.CurrentUser.UserName, username);
CS.CurrentUser.ut = comm;
}
CS.FileLevel++;
CS.CurrParent = base;
strcpy(CS.CurrentPath, "/");
printf("\n\t\t欢迎使用多用户多级目录文件系统\n");
printf("\t\t 班级:04计算机科学与技术1班\n");
printf("\t\t\t 姓名:胡海洪\n");
printf("\t\t\t学号:3104006429\n");
printf("\n[%s@localhost %s]$", username, CS.CurrentPath);
break;
}
else
{
printf("\n用户名或密码错误,请重新输入。\n");
}
}
if (c >= LOGIN_COUNT) //非法用户
{
printf("\n对不起,您不是该系统用户,按任意键退出系统。\n");
return false;
}
else
{
return true;
}
}
/*
* 函数介绍:创建一个文件
* 输入参数:无
* 输出参数:无
* 返 回 值:无
*/
void Create(char *filename)
{
if (strcmp(filename, "") == 0)
{
printf("对不起,文件名不能为空。\n");
}
else
{
CreateFileElement(protect, filename, file, NULL, CS.CurrParent);
}
printf("[%s@localhost %s]$", CS.CurrentUser.UserName, CS.CurrentPath);
}
/*
* 函数介绍:列出当前目录的文件和文件夹
* 输入参数:path 路径
* 输出参数:无
* 返 回 值:无
*/
void Dir(char *path)
{
char display[1000];
memset(display, '\0', 1000);
//查找显示内容
for (unsigned i = 0; i < FS.FI.FICount; i++)
{
if (strcmp(FS.FI.FIStart[i].ParentName, CS.CurrParent->FileName) == 0 &&
FS.FI.FIStart[i].FileLevel == CS.FileLevel && FS.FI.FIStart[i].effect == 1)
{
strcat(display, FS.FI.FIStart[i].FileName);
strcat(display, "\t\t");
}
}
printf("%s\n", display);
printf("[%s@localhost %s]$", CS.CurrentUser.UserName, CS.CurrentPath);
}
/*
* 函数介绍:创建一个文件夹
* 输入参数:文件夹名
* 输出参数:无
* 返 回 值:无
*/
void Mkdir(char *filename)
{
if (strcmp(filename, "") == 0)
{
printf("对不起,文件夹名不能为空。\n");
}
else
{
CreateFileElement(protect, filename, dir, NULL, CS.CurrParent);
}
printf("[%s@localhost %s]$", CS.CurrentUser.UserName, CS.CurrentPath);
}
/*
* 函数介绍:进入一个文件夹
* 输入参数:path 路径
* 输出参数:无
* 返 回 值:无
*/
void Cd(char *path)
{
int splitDisplayCou = 0; //分割符出现的次数
if(strcmp(path, "..") == 0) //返回上一级目录,即父目录
{
if (CS.FileLevel > 0)
{
CS.FileLevel--;
CS.CurrParent = CS.CurrParent->parent;
for (unsigned i = strlen(CS.CurrentPath) - 1; i > 0; i--)
{
if (CS.CurrentPath[i] == '/')
{
splitDisplayCou++;
if (splitDisplayCou == 2) //已过滤掉最后一个目录名
{
break;
}
}
}
char temppath[1000] ;
strcpy(temppath, CS.CurrentPath);
memset(CS.CurrentPath, '\0', 1000);
strncpy(CS.CurrentPath, temppath, i+1);
}
else
{
}
}
else
{
char display[100] = "";
for (unsigned i = 0; i < FS.FI.FICount; i++)
{
if (strcmp(FS.FI.FIStart[i].ParentName, CS.CurrParent->FileName) == 0 &&
FS.FI.FIStart[i].FileLevel == CS.FileLevel && FS.FI.FIStart[i].effect == 1 &&
strcmp(FS.FI.FIStart[i].FileName, path) == 0)
{
strcpy(display, "文件存在。\n");
CS.CurrParent = (FSElement *)FindBlankFileBlock(FS.FI.FIStart[i].FileBlockId);
CS.FileLevel++;
strcat(CS.CurrentPath, path);
strcat(CS.CurrentPath, "/");
break;
}
}
if (strcmp(display, "") == 0) //文件夹不存在,什么都不做
{
printf("当前目录下没有您要进入的文件夹。\n");
}
}
printf("[%s@localhost %s]$", CS.CurrentUser.UserName, CS.CurrentPath);
}
/*
* 函数介绍:删除当前目录的文件
* 输入参数:path 路径
* 输出参数:无
* 返 回 值:无
*/
void Delete(char *path)
{
char display[100] = "";
for (unsigned i = 0; i < FS.FI.FICount; i++)
{
if (strcmp(FS.FI.FIStart[i].ParentName, CS.CurrParent->FileName) == 0 &&
FS.FI.FIStart[i].FileLevel == CS.FileLevel && FS.FI.FIStart[i].effect == 1 &&
strcmp(FS.FI.FIStart[i].FileName, path) == 0)
{
FS.FI.FIStart[i].effect = 0; //删除标记
strcpy(display, "文件已删除。\n");
break;
}
}
if (strcmp(display, "") == 0)
{
strcpy(display, "当前目录下没有您要删除的文件。\n");
}
printf("%s\n", display);
printf("[%s@localhost %s]$", CS.CurrentUser.UserName, CS.CurrentPath);
}
/*
* 函数介绍:打开当前目录的文件
* 输入参数:path 路径
* 输出参数:无
* 返 回 值:无
*/
void Open(char *path)
{
char display[100];
for (unsigned i = 0; i < FS.FI.FICount; i++)
{
if (strcmp(FS.FI.FIStart[i].ParentName, CS.CurrParent->FileName) == 0 &&
FS.FI.FIStart[i].FileLevel == CS.FileLevel && FS.FI.FIStart[i].effect == 1 &&
strcmp(FS.FI.FIStart[i].FileName, path) == 0)
{
FSElement *fselem = (FSElement *)FindBlankFileBlock(FS.FI.FIStart[i].FileBlockId);
fselem->fileStu = opened;
strcpy(display, "文件已打开完毕。\n");
break;
}
}
if (strcmp(display, "") == 0)
{
strcpy(display, "当前目录下没有您要打开的文件。\n");
}
printf("%s\n", display);
printf("[%s@localhost %s]$", CS.CurrentUser.UserName, CS.CurrentPath);
}
/*
* 函数介绍:读取当前目录文件
* 输入参数:path 路径
* 输出参数:无
* 返 回 值:无
*/
void Read(char *path)
{
char display[BLOCK_SIZE];
for (unsigned i = 0; i < FS.FI.FICount; i++)
{
if (strcmp(FS.FI.FIStart[i].ParentName, CS.CurrParent->FileName) == 0 &&
FS.FI.FIStart[i].FileLevel == CS.FileLevel && FS.FI.FIStart[i].effect == 1 &&
strcmp(FS.FI.FIStart[i].FileName, path) == 0)
{
FSElement *fselem = (FSElement *)FindBlankFileBlock(FS.FI.FIStart[i].FileBlockId);
if (fselem->fileStu == closed)
{
strcpy(display, "文件尚未打开,请先打开文件。\n");
}
else
{
if (fselem->FileData == NULL || strcmp(fselem->FileData, "") == 0)
{
strcpy(display, "文件无内容。\n");
}
else
{
strcpy(display, fselem->FileData);
}
fselem->fileStu = reading;
}
break;
}
}
if (strcmp(display, "") == 0)
{
strcpy(display, "当前目录下没有您要打开的文件。\n");
}
printf("%s\n", display);
printf("[%s@localhost %s]$", CS.CurrentUser.UserName, CS.CurrentPath);
}
/*
* 函数介绍:写当前目录文件
* 输入参数:path 路径
* 输出参数:无
* 返 回 值:无
*/
void Write(char *path)
{
char display[BLOCK_SIZE];
memset(display, '\0', BLOCK_SIZE);
for (unsigned i = 0; i < FS.FI.FICount; i++)
{
if (strcmp(FS.FI.FIStart[i].ParentName, CS.CurrParent->FileName) == 0 &&
FS.FI.FIStart[i].FileLevel == CS.FileLevel && FS.FI.FIStart[i].effect == 1 &&
strcmp(FS.FI.FIStart[i].FileName, path) == 0)
{
FSElement *fselem = (FSElement *)FindBlankFileBlock(FS.FI.FIStart[i].FileBlockId);
if (fselem->fileStu == closed)
{
strcpy(display, "文件尚未打开,请先打开文件。\n");
}
else
{
printf("\n注意:文件最大不可以超过 %d 字节!!按CTRL+D结束编辑。\n", BLOCK_SIZE - sizeof(FSElement));
char c;
int i = 0;
while ((c = getchar()) != 0x04)
{
display[i++] = c;
}
getchar(); //处理回车
display[i] = '\0';
strcpy(fselem->FileData, display);
unsigned len = strlen(display) < BLOCK_SIZE - sizeof(FSElement) ? strlen(display) : BLOCK_SIZE - sizeof(FSElement);
strncpy(fselem->FileData, display, len);
fselem->fileStu = writing;
strcpy(display, "文件写入成功。\n");
}
break;
}
}
printf("%s\n", display);
printf("[%s@localhost %s]$", CS.CurrentUser.UserName, CS.CurrentPath);
}
/*
* 函数介绍:关闭当前目录的文件
* 输入参数:path 路径
* 输出参数:无
* 返 回 值:无
*/
void Close(char *path)
{
char display[100];
for (unsigned i = 0; i < FS.FI.FICount; i++)
{
if (strcmp(FS.FI.FIStart[i].ParentName, CS.CurrParent->FileName) == 0 &&
FS.FI.FIStart[i].FileLevel == CS.FileLevel && FS.FI.FIStart[i].effect == 1 &&
strcmp(FS.FI.FIStart[i].FileName, path) == 0)
{
FSElement *fselem = (FSElement *)FindBlankFileBlock(FS.FI.FIStart[i].FileBlockId);
fselem->fileStu = opened;
strcpy(display, "文件已关闭。\n");
break;
}
}
if (strcmp(display, "") == 0)
{
strcpy(display, "当前目录下没有您要关闭的文件。\n");
}
printf("%s\n", display);
printf("[%s@localhost %s]$", CS.CurrentUser.UserName, CS.CurrentPath);
}
/*
* 函数介绍:命令分解
* 输入参数:command 用户输入命令字,key 关键字,path 路径
* 输出参数:无
* 返 回 值:无
*/
void FindCommKey(char *command, char *key, char *path)
{
for (unsigned i = 0; i < strlen(command); i++)
{
if (command[i] == ' ')
{
i++;
if (i < strlen(command))
{
strcpy(path, command + i);
}
break;
}
if (i <= 9)
{
key[i] = command[i];
}
else
{
for (unsigned j = i; j < strlen(command); j++)
{
if (command[j] != ' ')
{
strcpy(path, command + j);
break;
}
}
break;
}
}
strlwr(key);
strlwr(path);
}
/*
* 函数介绍:命令解释模块
* 输入参数:无
* 输出参数:无
* 返 回 值:无
*/
void Shell()
{
char command[COMMAND_LEN];
gets(command);
char key[10];
char path[COMMAND_LEN - 10];
while (true)
{
memset(key, '\0', 10);
memset(path, '\0', COMMAND_LEN - 10);
FindCommKey(command, key, path);
unsigned i;
for (i = 0; i < PRO_SET_COMM_COU; i++)
{
if (strcmp(key, PRO_SET_COMM[i]) == 0)
{
break;
}
}
switch (i)
{
case 0:
Create(path);
break;
case 1:
Open(path);
break;
case 2:
Read(path);
break;
case 3:
Write(path);
break;
case 4:
Close(path);
break;
case 5:
Delete(path);
break;
case 6:
Mkdir(path);
break;
case 7:
Cd(path);
break;
case 8:
Dir(path);
break;
case 9:
//Logout
break;
default:
printf("\n命令错误。\n");
printf("[%s@localhost %s]$", CS.CurrentUser.UserName, CS.CurrentPath);
}
if (i == 9)
{
break;
}
gets(command);
}
}
/*
* 函数介绍:退出时清理分配的内存空间
* 输入参数:无
* 输出参数:无
* 返 回 值:无
*/
void ClearFileSys()
{
free( CS.CurrentUser.UserName);
free(CS.CurrentPath);
free(FS.FSStart);
}
void main()
{
//初始化系统
if (InitFileSys())
{
while (Login())
{
Shell();
strcpy(CS.CurrentUser.UserName,"man");
CS.CurrentUser.ut = admin;
CS.FileLevel = 0;
CS.CurrParent = NULL;
strcpy(CS.CurrentPath, "/");
printf("您已退出系统。\n");
}
}
else
{
printf("系统初始化失败,按任意键退出。\n");
}
//清理系统内存
ClearFileSys();
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -