📄 lab7-0572201-myfs.c
字号:
int nblock = createblock(pinode); if (nblock < 0) return 0; writeblock(nblock, buff); return 1;}void closefile(struct inode *pinode) { int index = exists(pinode->name); if (index < 0) return; writeinode(index, pinode); free(pinode);}void copyblocks(int root, int h, int count, FILE *fout) { int b = sb.s_block_size / 4; int i; char *buff = (char*)malloc(sb.s_block_size); readblock(root, buff); if (h == 0) { fwrite(buff, 1, count, fout); } else { int powerb = power(b, h - 1); int bound = count / sb.s_block_size / powerb; for (i = 0; i < bound; i++) { copyblocks(((int*)buff)[i], h - 1, powerb * sb.s_block_size, fout); } copyblocks(((int*)buff)[bound], h - 1, count - powerb * sb.s_block_size * bound, fout); } free(buff);}void removeblocks(int root, int h, int blockcount) { int b = sb.s_block_size / 4; int *intbuff; int i = 0; int powerb; if (h == 0) { releaseblock(root); return; } intbuff = (int*)malloc(sb.s_block_size); readblock(root, (char*)intbuff); powerb = power(b, h - 1); while (blockcount >= powerb) { removeblocks(intbuff[i], h - 1, powerb); blockcount -= powerb; i++; } removeblocks(intbuff[i], h - 1, blockcount); free(intbuff); releaseblock(root);}int mycpout(char* srcfilename, char* destfilename){ FILE *fout; struct inode in; int filenum; int blocks; int i; int b; char *buff = (char*)malloc(sb.s_block_size); int remainder; int index = 12; int h = 0; int powerb; b = sb.s_block_size / 4; powerb = b; filenum = exists(srcfilename); if (filenum < 0) { return 0; } readinode(filenum, &in); if (strcmp(destfilename, "stdout") == 0) { fout = stdout; } else { fout = fopen(destfilename, "wb"); } if (fout == 0) return 0; blocks = in.i_blocks; remainder = in.i_size; if (blocks <= 12) { for (i = 0; i < blocks - 1; i++) { copyblocks(in.i_block[i], 0, sb.s_block_size, fout); } copyblocks(in.i_block[i], 0, in.i_size % sb.s_block_size, fout); return 1; } blocks -= 12; for (i = 0; i < 12; i++) { copyblocks(in.i_block[i], 0, sb.s_block_size, fout); } remainder -= 12 * sb.s_block_size; h = 1; powerb = b; while (blocks >= powerb) { copyblocks(in.i_block[index], h, powerb * sb.s_block_size, fout); index++; h++; blocks -= powerb; remainder -= powerb * sb.s_block_size; powerb *= b; } copyblocks(in.i_block[index], h, remainder, fout); free(buff); if (fout != stdout) fclose(fout); return 1;}int myrm(char* filename){ int iinode = exists(filename); struct inode in; int i; int blocks; int b = sb.s_block_size / 4; int powerb = b; int index = 12; int h = 1; if (iinode < 0) return 0; readinode(iinode, &in); blocks = in.i_blocks; if (blocks <= 12) { for (i = 0; i < blocks; i++) releaseblock(in.i_block[i]); releaseinode(iinode); return 1; } for (i = 0; i < 12; i++) releaseblock(in.i_block[i]); blocks -= 12; h = 1; while (blocks >= powerb) { removeblocks(in.i_block[index++], h++, powerb); blocks -= powerb; powerb *= b; } removeblocks(in.i_block[index], h, blocks); releaseinode(iinode); return 1;}int myls(void){ int ibyte; int ibit; int iinode; int cur; struct inode in; char ch; fseek(disk, gd.bg_inode_bitmap * sb.s_block_size, 0); printf("filename\t\tfilesize\n"); for (ibyte = 0; ibyte < sb.s_block_size; ibyte++) { ch = fgetc(disk); for (ibit = 0; ibit < 8; ibit++) { if ( (ch >> ibit) & 1) { iinode = ibyte * 8 + ibit; cur = ftell(disk); fseek(disk, gd.bg_inode_table * sb.s_block_size + iinode * sb.s_inode_size, 0); fread(&in, sb.s_inode_size, 1, disk); printf("%s\t\t\t%-8dB\n", in.name, in.i_size); fseek(disk, cur, 0); } } } return 1;}int mytouch(char* filename){ struct inode *pinode = createfile(filename); int length = 0; int count = 0; char ch; char *buff = (char*)malloc(sb.s_block_size); if (pinode == 0) return 0; printf("Input file content below. enter '$' to end input.\n"); while ((ch = getchar()) != '$') { buff[count] = ch; count++; if (count>=sb.s_block_size) { appendfile(pinode, buff); length += count; count=0; } } if (count > 0) appendfile(pinode, buff); length += count; pinode->i_size = length; closefile(pinode); return length;}int mycpin(char* srcfilename, char* destfilename){ FILE *fin = fopen(srcfilename, "rb"); int readcount; int length = 0; int error = 0; char *buff = (char*)malloc(sb.s_block_size); struct inode *fout = createfile(destfilename); if (fin == 0) return 0; if (fout == 0) return 0; do { readcount = fread(buff, 1, sb.s_block_size, fin); if (!appendfile(fout, buff)) { error = 1; break; } if (readcount < sb.s_block_size) break; length += readcount; } while (1); if (!error) length += readcount; fout->i_size = length; closefile(fout); return 1;}int mydisplay(char* filename){ /*--open file to display in text mode--*/ return mycpout(filename, "stdout");}/*----------------------------------------------------------The following code is demo,do NOT change!----------------------------------------------------------------*/int test_initfs(void);int test_cpin(void);int test_cpout(void);int test_rm(void);int test_ls(void);int test_touch(void);int test_display(void);int get_filename(char*);int main(void){ int ch = 0; int result = 1; if (!mount_disk()) { perror("disk file not associated"); return -1; } while (ch!= 'q') { printf("\n\t-----------------MYFS FILE SYSTEM--------------- \n"); printf("\t\t 0 --- Format Disk\n"); printf("\t\t 1 --- Copy file from Linux to MYFS\n"); printf("\t\t 2 --- Copy file from MYFS to Linux\n"); printf("\t\t 3 --- Remove a file\n"); printf("\t\t 4 --- List all files in MYFS\n"); printf("\t\t 5 --- Create a new file\n"); printf("\t\t 6 --- Display file content\n"); printf("\t\t q --- Quit\n"); printf("\t------------------------------------------------\nEnter Your Choice:"); scanf(" %c", &ch); switch (ch) { case '0': result= test_initfs(); break; case '1': getchar(); result = test_cpin(); break; case '2': getchar(); result = test_cpout(); break; case '3': getchar(); result = test_rm(); break; case '4': result= test_ls(); break; case '5': getchar(); result = test_touch(); break; case '6': getchar(); result = test_display(); break; case '\n': continue; break; } if (!result) printf("\nError!!!!\n"); } unmount_disk(); return 0;}int test_initfs(void){ unsigned int bsize, icount; printf("Disk sector size=%d; total space=%d Bytes\n", SECTOR_SIZE, (SECTOR_SIZE*TOTAL_SECTOR)); printf("Please input block size: (preferred %d, %d, %d)\n",SECTOR_SIZE,(SECTOR_SIZE*2),(SECTOR_SIZE*4)); scanf("%d",&bsize); printf("Please input maximal inode count (e.g. 1000) \n"); scanf("%d",&icount); return myinitfs(bsize,icount);}int test_cpin(void){ char srcfile[MYFS_NAME_LEN]; char destfile[MYFS_NAME_LEN]; printf("Source File in Linux, "); get_filename(srcfile); printf("Destination File in MYFS, "); get_filename(destfile); return mycpin(srcfile,destfile);}int test_cpout(void){ char srcfile[MYFS_NAME_LEN]; char destfile[MYFS_NAME_LEN]; printf("Source File in MYFS, "); get_filename(srcfile); printf("Destination File in Linux, "); get_filename(destfile); return mycpout(srcfile,destfile);}int test_rm(void){ char srcfile[MYFS_NAME_LEN]; printf("Source File in MYFS, "); get_filename(srcfile); return myrm(srcfile);}int test_ls(void){ return myls();}int test_touch(void){ char fn[MYFS_NAME_LEN]; printf("Create new file, "); if(get_filename(fn)) { return mytouch(fn); } else { return 0; }}int test_display(void){ char fn[MYFS_NAME_LEN]; printf("display a file, "); if(get_filename(fn)) { return mydisplay(fn); } else { return 0; }}int get_filename(char* filename){ int len; printf("Enter filename:"); fgets(filename, MYFS_NAME_LEN, stdin); len = strlen(filename); if (filename[len-1] == '\n') { filename[len-1] = '\0'; len--; } return len;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -