public DirectoryItem(VFS.DirectoryInfo info) { this.modifyTime = new DateTime((long)info.modifyTime); this.creationTime = new DateTime((long)info.creationTime); this.name = info.name; this.path = info.path; this.isDirectory = info.isDirectory; this.flag = info.flags; this.owner = info.owner; this.inodeIndex = info.inodeIndex; this.blockPreserved = info.inode.blockPreserved; this.refCount = info.inode.linkCount; if (info.isDirectory) { this.extension = "文件夹"; this.icon = ShellFileInfo.GetFolderIcon(ShellFileInfo.IconSize.Small, ShellFileInfo.FolderType.Closed); this.size = ""; } else { this.extension = ShellFileInfo.GetFileTypeDescription(this.name); this.icon = ShellFileInfo.GetFileIcon(this.name, ShellFileInfo.IconSize.Small, false); this.size = Utils.FormatSize(info.size); } }
/// <summary> /// 创建一个文件夹 /// </summary> /// <param name="name"></param> public void CreateDirectory(String name) { VFS.AssertNameValid(name); INode inode = vfs.AllocateINode(1, 2333); if (!dir.Add(name, new INodeDirectory(vfs, inode))) { throw new Exception("创建文件夹失败"); } }
/// <summary> /// 重命名文件或文件夹 /// </summary> /// <param name="oldName"></param> /// <param name="newName"></param> public void Rename(String oldName, String newName) { VFS.AssertNameValid(oldName); VFS.AssertNameValid(newName); if (!dir.Contains(oldName)) { throw new Exception("文件或文件夹未找到"); } if (dir.Contains(newName)) { throw new Exception("新文件名与现有文件或文件夹名称冲突"); } if (!dir.Rename(oldName, newName)) { throw new Exception("重命名失败"); } }
private void Window_Loaded(object sender, RoutedEventArgs e) { if (Properties.Settings.Default["vfsfile"] == null || ((String)(Properties.Settings.Default["vfsfile"])).Length == 0) { var r = System.Windows.Forms.MessageBox.Show("首次启动 VFS 需要在磁盘上建立一个虚拟磁盘镜像文件,镜像文件大小为 1GB。\n点击确定后请选择一个可以写入的位置来建立此文件,点击取消关闭程序。", "磁盘镜像文件未找到", System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Information); if (r == System.Windows.Forms.DialogResult.Cancel) { Close(); return; } using (var dialog = new System.Windows.Forms.SaveFileDialog()) { dialog.Title = "保存磁盘镜像文件"; dialog.FileName = "vfs.bin"; var result = dialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.Cancel) { Close(); return; } else { try { device = new FileAdapter(dialog.FileName, 1 << 10 << 10 << 10); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("打开镜像文件失败,可能您没有权限在该位置写入文件,或您内存不足。程序即将退出,请重新打开程序。"); Close(); return; } // 更新配置 Properties.Settings.Default["vfsfile"] = dialog.FileName; Properties.Settings.Default.Save(); } } } else { device = new FileAdapter((String)Properties.Settings.Default["vfsfile"], 1 << 10 << 10 << 10); } vfs = new VFS(device); if (!vfs.IsFormated()) { var result = System.Windows.Forms.MessageBox.Show("该磁盘镜像文件尚未格式化,是否格式化?", "VFS", System.Windows.Forms.MessageBoxButtons.YesNo); if (result == System.Windows.Forms.DialogResult.No) { Close(); return; } vfs.Format(device.Size() >> 10, 4); // 写入一个示例文件 var file = vfs.NewFile("/README.txt", VFS.FileMode.Create); file.Write(Encoding.UTF8.GetBytes("Hello world!\r\n\r\n这个文件是格式化时自动建立的,如果你看到了这个文件,说明一切工作正常。\r\n\r\n当你使用记事本修改这个文件并保存以后,它会同步到虚拟文件系统中。\r\n\r\n该虚拟文件系统是索引文件系统,inode 默认占总空间的 10%,理论可支持单一文件最大 2GB。")); System.Windows.Forms.MessageBox.Show("磁盘格式化成功!", "VFS", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } LoadDirectory("/"); UpdateInfo(); }
public File(VFSCore vfs, String path, FileMode fileMode) { this.vfs = vfs; var directory = VFS.GetPathDirectory(path); var name = VFS.GetPathName(path); VFS.AssertNameValid(name); INodeDirectory dir = INodeDirectory.Resolve(vfs, directory); if (dir == null) { throw new Exception("无法访问目录"); } switch (fileMode) { case FileMode.CreateNew: if (dir.Contains(name)) { throw new Exception("文件已存在"); } CreateFile(dir, name); break; case FileMode.Create: if (dir.Contains(name)) { OpenFile(dir, name); inode.Resize(0); } else { CreateFile(dir, name); } break; case FileMode.Open: if (!dir.Contains(name)) { throw new Exception("文件未找到"); } OpenFile(dir, name); break; case FileMode.OpenOrCreate: if (dir.Contains(name)) { OpenFile(dir, name); } else { CreateFile(dir, name); } break; case FileMode.Truncate: if (!dir.Contains(name)) { throw new Exception("文件未找到"); } OpenFile(dir, name); inode.Resize(0); break; case FileMode.Append: if (!dir.Contains(name)) { CreateFile(dir, name); } else { OpenFile(dir, name); position = inode.data.sizeByte; } break; default: throw new ArgumentException(); } }