/// <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); } }
/// <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 Nfs3ReadDirPlusResult(XdrDataReader reader) { Status = (Nfs3Status)reader.ReadInt32(); if (reader.ReadBool()) { DirAttributes = new Nfs3FileAttributes(reader); } if (Status == Nfs3Status.Ok) { CookieVerifier = reader.ReadBytes(Nfs3.CookieVerifierSize); DirEntries = new List <Nfs3DirectoryEntry>(); while (reader.ReadBool()) { Nfs3DirectoryEntry dirEntry = new Nfs3DirectoryEntry(reader); DirEntries.Add(dirEntry); } Eof = reader.ReadBool(); } }
/// <summary> /// Copies a file from one location to another. /// </summary> /// <param name="sourceFile">The source file to copy</param> /// <param name="destinationFile">The destination path</param> /// <param name="overwrite">Whether to overwrite any existing file (true), or fail if such a file exists</param> public override void CopyFile(string sourceFile, string destinationFile, bool overwrite) { try { Nfs3FileHandle sourceParent = GetParentDirectory(sourceFile); Nfs3FileHandle destParent = GetParentDirectory(destinationFile); string sourceFileName = Utilities.GetFileFromPath(sourceFile); string destFileName = Utilities.GetFileFromPath(destinationFile); Nfs3FileHandle sourceFileHandle = _client.Lookup(sourceParent, sourceFileName); if (sourceFileHandle == null) { throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "The file '{0}' does not exist", sourceFile), sourceFile); } 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", sourceFile), sourceFile); } Nfs3FileHandle destFileHandle = _client.Lookup(destParent, destFileName); if (destFileHandle != null) { if (overwrite == false) { throw new IOException(string.Format(CultureInfo.InvariantCulture, "The destination file '{0}' already exists", destinationFile)); } } // Create the file, with temporary permissions Nfs3SetAttributes setAttrs = new Nfs3SetAttributes(); setAttrs.Mode = UnixFilePermissions.OwnerRead | UnixFilePermissions.OwnerWrite; setAttrs.SetMode = true; setAttrs.Size = sourceAttrs.Size; setAttrs.SetSize = true; destFileHandle = _client.Create(destParent, destFileName, !overwrite, setAttrs); // Copy the file contents using (Nfs3FileStream sourceFs = new Nfs3FileStream(_client, sourceFileHandle, FileAccess.Read)) using (Nfs3FileStream destFs = new Nfs3FileStream(_client, destFileHandle, FileAccess.Write)) { int bufferSize = (int)Math.Max(1 * Sizes.OneMiB, Math.Min(_client.FileSystemInfo.WritePreferredBytes, _client.FileSystemInfo.ReadPreferredBytes)); byte[] buffer = new byte[bufferSize]; int numRead = sourceFs.Read(buffer, 0, bufferSize); while (numRead > 0) { destFs.Write(buffer, 0, numRead); numRead = sourceFs.Read(buffer, 0, bufferSize); } } // Set the new file's attributes based on the source file setAttrs = new Nfs3SetAttributes(); setAttrs.Mode = sourceAttrs.Mode; setAttrs.SetMode = true; setAttrs.AccessTime = sourceAttrs.AccessTime; setAttrs.SetAccessTime = Nfs3SetTimeMethod.ClientTime; setAttrs.ModifyTime = sourceAttrs.ModifyTime; setAttrs.SetModifyTime = Nfs3SetTimeMethod.ClientTime; setAttrs.Gid = sourceAttrs.Gid; setAttrs.SetGid = true; _client.SetAttributes(destFileHandle, setAttrs); } catch (Nfs3Exception ne) { throw ConvertNfsException(ne); } }