コード例 #1
0
ファイル: SftpFilesystem.cs プロジェクト: zweecn/sshfs4win
        DokanError IDokanOperations.MoveFile(string oldName, string newName, bool replace, DokanFileInfo info)
        {
            Log("MoveFile |Name:{0} ,NewName:{3},Reaplace{4},IsDirectory:{1} ,Context:{2}",
                oldName, info.IsDirectory,
                info.Context, newName, replace);
            string oldpath = GetUnixPath(oldName);

            /*  if (_generalSftpSession.RequestLStat(oldpath, true) == null)
             *  return DokanError.ErrorPathNotFound;
             * if (oldName.Equals(newName))
             *  return DokanError.ErrorSuccess;*/
            string newpath = GetUnixPath(newName);

            if (_sftpSession.RequestLStat(newpath, true) == null)
            {
                (info.Context as SftpContext).Release();

                info.Context = null;
                try
                {
                    // New file need move but parent directory not exists ?
                    EnsureParentDirExist(newpath);
                    _sftpSession.RequestRename(oldpath, newpath);
                    InvalidateParentCache(oldName);
                    InvalidateParentCache(newName);
                    _cache.Remove(oldpath);
                }
                catch (SftpPermissionDeniedException)
                {
                    return(DokanError.ErrorAccessDenied);
                }

                return(DokanError.ErrorSuccess);
            }
            else if (replace)
            {
                (info.Context as SftpContext).Release();

                info.Context = null;


                try
                {
                    if (_supportsPosixRename)
                    {
                        _sftpSession.RequestPosixRename(oldpath, newpath);
                    }
                    else
                    {
                        if (!info.IsDirectory)
                        {
                            _sftpSession.RequestRemove(newpath);
                        }
                        _sftpSession.RequestRename(oldpath, newpath);
                    }


                    InvalidateParentCache(oldName);
                    InvalidateParentCache(newName);
                    _cache.Remove(oldpath);
                }
                catch (SftpPermissionDeniedException)
                {
                    return(DokanError.ErrorAccessDenied);
                } // not tested on sftp3
                return(DokanError.ErrorSuccess);
            }
            return(DokanError.ErrorAlreadyExists);
        }