1、操作系统实验文件管理C+代码#include #include #include #include #includeusing namespace std;#define BLKSIZE 512 / 数据块的大小 #define BLKNUM 512 / 数据块的块数#define INODESIZE 32 / i节点的大小#define INODENUM 32 / i节点的数目#define FILENUM 8 / 打开文件表的数目/用户 typedef struct char user_name10; / 用户名 char password10; / 密码 User;/i节点 typede
2、f struct short inum; / 文件i节点号 char file_name10; / 文件名 char type; / 文件类型 char user_name10; / 文件所有者 short iparent; / 父目录的i节点号 short length; / 文件长度 short address2; / 存放文件的地址 Inode;/打开文件表 typedef struct short inum; / i节点号 char file_name10; / 文件名 short mode; / 读写模式(1:read, 2:write, / 3:read and write) Fi
3、le_table;/ 申明函数void login(void);void init(void);int analyse(char *);void save_inode(int);int get_blknum(void);void read_blk(int);void write_blk(int);void release_blk(int);void pathset();void del(int);/ 用户命令处理函数 void help(void);void cd(void);void dir(void);void mkdir(void);void creat(void);void open(
4、void);void read(void);void write(void);void close(void);void delet(void);void logout(void);void command(void);void quit();/main.cpp文件/#include head.h/定义全局变量 char choice;int argc; / 用户命令的参数个数char *argv5; / 用户命令的参数int inum_cur; / 当前目录char temp2*BLKSIZE; / 缓冲区User user; / 当前的用户char bitmapBLKNUM; / 位图数组
5、Inode inode_arrayINODENUM; / i节点数组File_table file_arrayFILENUM; / 打开文件表数组char image_name10 = data.dat; / 文件系统名称FILE *fp; / 打开文件指针/创建映像hd,并将所有用户和文件清除void format(void) int i; Inode inode; printf(Will be to format filesystem.n); printf(WARNING:ALL DATA ON THIS FILESYSTEM WILL BE LOST!n); printf(Proceed
6、 with Format(Y/N)?); scanf(%c, &choice); getchar(); if(choice = y) | (choice = Y) if(fp=fopen(image_name, w+b) = NULL) printf(Cant create file %sn, image_name); exit(-1); for(i = 0; i BLKSIZE; i+) fputc(0, fp); inode.inum = 0; strcpy(inode.file_name, /); inode.type = d; strcpy(inode.user_name, /); i
7、node.iparent = 0; inode.length = 0; inode.address0 = -1; inode.address1 = -1; fwrite(&inode, sizeof(Inode), 1, fp); inode.inum = -1; for(i = 0; i 31; i+) fwrite(&inode, sizeof(Inode), 1, fp); for(i = 0; i BLKNUM*BLKSIZE; i+) fputc(0, fp); fclose(fp); / 打开文件user.txt if(fp=fopen(user.txt, w+) = NULL)
8、printf(Cant create file %sn, user.txt); exit(-1); fclose(fp); printf(Filesystem created successful.Please first login!n); return ;/ 功能: 用户登陆,如果是新用户则创建用户void login(void) char *p; int flag; char user_name10; char password10; char file_name10 = user.txt; do printf(login:); gets(user_name); printf(passw
9、ord:); p=password; while(*p=getch() if(*p = 0x0d) *p=0; /将输入的回车键转换成空格 break; printf(*); /将输入的密码以*号显示 p+; flag = 0; if(fp = fopen(file_name, r+) = NULL) printf(nCant open file %s.n, file_name); printf(This filesystem not exist, it will be create!n); format(); login(); while(!feof(fp) fread(&user, siz
10、eof(User), 1, fp); / 已经存在的用户, 且密码正确 if(!strcmp(user.user_name, user_name) & !strcmp(user.password, password) fclose(fp); printf(n); return ; / 已经存在的用户, 但密码错误 else if(!strcmp(user.user_name, user_name) printf(nThis user is exist, but password is incorrect.n); flag = 1; fclose(fp); break; if(flag = 0)
11、 break; while(flag); / 创建新用户 if(flag = 0) printf(nDo you want to creat a new user?(y/n):); scanf(%c, &choice); gets(temp); if(choice = y) | (choice = Y) strcpy(user.user_name, user_name); strcpy(user.password, password); fwrite(&user, sizeof(User), 1, fp); fclose(fp); return ; if(choice = n) | (choi
12、ce = N) login(); / 功能: 将所有i节点读入内存void init(void) int i; if(fp = fopen(image_name, r+b) = NULL) printf(Cant open file %s.n, image_name); exit(-1); / 读入位图 for(i = 0; i BLKNUM; i+) bitmapi = fgetc(fp); / 显示位图 / 读入i节点信息 for(i = 0; i INODENUM; i+) fread(&inode_arrayi, sizeof(Inode), 1, fp); / 显示i节点 / 当前目
13、录为根目录 inum_cur = 0; / 初始化打开文件表 for(i = 0; i FILENUM; i+) file_arrayi.inum = -1;/ 功能: 分析用户命令, 将分析结果填充argc和argv/ 结果: 0-13为系统命令, 14为命令错误int analyse(char *str) int i; char temp20; char *ptr_char; char *syscmd=help, cd, dir, mkdir, create, open, read, write, close, delet, logout, clear,format,quit; argc
14、= 0; for(i = 0, ptr_char = str; *ptr_char != 0; ptr_char+) if(*ptr_char != ) while(*ptr_char != & (*ptr_char != 0) tempi+ = *ptr_char+; argvargc = (char *)malloc(i+1); strncpy(argvargc, temp, i); argvargci = 0; argc+; i = 0; if(*ptr_char = 0) break; if(argc != 0) for(i = 0; (i 14) & strcmp(argv0, sy
15、scmdi); i+); return i; else return 14;/ 功能: 将num号i节点保存到hd.datvoid save_inode(int num) if(fp=fopen(image_name, r+b) = NULL) printf(Cant open file %sn, image_name); exit(-1); fseek(fp, BLKNUM +num*sizeof(Inode), SEEK_SET); fwrite(&inode_arraynum, sizeof(Inode), 1, fp); fclose(fp);/ 功能: 申请一个数据块int get_
16、blknum(void) int i; for(i = 0; i 512) add1 = inode_arraynum.address1; if(fp = fopen(image_name, r+b) = NULL) printf(Cant open file %s.n, image_name); exit(-1); fseek(fp, BLKSIZE+INODESIZE*INODENUM +add0*BLKSIZE, SEEK_SET); ch = fgetc(fp); for(i=0; (i len) & (ch != 0) & (i = 512) fseek(fp,BLKSIZE+INO
17、DESIZE*INODENUM+add1*BLKSIZE, SEEK_SET); ch = fgetc(fp); for(; (i len) & (ch != 0); i+) tempi = ch; ch = fgetc(fp); tempi = 0; fclose(fp);/ 功能: 将temp的内容输入hd的数据区void write_blk(int num) int i, len; int add0, add1; add0 = inode_arraynum.address0; len = inode_arraynum.length; if(fp = fopen(image_name, r
18、+b) = NULL) printf(Cant open file %s.n, image_name); exit(-1); fseek(fp, BLKSIZE+INODESIZE*INODENUM+add0*BLKSIZE, SEEK_SET); for(i=0; (ilen)&(tempi!=0)&(i 512); i+) fputc(tempi, fp); if(i = 512) add1 = inode_arraynum.address1; fseek(fp, BLKSIZE+INODESIZE*INODENUM+add1*BLKSIZE, SEEK_SET); for(; (i le
19、n) & (tempi != 0); i+) fputc(tempi, fp); fputc(0, fp); fclose(fp);/ 功能: 释放文件块号为num的文件占用的空间void release_blk(int num) FILE *fp; if(fp=fopen(image_name, r+b) = NULL) printf(Cant open file %sn, image_name); exit(-1); bitmapnum = 0; fseek(fp, num, SEEK_SET); fputc(0, fp); fclose(fp);/ 功能: 显示帮助命令void help
20、(void) printf(command: n help - show help menu n clear - clear the screen n cd - change directory n mkdir - make directory n create - create a new file n open - open a exist file n read - read a file n write - write something to a file n close - close a file n delet - delete a exist file or director
21、y n format - format a exist filesystem n logout - exit user n quit - exit this systemn);/设置文件路径void pathset() char path50; int m,n; if(inode_arrayinum_cur.inum = 0) strcpy(path,user.user_name); else strcpy(path,user.user_name); m=0; n=inum_cur; while(m != inum_cur) while(inode_arrayn.iparent != m) n
22、 = inode_arrayn.iparent; strcat(path,/); strcat(path,inode_arrayn.file_name); m = n; n = inum_cur; printf(%s$,path);/ 功能: 切换目录(cd . 或者 cd dir1)void cd(void) int i; if(argc != 2) printf(Command cd must have two args. n); return ; if(!strcmp(argv1, .) inum_cur = inode_arrayinum_cur.iparent; else for(i
23、 = 0; i 0)& (inode_arrayi.type=d)& (inode_arrayi.iparent=inum_cur)& !strcmp(inode_arrayi.file_name,argv1)& !strcmp(inode_arrayi.user_name,user.user_name) break; if(i = INODENUM) printf(This directory isnt exsited.n); else inum_cur = i; / 功能: 显示当前目录下的子目录和文件(dir)void dir(void) int i; int dcount=0,fcou
24、nt=0; short bcount=0; if(argc != 1) printf(Command dir must have one args. n); return ; / 遍历i节点数组, 显示当前目录下的子目录和文件名 for(i = 0; i 0) & (inode_arrayi.iparent = inum_cur)& !strcmp(inode_arrayi.user_name,user.user_name) if(inode_arrayi.type = d) dcount+; printf(%-20sn, inode_arrayi.file_name); else fcoun
25、t+; bcount+=inode_arrayi.length; printf(%-20s%12d bytesn, inode_arrayi.file_name,inode_arrayi.length); printf(n %d file(s)%11d bytesn,fcount,bcount); printf( %d dir(s) %11d bytes FreeSpacen,dcount,1024*1024-bcount); / 功能: 在当前目录下创建子目录(mkdir dir1)void mkdir(void) int i; if(argc != 2) printf(command mkdir must have two args. n); return ; / 遍历i节点数组, 查找未用的i节点 for(i = 0; i INODENUM; i+) if(inode_arrayi.inum 0) brea
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2