filesys.cc

来自「操作系统课程设计。在UNIX平台下实现Solary操作系统的一些功能」· CC 代码 · 共 495 行

CC
495
字号
/////////////////////////////////////////////////////////////
//FileName		: filesys.cc
//
//Creator		: Fang Wenbin(0410706)
//CreateTime	: 2006-12-26
//
//File Desc:
//	1.s_SuperBlock: super block for this file system
//	2.s_FileSystem: high level operations of files.
/////////////////////////////////////////////////////////////
#include "copyright.h"#include "disk.h"#include "bitmap.h"#include "directory.h"#include "filehdr.h"#include "filesys.h"#include "synchdisk.h"#include "system.h"#define FreeMapSector 		0#define DirectorySector 	1#define FreeMapFileSize 	(NumSectors / BitsInByte)#define NumDirEntries 		10#define DirectoryFileSize 	(sizeof(DirectoryEntry) * NumDirEntries)#define s_LIMIT_OF_FILE 2097152			//the total bytes limit of a file#define s_NUM_OF_SUPER_BLOCK 1			//the number of super block#define s_SIZE_OF_INODE 32				//a inode size#define s_SIZE_OF_DIR_ENTRY 16			//a dir entry sizes_SuperBlock::s_SuperBlock(){}s_SuperBlock::~s_SuperBlock(){}
//////////////////////////////////////////////////////
// Function name	: s_SuperBlock::Init
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 20:32:31
//
// Function Desc    : init a the super block
// 	The steps of s_SuperBlock::Init
//	1. use the macro constants to init
//
//	Fails if:
//	
// Note				:
// Return type		: void 
//////////////////////////////////////////////////////
void s_SuperBlock::Init(){	limitOfFile = s_LIMIT_OF_FILE;	numOfZones = NumSectors;	numOfInodes = NumSectors/4;	numOfInodeBlocks = numOfInodes*s_SIZE_OF_INODE/128;	sizeOfInode = s_SIZE_OF_INODE;	sizeOfDirEntry = s_SIZE_OF_DIR_ENTRY;	numOfImapBlocks = numOfInodes/128/8;	if (numOfInodes/128%8!=0)		numOfImapBlocks++;	numOfZmapBlocks = numOfZones/128/8;	if (numOfZones/128%8!=0)		numOfZmapBlocks++;	firstDataBlock = s_NUM_OF_SUPER_BLOCK + 						  numOfImapBlocks + 						  numOfZmapBlocks + 						  numOfInodeBlocks;	firstImapBlock = s_NUM_OF_SUPER_BLOCK;	firstZmapBlock = s_NUM_OF_SUPER_BLOCK + 						  numOfImapBlocks;	firstInodeBlock = s_NUM_OF_SUPER_BLOCK + 						   numOfImapBlocks + 						   numOfZmapBlocks;	numOfDataBlocks = numOfZones - firstDataBlock;}
//////////////////////////////////////////////////////
// Function name	: FileSystem::FileSystem
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 20:38:02
//
// Function Desc    : constructor of FileSystem
// 	The steps of FileSystem::FileSystem
//	1. if argument format equals true, then format the disk
//		1.1 init the super block
//		1.2 setup inode bitmap
//		1.3 setup zones bitmap
//		1.4 create root directory
//	2. if format equals false, then just read super block from disk
//	
//	Fails if:
//	
// Note				:
// Return type		: 
// Argument         : bool format -- true denotes do format
//////////////////////////////////////////////////////
FileSystem::FileSystem(bool format){     DEBUG('f', "Initializing the file system.\n");	s_superBlock = new s_SuperBlock;    if (format) 	{    	DEBUG('f', "Formatting the file system.\n");		s_superBlock->Init();		synchDisk->WriteSector(0, (char*)s_superBlock);		s_Bitmap *iMap = new s_Bitmap(4096);		s_Bitmap *zMap = new s_Bitmap(16384);		for (int i = 0; i < 1045; i++) 			zMap->Mark(i);		iMap->WriteBack(s_superBlock->firstImapBlock, 						4);		zMap->WriteBack(s_superBlock->firstZmapBlock, 						16);		Directory::s_CreateRoot(s_superBlock);	}//end if (format)	else	{		synchDisk->ReadSector(0, (char*)s_superBlock);	}// end if (format) else}FileSystem::~FileSystem(){	delete s_superBlock;}
//////////////////////////////////////////////////////
// Function name	: FileSystem::s_List
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 20:45:03
//
// Function Desc    : List the names of files in dir
// 	The steps of FileSystem::s_List
//	1. call Directory::s_List
//
//	Fails if:
//	
//
// Note				:
// Return type		: void 
// Argument         : char *dirName
//////////////////////////////////////////////////////
void FileSystem::s_List(char *dirName){	Directory *dir = new Directory(dirName);	dir->s_List();	delete dir;}
//////////////////////////////////////////////////////
// Function name	: FileSystem::s_WriteFile
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 20:45:53
//
// Function Desc    : 
// 	The steps of FileSystem::s_WriteFile
//	1. call s_RemoveFile to remove original file
//	2. call s_CreateFile to create a file with the same
//		name as the former deleted file
//	3. call Directory->s_WriteFile to write data into file
//
//	Fails if:
//	1. file type is dir
//
// Note				:
// Return type		: void 
// Argument         : char *filePath
// Argument         : int size	-- the size you will write in
// Argument         : char *data
//////////////////////////////////////////////////////
void FileSystem::s_WriteFile(char *filePath, int size, char *data){	if (s_GetFileType(filePath)&s_DIR_TYPE)	{		printf("%s is a dir, can't be written!\n", filePath);		return;	}//end if (s_GetFileType...)	if (size > s_LIMIT_OF_FILE)	{		printf("Over the file size limit %d bytes!!\n", s_LIMIT_OF_FILE);	}//enf if (size > ...)	s_RemoveFile(filePath);	s_CreateFile(filePath);	Directory *dir = new Directory(filePath);		dir->s_WriteFile(size, data);		delete dir;}
//////////////////////////////////////////////////////
// Function name	: FileSystem::s_ReadFile
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 20:49:52
//
// Function Desc    : read data from file
// 	The steps of FileSystem::s_ReadFile
//	1. call Directory::s_ReadFile to read data
//	
//	Fails if:
//	
// Note				:
// Return type		: void 
// Argument         : char *filePath
// Argument         : int size
// Argument         : char *data -- out param
//////////////////////////////////////////////////////
void FileSystem::s_ReadFile(char *filePath, int size, char *data){	Directory *dir = new Directory(filePath);	dir->s_ReadFile(size, data);	delete dir;}
//////////////////////////////////////////////////////
// Function name	: FileSystem::s_GetFileLength
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 20:50:47
//
// Function Desc    : get the file size in bytes
// 	The steps of FileSystem::s_GetFileLength
//	1. get Directory::dirHdr->numbytes' value
//
//	Fails if:
//	
// Note				:
// Return type		: int -- file size
// Argument         : char *filePath
//////////////////////////////////////////////////////
int FileSystem::s_GetFileLength(char *filePath){	Directory *dir = new Directory(filePath);	int len = dir->dirHdr->numBytes;	delete dir;	return len;}

//////////////////////////////////////////////////////
// Function name	: FileSystem::s_CreateFile
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 20:54:50
//
// Function Desc    : create a plain file
// 	The steps of FileSystem::s_CreateFile
//	1. call Directory::s_CreateFile to create file
//	
//	Fails if:
//	
// Note				:
// Return type		: void 
// Argument         : char *filePath
//////////////////////////////////////////////////////
void FileSystem::s_CreateFile(char *filePath){	char parent[64];	char file[s_FILE_NAME_MAX_LEN];	Directory::s_SplitParentAndFile(filePath, parent, file);	Directory *dir = new Directory(parent);	dir->s_CreateFile(file);	delete dir;}
//////////////////////////////////////////////////////
// Function name	: FileSystem::s_CreateDir
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 20:55:33
//
// Function Desc    : create a dir
// 	The steps of FileSystem::s_CreateDir
//	1. call Directory::s_SplitParentAndFile to split
//		the filePath into 2 parts--parent dir name and
//		new dir name.
//	2. call Directory::s_CreateDir to create dir
//
//	Fails if:
//	
// Note				:
// Return type		: void 
// Argument         : char *filePath
//////////////////////////////////////////////////////
void FileSystem::s_CreateDir(char *filePath){	char parent[64];	char file[s_FILE_NAME_MAX_LEN];	Directory::s_SplitParentAndFile(filePath, parent, file);	Directory *dir = new Directory(parent);	dir->s_CreateDir(file);	delete dir;}
//////////////////////////////////////////////////////
// Function name	: FileSystem::s_CopyFile
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 20:57:48
//
// Function Desc    : copy file
// 	The steps of FileSystem::s_CopyFile
//	1. create a new file.
//	2. read data from the source file
//	3. write data into the new file
//
//	Fails if:
//
// Note				:
// Return type		: void 
// Argument         : char *src	-- source file name
// Argument         : char *des -- target file name
//////////////////////////////////////////////////////
void FileSystem::s_CopyFile(char *src, char *des){	Directory *dirSrc = new Directory(src);	int size = dirSrc->dirHdr->numBytes;	char parent[64];	char file[s_FILE_NAME_MAX_LEN];	Directory::s_SplitParentAndFile(des, parent, file);	Directory *dirDes = new Directory(parent);	dirDes->s_CreateFile(file);	if (size != 0) 	{		char *data = NULL;		data = new char[size];		dirSrc->s_ReadFile(size, data);		s_WriteFile(des, size, data);		delete data;	}// end if (size != 0)	delete dirSrc;	delete dirDes;}
//////////////////////////////////////////////////////
// Function name	: FileSystem::s_RenameFile
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 20:59:42
//
// Function Desc    : rename file
// 	The steps of FileSystem::s_RenameFile
//	1. get the file's dir entry
//	2. change the dir entry's s_name field
//	
//	Fails if:
//	1. can't find the file
//	
// Note				:
// Return type		: void 
// Argument         : char *src
// Argument         : char *des
//////////////////////////////////////////////////////
void FileSystem::s_RenameFile(char *src, char *des){	char parent[64];	char file[s_FILE_NAME_MAX_LEN];		Directory::s_SplitParentAndFile(src,parent, file);	Directory *dir = new Directory(parent);	
///
/// can't find the file
///	if (dir->s_FindIndex(dir->dirHdr, des)!=-1)	{		delete dir;		return;	}//end if (dir->...)	s_DirEntry *entry = new s_DirEntry;	int slot, offset;	entry = dir->s_FindDirEntry(file, &slot, &offset);	strcpy(entry->s_name, des);	synchDisk->s_WriteDataInSector(slot, offset, 16, (char*)entry);	delete entry;}
//////////////////////////////////////////////////////
// Function name	: FileSystem::s_RemoveFile
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 21:07:37
//
// Function Desc    : remove file from disk
// 	The steps of FileSystem::s_RemoveFile
//	1. deallocate the blocks the file covers
//	2. delete the dir entry from its parent dir
//	3. move the last dir entry of parent dir to where the 
//		removed file's coverd.
//	4. clear the removed file's inode bit in iMap
//
//	Fails if:
//	
// Note				:
// Return type		: void 
// Argument         : char *filePath
//////////////////////////////////////////////////////
void FileSystem::s_RemoveFile(char *filePath){	Directory *dirFile = new Directory(filePath);
///
/// deallocate blocks the file covers
///	s_Bitmap *zMap = new s_Bitmap(16384);	zMap->FetchFrom(s_superBlock->firstZmapBlock, 16);	dirFile->dirHdr->s_DeAllocate(zMap);	char parent[64];	char file[s_FILE_NAME_MAX_LEN];	Directory::s_SplitParentAndFile(filePath, parent, file);	Directory *dir = new Directory(parent);		FileHeader *bakInode = new FileHeader;	memcpy(bakInode, dir->dirHdr, 32);
///
/// clear the bit of imap
///	int slot, offset;	s_DirEntry *fileEntry = dir->s_FindDirEntry(file, &slot, &offset);		s_Bitmap *iMap = new s_Bitmap(4096);	iMap->FetchFrom(s_superBlock->firstImapBlock, 4);	iMap->Clear(fileEntry->s_inodeIndex);
///
/// write the last dir entry to where the removed file's dir entry
///	covered.
///	s_DirEntry *lastEntry = dir->s_GetLastEntry();	synchDisk->s_WriteDataInSector(slot, offset, 16, (char*)lastEntry);	bakInode->numBytes -= 16;	if (bakInode->numBytes % 128 == 0)		zMap->Clear(bakInode->s_dataSectors[bakInode->numBytes/128]);	bakInode->WriteBack(s_superBlock->firstInodeBlock+dir->dirHdrIndex/4, dir->dirHdrIndex%4);	iMap->WriteBack(s_superBlock->firstImapBlock, 4);	zMap->WriteBack(s_superBlock->firstZmapBlock, 16);	delete zMap;	delete bakInode;	delete iMap;		delete dir;	delete lastEntry;	delete fileEntry;	delete dirFile;	}
//////////////////////////////////////////////////////
// Function name	: FileSystem::s_GetFileType
// Creator			: Fang Wenbin(0410706)
// CreateTime		: 2007-1-9 21:15:27
//
// Function Desc    : get the file type(dir or plain file)
// 	The steps of FileSystem::s_GetFileType
//	1. get Directory::dirHdr->s_fileMode
//
//	Fails if:
//	
// Note				:
// Return type		: int -- the value of Diretory::dirHdr->s_fileMode
// Argument         : char *filePath
//////////////////////////////////////////////////////
int FileSystem::s_GetFileType(char *filePath){	int imode = 0;	Directory *dir = new Directory(filePath);	imode = dir->dirHdr->s_fileMode;	delete dir;	return imode;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?