Ejemplo n.º 1
0
        /// <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);
            }
        }
Ejemplo n.º 2
0
        /// <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 = 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);
            }
        }
        /// <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);
            }
        }
Ejemplo n.º 4
0
        internal Nfs3AccessResult(XdrDataReader reader)
        {
            Status = (Nfs3Status)reader.ReadInt32();
            if (reader.ReadBool())
            {
                ObjectAttributes = new Nfs3FileAttributes(reader);
            }

            Access = (Nfs3AccessPermissions)reader.ReadInt32();
        }
Ejemplo n.º 5
0
        internal Nfs3WeakCacheConsistency(XdrDataReader reader)
        {
            if (reader.ReadBool())
            {
                Before = new Nfs3WeakCacheConsistencyAttr(reader);
            }

            if (reader.ReadBool())
            {
                After = new Nfs3FileAttributes(reader);
            }
        }
        internal Nfs3FileSystemStatResult(XdrDataReader reader)
        {
            Status = (Nfs3Status)reader.ReadInt32();
            if (reader.ReadBool())
            {
                PostOpAttributes = new Nfs3FileAttributes(reader);
            }

            if (Status == Nfs3Status.Ok)
            {
                FileSystemStat = new Nfs3FileSystemStat(reader);
            }
        }
Ejemplo n.º 7
0
 /// <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);
     }
 }
Ejemplo n.º 8
0
 /// <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);
     }
 }
Ejemplo n.º 9
0
 /// <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);
     }
 }
Ejemplo n.º 10
0
        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);
            }
        }
Ejemplo n.º 11
0
        public Nfs3PathConfResult(XdrDataReader reader)
        {
            Status           = (Nfs3Status)reader.ReadInt32();
            ObjectAttributes = new Nfs3FileAttributes(reader);

            if (Status == Nfs3Status.Ok)
            {
                LinkMax         = reader.ReadUInt32();
                NameMax         = reader.ReadUInt32();
                NoTrunc         = reader.ReadBool();
                ChownRestricted = reader.ReadBool();
                CaseInsensitive = reader.ReadBool();
                CasePreserving  = reader.ReadBool();
            }
        }
Ejemplo n.º 12
0
        internal Nfs3ReadResult(XdrDataReader reader)
        {
            Status = (Nfs3Status)reader.ReadInt32();
            if (reader.ReadBool())
            {
                FileAttributes = new Nfs3FileAttributes(reader);
            }

            if (Status == Nfs3Status.Ok)
            {
                Count = reader.ReadInt32();
                Eof   = reader.ReadBool();
                Data  = reader.ReadBuffer();
            }
        }
Ejemplo n.º 13
0
        internal Nfs3LookupResult(XdrDataReader reader)
        {
            Status = (Nfs3Status)reader.ReadInt32();
            if (Status == Nfs3Status.Ok)
            {
                ObjectHandle = new Nfs3FileHandle(reader);
                if (reader.ReadBool())
                {
                    ObjectAttributes = new Nfs3FileAttributes(reader);
                }
            }

            if (reader.ReadBool())
            {
                DirAttributes = new Nfs3FileAttributes(reader);
            }
        }
Ejemplo n.º 14
0
        internal Nfs3CreateResult(XdrDataReader reader)
        {
            Status = (Nfs3Status)reader.ReadInt32();
            if (Status == Nfs3Status.Ok)
            {
                if (reader.ReadBool())
                {
                    FileHandle = new Nfs3FileHandle(reader);
                }

                if (reader.ReadBool())
                {
                    FileAttributes = new Nfs3FileAttributes(reader);
                }
            }

            CacheConsistency = new Nfs3WeakCacheConsistency(reader);
        }
Ejemplo n.º 15
0
        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();
            }
        }
Ejemplo n.º 16
0
        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();
            }
        }
Ejemplo n.º 17
0
        public bool Equals(Nfs3FileAttributes other)
        {
            if (other == null)
            {
                return(false);
            }

            return(other.Type == Type &&
                   other.Mode == Mode &&
                   other.LinkCount == LinkCount &&
                   other.Uid == Uid &&
                   other.Gid == Gid &&
                   other.Size == Size &&
                   other.BytesUsed == BytesUsed &&
                   other.RdevMajor == RdevMajor &&
                   other.RdevMinor == RdevMinor &&
                   other.FileSystemId == FileSystemId &&
                   other.FileId == FileId &&
                   object.Equals(other.AccessTime, AccessTime) &&
                   object.Equals(other.ModifyTime, ModifyTime) &&
                   object.Equals(other.ChangeTime, ChangeTime));
        }
Ejemplo n.º 18
0
        public Nfs3ReadDirResult(XdrDataReader reader)
        {
            Status = (Nfs3Status)reader.ReadInt32();

            if (reader.ReadBool())
            {
                DirAttributes = new Nfs3FileAttributes(reader);
            }

            DirEntries = new List <Nfs3DirectoryEntry>();
            if (Status == Nfs3Status.Ok)
            {
                CookieVerifier = reader.ReadUInt64();

                while (reader.ReadBool())
                {
                    DirEntries.Add(new Nfs3DirectoryEntry(reader));
                }

                Eof = reader.ReadBool();
            }
        }
Ejemplo n.º 19
0
 internal Nfs3GetAttributesResult(XdrDataReader reader)
 {
     Status     = (Nfs3Status)reader.ReadInt32();
     Attributes = new Nfs3FileAttributes(reader);
 }
Ejemplo n.º 20
0
        /// <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);
            }
        }