📄 format.c
字号:
#include <stdio.h>
#include "filesys.h"
format()
{
struct inode *inode;
struct direct dir_buf[BLOCKSIZ/(DIRSIZ+2)];
struct hblock hblock;
char *buf;
int i,j;
/* creat the file system file*/
fd=fopen("filesystem","wb");/*见"谭浩强,C程序设计第二版P313"*/
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,1,(DINODEBLK+FILEBLK+2)*BLOCKSIZ*sizeof(char),fd);
fclose(fd);
fd=fopen("filesystem","r+b");
for (i = 0; i < FILEBLK; i += NICFREE) {
for (j = 1; j < NICFREE && i+j < FILEBLK; ++j) {
hblock.hb_fblock[j-1] = i + j;
hblock.hb_bmap[j-1] = 0;
}
hblock.hb_pblock = 0;
hblock.hb_cur = i*BLOCKSIZ+DATASTART;
hblock.hb_fowd = (i+NICFREE)*BLOCKSIZ+DATASTART;
hblock.hb_back = (i-NICFREE)*BLOCKSIZ+DATASTART;
if ( j == NICFREE )
hblock.hb_size = NICFREE-1;
else
hblock.hb_size = FILEBLK%NICFREE;
fseek(fd, DATASTART+(i*BLOCKSIZ), SEEK_SET);
if (!fwrite(&hblock, 1, sizeof(struct hblock),fd)) printf("ERROR1");
}
/* 2.initialize the superblock */
fseek(fd, DATASTART, SEEK_SET);
if (!fread(&filsys.s_hblock, 1, sizeof(struct hblock), fd)) printf("ERROR2");
filsys.s_isize=DINODEBLK;
filsys.s_fsize=FILEBLK;
filsys.s_nfree=FILEBLK-FILEBLK/NICFREE - (FILEBLK%NICFREE != 0);
filsys.s_ninode=DINODEBLK*BLOCKSIZ/DINODESIZ-4;
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;
/*
for (i = 0; i < NICFREE-1; ++i) {
printf("<%d>", filsys.s_hblock.hb_fblock[i]);
printf("(%d)", filsys.s_hblock.hb_bmap[i]);
}
printf("\n");
// getch();*/
/*... */
strcpy(cur_path[0], "");
dir_lv = 1;
/* 0.initialize the user */
for(i=0;i<USERNUM;i++)
{
user[i].u_uid=0;
user[i].u_gid=0;
strcpy(user[i].u_name, "");
strcpy(user[i].password,"");
for(j=0;j<NOFILE;j++)
{
user[i].u_ofile[j]=SYSOPENFILE+1;
}
}
strcpy(user[0].u_name, "root");
strcpy(user[0].password,"root");
user[0].u_uid=0;
user[0].u_gid=03;
/* 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);
//mkdir("/");
inode=iget(1); /* l main dir id */
inode->di_number=1;
inode->di_mode=DEFAULTMODE | DIDIR;
inode->di_size=3*(DIRSIZ+2);
inode->di_addr[0]=balloc(); /* 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+inode->di_addr[0]*BLOCKSIZ,SEEK_SET);
fwrite(dir_buf,1,3*(DIRSIZ+2),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]=balloc(); /* block 1# is used by the etc directory*/
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,"userinfo");
dir_buf[2].d_ino=3;
fseek(fd,DATASTART+inode->di_addr[0]*BLOCKSIZ,SEEK_SET);
if (!fwrite(dir_buf,1,3*(DIRSIZ+2),fd)) printf("AAAAAA");
iput(inode);
inode=iget(3); /* 3 user id */
inode->di_number=1;
inode->di_mode=DEFAULTMODE | DIFILE;
inode->di_size=BLOCKSIZ;
inode->di_addr[0]=balloc(); /* block 2# is used by the user file */
fseek(fd,DATASTART+inode->di_addr[0]*BLOCKSIZ,SEEK_SET);
if (!fwrite(user,1,BLOCKSIZ,fd)) printf("&&&&&&&");
iput(inode);
/* 2.initialize the superblock */
fseek(fd,0,SEEK_SET);
fwrite(&filsys.s_isize,1,sizeof(struct filsys),fd);
fseek(fd,0,SEEK_SET);
fread(&filsys.s_isize,1,sizeof(struct filsys),fd);
fclose(fd);
printf("\niget(3)---->out\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -