📄 directory.cc
字号:
}//end if (!strcmp...) }//end for (int i = 0;...) }//end for (int j = 0; ...) }//end for (int k = 0; ...) }//if (second == 1)
///
/// Third indirect index search
/// if (third == 1) { int thirdAddrs[32]; int thirdFirstMidAddrs[32]; int thirdSecondMidAddrs[32]; synchDisk->ReadSector(inode->s_dataSectors[5], (char*)thirdFirstMidAddrs); for (int l = 0; l < 32 && leftSize > 0; l++) { synchDisk->ReadSector(thirdFirstMidAddrs[l], (char*)thirdSecondMidAddrs); for (int k = 0; k < 32 && leftSize > 0; k++) { synchDisk->ReadSector(thirdSecondMidAddrs[k], (char*)thirdAddrs); for (int j = 0; j < 32 && leftSize > 0; j++) { for (int i = 0; i < 128 && leftSize > 0; i += 16, leftSize -= 16) { synchDisk->s_ReadDataInSector(thirdAddrs[j], i, 16, (char*)entryBuf); if (!strcmp(entryBuf->s_name, fileName)) { index = entryBuf->s_inodeIndex; delete entryBuf; return index; }//end if (!strcmp...) }//end for (int i = 0...) }//end for (int j = 0...) }//end for (int k = 0...) }//end for(int l = 0...) }//if (third == 1) delete entryBuf; return -1;}
//////////////////////////////////////////////////////
// Function name : Directory::s_SplitPath
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-8 21:36:51
//
// Function Desc : split the path into seperate block.
// for example:
// /root/mail/test => root, mail, test
// => char *buf[4] = {"root", "mail", "test"}
// The steps of Directory::s_SplitPath
//
// Fails if:
// 1.the path is illegal
//
// Note : This is a static function
// Return type : int --the level of dir(the highest is 4)
// Argument : char *path -- the raw path
// Argument : char **buf -- the result buffer
//////////////////////////////////////////////////////
int Directory::s_SplitPath(char *path, char **buf){ if (*path != '/') return -1; char *tmpPath = path; char *tmpBuf = NULL; int index = -1; int count = -1; while(1) { if (*tmpPath == '/') { if (tmpBuf != NULL) *tmpBuf = '\0'; if (*(tmpPath+1) == '\0') break; index++; buf[index] = new char[14]; tmpBuf = buf[index]; tmpPath++; count++; }//end if (*tmpPath == '/') if (*tmpPath == '\0') { *tmpBuf = '\0'; break; }//end if (*tmpPath...) *tmpBuf = *tmpPath; tmpBuf++; tmpPath++; }//end while (1) return count;}
//////////////////////////////////////////////////////
// Function name : Directory::s_GetInode
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-8 21:41:53
//
// Function Desc : Get inode according the index
// The steps of Directory::s_GetInode
// read DISK directly, and fetch the inode structure.
//
// Fails if:
//
// Note :
// Return type : FileHeader*
// Argument : int index
//////////////////////////////////////////////////////
FileHeader* Directory::s_GetInode(int index){ s_Error(); FileHeader *inode = new FileHeader; synchDisk->s_ReadDataInSector(s_SUPER->firstInodeBlock + index/4, index%4*32, 32, (char*)inode); return inode;}
//////////////////////////////////////////////////////
// Function name : Directory::s_FindInode
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-8 21:44:11
//
// Function Desc : get inode accord file path
// The steps of Directory::s_FindInode
// 1. split path
// 2. new a directory obj with parent dir path
// 3. get inode index
// 4. call s_GetInode to get inode
//
// Fails if:
// 1. split path fails
//
// Note :
// Return type : FileHeader*
// Argument : char *path
//////////////////////////////////////////////////////
FileHeader* Directory::s_FindInode(char *path){ s_Error(); char *buf[4] = {NULL, NULL, NULL, NULL}; if (s_SplitPath(path, buf)==-1) { error = true; s_Error(); }//end if (s_SplitPath...) FileHeader *inode = new FileHeader; if (buf[0] != NULL) { char pathBuf[s_FILE_NAME_MAX_LEN] = "/"; int index = -1; Directory *dir0 = new Directory("/"); inode = dir0->dirHdr; for (int i = 0; i < 4; i++) { if (buf[i] == NULL) break; index = dir0->s_FindIndex(inode, buf[i]); if (index != -1) inode = dir0->s_GetInode(index); else goto ERR_EXIT; }//end for (int i = 0; ..) dirHdrIndex = index; delete dir0; return inode;ERR_EXIT: delete dir0; }//end if (buf[0]...) return NULL;}
//////////////////////////////////////////////////////
// Function name : Directory::s_List
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-8 21:48:20
//
// Function Desc : List all files in directory
// The steps of Directory::s_List
// 1. search dir entry according parent dir's inode.
// 2. whenever get a dir entry, print its filename
//
// Fails if:
//
// Note :
// Return type : void
//////////////////////////////////////////////////////
void Directory::s_List(){ s_Error(); if (dirHdr->numBytes == 0) { printf("No Items in %s", dirPath); return; } s_DirEntry *entry = new s_DirEntry; FileHeader *inode = new FileHeader; char direct = 0; char first = 0; char second = 0; char third = 0; int lastSlot = dirHdr->numBytes / 128; int lastOffset = dirHdr->numBytes % 128; int maxDirect = 3; int maxFirst = maxDirect + 32; int maxSecond = maxFirst + 32*32; int maxThird = maxSecond + 32*32*32; if (lastSlot < maxDirect-1) direct = 1; else if (lastSlot < maxFirst-1) direct = first = 1; else if (lastSlot < maxSecond-1) direct = first = second = 1; else if (lastSlot < maxThird-1) direct = first = second = third = 1; printf("%s contains:\n", dirPath); int leftSize = dirHdr->numBytes;
///
/// Direct index search
/// if (direct == 1) { for (int i = 0; i < 3 && leftSize > 0; i++) { for (int j = 0; leftSize > 0; j+=16, leftSize-=16) { synchDisk->s_ReadDataInSector(dirHdr->s_dataSectors[i], j, 16, (char*)entry); inode = s_GetInode(entry->s_inodeIndex); if (inode->s_fileMode & s_DIR_TYPE) printf("Dir:\t"); else printf("File:\t"); printf("%s\n",entry->s_name); } }//for (int i = 0; ...) }//if (direct == 1)
///
/// first indirect index search
/// if (first == 1) { int firstAddrs[32]; synchDisk->s_ReadDataInSector(dirHdr->s_dataSectors[3], 0, 128, (char*)firstAddrs); for (int i = 0; i < 32 && leftSize; i++) { for (int j = 0; j < 128 && leftSize > 0; j+=16, leftSize-=16) { synchDisk->s_ReadDataInSector(firstAddrs[i], j, 16, (char*)entry); inode = s_GetInode(entry->s_inodeIndex); if (inode->s_fileMode & s_DIR_TYPE) printf("Dir:\t"); else printf("File:\t"); printf("%s\n",entry->s_name); }//for (int j = 0;...) }//for (int i = 0;...) }//end if (first == 1)
///
/// second indirect index search
/// if (second == 1) { int secondAddrs[32]; int secondMidAddrs[32]; synchDisk->ReadSector(dirHdr->s_dataSectors[4], (char*)secondMidAddrs); for (int k = 0; k < 32 && leftSize; k++) { synchDisk->ReadSector(secondMidAddrs[k], (char*)secondAddrs); for (int j = 0; j < 32 && leftSize; j++) { for (int i = 0; i < 128 && leftSize; i+=16, leftSize-=16) { synchDisk->s_ReadDataInSector(secondAddrs[j], i, 16, (char*)entry); inode = s_GetInode(entry->s_inodeIndex); if (inode->s_fileMode & s_DIR_TYPE) printf("Dir:\t"); else printf("File:\t"); printf("%s\n",entry->s_name); }//for (int i = 0;...) }//for (int j = 0; ...) }//for (int k = 0;...) }
///
/// Third indirect index search
/// if (third == 1) { int thirdAddrs[32]; int thirdFirstMidAddrs[32]; int thirdSecondMidAddrs[32]; synchDisk->ReadSector(dirHdr->s_dataSectors[5], (char*)thirdFirstMidAddrs); for (int l = 0; l < 32 && leftSize > 0; l++) { synchDisk->ReadSector(thirdFirstMidAddrs[l], (char*)thirdSecondMidAddrs); for (int k = 0; k < 32 && leftSize > 0; k++) { synchDisk->ReadSector(thirdSecondMidAddrs[k], (char*)thirdAddrs); for (int j = 0; j < 32 && leftSize > 0; j++) { for (int i = 0; i < 32 && leftSize > 0; i+=16, leftSize -= 16) { synchDisk->s_ReadDataInSector(thirdAddrs[j], i, 16, (char*)entry); inode = s_GetInode(entry->s_inodeIndex); if (inode->s_fileMode & s_DIR_TYPE) printf("Dir:\t"); else printf("File:\t"); printf("%s\n",entry->s_name); }//for (int i =0;...) }//for (int j = 0; ...) }//for (int k = 0;...) }//for (int l = 0;...) } delete entry; delete inode;}
//////////////////////////////////////////////////////
// Function name : Directory::s_WriteFile
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-8 21:53:30
//
// Function Desc : Write data into file
// The steps of Directory::s_WriteFile
// call FileHeader::s_Allocate to write data
//
// Fails if:
//
// Note : in this function, the inode isn't
// the parent dir's inode, however,
// the inode is the file itself.
// We pass the file path to the Directory
// constructor directly, no the parent
// dir path!!
// Return type : void
// Argument : int size
// Argument : char *data
//////////////////////////////////////////////////////
void Directory::s_WriteFile(int size, char *data){ s_Error(); s_Bitmap *zMap = new s_Bitmap(16384); zMap->FetchFrom(s_SUPER->firstZmapBlock, 16); dirHdr->s_Allocate(zMap, data, size); zMap->WriteBack(s_SUPER->firstZmapBlock, 16); dirHdr->WriteBack(s_SUPER->firstInodeBlock+dirHdrIndex/4, dirHdrIndex%4); delete zMap;}
//////////////////////////////////////////////////////
// Function name : Directory::s_ReadFile
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-8 21:55:04
//
// Function Desc : Read file data into buffer
// The steps of Directory::s_ReadFile
// 1. according to file inode, read the DISK directly
// 2. if necessary, go through first indirect index,
// second indirect index or even third indirect
// index.
//
// Fails if:
//
// Note :
// Return type : void
// Argument : int size
// Argument : char *data
//////////////////////////////////////////////////////
void Directory::s_ReadFile(int size, char *data){ s_Error(); char direct = 0; char first = 0; char second = 0; char third = 0; int lastSlot = dirHdr->numBytes / 128; int lastOffset = dirHdr->numBytes % 128; int maxDirect = 3; int maxFirst = maxDirect + 32; int maxSecond = maxFirst + 32*32; int maxThird = maxSecond + 32*32*32;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -