private void RecoverFile(FCB rootNode, int BlockIndex, string systemPath) { disk.disk[BlockIndex - 1].data = ""; disk.bitmap[BlockIndex - 1] = true; StreamReader reader = new StreamReader(systemPath + "\\DataBlock_" + Convert.ToString(BlockIndex) + ".dat"); disk.disk[BlockIndex - 1].type = Convert.ToInt16(reader.ReadLine()); disk.disk[BlockIndex - 1].data += reader.ReadLine(); disk.disk[BlockIndex - 1].nextBlock = Convert.ToInt16(reader.ReadLine()); if (disk.disk[BlockIndex - 1].nextBlock != -1) { RecoverFile(rootNode, disk.disk[BlockIndex - 1].nextBlock, systemPath); } }
public void RemoveFCB(FCB targetFolder) { if (targetFolder == null) { return; } disk[targetFolder.blockPosID - 1].FCBList.Remove(targetFolder); //如果此时该disk内PCBList已空 //就完全初始化 if (disk[targetFolder.blockPosID - 1].FCBList.Count() == 0) { disk[targetFolder.blockPosID - 1].FCBList = null; disk[targetFolder.blockPosID - 1].type = -1; bitmap[targetFolder.blockPosID - 1] = false; } }
private void NewFile_Button_Click(object sender, RoutedEventArgs e) { FileAdd fileAddWindow = new FileAdd(); fileAddWindow.ShowDialog(); string name = fileAddWindow._newFileName; //窗体直接关闭 if (name == null) { return; } //命名含有非法字符检测 if (name.Contains(" ") || name == "") { System.Windows.MessageBox.Show("含有非法字符"); return; } //重名检测 if (FindFCBName(name) != null) { System.Windows.MessageBox.Show("“" + name + "”已存在"); return; } string newFileName = fileAddWindow._newFileName; string newFileContent = fileAddWindow._newFileContent; int size = newFileContent.Count() / BLOCK_CONTENT_LENGTH + 1; FCB newFile = new FCB(Type.File, newFileName, size, ++FCBID); newFile.father = currentDirectory; currentDirectory.fileSon.Add(newFile); UpdateAncestorSize(newFile, size); disk.AddNewFCB(newFile); disk.AddNewFileContent(newFile, newFileContent); UpdateCurrentDir(); UpdateFCBList(); }
public string ExtractFileContent(FCB targetFile) { string wholeContent = ""; int currentBlockID = targetFile.beginBlockID; while (true) { wholeContent += disk[currentBlockID - 1].data; if (disk[currentBlockID - 1].nextBlock == -1) { break; } else { currentBlockID = disk[currentBlockID - 1].nextBlock; } } return(wholeContent); }
private void FCBList_MouseDoubleClick(object sender, MouseButtonEventArgs e) { //无选中项目 if (FCBList.SelectedItem == null) { return; } string targetName = FCBList.SelectedItem.ToString(); for (int i = 0; i < currentDirectory.folderSon.Count(); i++) { if (currentDirectory.folderSon[i].name == targetName) { currentDirectory = currentDirectory.folderSon[i]; UpdateCurrentDir(); UpdateFCBList(); break; } } for (int i = 0; i < currentDirectory.fileSon.Count(); i++) { if (currentDirectory.fileSon[i].name == targetName) { int beforeSize = currentDirectory.fileSon[i].size; FileShow fileShow = new FileShow(disk, currentDirectory.fileSon[i]); fileShow.ShowDialog(); int afterSize = currentDirectory.fileSon[i].size; UpdateAncestorSize(currentDirectory.fileSon[i], afterSize - beforeSize); UpdateCurrentDir(); break; } } }
private void NewFolder_Button_Click(object sender, RoutedEventArgs e) { FolderAdd folderAddWindow = new FolderAdd(); folderAddWindow.ShowDialog(); string name = folderAddWindow._newFolderName; //窗口直接关闭的情况 if (name == null) { return; } //命名含有非法字符检测 if (name.Contains(" ") || name == "") { System.Windows.MessageBox.Show("含有非法字符"); return; } //重名检测 if (FindFCBName(name) != null) { System.Windows.MessageBox.Show("“" + name + "”已存在"); return; } string newFolderName = folderAddWindow._newFolderName; FCB newFolder = new FCB(Type.Folder, newFolderName, 1, ++FCBID); newFolder.father = currentDirectory; currentDirectory.folderSon.Add(newFolder); UpdateAncestorSize(newFolder, newFolder.size); disk.AddNewFCB(newFolder); UpdateCurrentDir(); UpdateFCBList(); }
private FCB Recursion_Serach_Folder(FCB rootNode, int targetID) { if (rootNode.ID == targetID) { return(rootNode); } for (int i = 0; i < rootNode.folderSon.Count(); i++) { FCB currentNode = Recursion_Serach_Folder(rootNode.folderSon[i], targetID); if (currentNode == null) { continue; } if (currentNode.ID == targetID) { return(currentNode); } } return(null); }
public void AddNewFCB(FCB newFolder) { int posID = FindFreeBlock(1); if (posID == -1) { System.Windows.MessageBox.Show("文件系统已满!"); return; } bitmap[posID - 1] = true; //更新位图 disk[posID - 1].type = 1; newFolder.blockPosID = posID; //新Block鉴定 if (disk[posID - 1].FCBList == null) { disk[posID - 1].FCBList = new List <FCB>(); } disk[posID - 1].FCBList.Add(newFolder); //更新disk }
private void SaveAndExit_Button_Click(object sender, RoutedEventArgs e) { string systemPath = ""; FolderBrowserDialog folderBrowser = new FolderBrowserDialog(); folderBrowser.RootFolder = Environment.SpecialFolder.Desktop; if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK) { systemPath = folderBrowser.SelectedPath; } else if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) { return; } //清空当前文件夹 SystemFolderClear(systemPath); //新建文件流指针 StreamWriter writer; writer = new StreamWriter(systemPath + "\\BlockNum.dat", false); writer.WriteLine(Convert.ToString(blockNum), false); writer.Close(); for (int i = 0; i < blockNum; i++) { Block currentBlock = disk.disk[i]; //因为type为-1的Block没有存储任何信息可以直接还原 //所以不对其做存储操作 if (currentBlock.type == -1) { } //将所有type为1的Block存储在单个文件内 //以保证目录树的完整还原 else if (currentBlock.type == 1) { writer = new StreamWriter(systemPath + "\\FCBBlock.dat", true); for (int j = 0; j < currentBlock.FCBList.Count(); j++) { FCB currentFCB = disk.disk[i].FCBList[j]; //文件型FCB if (currentFCB.type == Type.File) { writer.WriteLine(currentFCB.ID + " " + 2 + " " + currentFCB.name + " " + currentFCB.size + " " + currentFCB.blockPosID + " " + currentFCB.beginBlockID + " " + currentFCB.endBlockID + " "); } //文件夹型FCB else if (currentFCB.type == Type.Folder) { writer.Write(currentFCB.ID + " " + 1 + " " + currentFCB.name + " " + currentFCB.size + " " + currentFCB.blockPosID + " "); writer.Write("("); for (int k = 0; k < currentFCB.folderSon.Count(); k++) { writer.Write(currentFCB.folderSon[k].ID + " "); } writer.Write(")"); writer.Write("("); for (int k = 0; k < currentFCB.fileSon.Count(); k++) { writer.Write(currentFCB.fileSon[k].ID + " "); } writer.Write(") "); writer.WriteLine(""); } } writer.Close(); } //将所有type为2的Block存储在不同文件中 else if (currentBlock.type == 2) { writer = new StreamWriter(systemPath + "\\DataBlock_" + Convert.ToString(currentBlock.ID) + ".dat", false); writer.WriteLine(currentBlock.type); //writer.WriteLine(currentBlock.ID); //不需要 writer.WriteLine(currentBlock.data); writer.WriteLine(currentBlock.nextBlock); writer.Close(); } } this.Close(); }
//根据路径名找到FCBBLock.dat 文件 //根据文件复原加载系统的FCB tree private void SystemRecover(string systemPath) { rootFolder = new FCB(Type.Folder, "root", ++FCBID); //basic Information Recover RecoverTree(rootFolder, 1, null, systemPath); }
private void RecoverTree(FCB rootNode, int FCBID, FCB father, string systemPath) { string fullSystemPath = systemPath + "\\FCBBlock.dat"; rootNode.father = father; rootNode.fileSon = new List <FCB>(); rootNode.folderSon = new List <FCB>(); StreamReader reader = new StreamReader(fullSystemPath); string singleLine = reader.ReadLine(); while (Convert.ToInt16(singleLine.Substring(0, singleLine.IndexOf(" ") + 1)) != FCBID) { singleLine = reader.ReadLine(); } rootNode.ID = Convert.ToInt16(singleLine.Substring(0, singleLine.IndexOf(" "))); singleLine = singleLine.Remove(0, singleLine.IndexOf(" ") + 1); rootNode.type = (Type)Convert.ToInt16(singleLine.Substring(0, singleLine.IndexOf(" "))); ///////////////////problem singleLine = singleLine.Remove(0, singleLine.IndexOf(" ") + 1); rootNode.name = singleLine.Substring(0, singleLine.IndexOf(" ")); singleLine = singleLine.Remove(0, singleLine.IndexOf(" ") + 1); rootNode.size = Convert.ToInt16(singleLine.Substring(0, singleLine.IndexOf(" "))); singleLine = singleLine.Remove(0, singleLine.IndexOf(" ") + 1); rootNode.blockPosID = Convert.ToInt16(singleLine.Substring(0, singleLine.IndexOf(" "))); singleLine = singleLine.Remove(0, singleLine.IndexOf(" ") + 1); //更新disk 的type disk.disk[rootNode.blockPosID - 1].type = 1; //更新disk 内Block的FCBList if (disk.disk[rootNode.blockPosID - 1].FCBList == null) { disk.disk[rootNode.blockPosID - 1].FCBList = new List <FCB>(); } disk.disk[rootNode.blockPosID - 1].FCBList.Add(rootNode); //更新位图 disk.bitmap[rootNode.blockPosID - 1] = true; if (rootNode.type == Type.File) { //父节点添加该节点指针 father.fileSon.Add(rootNode); rootNode.beginBlockID = Convert.ToInt16(singleLine.Substring(0, singleLine.IndexOf(" "))); singleLine = singleLine.Remove(0, singleLine.IndexOf(" ") + 1); rootNode.endBlockID = Convert.ToInt16(singleLine.Substring(0, singleLine.IndexOf(" "))); singleLine = singleLine.Remove(0, singleLine.IndexOf(" ") + 1); RecoverFile(rootNode, rootNode.beginBlockID, systemPath); } else if (rootNode.type == Type.Folder) { //父节点添加该节点指针 if (father != null) { father.folderSon.Add(rootNode); } //提取子文件夹FCB 的索引 List <int> folderSonIndex = new List <int>(); singleLine = singleLine.Remove(0, singleLine.IndexOf("(") + 1); while (!singleLine.Substring(0, 1).Equals(")")) { folderSonIndex.Add(Convert.ToInt16(singleLine.Substring(0, singleLine.IndexOf(" ")))); singleLine = singleLine.Remove(0, singleLine.IndexOf(" ") + 1); } singleLine = singleLine.Remove(0, 1); //递归添加子节点 for (int i = 0; i < folderSonIndex.Count(); i++) { FCB newFolder = new FCB(Type.Folder, null, 1, folderSonIndex[i]); RecoverTree(newFolder, folderSonIndex[i], rootNode, systemPath); } //提取子文件FCB 的索引 List <int> fileSonIndex = new List <int>(); singleLine = singleLine.Remove(0, singleLine.IndexOf("(") + 1); while (!singleLine.Substring(0, 1).Equals(")")) { fileSonIndex.Add(Convert.ToInt16(singleLine.Substring(0, singleLine.IndexOf(" ")))); singleLine = singleLine.Remove(0, singleLine.IndexOf(" ") + 1); } singleLine = singleLine.Remove(0, 1); //根据Index 恢复该FCB的文件子集 for (int i = 0; i < fileSonIndex.Count(); i++) { FCB newFile = new FCB(Type.File, null, 1, fileSonIndex[i]); RecoverTree(newFile, fileSonIndex[i], rootNode, systemPath); } } return; }