Ejemplo n.º 1
0
        public static FtpDirectory Create(FtpDirectory baseFtpDirectory,
                                          string directory)
        {
            var fullName = baseFtpDirectory.FullName;
            var path     = Path.Combine(fullName,
                                        directory);

            var ftpDirectory = Create(path);

            return(ftpDirectory);
        }
        public static FtpDirectory Create(FtpDirectory baseFtpDirectory,
                                          string directory)
        {
            var fullName = baseFtpDirectory.FullName;
            var path = Path.Combine(fullName,
                                    directory);

            var ftpDirectory = Create(path);

            return ftpDirectory;
        }
        public FtpDirectory GetParentFtpDirectory()
        {
            var containingDirectory = this.GetParentDirectory();

            if (string.IsNullOrEmpty(containingDirectory))
            {
                return(null);
            }
            var ftpDirectory = FtpDirectory.Create(containingDirectory);

            return(ftpDirectory);
        }
        public static IEnumerable<string> DirectoryChanges(FtpDirectory sourceFtpDirectory,
                                                           FtpDirectory targetFtpDirectory)
        {
            var sourceHierarchy = sourceFtpDirectory.GetHierarchy()
                                                    .ToList();
            var targetHierarchy = targetFtpDirectory.GetHierarchy()
                                                    .ToList();

            var wereEqualBefore = true;
            var i = 0;
            var levelOfEqual = 0;
            for (; i < sourceHierarchy.Count; i++)
            {
                var sourceDirectory = sourceHierarchy.ElementAt(i);
                var targetDirectory = targetHierarchy.ElementAtOrDefault(i);

                if (wereEqualBefore)
                {
                    if (!string.Equals(sourceDirectory,
                                       targetDirectory))
                    {
                        levelOfEqual = i;
                        wereEqualBefore = false;
                    }
                }
                if (!wereEqualBefore)
                {
                    yield return FtpFileSystemObject.ParentChangeCommand;
                }
            }

            foreach (var directory in targetHierarchy.Skip(levelOfEqual))
            {
                yield return directory;
            }
        }
Ejemplo n.º 5
0
        public static FtpDirectory Create(string path)
        {
            var ftpDirectory = new FtpDirectory(path);

            return(ftpDirectory);
        }
        public static FtpDirectory Create(string path)
        {
            var ftpDirectory = new FtpDirectory(path);

            return ftpDirectory;
        }
Ejemplo n.º 7
0
 private async Task<bool> ChangeWorkingDirectoryAsync(ComplexSocket controlComplexSocket,
                                                      string directory,
                                                      CancellationToken cancellationToken,
                                                      bool createDirectoryIfNotExists = false)
 {
     var success = await this.SendAndLogAsync(controlComplexSocket,
                                              cancellationToken,
                                              "CWD {0}",
                                              directory);
     if (!success)
     {
         return false;
     }
     var ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                  cancellationToken);
     switch (ftpReply.FtpResponseType)
     {
         case FtpResponseType.PermanentNegativeCompletion:
             // TODO some parsing of the actual FtpReply.ResponseCode should be done in here. i assume 5xx-state means "directory does not exist" all the time, which might be wrong
             if (!createDirectoryIfNotExists)
             {
                 return false;
             }
             success = await this.SendAndLogAsync(controlComplexSocket,
                                                  cancellationToken,
                                                  "MKD {0}",
                                                  directory);
             if (!success)
             {
                 return false;
             }
             ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                      cancellationToken);
             if (!ftpReply.Success)
             {
                 return false;
             }
             goto case FtpResponseType.PositiveCompletion;
         case FtpResponseType.PositiveCompletion:
             this._currentFtpDirectory = FtpDirectory.Create(this._currentFtpDirectory,
                                                             directory);
             break;
         default:
             return false;
     }
     return true;
 }
Ejemplo n.º 8
0
        private async Task<bool> GotoParentDirectoryAsync(ComplexSocket controlComplexSocket,
                                                          FtpFileSystemObject ftpFileSystemObject,
                                                          CancellationToken cancellationToken,
                                                          bool createDirectoryIfNotExists = false)
        {
            var ftpDirectory = ftpFileSystemObject.GetParentFtpDirectory();
            var directoryChanges = FtpClientHelper.DirectoryChanges(this._currentFtpDirectory,
                                                                    ftpDirectory);

            foreach (var directoryChange in directoryChanges)
            {
                if (string.Equals(directoryChange,
                                  FtpFileSystemObject.ParentChangeCommand))
                {
                    var success = await this.SendAndLogAsync(controlComplexSocket,
                                                             cancellationToken,
                                                             "CDUP");
                    if (success)
                    {
                        var ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                                     cancellationToken);
                        if (ftpReply.Success)
                        {
                            this._currentFtpDirectory = this._currentFtpDirectory.GetParentFtpDirectory();
                        }
                    }
                }
                else
                {
                    var success = await this.ChangeWorkingDirectoryAsync(controlComplexSocket,
                                                                         directoryChange,
                                                                         cancellationToken,
                                                                         createDirectoryIfNotExists);
                    if (!success)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
Ejemplo n.º 9
0
        public async Task<bool> DeleteAsync(FtpDirectory ftpDirectory,
                                            CancellationToken cancellationToken)
        {
            using (await this._mutex.LockAsync(cancellationToken))
            {
                var controlComplexSocket = await this.EnsureConnectionAndAuthenticationAsync(cancellationToken);
                if (controlComplexSocket == null)
                {
                    return false;
                }

                var success = await this.GotoParentDirectoryAsync(controlComplexSocket,
                                                                  ftpDirectory,
                                                                  cancellationToken);
                if (!success)
                {
                    return false;
                }

                success = await this.SendAndLogAsync(controlComplexSocket,
                                                     cancellationToken,
                                                     "RMD {0}",
                                                     ftpDirectory.DirectoryName);
                if (!success)
                {
                    return false;
                }

                var ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                             cancellationToken);
                if (!ftpReply.Success)
                {
                    return false;
                }
            }

            return true;
        }
Ejemplo n.º 10
0
 public async Task<bool> DeleteAsync(FtpDirectory ftpDirectory)
 {
     return await this.DeleteAsync(ftpDirectory,
                                   CancellationToken.None);
 }
Ejemplo n.º 11
0
 public bool Delete(FtpDirectory ftpDirectory,
                    CancellationToken cancellationToken)
 {
     return this.DeleteAsync(ftpDirectory,
                             cancellationToken)
                .Result;
 }
Ejemplo n.º 12
0
 public bool Delete(FtpDirectory ftpDirectory)
 {
     return this.Delete(ftpDirectory,
                        CancellationToken.None);
 }