📄 format.c
字号:
/*初始化磁盘格式化程序format.c*/
#include <stdio.h>
#include "filesys.h"
#include <stdlib.h>
#include <string.h>
format()
{
struct inode *inode;
struct direct dir_buf [BLOCKSIZ/(DIRSIZ+2)];
struct pwd passwd[BLOCKSIZ/(PWDSIZ+4)];
struct filsys filsys;
unsigned int block_buf[BLOCKSIZ/sizeof(int)];
char *buf;
int i, j;
/*creat the file system file */
fd = fopen("filesystem", "w+b");//我修改了"r+w+b"为"w+b"
buf = (char *)malloc((DINODEBLK+FILEBLK+2)*BLOCKSIZ*sizeof(char));
if(buf == NULL)
{
printf ("\nfile system file creat failed! \n");
exit(0);
}
fseek(fd, 0, SEEK_SET);
fwrite(buf, (DINODEBLK+FILEBLK+2)*BLOCKSIZ*sizeof(char), 1, fd);
/* 0.initialize the passwd */
pwd[0].p_uid = 2116; pwd[0].p_gid = 03;
strcpy(pwd[0].password, "dddd");
pwd[1].p_uid = 2117; pwd[1].p_gid = 03;
strcpy(pwd[1].password, "bbbb");
pwd[2].p_uid = 2118; pwd[2].p_gid = 04;
strcpy(pwd[2].password, "abcd");
pwd[3].p_uid = 2119; pwd[3].p_gid = 04;
strcpy(pwd[3].password, "cccc");
pwd[4].p_uid = 2220; pwd[4].p_gid = 05;
strcpy(pwd[4].password, "eeee");
passwd[0].p_uid=2116;passwd[0].p_gid=03;
strcpy(passwd[0].password,"dddd");
passwd[1].p_uid=2117;passwd[1].p_gid=03;
strcpy(passwd[1].password,"bbbb");
passwd[2].p_uid=2118;passwd[2].p_gid=04;
strcpy(passwd[2].password,"abcd");
passwd[3].p_uid=2119;passwd[3].p_gid=04;
strcpy(passwd[3].password,"cccc");
passwd[4].p_uid=2220;passwd[4].p_gid=05;
strcpy(passwd[4].password,"eeee");
/* 1.creat the main directory and its sub dir etc and the file password */
inode = iget(0); /* 0 empty dinode id */
inode->di_mode = DIEMPTY;
iput(inode);
inode = iget(1); /* 1 main dir id */
inode->di_number = 1;
inode->di_mode = DEFAULTMODE|DIDIR;
inode->di_size = 3*(DIRSIZ+2);
inode->di_addr[0] = 0; /* block 0# is used by the main directory */
strcpy(dir_buf[0].d_name, "..");
dir_buf[0].d_ino = 1;
strcpy(dir_buf[1].d_name,".");
dir_buf[1].d_ino = 1;
strcpy(dir_buf[2].d_name, "etc");
dir_buf[2].d_ino = 2;
fseek(fd, DATASTART, SEEK_SET);
fwrite(dir_buf, 3*(DIRSIZ+2), 1, fd);
iput(inode);
inode=iget(2);/* 2 etc dir id */
inode ->di_number =1;
inode->di_mode=DEFAULTMODE|DIDIR;
inode->di_size=3 * (DIRSIZ+2);
inode->di_addr[0] = 1; /* block 1# is used by the etc */
strcpy (dir_buf[0].d_name, "..");
dir_buf[0].d_ino=1;
strcpy(dir_buf[1].d_name, ".");//我修改了".."为"."
dir_buf[1].d_ino=2;
strcpy(dir_buf[2].d_name, "password");
dir_buf[2].d_ino=3;
fseek(fd, DATASTART+BLOCKSIZ*1, SEEK_SET);
fwrite (dir_buf, 3* (DIRSIZ+2), 1, fd);
iput(inode);
inode=iget(3); /* 3 password id */
inode->di_number= 1;
inode->di_mode=DEFAULTMODE|DIFILE;
inode->di_size=BLOCKSIZ;
inode->di_addr[0]=2; /* block 2# is used by the password */
for (i=5; i<PWDNUM; i++)
{ passwd[i].p_uid=0;
passwd[i].p_gid=0;
strcpy(passwd[i].password," ");
}
fseek(fd, DATASTART+2*BLOCKSIZ, SEEK_SET);
fwrite(passwd, BLOCKSIZ, 1, fd);
iput(inode);
/* 2. initialize the superblock */
filsys.s_isize = DINODEBLK;
filsys.s_fsize = FILEBLK;
filsys.s_ninode = DINODEBLK*BLOCKSIZ/DINODESIZ-4;
filsys.s_nfree = FILEBLK-3;
for(i=0; i<NICINOD; i++)
{
/*begin with 4, 0,1,2,3, is used by main, etc, password */
filsys.s_inode[i] = 4+i;
}
filsys.s_pinode = 0;
filsys.s_rinode = NICINOD+4;
/*修改后的成组链法超级块中是3-52号块,栈顶是3*/
for(i=0; i<NICINOD; i++)
{
filsys.s_free[NICINOD-1-i] = i+3;
}
for(j=i+3; BLOCKSIZ-j>=NICFREE; j+=NICFREE)
{
for(i=0; i<NICFREE; i++)
{
block_buf[NICFREE-1-i] = j+i;
}
block_buf[NICFREE] = NICFREE;
fseek(fd, DATASTART+BLOCKSIZ*(j-2), SEEK_SET);
fwrite(block_buf, BLOCKSIZ, 1, fd);
}
i = BLOCKSIZ-j;
for(j=j; j<BLOCKSIZ; j++)
{
block_buf[BLOCKSIZ-1-j] = j;
}
block_buf[NICFREE] = i;
fseek(fd, DATASTART+BLOCKSIZ*(FILEBLK-i-3), SEEK_SET);
fwrite(block_buf, BLOCKSIZ, 1, fd);
filsys.s_pfree = NICFREE-1;
fseek(fd, BLOCKSIZ, SEEK_SET);
fwrite(&filsys, sizeof(struct filsys), 1, fd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -