#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
//要正常运行应该把所有出现路径的地方修改为自己的当前文件目录
#define SHOWSIZE 15
#define BLOCKSIZE 1024
#define SIZE 1024000
#define END 65535 //2^16 -> FAT16
#define FREE 0
#define ROOTBLOCKNUM 2
#define MAXOPENFILE 10
#define DISKFSADD "/home/oz/FileSys"
#define MAXFCBNUM (int)1024/sizeof(fcb)
#define CURDIR_DES (DE*)((((DE*)(openfilelist[curdir].DE_open+basis))->file_fcb).first * BLOCKSIZE+my_v_disk)
typedef struct FCB{
char filename[20];
char exname[3];
unsigned char attribute; //0 dir 1 data
unsigned short time;
unsigned short date;
unsigned short first; //starting disk-block
unsigned long length;
char free; //0 empty 1 allocated
}fcb;
typedef struct DE{
struct DE* p;
int valid;
fcb file_fcb;
char dir[80];
unsigned short last_modify_time;
long next; //point to sibling directories
long child;//point to child directories
long parent;
}DE;
unsigned char* basis;
typedef struct FAT{ // [present-block] : next-block
unsigned short id[SIZE/BLOCKSIZE];
}fat;
fcb file_fcb;
typedef struct USEROPEN{
fcb open_file_fcb;
long DE_open;
char dir[80]; //file address
int count; //
char fcbstate; // fcb changed? if so, write back to update
char topenfile; //notify if this item has been taken
}useropen;
typedef struct BLOCK0{
char information[200];
unsigned short root;
unsigned char* startblock;
}block0;
DE CurDirDEarray[SHOWSIZE];
char currentdir[80];
char dir_new[80];
unsigned char *data_start_p;
unsigned char *my_v_disk;
useropen openfilelist[MAXOPENFILE];
int curdir = -1;
//from textbook
unsigned char *guideBlock, *dataBlock;
DE* rootBlock; // *** All directories are DE pointer
int *fat1, *fat2;
void fcbcpy(fcb* dst, fcb* src){
strcpy(dst->filename, src->filename);
strcpy(dst->exname, src->exname);
dst->attribute = src->attribute;
dst->time = src->time;
dst->date = src->date;
dst->first = src->first;
dst->length = src->length;
dst->free = src->free;
}
DE rootDE = {.file_fcb = { .attribute = 0, .first = 5, .length = 0, .free = 1}, .valid = 1, };
void showCURDIR_DES(DE* CurDirDEarray){
for (int i =0; i<SHOWSIZE; i++) {
CurDirDEarray[i] = *(CURDIR_DES+i);
CurDirDEarray[i].p = CURDIR_DES+i;
}
}
//全局变量一级连接局部变量可以保存,二级链接的会被回收
void my_format(void);
//
void startsys(void){
my_v_disk = (unsigned char*)malloc(sizeof(char) * SIZE);FILE* fs_on_disk; //saved_fs
basis = my_v_disk;
guideBlock = my_v_disk;
fat1 = (int*)my_v_disk + sizeof(char)*BLOCKSIZE*1;
fat2 = (int*)my_v_disk + sizeof(char)*BLOCKSIZE*3;
rootBlock = (DE*)(my_v_disk + sizeof(char)*BLOCKSIZE*5);
dataBlock = (unsigned char*)(my_v_disk + sizeof(char)*BLOCKSIZE*7);
rootDE.next = (unsigned char*)&rootDE-basis;
if( (fs_on_disk = fopen("/home/oz/FileSys/myfilesys", "rb")) == NULL){
printf("FS has not been created, creating now....\n");
my_format();
}
else{
if(fread(my_v_disk,sizeof(char), SIZE, fs_on_disk) == 0){
printf("Read FS_on_disk error!\n");
return;
}
}
//printf("DESIZE:%d\n", sizeof(DE));
memset(openfilelist , 0 ,sizeof(useropen)*MAXOPENFILE);
strcpy( openfilelist[0].dir, "");
//every time the v-disk starts, fs should make these basic pointers point to right place.
openfilelist[0].count = 0;openfilelist[0].fcbstate = 0;openfilelist[0].topenfile = 1;
openfilelist[0].DE_open = ( guideBlock + sizeof(block0) - basis );
curdir = 0;
guideBlock = my_v_disk;
showCURDIR_DES(CurDirDEarray);
printf("FS Intials successfully!\n");
}
void my_format(void){
strcpy( rootDE.file_fcb.filename, "");strcpy( rootDE.file_fcb.exname, ""); strcpy( rootDE.dir, "");
rootDE.child = (unsigned char*)rootBlock-basis;
//set all-0
memset(my_v_disk, 0, sizeof(char)*SIZE);
//initial block0
block0 block_guide = {.information = "disk_size:1024\ndisk_num:1000\n", .root = 5, .startblock = my_v_disk + sizeof(char)*BLOCKSIZE*7};
*(block0*)guideBlock = block_guide;
DE* rootDE_pointer = (DE*)(guideBlock+sizeof(block0));
*rootDE_pointer = rootDE;
//initial fat/root/data address
fat1[0] = -1; fat1[1] = 2; fat1[2] = -1; fat1[3] = 2; fat1[4] = -1; fat1[5] = 6; fat1[6] = -1;
fat2[0] = -1; fat2[1] = 2; fat2[2] = -1; fat2[3] = 2; fat2[4] = -1; fat2[5] = 6; fat2[6] = -1;
DE dotDE = {.file_fcb = {.attribute = 0,
杭电操作系统实验5-简单文件系统的实现
于 2024-01-01 02:52:29 首次发布