📄 directory.cc
字号:
if (lastSlot < maxDirect) direct = 1; else if (lastSlot < maxFirst) direct = first = 1; else if (lastSlot < maxSecond) direct = first = second = 1; else if (lastSlot < maxThird) direct = first = second = third = 1; char *tmpBuf = new char[128]; char *guard = data; int leftSize = size;
///
/// direct index search
/// if (direct == 1) { for (int i = 0; i < 3 && leftSize>0; i++, leftSize-=128) { int slot = dirHdr->s_dataSectors[i]; int readSize = 0; if (leftSize == lastOffset) readSize = lastOffset; else readSize = 128; synchDisk->s_ReadDataInSector(dirHdr->s_dataSectors[i], 0, readSize, tmpBuf); memcpy(guard, tmpBuf, readSize); guard += readSize; }//end for (int i = 0; i < ...); }//end if (direct == 1)
///
/// first indirect index search
/// if (first) { int firstAddrs[32]; int firstAddrSlot = dirHdr->s_dataSectors[3]; synchDisk->s_ReadDataInSector(firstAddrSlot, 0, 128, (char*)firstAddrs); for (int i = 0; i < 32 && leftSize > 0; i++, leftSize-=128) { int slot = firstAddrs[i]; int readSize = 0; if (leftSize == lastOffset) readSize = lastOffset; else readSize = 128; synchDisk->s_ReadDataInSector(slot, 0, readSize, tmpBuf); memcpy(guard, tmpBuf, readSize); guard += readSize; }//end for (int i = 0; ...) }// end if (first)
///
/// second indirect index search
/// if (second) { int secondAddrs[32]; int secondMidAddrs[32]; synchDisk->ReadSector(dirHdr->s_dataSectors[4],(char*)secondMidAddrs); for (int j = 0; j < 32 && leftSize > 0; j++) { synchDisk->ReadSector(secondMidAddrs[j], (char*)secondAddrs); for (int i = 0; i < 32 && leftSize > 0; i++, leftSize-=128) { int readSize = 128; if (leftSize == lastOffset) readSize = lastOffset; synchDisk->s_ReadDataInSector(secondAddrs[i], 0, readSize, tmpBuf); memcpy(guard, tmpBuf, readSize); guard += readSize; }//end for(int i = 0;...) }//end for (int j = 0;...) }// end if (second)
///
/// third indirect index search
/// if (third) { int thirdAddrs[32]; int thirdFirstMidAddrs[32]; int thirdSecondMidAddrs[32]; synchDisk->ReadSector(dirHdr->s_dataSectors[5], (char*)thirdFirstMidAddrs); for (int k = 0; k < 32 && leftSize > 0; k++) { synchDisk->ReadSector(thirdFirstMidAddrs[k], (char*)thirdSecondMidAddrs); for (int j = 0; j < 32 && leftSize > 0; j++) { synchDisk->ReadSector(thirdSecondMidAddrs[j], (char*)thirdAddrs); for (int i = 0; i < 32 && leftSize > 0; i++, leftSize -=128) { int readSize = 128; if (leftSize == lastOffset) readSize = lastOffset; synchDisk->s_ReadDataInSector(thirdAddrs[i], 0, readSize, tmpBuf); memcpy(guard, tmpBuf, readSize); guard += readSize; }//end for (int i = 0;...) }//end for (int j = 0; ...) }//end for (int k = 0;...) }//end if (third) delete tmpBuf;}
//////////////////////////////////////////////////////
// Function name : Directory::~Directory
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-8 22:08:21
//
// Function Desc :
// The steps of Directory::~Directory
// 1.
// 2.
// 3.
// Fails if:
// 1.
// 2.
// 3.
// Note :
// Return type :
//////////////////////////////////////////////////////
Directory::~Directory(){ if (dirPath != NULL) delete dirPath; if (dirHdr != NULL) delete dirHdr;}
//////////////////////////////////////////////////////
// Function name : Directory::s_SplitParentAndFile
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-8 22:19:51
//
// Function Desc : split path into two parts: parent dir
// and filename
// The steps of Directory::s_SplitParentAndFile
// mainly call s_SplitPath
// Fails if:
//
// Note : This function is a static function
// Return type : void
// Argument : char *path -- in param
// Argument : char *parent -- out param
// Argument : char *file -- out param
//////////////////////////////////////////////////////
void Directory::s_SplitParentAndFile(char *path, char *parent, char *file){ char *buf[4] = {NULL, NULL, NULL, NULL}; int lastIndex = Directory::s_SplitPath(path, buf); if (lastIndex == -1) return; strcpy(file, buf[lastIndex]); strcpy(parent, "/"); for (int i = 0; i < lastIndex; i++) { strcat(parent, buf[i]); strcat(parent, "/"); }//end for (int i=0;...)}
//////////////////////////////////////////////////////
// Function name : Directory::s_FindDirEntry
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-8 22:21:32
//
// Function Desc : Find the file dir entry
// The steps of Directory::s_FindDirEntry
// 1. iterate parent dir's inode index
// 2. read DISK directly
// 3. if necessary, use first indirect index, second
// indirect index, or even third indirect index
//
// Fails if:
// can't find dir entry
// Note :
// Return type : s_DirEntry* --if fail, return NULL
// Argument : char *fileName --in param
// Argument : int *theSlot --out param, dir entry's block number
// Argument : int *offset --out param, dir entry's block offset
//////////////////////////////////////////////////////
s_DirEntry* Directory::s_FindDirEntry(char *fileName, int *theSlot, int *offset){ s_Error(); char direct = 0; char first = 0; char second = 0; char third = 0; int lastSlot = dirHdr->numBytes / 128; int maxDirect = 3; int maxFirst = maxDirect + 32; int maxSecond = maxFirst + 32*32; int maxThird = maxSecond + 32*32*32; if (lastSlot < 3) direct = 1; else if (lastSlot < maxFirst) direct = first = 1; else if (lastSlot < maxSecond) direct = first = second = 1; else if (lastSlot < maxThird) direct = first = second = third = 1; s_DirEntry *entryBuf = new s_DirEntry; int index = -1; int leftSize = dirHdr->numBytes;
///
/// direct index search
/// if (direct == 1) { for (int i = 0; i < 3 && leftSize > 0; i++) { int slot = dirHdr->s_dataSectors[i]; for (int j = 0; j < 128 && leftSize > 0; j+=16, leftSize-=16) { synchDisk->s_ReadDataInSector(slot, j, 16, (char*)entryBuf); if (!strcmp(entryBuf->s_name, fileName)) { *theSlot = slot; *offset = j; return entryBuf; }//end if (!strcmp...) }//end for (int j = 0; j < 128...) }//end for (int i = 0; i < maxDirect...) }//end 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 > 0; i++) { int slot = firstAddrs[i]; for (int j = 0; j < 128 && leftSize > 0; j+=16, leftSize-=16) { synchDisk->s_ReadDataInSector(slot, j, 16, (char*)entryBuf); if (!strcmp(entryBuf->s_name, fileName)) { *theSlot = slot; *offset = j; return entryBuf; }//end if (!strcmp...) }//end for (int j = 0; j < 128...) }//end for (int i = 0; i < maxDirect...) }//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*)entryBuf); if (!strcmp(entryBuf->s_name, fileName)) { *theSlot = secondAddrs[j]; *offset = i; return entryBuf; }//end if (!strcmp...) }//end for (int i...) }//end for (int j...) }//end for (int k...) }//end if (second == 1)
///
/// 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*)entryBuf); if (!strcmp(entryBuf->s_name, fileName)) { *theSlot = thirdAddrs[j]; *offset = i; return entryBuf; }//end if (!strcmp(...) }//end for (int i...) }//end for (int j...) }//end for (int k...) }//end for (int l...) }//end if (third == 1) return NULL;}
//////////////////////////////////////////////////////
// Function name : Directory::s_GetLastEntry
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-8 22:28:43
//
// Function Desc : Get the last entry in parent directory
// The steps of Directory::s_GetLastEntry
// 1. read DISK directly, according the parent directory size
//
// Fails if:
//
// Note : this function is used only in removing file.
// when remove a file, we let the last dir entry
// of parent dir to fill up the deleted dir entry
// Return type : s_DirEntry*
//////////////////////////////////////////////////////
s_DirEntry* Directory::s_GetLastEntry(){ 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 (lastOffset == 0) { lastOffset = 128 - 16; lastSlot--; }//end if (lastOffset == 0) else { lastOffset -= 16; }//end if (lastOffset == 0) else s_DirEntry *dir = NULL; if (lastSlot < maxDirect) { dir = new s_DirEntry; synchDisk->s_ReadDataInSector(dirHdr->s_dataSectors[lastSlot], lastOffset, 16, (char*)dir); return dir; }//end if (lastSlot < maxDirect) else if (lastSlot < maxFirst) { dir = new s_DirEntry; int firstAddrs[32]; synchDisk->ReadSector(dirHdr->s_dataSectors[3], (char*)firstAddrs); synchDisk->s_ReadDataInSector(firstAddrs[lastSlot-maxDirect], lastOffset, 16, (char*)dir); return dir; }//end if (lastSlot < maxFirst) else if (lastSlot < maxSecond) { dir = new s_DirEntry; int secondAddrs[32]; int secondMidAddrs[32]; synchDisk->ReadSector(dirHdr->s_dataSectors[4], (char*)secondMidAddrs); synchDisk->ReadSector(secondMidAddrs[(lastSlot-maxFirst)/32], (char*)secondAddrs); synchDisk->s_ReadDataInSector(secondAddrs[(lastSlot-maxFirst)%32], lastOffset, 16, (char*)dir); return dir; }//end if (lastSlot < maxSecond) else if (lastSlot < maxThird) { dir = new s_DirEntry; int thirdAddrs[32]; int thirdFirstMidAddrs[32]; int thirdSecondMidAddrs[32]; synchDisk->ReadSector(dirHdr->s_dataSectors[5], (char*)thirdFirstMidAddrs); synchDisk->ReadSector(thirdFirstMidAddrs[(lastSlot-maxSecond)/32/32], (char*)thirdSecondMidAddrs); synchDisk->ReadSector(thirdSecondMidAddrs[(lastSlot-maxSecond)/32%32], (char*)thirdAddrs); synchDisk->s_ReadDataInSector(thirdAddrs[(lastSlot-maxSecond)%32], lastOffset, 16, (char*)dir); return dir; }//end if (lastSlot < maxThird) return dir;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -