public DokanError CloseFile(string fileName, DokanFileInfo info) { //Console.WriteLine("CloseFile: {0}", fileName); // 可能打开文件时有 deleteOnClose 标记(Windows 8+) if (info.Context != null) { File f = (File)info.Context; if (f.flagDeleteOnClose) { Directory dir = new Directory(Util.GetPathDirectory(f.path)); if (!dir.Exists()) { return DokanError.ErrorSuccess; } String name = Util.GetPathFileName(f.path); if (!dir.Contains(name)) { return DokanError.ErrorSuccess; } dir.Delete(name); } } return DokanError.ErrorSuccess; }
public DokanError CreateDirectory(string fileName, DokanFileInfo info) { //Console.WriteLine("CreateDirectory: {0}", fileName); if (fileName == "\\") { return DokanError.ErrorSuccess; } if (fileName.EndsWith("\\")) { fileName = fileName.Substring(0, fileName.Length - 1); } Directory dir = new Directory(Util.GetPathDirectory(fileName)); if (!dir.Exists()) { return DokanError.ErrorPathNotFound; } String name = Util.GetPathFileName(fileName); if (dir.Contains(name)) { return DokanError.ErrorAlreadyExists; } dir.CreateDirectory(name); return DokanError.ErrorSuccess; }
public DokanError CreateFile(string fileName, DokanNet.FileAccess access, FileShare share, FileMode mode, FileOptions options, FileAttributes attributes, DokanFileInfo info) { info.DeleteOnClose = (options & FileOptions.DeleteOnClose) != 0; //Console.WriteLine("CreateFile: {0}, mode = {1}", fileName, mode); if (fileName == "\\") { return DokanError.ErrorSuccess; } Directory dir = new Directory(Util.GetPathDirectory(fileName)); if (!dir.Exists()) { return DokanError.ErrorPathNotFound; } String name = Util.GetPathFileName(fileName); if (name.Length == 0) { return DokanError.ErrorInvalidName; } if (name.IndexOfAny(Path.GetInvalidFileNameChars()) > -1) { return DokanError.ErrorInvalidName; } // dokan API 要求在目标文件是目录时候,设置 info.IsDirectory = true if (dir.Contains(name) && (dir.GetItemInfo(name).attribute & FileAttributes.Directory) != 0) { info.IsDirectory = true; return DokanError.ErrorSuccess; } try { File f = new File(fileName, mode); f.flagDeleteOnClose = info.DeleteOnClose; info.Context = f; } catch (FileNotFoundException) { return DokanError.ErrorFileNotFound; } catch (IOException) { return DokanError.ErrorAlreadyExists; } catch (NotImplementedException) { return DokanError.ErrorAccessDenied; } catch (Exception) { return DokanError.ErrorError; } return DokanError.ErrorSuccess; }
public DokanError SetFileTime(string fileName, DateTime? creationTime, DateTime? lastAccessTime, DateTime? lastWriteTime, DokanFileInfo info) { Directory dir = new Directory(Util.GetPathDirectory(fileName)); if (!dir.Exists()) { return DokanError.ErrorPathNotFound; } String name = Util.GetPathFileName(fileName); if (!dir.Contains(name)) { return DokanError.ErrorFileNotFound; } dir.SetFileTime(name, creationTime, lastAccessTime, lastWriteTime); return DokanError.ErrorSuccess; }
public DokanError SetFileAttributes(string fileName, FileAttributes attributes, DokanFileInfo info) { //Console.WriteLine("SetFileAttribute: {0}", fileName); Directory dir = new Directory(Util.GetPathDirectory(fileName)); if (!dir.Exists()) { return DokanError.ErrorPathNotFound; } String name = Util.GetPathFileName(fileName); if (!dir.Contains(name)) { return DokanError.ErrorFileNotFound; } dir.SetFileAttribute(name, attributes); return DokanError.ErrorSuccess; }
public DokanError SetEndOfFile(string fileName, long length, DokanFileInfo info) { Directory dir = new Directory(Util.GetPathDirectory(fileName)); if (!dir.Exists()) { return DokanError.ErrorPathNotFound; } String name = Util.GetPathFileName(fileName); if (!dir.Contains(name)) { return DokanError.ErrorFileNotFound; } INode inode = Disk.iNodes[dir.GetItemINodeIndex(name)]; inode.SetEndOfFile((int)length); return DokanError.ErrorSuccess; }
public DokanError MoveFile(string oldName, string newName, bool replace, DokanFileInfo info) { //Console.WriteLine("MoveFile: {0} -> {1}, replace = {2}", oldName, newName, replace); var dirNameOld = Util.GetPathDirectory(oldName); var dirNameNew = Util.GetPathDirectory(newName); if (dirNameNew.IndexOf(dirNameOld) > -1 && dirNameNew != dirNameOld) { return DokanError.ErrorError; } var dirOld = new Directory(dirNameOld); var dirNew = new Directory(dirNameNew); if (!dirOld.Exists() || !dirNew.Exists()) { return DokanError.ErrorPathNotFound; } var nameOld = Util.GetPathFileName(oldName); var nameNew = Util.GetPathFileName(newName); if (dirNew.Contains(nameNew)) { return DokanError.ErrorAlreadyExists; } var inode = dirOld.GetItemINodeIndex(nameOld); dirOld.Delete(nameOld); dirNew.AddItem(nameNew, inode); return DokanError.ErrorSuccess; }
public DokanError GetFileInformation(string fileName, out FileInformation fileInfo, DokanFileInfo info) { //Console.WriteLine("GetFileInformation: {0}, isDirectory = {1}", fileName, info.IsDirectory); if (fileName == "\\" || info.IsDirectory) { Directory dir = new Directory(fileName); if (!dir.Exists()) { //Console.WriteLine("GetFileInformation: PathNotFound"); fileInfo = new FileInformation(); return DokanError.ErrorPathNotFound; } String name; String path; if (fileName == "\\") { name = "\\"; path = "\\"; } else { name = Util.GetPathFileName(fileName); path = fileName; } fileInfo = new DirectoryItem(name, path, dir.GetSelfINodeIndex()).GetFileInformation(); //Console.WriteLine("GetFileInformation: Success"); return DokanError.ErrorSuccess; } { Directory dir = new Directory(Util.GetPathDirectory(fileName)); if (!dir.Exists()) { fileInfo = new FileInformation(); return DokanError.ErrorPathNotFound; } String name = Util.GetPathFileName(fileName); if (!dir.Contains(name)) { fileInfo = new FileInformation(); return DokanError.ErrorPathNotFound; } fileInfo = dir.GetItemInfo(name).GetFileInformation(); //Console.WriteLine("GetFileInformation: Success"); return DokanError.ErrorSuccess; } }
public DokanError DeleteDirectory(string fileName, DokanFileInfo info) { Directory currentDir = new Directory(fileName); if (!currentDir.Exists()) { return DokanError.ErrorPathNotFound; } // Windows Explorer 将会处理递归删除部分,所以这里不需要处理 if (currentDir.Count() > 0) { return DokanError.ErrorDirNotEmpty; } Directory dir = new Directory(Util.GetPathDirectory(fileName)); if (!dir.Exists()) { return DokanError.ErrorPathNotFound; } String name = Util.GetPathFileName(fileName); if (!dir.Contains(name)) { return DokanError.ErrorFileNotFound; } dir.Delete(name); return DokanError.ErrorSuccess; }
public File(string path, System.IO.FileMode fileMode) { this.path = path; var dir = new Directory(Util.GetPathDirectory(path)); var name = Util.GetPathFileName(path); switch (fileMode) { case System.IO.FileMode.Append: // 不支持 throw new NotImplementedException(); break; case System.IO.FileMode.Create: if (dir.Contains(name)) { this.inode = Disk.iNodes[dir.GetItemINodeIndex(name)]; this.inode.SetEndOfFile(0); } else { this.inode = Disk.AllocateNewINode(System.IO.FileAttributes.Normal); dir.AddItem(name, inode.index); } break; case System.IO.FileMode.CreateNew: //Console.WriteLine("*** CreateNew {0}", name); if (dir.Contains(name)) { throw new System.IO.IOException(); } this.inode = Disk.AllocateNewINode(System.IO.FileAttributes.Normal); dir.AddItem(name, inode.index); break; case System.IO.FileMode.Open: //Console.WriteLine("*** Open {0}", name); if (!dir.Contains(name)) { throw new System.IO.FileNotFoundException(); } this.inode = Disk.iNodes[dir.GetItemINodeIndex(name)]; break; case System.IO.FileMode.OpenOrCreate: if (!dir.Contains(name)) { this.inode = Disk.AllocateNewINode(System.IO.FileAttributes.Normal); dir.AddItem(name, inode.index); } else { this.inode = Disk.iNodes[dir.GetItemINodeIndex(name)]; } break; case System.IO.FileMode.Truncate: if (!dir.Contains(name)) { throw new System.IO.FileNotFoundException(); } this.inode = Disk.iNodes[dir.GetItemINodeIndex(name)]; this.inode.SetEndOfFile(0); break; } }