Beispiel #1
0
        public Nfs3Procedure.Status RemoveFileOrDirectory(String parentDirectory, String name)
        {
            String localPathAndName = PlatformPath.LocalCombine(parentDirectory, name);

            ShareObject shareObject;

            if (shareObjectsByLocalPath.TryGetValue(localPathAndName, out shareObject))
            {
                DisposeShareObject(shareObject);
            }

            if (File.Exists(localPathAndName))
            {
                File.Delete(localPathAndName);
                return(Nfs3Procedure.Status.Ok);
            }

            if (Directory.Exists(localPathAndName))
            {
                Directory.Delete(localPathAndName);
                return(Nfs3Procedure.Status.Ok);
            }

            return(Nfs3Procedure.Status.ErrorNoSuchFileOrDirectory);
        }
Beispiel #2
0
        public Nfs3Procedure.Status TryGetDirectory(String shareDirectoryName, out RootShareDirectory rootShareDirectory, out ShareObject shareDirectoryObject)
        {
            String subPath;
            String rootShareName = NfsPath.SplitShareNameAndSubPath(shareDirectoryName, out subPath);

            if (rootShareName == null)
            {
                rootShareDirectory   = null;
                shareDirectoryObject = null;
                return(Nfs3Procedure.Status.ErrorInvalidArgument);
            }

            Nfs3Procedure.Status status = TryGetRootSharedDirectory(rootShareName, out rootShareDirectory);
            if (status != Nfs3Procedure.Status.Ok)
            {
                shareDirectoryObject = null; return(status);
            }
            if (rootShareDirectory == null)
            {
                shareDirectoryObject = null; return(Nfs3Procedure.Status.ErrorNoSuchFileOrDirectory);
            }

            if (subPath == null)
            {
                shareDirectoryObject = rootShareDirectory.shareObject;
            }
            else
            {
                String localPathAndName = PlatformPath.LocalCombine(rootShareDirectory.localShareDirectory, subPath);

                status = TryGetSharedObject(localPathAndName, subPath, out shareDirectoryObject);
                if (status != Nfs3Procedure.Status.Ok)
                {
                    return(status);
                }
                if (shareDirectoryObject == null)
                {
                    return(Nfs3Procedure.Status.ErrorNoSuchFileOrDirectory);
                }

                shareDirectoryObject.RefreshFileAttributes(permissions);
            }
            return(Nfs3Procedure.Status.Ok);
        }
Beispiel #3
0
        public MkdirReply MKDIR(MkdirCall mkdirCall)
        {
            ShareObject parentDirectoryShareObject;
            Status      status = sharedFileSystem.TryGetSharedObject(mkdirCall.directoryHandle, out parentDirectoryShareObject);

            if (status != Status.Ok)
            {
                return(new MkdirReply(status, BeforeAndAfterAttributes.None));
            }

            String localPathAndName = PlatformPath.LocalCombine(parentDirectoryShareObject.localPathAndName, mkdirCall.newDirectoryName);

            ShareObject mkdirDirectoryShareObject;

            status = sharedFileSystem.TryGetSharedObject(localPathAndName, mkdirCall.newDirectoryName, out mkdirDirectoryShareObject);
            if (status == Nfs3Procedure.Status.Ok)
            {
                return(new MkdirReply(Status.ErrorAlreadyExists, BeforeAndAfterAttributes.None));
            }
            if (status != Nfs3Procedure.Status.ErrorNoSuchFileOrDirectory)
            {
                return(new MkdirReply(status, BeforeAndAfterAttributes.None));
            }

            parentDirectoryShareObject.RefreshFileAttributes(sharedFileSystem.permissions);
            SizeAndTimes directorySizeAndTimesBeforeCreate = AutoExtensions.CreateSizeAndTimes(parentDirectoryShareObject.fileAttributes);

            // Todo: handle exceptions
            Directory.CreateDirectory(localPathAndName);

            status = sharedFileSystem.TryGetSharedObject(localPathAndName, mkdirCall.newDirectoryName, out mkdirDirectoryShareObject);
            if (status != Nfs3Procedure.Status.Ok)
            {
                return(new MkdirReply(status, BeforeAndAfterAttributes.None));
            }

            mkdirDirectoryShareObject.RefreshFileAttributes(sharedFileSystem.permissions);
            parentDirectoryShareObject.RefreshFileAttributes(sharedFileSystem.permissions);

            return(new MkdirReply(mkdirDirectoryShareObject.optionalFileHandleClass,
                                  parentDirectoryShareObject.optionalFileAttributes,
                                  new BeforeAndAfterAttributes(directorySizeAndTimesBeforeCreate, parentDirectoryShareObject.fileAttributes)));
        }
Beispiel #4
0
        public LookupReply LOOKUP(LookupCall lookupCall)
        {
            //
            // Get Directory Object
            //
            ShareObject directoryShareObject;
            Status      status = sharedFileSystem.TryGetSharedObject(lookupCall.directoryHandle, out directoryShareObject);

            if (status != Status.Ok)
            {
                return(new LookupReply(status, OptionalFileAttributes.None));
            }

            if (directoryShareObject.fileType != FileType.Directory)
            {
                return(new LookupReply(Status.ErrorNotDirectory, OptionalFileAttributes.None));
            }

            //
            // Get File
            //
            String      localPathAndName = PlatformPath.LocalCombine(directoryShareObject.localPathAndName, lookupCall.fileName);
            ShareObject fileShareObject;

            sharedFileSystem.TryGetSharedObject(localPathAndName, lookupCall.fileName, out fileShareObject);

            if (status != Status.Ok)
            {
                return(new LookupReply(status, OptionalFileAttributes.None));
            }
            if (fileShareObject == null)
            {
                return(new LookupReply(Status.ErrorNoSuchFileOrDirectory, OptionalFileAttributes.None));
            }

            directoryShareObject.RefreshFileAttributes(sharedFileSystem.permissions);
            fileShareObject.RefreshFileAttributes(sharedFileSystem.permissions);

            return(new LookupReply(fileShareObject.fileHandleBytes, directoryShareObject.optionalFileAttributes,
                                   fileShareObject.optionalFileAttributes));
        }
Beispiel #5
0
        public CreateReply CREATE(CreateCall createCall)
        {
            if (createCall.mode == CreateModeEnum.Exclusive)
            {
                return(new CreateReply(Status.ErrorNotSupported, BeforeAndAfterAttributes.None));
            }

            ShareObject directoryShareObject;
            Status      status = sharedFileSystem.TryGetSharedObject(createCall.directoryHandle, out directoryShareObject);

            if (status != Status.Ok)
            {
                return(new CreateReply(status, BeforeAndAfterAttributes.None));
            }

            FileStream fileStream = null;

            try
            {
                String localPathAndName = PlatformPath.LocalCombine(directoryShareObject.localPathAndName, createCall.newFileName);

                ShareObject fileShareObject;
                status = sharedFileSystem.TryGetSharedObject(localPathAndName, createCall.newFileName, out fileShareObject);

                if (status == Nfs3Procedure.Status.Ok)
                {
                    fileShareObject.RefreshFileAttributes(sharedFileSystem.permissions);

                    // The file already exists
                    if (createCall.mode == CreateModeEnum.Guarded)
                    {
                        return(new CreateReply(Status.ErrorAlreadyExists, BeforeAndAfterAttributes.None));
                    }
                }
                else
                {
                    if (status != Nfs3Procedure.Status.ErrorNoSuchFileOrDirectory)
                    {
                        return(new CreateReply(status, BeforeAndAfterAttributes.None));
                    }
                }

                directoryShareObject.RefreshFileAttributes(sharedFileSystem.permissions);
                SizeAndTimes directorySizeAndTimesBeforeCreate = AutoExtensions.CreateSizeAndTimes(directoryShareObject.fileAttributes);

                // Todo: handle exceptions
                fileStream = new FileStream(localPathAndName, FileMode.Create);
                fileStream.Dispose();

                status = sharedFileSystem.TryGetSharedObject(localPathAndName, createCall.newFileName, out fileShareObject);
                if (status != Nfs3Procedure.Status.Ok)
                {
                    return(new CreateReply(status, BeforeAndAfterAttributes.None));
                }

                fileShareObject.RefreshFileAttributes(sharedFileSystem.permissions);
                directoryShareObject.RefreshFileAttributes(sharedFileSystem.permissions);

                return(new CreateReply(fileShareObject.optionalFileHandleClass,
                                       fileShareObject.optionalFileAttributes,
                                       new BeforeAndAfterAttributes(directorySizeAndTimesBeforeCreate, directoryShareObject.fileAttributes)));
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }
            }
        }
Beispiel #6
0
        public Nfs3Procedure.Status Move(ShareObject oldParentShareObject, String oldName,
                                         ShareObject newParentShareObject, String newName)
        {
            Nfs3Procedure.Status status;

            status = newParentShareObject.CheckStatus();
            if (status != Nfs3Procedure.Status.Ok)
            {
                DisposeShareObject(newParentShareObject);
                return(status);
            }

            status = oldParentShareObject.CheckStatus();
            if (status != Nfs3Procedure.Status.Ok)
            {
                DisposeShareObject(oldParentShareObject);
                return(status);
            }

            //
            // Get Old Share Object
            //
            String oldLocalPathAndName = PlatformPath.LocalCombine(oldParentShareObject.localPathAndName, oldName);

            ShareObject oldShareObject;

            if (!shareObjectsByLocalPath.TryGetValue(oldLocalPathAndName, out oldShareObject))
            {
                return(Nfs3Procedure.Status.ErrorNoSuchFileOrDirectory);
            }

            status = oldShareObject.CheckStatus();
            if (status != Nfs3Procedure.Status.Ok)
            {
                DisposeShareObject(oldShareObject);
                return(status);
            }

            //
            // Move
            //
            String   newLocalPathAndName = PlatformPath.LocalCombine(newParentShareObject.localPathAndName, newName);
            FileType fileType            = oldShareObject.fileType;

            if (Directory.Exists(newLocalPathAndName))
            {
                if (oldShareObject.fileType != FileType.Directory)
                {
                    return(Nfs3Procedure.Status.ErrorAlreadyExists);
                }

                try
                {
                    Directory.Delete(newLocalPathAndName);
                }
                catch (IOException)
                {
                    return(Nfs3Procedure.Status.ErrorDirectoryNotEmpty); // The directory is not empty
                }

                Directory.Move(oldLocalPathAndName, newLocalPathAndName);
            }
            else if (File.Exists(newLocalPathAndName))
            {
                if (oldShareObject.fileType != FileType.Regular)
                {
                    return(Nfs3Procedure.Status.ErrorAlreadyExists);
                }

                File.Delete(newLocalPathAndName);

                File.Move(oldLocalPathAndName, newLocalPathAndName);
            }
            else
            {
                if (oldShareObject.fileType == FileType.Regular)
                {
                    File.Move(oldLocalPathAndName, newLocalPathAndName);
                }
                else if (oldShareObject.fileType == FileType.Directory)
                {
                    Directory.Move(oldLocalPathAndName, newLocalPathAndName);
                }
                else
                {
                    return(Nfs3Procedure.Status.ErrorInvalidArgument);
                }
            }

            //
            // Update the share object and return
            //
            UpdateShareObjectPathAndName(oldShareObject, newLocalPathAndName, newName);
            oldShareObject.RefreshFileAttributes(permissions);
            status = oldShareObject.CheckStatus();
            if (status != Nfs3Procedure.Status.Ok)
            {
                DisposeShareObject(oldShareObject);
            }
            return(status);
        }