filehdr.cc
来自「操作系统课程设计。在UNIX平台下实现Solary操作系统的一些功能」· CC 代码 · 共 792 行 · 第 1/2 页
CC
792 行
///////////////////////////////////////////////////////////////FileName :filehdr.cc////Creator :Fang Wenbin(0410706)//CreateTime :////File Desc:// 1.s_Bitmap: bitmap operations// 2.FileHeader: inode!/////////////////////////////////////////////////////////////#include "copyright.h"#include "system.h"#include "filehdr.h"//////////////////////////////////////////////////////// Function name : s_Bitmap::s_Bitmap// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:43:21//// Function Desc : constructor of _Bitmap// The steps of s_Bitmap::s_Bitmap// allocate space to map// // Fails if:// // Note :// Return type : // Argument : int nitems//////////////////////////////////////////////////////s_Bitmap::s_Bitmap(int nitems){ numBits = nitems; numBytes = divRoundUp(numBits, 8); map = new char[numBytes]; for (int i = 0; i < numBits; i++) Clear(i);}s_Bitmap::~s_Bitmap(){ delete map;}//////////////////////////////////////////////////////// Function name : s_Bitmap::Mark// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:43:31//// Function Desc : mark the bit with 1// The steps of s_Bitmap::Mark//// Fails if://// Note :// Return type : void // Argument : int which//////////////////////////////////////////////////////void s_Bitmap::Mark(int which){ map[which / 8] |= 1 << (which % 8);}//////////////////////////////////////////////////////// Function name : s_Bitmap::Clear// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:43:36//// Function Desc : clear the bit with 0// The steps of s_Bitmap::Clear//// Fails if:// 1.//// Note :// Return type : void // Argument : int which//////////////////////////////////////////////////////void s_Bitmap::Clear(int which){ map[which/8] &= ~(1 << (which % 8));}//////////////////////////////////////////////////////// Function name : s_Bitmap::Test// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:43:41//// Function Desc : test if the bit is 1// The steps of s_Bitmap::Test// 1.// Fails if://// Note :// Return type : bool -- true denotes the bit is set.// Argument : int which//////////////////////////////////////////////////////bool s_Bitmap::Test(int which){ if (map[which / 8] & (1 << (which % 8))) return true; else return false;}//////////////////////////////////////////////////////// Function name : s_Bitmap::Find// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:43:46//// Function Desc : find the first 0 bit// The steps of s_Bitmap::Find//// Fails if://// Note :// Return type : int -- the found bit number//////////////////////////////////////////////////////int s_Bitmap::Find(){ for (int i = 0; i < numBits; i++) if (!Test(i)) { Mark(i); return i; } return -1;}//////////////////////////////////////////////////////// Function name : s_Bitmap::NumClear// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:43:50//// Function Desc : get the total number of non-marked bit// The steps of s_Bitmap::NumClear//// Fails if://// Note :// Return type : int -- the unset bits' total number//////////////////////////////////////////////////////int s_Bitmap::NumClear(){ int count = 0; for (int i = 0; i < numBits; i++) if (!Test(i)) count++; return count;}void s_Bitmap::Print(){}//////////////////////////////////////////////////////// Function name : s_Bitmap::FetchFrom// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:43:59//// Function Desc : get the bitmap from disk// The steps of s_Bitmap::FetchFrom//// Fails if://// Note :// Return type : void // Argument : int startSector -- the first sector// which the bitmap covers// Argument : int totalSector -- the total number// of sectors the bitmap covers//////////////////////////////////////////////////////
void s_Bitmap::FetchFrom(int startSector, int totalSector){ int lastSector = totalSector + startSector; char tmp[128]; char *buf = map; for (int i = startSector; i < lastSector; i++) { synchDisk->ReadSector(i, tmp); memcpy(buf, tmp, 128); buf += 128; }//end for (int i = ...)}//////////////////////////////////////////////////////// Function name : s_Bitmap::WriteBack// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:44:05//// Function Desc : write back the bitmap to disk// The steps of s_Bitmap::WriteBack//// Fails if://// Note :// Return type : void // Argument : int startSector// Argument : int totalSector//////////////////////////////////////////////////////void s_Bitmap::WriteBack(int startSector, int totalSector){ int lastSector = totalSector + startSector; char tmp[128]; char *buf = map; for (int i = startSector; i < lastSector; i++) { memcpy(tmp, buf, 128); synchDisk->WriteSector(i, tmp); buf += 128; }//end for (int i = ...)}boolFileHeader::Allocate(BitMap *freeMap, int fileSize){ }void FileHeader::Deallocate(BitMap *freeMap){}//////////////////////////////////////////////////////// Function name : FileHeader::FetchFrom// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:44:13//// Function Desc : get the inode from disk// The steps of FileHeader::FetchFrom//// Fails if://// Note :// Return type : void // Argument : int sector -- the sector which inode covers.// Argument : int pos -- the inner offset of a sector// which a inode covers(0~3)//////////////////////////////////////////////////////
void FileHeader::FetchFrom(int sector, int pos){ synchDisk->s_ReadDataInSector(sector, pos*32, 32, (char*)this);}//////////////////////////////////////////////////////// Function name : FileHeader::WriteBack// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:44:19//// Function Desc : write back the inode to disk// The steps of FileHeader::WriteBack//// Fails if://// Note :// Return type : void // Argument : int sector// Argument : int pos//////////////////////////////////////////////////////void FileHeader::WriteBack(int sector, int pos){ synchDisk->s_WriteDataInSector(sector, pos*32, 32, (char*)this);}int FileHeader::ByteToSector(int offset){}intFileHeader::FileLength(){ return numBytes;}voidFileHeader::Print(){}//////////////////////////////////////////////////////// Function name : FileHeader::s_Allocate// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:44:29//// Function Desc : allocate file space// The steps of FileHeader::s_Allocate// //// Fails if:// 1. the left size of disk is smaller than wanted allocate size// Note :// Return type : void // Argument : s_Bitmap *zMap// Argument : char *data// Argument : int size//////////////////////////////////////////////////////
void FileHeader::s_Allocate(s_Bitmap *zMap, char *data, int size){ if (zMap->NumClear()*128 < size) { printf("No enough space!\n"); return; } char direct = 0; char first = 0; char second = 0; char third = 0; int maxDirect = 3; int maxFirst = maxDirect+32; int maxSecond = maxFirst + 32*32; int maxThird = maxThird + 32*32*32; int lastSlot = numBytes / 128; int lastOffset = numBytes % 128; if (lastSlot < maxDirect-1) direct = 1; else if (lastSlot < maxFirst-1) first = 1; else if (lastSlot < maxSecond-1) second = 1; else if (lastSlot < maxThird-1) third = 1; char *tmp = data; int leftSize = size; if (direct == 1) { /// ///if the orginal last sector isn't full /// if (lastOffset != 0 && first == 0) { int leftInOneSlot = 128 - lastOffset; int writeSector = s_dataSectors[lastSlot]; synchDisk->s_WriteDataInSector(writeSector, lastOffset, leftInOneSlot, tmp); tmp += leftInOneSlot; leftSize -= leftInOneSlot; lastSlot += 1; }//end if (lastOffset!=0) /// ///the first sector begin with 0 offset /// for (int i = lastSlot; i < 3 && leftSize > 0; i++, leftSize-=128) { int writeSlot = zMap->Find(); int writeSize = 128; if (leftSize < 128) writeSize = leftSize; s_dataSectors[i] = writeSlot; synchDisk->s_WriteDataInSector(writeSlot, 0, writeSize, tmp); tmp += writeSize; }//end for (int i...) /// ///determine whether to goto first indirect index /// if (leftSize > 0) first = 1; }//end if (direct == 1)//////first indirect index/// if (first == 1) { int firstAddrs[32]; /// ///begin to write data before first indirect index /// if (direct == 1) { for (int i = 0; i < 32 && leftSize > 0; i++, leftSize-=128) { int writeSlot = zMap->Find(); int writeSize = 128; if (leftSize < 128) writeSize = leftSize; firstAddrs[i] = writeSlot; synchDisk->s_WriteDataInSector(writeSlot, 0, writeSize, tmp);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?