Esempio n. 1
0
        /// <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);
            }
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        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);
            }
            else
            {
                throw new Nfs3Exception(result.Status);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a directory at the specified path.
        /// </summary>
        /// <param name="path">The path of the directory to create</param>
        public override void CreateDirectory(string path)
        {
            try
            {
                Nfs3FileHandle parent = GetParentDirectory(path);

                Nfs3SetAttributes setAttrs = new Nfs3SetAttributes();
                setAttrs.Mode    = NfsOptions.NewDirectoryPermissions;
                setAttrs.SetMode = true;

                _client.MakeDirectory(parent, Utilities.GetFileFromPath(path), setAttrs);
            }
            catch (Nfs3Exception ne)
            {
                throw ConvertNfsException(ne);
            }
        }
Esempio n. 5
0
        public Nfs3CreateResult MakeDirectory(Nfs3FileHandle dirHandle, string name, Nfs3SetAttributes attributes)
        {
            MemoryStream  ms     = new MemoryStream();
            XdrDataWriter writer = StartCallMessage(ms, _client.Credentials, 9);

            dirHandle.Write(writer);
            writer.Write(name);
            attributes.Write(writer);

            RpcReply reply = DoSend(ms);

            if (reply.Header.IsSuccess)
            {
                return(new Nfs3CreateResult(reply.BodyReader));
            }
            else
            {
                throw new RpcException(reply.Header.ReplyHeader);
            }
        }
Esempio n. 6
0
        public Nfs3ModifyResult SetAttributes(Nfs3FileHandle handle, Nfs3SetAttributes newAttributes)
        {
            MemoryStream  ms     = new MemoryStream();
            XdrDataWriter writer = StartCallMessage(ms, _client.Credentials, 2);

            handle.Write(writer);
            newAttributes.Write(writer);
            writer.Write(false);

            RpcReply reply = DoSend(ms);

            if (reply.Header.IsSuccess)
            {
                return(new Nfs3ModifyResult(reply.BodyReader));
            }
            else
            {
                throw new RpcException(reply.Header.ReplyHeader);
            }
        }
Esempio n. 7
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);
            }
        }