void BufferManager::writeFile(const std::string& fileName) { int blockNum; int n; if ((n = fileName.find(".table", 0)) != std::string::npos) { Table* tablePtr = Cat.getTablePtr(fileName.substr(0, n)); blockNum = tablePtr->blockNum; } if ((n = fileName.find(".index", 0)) != std::string::npos) { Index* indexPtr = Cat.getIndexPtr(fileName.substr(0, n)); blockNum = indexPtr->blockNum; } for (int i = 0; i < blockNum; i++) writeFileBlock(fileName, i); }
/* * write file data to disk */ bool Directory::writeFile(int iNodeNumber, string &data) { // get the number of nodes the file currently has allocated (must be at least 1) int currentNumberOfBlocks = iNodeList->getNumberOfBlocks(iNodeNumber); // calculate number of Blocks needed int numberOfBlocksNeeded = (data.length() + getDataInBlock() - 1) / getDataInBlock(); if (numberOfBlocksNeeded > currentNumberOfBlocks + iNodeList->getNumberOfFreeINodes()) { cout << "Not enough space on disk for file" << endl; return false; } // point to the first block number int currentDiskBlockNumber = 0; int dataPointer = 0; FileHeaderData fileHeaderData; for (int i=0;i<numberOfBlocksNeeded;i++) { // get currentDiskBlockNumber, add to iNode if necessary if (i == currentNumberOfBlocks) { currentDiskBlockNumber = freeBlockList->getBlock(); iNodeList->addBlockToINode(iNodeNumber, currentDiskBlockNumber); currentNumberOfBlocks ++; } else { currentDiskBlockNumber = iNodeList->getBlockNumber(iNodeNumber,i); } // current block header dataPointer = i * getDataInBlock(); fileHeaderData.lengthOfBlock = min((unsigned int)data.length() - dataPointer, getDataInBlock()); // write the file block header writeFileBlockHeader(currentDiskBlockNumber, fileHeaderData); // write the file block writeFileBlock(currentDiskBlockNumber, data, dataPointer, dataPointer+fileHeaderData.lengthOfBlock); } // remove any un-needed blocks from the iNode for (int i=currentNumberOfBlocks;i>numberOfBlocksNeeded;i--) { iNodeList->removeBlockFromINode(iNodeNumber); } // set the iNodeSize iNodeList->setINodeSize(iNodeNumber,data.length()); return true; }