internal IEnumerable <Nfs3DirectoryEntry> ReadDirectory(Nfs3FileHandle parent, bool silentFail) { ulong cookie = 0; byte[] cookieVerifier = null; Nfs3ReadDirPlusResult result; do { result = _nfsClient.ReadDirPlus(parent, cookie, cookieVerifier, _fsInfo.DirectoryPreferredBytes, _fsInfo.ReadMaxBytes); if (result.Status == Nfs3Status.AccessDenied && silentFail) { break; } else if (result.Status != Nfs3Status.Ok) { throw new Nfs3Exception(result.Status); } foreach (var entry in result.DirEntries) { _cachedAttributes[entry.FileHandle] = entry.FileAttributes; yield return(entry); cookie = entry.Cookie; } cookieVerifier = result.CookieVerifier; }while (!result.Eof); }
public Nfs3FileSystemInfoResult FileSystemInfo(Nfs3FileHandle fileHandle) { MemoryStream ms = new MemoryStream(); XdrDataWriter writer = StartCallMessage(ms, _client.Credentials, 19); fileHandle.Write(writer); RpcReply reply = DoSend(ms); if (reply.Header.IsSuccess) { Nfs3FileSystemInfoResult fsiReply = new Nfs3FileSystemInfoResult(reply.BodyReader); if (fsiReply.Status == Nfs3Status.Ok) { return(fsiReply); } else { throw new Nfs3Exception(fsiReply.Status); } } else { throw new RpcException(reply.Header.ReplyHeader); } }
/// <summary> /// Moves a directory. /// </summary> /// <param name="sourceDirectoryName">The directory to move.</param> /// <param name="destinationDirectoryName">The target directory name.</param> public override void MoveDirectory(string sourceDirectoryName, string destinationDirectoryName) { try { Nfs3FileHandle sourceParent = GetParentDirectory(sourceDirectoryName); Nfs3FileHandle destParent = GetParentDirectory(destinationDirectoryName); string sourceName = Utilities.GetFileFromPath(sourceDirectoryName); string destName = Utilities.GetFileFromPath(destinationDirectoryName); Nfs3FileHandle fileHandle = _client.Lookup(sourceParent, sourceName); if (fileHandle == null) { throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "The directory '{0}' does not exist", sourceDirectoryName)); } Nfs3FileAttributes sourceAttrs = _client.GetAttributes(fileHandle); if ((sourceAttrs.Type & Nfs3FileType.Directory) == 0) { throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "The path '{0}' is not a directory", sourceDirectoryName)); } _client.Rename(sourceParent, sourceName, destParent, destName); } catch (Nfs3Exception ne) { throw ConvertNfsException(ne); } }
private void DoSearch(List <string> results, string path, Regex regex, bool subFolders, bool dirs, bool files) { Nfs3FileHandle dir = GetDirectory(path); foreach (Nfs3DirectoryEntry de in _client.ReadDirectory(dir, true)) { if (de.Name == "." || de.Name == "..") { continue; } bool isDir = de.FileAttributes.Type == Nfs3FileType.Directory; if ((isDir && dirs) || (!isDir && files)) { string searchName = (de.Name.IndexOf('.') == -1) ? de.Name + "." : de.Name; if (regex.IsMatch(searchName)) { results.Add(Utilities.CombinePaths(path, de.Name)); } } if (subFolders && isDir) { DoSearch(results, Utilities.CombinePaths(path, de.Name), regex, subFolders, dirs, files); } } }
public Nfs3FileHandle Lookup(Nfs3FileHandle dirHandle, string name) { Nfs3LookupResult result = _nfsClient.Lookup(dirHandle, name); if (result.ObjectAttributes != null && result.ObjectHandle != null) { _cachedAttributes[result.ObjectHandle] = result.ObjectAttributes; } if (result.DirAttributes != null) { _cachedAttributes[dirHandle] = result.DirAttributes; } if (result.Status == Nfs3Status.Ok) { return(result.ObjectHandle); } else if (result.Status == Nfs3Status.NoSuchEntity) { return(null); } else { throw new Nfs3Exception(result.Status); } }
/// <summary> /// Moves a file, allowing an existing file to be overwritten. /// </summary> /// <param name="sourceName">The file to move.</param> /// <param name="destinationName">The target file name.</param> /// <param name="overwrite">Whether to permit a destination file to be overwritten.</param> public override void MoveFile(string sourceName, string destinationName, bool overwrite) { try { Nfs3FileHandle sourceParent = GetParentDirectory(sourceName); Nfs3FileHandle destParent = GetParentDirectory(destinationName); string sourceFileName = Utilities.GetFileFromPath(sourceName); string destFileName = Utilities.GetFileFromPath(destinationName); Nfs3FileHandle sourceFileHandle = _client.Lookup(sourceParent, sourceFileName); if (sourceFileHandle == null) { throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "The file '{0}' does not exist", sourceName), sourceName); } Nfs3FileAttributes sourceAttrs = _client.GetAttributes(sourceFileHandle); if ((sourceAttrs.Type & Nfs3FileType.Directory) != 0) { throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "The path '{0}' is not a file", sourceName), sourceName); } Nfs3FileHandle destFileHandle = _client.Lookup(destParent, destFileName); if (destFileHandle != null && overwrite == false) { throw new IOException(string.Format(CultureInfo.InvariantCulture, "The destination file '{0}' already exists", destinationName)); } _client.Rename(sourceParent, sourceFileName, destParent, destFileName); } catch (Nfs3Exception ne) { throw ConvertNfsException(ne); } }
/// <summary> /// Gets the attributes of a file or directory. /// </summary> /// <param name="path">The file or directory to inspect.</param> /// <returns>The attributes of the file or directory.</returns> public override FileAttributes GetAttributes(string path) { try { Nfs3FileHandle handle = GetFile(path); Nfs3FileAttributes nfsAttrs = _client.GetAttributes(handle); FileAttributes result = (FileAttributes)0; if (nfsAttrs.Type == Nfs3FileType.Directory) { result |= FileAttributes.Directory; } else if (nfsAttrs.Type == Nfs3FileType.BlockDevice || nfsAttrs.Type == Nfs3FileType.CharacterDevice) { result |= FileAttributes.Device; } else { result |= FileAttributes.Normal; } if (Utilities.GetFileFromPath(path).StartsWith(".", StringComparison.Ordinal)) { result |= FileAttributes.Hidden; } return(result); } catch (Nfs3Exception ne) { throw ConvertNfsException(ne); } }
private Nfs3FileHandle GetParentDirectory(string path) { string[] dirs = Utilities.GetDirectoryFromPath(path).Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); Nfs3FileHandle parent = GetDirectory(_client.RootHandle, dirs); return(parent); }
/// <summary> /// Opens the specified file. /// </summary> /// <param name="path">The full path of the file to open.</param> /// <param name="mode">The file mode for the created stream.</param> /// <param name="access">The access permissions for the created stream.</param> /// <returns>The new stream.</returns> public override SparseStream OpenFile(string path, FileMode mode, FileAccess access) { try { Nfs3AccessPermissions requested; if (access == FileAccess.Read) { requested = Nfs3AccessPermissions.Read; } else if (access == FileAccess.ReadWrite) { requested = Nfs3AccessPermissions.Read | Nfs3AccessPermissions.Modify; } else { requested = Nfs3AccessPermissions.Modify; } if (mode == FileMode.Create || mode == FileMode.CreateNew || (mode == FileMode.OpenOrCreate && !FileExists(path))) { Nfs3FileHandle parent = GetParentDirectory(path); Nfs3SetAttributes setAttrs = new Nfs3SetAttributes(); setAttrs.Mode = NfsOptions.NewFilePermissions; setAttrs.SetMode = true; setAttrs.Size = 0; setAttrs.SetSize = true; Nfs3FileHandle handle = _client.Create(parent, Utilities.GetFileFromPath(path), mode != FileMode.Create, setAttrs); return(new Nfs3FileStream(_client, handle, access)); } else { Nfs3FileHandle handle = GetFile(path); Nfs3AccessPermissions actualPerms = _client.Access(handle, requested); if (actualPerms != requested) { throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access denied opening '{0}'. Requested permission '{1}', got '{2}'", path, requested, actualPerms)); } Nfs3FileStream result = new Nfs3FileStream(_client, handle, access); if (mode == FileMode.Append) { result.Seek(0, SeekOrigin.End); } else if (mode == FileMode.Truncate) { result.SetLength(0); } return(result); } } catch (Nfs3Exception ne) { throw ConvertNfsException(ne); } }
public Nfs3FileStream(Nfs3Client client, Nfs3FileHandle handle, FileAccess access) { _client = client; _handle = handle; _access = access; _length = _client.GetAttributes(_handle).Size; }
public void RemoveDirectory(Nfs3FileHandle dirHandle, string name) { Nfs3ModifyResult result = _nfsClient.RemoveDirectory(dirHandle, name); _cachedAttributes[dirHandle] = result.CacheConsistency.After; if (result.Status != Nfs3Status.Ok) { throw new Nfs3Exception(result.Status); } }
internal Nfs3MountResult(XdrDataReader reader) { FileHandle = new Nfs3FileHandle(reader); int numAuthFlavours = reader.ReadInt32(); AuthFlavours = new List <int>(numAuthFlavours); for (int i = 0; i < numAuthFlavours; ++i) { AuthFlavours.Add(reader.ReadInt32()); } }
public Nfs3FileHandle MakeDirectory(Nfs3FileHandle dirHandle, string name, Nfs3SetAttributes attributes) { Nfs3CreateResult result = _nfsClient.MakeDirectory(dirHandle, name, attributes); if (result.Status == Nfs3Status.Ok) { _cachedAttributes[result.FileHandle] = result.FileAttributes; return(result.FileHandle); } throw new Nfs3Exception(result.Status); }
public Nfs3FileHandle Create(Nfs3FileHandle dirHandle, string name, bool createNew, Nfs3SetAttributes attributes) { Nfs3CreateResult result = _nfsClient.Create(dirHandle, name, createNew, attributes); if (result.Status == Nfs3Status.Ok) { _cachedAttributes[result.FileHandle] = result.FileAttributes; return(result.FileHandle); } throw new Nfs3Exception(result.Status); }
public void Rename(Nfs3FileHandle fromDirHandle, string fromName, Nfs3FileHandle toDirHandle, string toName) { Nfs3RenameResult result = _nfsClient.Rename(fromDirHandle, fromName, toDirHandle, toName); _cachedAttributes[fromDirHandle] = result.FromDirCacheConsistency.After; _cachedAttributes[toDirHandle] = result.ToDirCacheConsistency.After; if (result.Status != Nfs3Status.Ok) { throw new Nfs3Exception(result.Status); } }
public void SetAttributes(Nfs3FileHandle handle, Nfs3SetAttributes newAttributes) { Nfs3ModifyResult result = _nfsClient.SetAttributes(handle, newAttributes); _cachedAttributes[handle] = result.CacheConsistency.After; if (result.Status != Nfs3Status.Ok) { throw new Nfs3Exception(result.Status); } }
public int Write(Nfs3FileHandle fileHandle, long position, byte[] buffer, int offset, int count) { Nfs3WriteResult result = _nfsClient.Write(fileHandle, position, buffer, offset, count); _cachedAttributes[fileHandle] = result.CacheConsistency.After; if (result.Status == Nfs3Status.Ok) { return(result.Count); } throw new Nfs3Exception(result.Status); }
/// <summary> /// Gets the length of a file. /// </summary> /// <param name="path">The path to the file.</param> /// <returns>The length in bytes.</returns> public override long GetFileLength(string path) { try { Nfs3FileHandle handle = GetFile(path); Nfs3FileAttributes attrs = _client.GetAttributes(handle); return(attrs.Size); } catch (Nfs3Exception ne) { throw ConvertNfsException(ne); } }
/// <summary> /// Gets the last modification time (in UTC) of a file or directory. /// </summary> /// <param name="path">The path of the file or directory.</param> /// <returns>The last write time.</returns> public override DateTime GetLastWriteTimeUtc(string path) { try { Nfs3FileHandle handle = GetFile(path); Nfs3FileAttributes attrs = _client.GetAttributes(handle); return(attrs.ModifyTime.ToDateTime()); } catch (Nfs3Exception ne) { throw ConvertNfsException(ne); } }
private Nfs3FileHandle GetFile(string path) { string file = Utilities.GetFileFromPath(path); Nfs3FileHandle parent = GetParentDirectory(path); Nfs3FileHandle handle = _client.Lookup(parent, file); if (handle == null) { throw new FileNotFoundException("No such file or directory", path); } return(handle); }
/// <summary> /// Gets the creation time (in UTC) of a file or directory. /// </summary> /// <param name="path">The path of the file or directory.</param> /// <returns>The creation time.</returns> public override DateTime GetCreationTimeUtc(string path) { try { // Note creation time is not available, so simulating from last modification time Nfs3FileHandle handle = GetFile(path); Nfs3FileAttributes attrs = _client.GetAttributes(handle); return(attrs.ModifyTime.ToDateTime()); } catch (Nfs3Exception ne) { throw ConvertNfsException(ne); } }
public Nfs3Client(string address, RpcCredentials credentials, string mountPoint) { _rpcClient = new RpcClient(address, credentials); _mountClient = new Nfs3Mount(_rpcClient); _rootHandle = _mountClient.Mount(mountPoint).FileHandle; _nfsClient = new Nfs3(_rpcClient); Nfs3FileSystemInfoResult fsiResult = _nfsClient.FileSystemInfo(_rootHandle); _fsInfo = fsiResult.FileSystemInfo; _cachedAttributes = new Dictionary <Nfs3FileHandle, Nfs3FileAttributes>(); _cachedAttributes[_rootHandle] = fsiResult.PostOpAttributes; }
/// <summary> /// Sets the last modification time (in UTC) of a file or directory. /// </summary> /// <param name="path">The path of the file or directory.</param> /// <param name="newTime">The new time to set.</param> public override void SetLastWriteTimeUtc(string path, DateTime newTime) { try { Nfs3FileHandle handle = GetFile(path); _client.SetAttributes(handle, new Nfs3SetAttributes() { SetModifyTime = Nfs3SetTimeMethod.ClientTime, ModifyTime = new Nfs3FileTime(newTime) }); } catch (Nfs3Exception ne) { throw ConvertNfsException(ne); } }
public Nfs3AccessPermissions Access(Nfs3FileHandle handle, Nfs3AccessPermissions requested) { Nfs3AccessResult result = _nfsClient.Access(handle, requested); if (result.ObjectAttributes != null) { _cachedAttributes[handle] = result.ObjectAttributes; } if (result.Status == Nfs3Status.Ok) { return(result.Access); } throw new Nfs3Exception(result.Status); }
public Nfs3ReadResult Read(Nfs3FileHandle fileHandle, long position, int count) { Nfs3ReadResult result = _nfsClient.Read(fileHandle, position, count); if (result.FileAttributes != null) { _cachedAttributes[fileHandle] = result.FileAttributes; } if (result.Status == Nfs3Status.Ok) { return(result); } throw new Nfs3Exception(result.Status); }
internal Nfs3DirectoryEntry(XdrDataReader reader) { FileId = reader.ReadUInt64(); Name = reader.ReadString(); Cookie = reader.ReadUInt64(); if (reader.ReadBool()) { FileAttributes = new Nfs3FileAttributes(reader); } if (reader.ReadBool()) { FileHandle = new Nfs3FileHandle(reader); } }
public Nfs3LookupResult Lookup(Nfs3FileHandle dir, string name) { MemoryStream ms = new MemoryStream(); XdrDataWriter writer = StartCallMessage(ms, _client.Credentials, 3); dir.Write(writer); writer.Write(name); RpcReply reply = DoSend(ms); if (reply.Header.IsSuccess) { return(new Nfs3LookupResult(reply.BodyReader)); } throw new RpcException(reply.Header.ReplyHeader); }
public Nfs3AccessResult Access(Nfs3FileHandle handle, Nfs3AccessPermissions requested) { MemoryStream ms = new MemoryStream(); XdrDataWriter writer = StartCallMessage(ms, _client.Credentials, 4); handle.Write(writer); writer.Write((int)requested); RpcReply reply = DoSend(ms); if (reply.Header.IsSuccess) { return(new Nfs3AccessResult(reply.BodyReader)); } throw new RpcException(reply.Header.ReplyHeader); }
public Nfs3GetAttributesResult GetAttributes(Nfs3FileHandle handle) { MemoryStream ms = new MemoryStream(); XdrDataWriter writer = StartCallMessage(ms, _client.Credentials, 1); handle.Write(writer); writer.Write(false); RpcReply reply = DoSend(ms); if (reply.Header.IsSuccess) { return(new Nfs3GetAttributesResult(reply.BodyReader)); } throw new RpcException(reply.Header.ReplyHeader); }
public Nfs3ModifyResult RemoveDirectory(Nfs3FileHandle dirHandle, string name) { MemoryStream ms = new MemoryStream(); XdrDataWriter writer = StartCallMessage(ms, _client.Credentials, NfsProc3.Rmdir); dirHandle.Write(writer); writer.Write(name); RpcReply reply = DoSend(ms); if (reply.Header.IsSuccess) { return(new Nfs3ModifyResult(reply.BodyReader)); } throw new RpcException(reply.Header.ReplyHeader); }