Beispiel #1
0
 /// <summary>
 /// Processes the list.
 /// </summary>
 /// <param name="session">The handle.</param>
 /// <param name="path">The path.</param>
 /// <returns></returns>
 private static IList <string> ProcessList(FtpSession session, FtpPath path)
 {
     // Open data channel
     using (var dataStream = OpenDataStream(session, FtpTransferMode.Binary, FtpStreamMode.Read))
     {
         // then command is sent
         var reply = session.Expect(session.SendCommand("LIST", session.Connection.Client.GetPlatform(session).EscapePath(path.ToString())), 125, 150, 425);
         if (!reply.Code.IsSuccess)
         {
             dataStream.Abort();
             session.ThrowException(reply);
         }
         using (var streamReader = new StreamReader(dataStream.Validated(), session.Connection.Encoding))
         {
             var list = new List <string>();
             for (;;)
             {
                 var line = streamReader.ReadLine();
                 if (line == null)
                 {
                     break;
                 }
                 list.Add(line);
             }
             return(list);
         }
     }
 }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FtpEntry"/> class.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="size">The size.</param>
 /// <param name="type">The type.</param>
 /// <param name="date">The date.</param>
 /// <param name="target">The target.</param>
 public FtpEntry(FtpPath path, long?size, FtpEntryType type, DateTime date, FtpPath target)
 {
     Path   = path;
     Name   = path.GetFileName();
     Date   = date;
     Type   = type;
     Target = target;
     Size   = size;
 }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FtpEntry" /> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 /// <param name="name">The name.</param>
 /// <param name="size">The size.</param>
 /// <param name="type">The type.</param>
 /// <param name="date">The date.</param>
 /// <param name="target">The target.</param>
 public FtpEntry(FtpPath parent, string name, long? size, FtpEntryType type, DateTime date, FtpPath target)
 {
     if (parent != null)
         Path = parent + name;
     Name = name;
     Date = date;
     Type = type;
     Target = target;
     Size = size;
 }
Beispiel #4
0
        /// <summary>
        /// Sends a MLST command.
        /// </summary>
        /// <param name="ftpClient">The FTP client.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static string Mlst(this FtpClient ftpClient, FtpPath path)
        {
            var reply = ftpClient.Process(session =>
            {
                session.CheckProtection(FtpProtection.ControlChannel);
                return(session.Expect(session.SendCommand("MLST", ftpClient.GetPlatform(session).EscapePath(path.ToString())), 250));
            });

            return(reply.Lines[1]);
        }
Beispiel #5
0
        /// <summary>
        /// Sends a STAT command.
        /// </summary>
        /// <param name="ftpClient">The FTP client.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static IEnumerable <string> Stat(this FtpClient ftpClient, FtpPath path)
        {
            var reply = ftpClient.Process(session =>
            {
                session.CheckProtection(FtpProtection.ControlChannel);
                return(session.Expect(session.SendCommand("STAT", ftpClient.GetPlatform(session).EscapePath(path.ToString())), 213, 211));
            });

            return(reply.Lines.Skip(1).Take(reply.Lines.Length - 2));
        }
Beispiel #6
0
        /// <summary>
        /// Parses a MLSx line.
        /// </summary>
        /// <param name="line">The line.</param>
        /// <param name="parent">The parent.</param>
        /// <returns></returns>
        internal /* test */ static FtpEntry ParseMlsx(string line, FtpPath parent)
        {
            var mlsxLine = ReadMlsxLine(line);

            return(new FtpEntry(parent, mlsxLine.Item1,
                                size: GetFact(mlsxLine.Item2, "Size", f => long.Parse(f), () => (long?)null),
                                type: GetFact(mlsxLine.Item2, "Type", GetTypeFact, () => FtpEntryType.File),
                                date: GetFact(mlsxLine.Item2, "Modify", GetDateFact, () => DateTime.MinValue),
                                target: null));
        }
Beispiel #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FtpEntry" /> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 /// <param name="name">The name.</param>
 /// <param name="size">The size.</param>
 /// <param name="type">The type.</param>
 /// <param name="date">The date.</param>
 /// <param name="target">The target.</param>
 public FtpEntry(FtpPath parent, string name, long?size, FtpEntryType type, DateTime date, FtpPath target)
 {
     if (parent != null)
     {
         Path = parent + name;
     }
     Name   = name;
     Date   = date;
     Type   = type;
     Target = target;
     Size   = size;
 }
Beispiel #8
0
        /// <summary>
        /// Processes the stor.
        /// </summary>
        /// <param name="session">The handle.</param>
        /// <param name="path">The path.</param>
        /// <param name="mode">The mode.</param>
        /// <returns></returns>
        /// <exception cref="IOException"></exception>
        private static Stream ProcessStor(FtpSession session, FtpPath path, FtpTransferMode mode = FtpTransferMode.Binary)
        {
            var stream = OpenDataStream(session, mode, FtpStreamMode.Write);
            var reply  = session.Expect(session.SendCommand("STOR", path.ToString()), 125, 150, 425, 550);

            if (!reply.Code.IsSuccess)
            {
                stream.Abort();
                session.ThrowException(reply);
                return(null);
            }
            return(stream.Validated());
        }
Beispiel #9
0
        private static FtpEntry ProcessGetEntry(FtpSession session, FtpPath path)
        {
            session.CheckProtection(FtpProtection.ControlChannel);
            var reply = session.SendCommand("STAT", session.Connection.Client.GetPlatform(session).EscapePath(path.ToString()));

            if ((reply.Code != 213 && reply.Code != 211) || reply.Lines.Length <= 2)
            {
                return(null);
            }
            // now get the type: the first entry is "." for folders or file itself for files/links
            var entry = EnumerateEntries(session.Connection.Client, path, reply.Lines.Skip(1), ignoreSpecialEntries: false).First();

            // actually, it's always good here
            return(new FtpEntry(path, entry.Size, entry.Type, entry.Date, entry.Target));
        }
Beispiel #10
0
 /// <summary>
 /// Deletes the specified path.
 /// </summary>
 /// <param name="ftpClient">The FTP client.</param>
 /// <param name="path">The path.</param>
 /// <param name="isDirectory">The is directory.</param>
 /// <returns></returns>
 private static bool Delete(this FtpClient ftpClient, FtpPath path, bool?isDirectory)
 {
     // if we don't know, try to delete as a directory
     if (!isDirectory.HasValue || isDirectory.Value)
     {
         var deleted = Rmd(ftpClient, path);
         // if we wanted to delete a directory or if we actually deleted something, then consider the operation as complete
         if (deleted || isDirectory.HasValue)
         {
             return(deleted);
         }
     }
     // otherwise, either it is a directory, or the type is unknown and the file delete failed
     return(Dele(ftpClient, path));
 }
Beispiel #11
0
        /// <summary>
        /// Enumerates the entries.
        /// </summary>
        /// <param name="ftpClient">The FTP client.</param>
        /// <param name="parent">The parent.</param>
        /// <param name="lines">The lines.</param>
        /// <param name="ignoreSpecialEntries">if set to <c>true</c> [ignore special entries].</param>
        /// <returns></returns>
        /// <exception cref="FtpProtocolException">Impossible to parse line:  + line;new FtpReplyCode(553)</exception>
        /// <exception cref="FtpReplyCode">553</exception>
        /// <exception cref="FtpException">Unhandled server type</exception>
        private static IEnumerable <FtpEntry> EnumerateEntries(FtpClient ftpClient, FtpPath parent, IEnumerable <string> lines, bool ignoreSpecialEntries = true)
        {
            // ToArray() here in order to release the FtpSession
            foreach (var line in lines.ToArray())
            {
                var ftpEntry = ftpClient.Platform.Parse(line, parent);
                if (ftpEntry == null)
                {
                    throw new FtpProtocolException("Impossible to parse line: " + line, new FtpReplyCode(553));
                }

                if (ignoreSpecialEntries && (ftpEntry.Name == "." || ftpEntry.Name == ".."))
                {
                    continue;
                }

                yield return(ftpEntry);
            }
        }
Beispiel #12
0
 /// <summary>
 /// Sends a MLST command.
 /// </summary>
 /// <param name="ftpClient">The FTP client.</param>
 /// <param name="path">The path.</param>
 /// <returns></returns>
 public static FtpEntry MlstEntry(this FtpClient ftpClient, FtpPath path)
 {
     return(ParseMlsx(Mlst(ftpClient, path), path));
 }
Beispiel #13
0
 /// <summary>
 /// Gets a <see cref="FtpEntry" /> about given path.
 /// </summary>
 /// <param name="ftpClient">The FTP client.</param>
 /// <param name="path">The path.</param>
 /// <returns>
 /// The entry, or null if entry does not exist
 /// </returns>
 public static FtpEntry GetEntry(this FtpClient ftpClient, FtpPath path)
 {
     return(ftpClient.Process(handle => ProcessGetEntry(handle, path)));
 }
Beispiel #14
0
 /// <summary>
 /// Sends a MKD command (MaKe Directory).
 /// </summary>
 /// <param name="ftpClient">The FTP client.</param>
 /// <param name="path">The path.</param>
 public static void Mkd(this FtpClient ftpClient, FtpPath path)
 {
     ftpClient.Process(session => session.Expect(session.SendCommand("MKD", path.ToString()), 257));
 }
Beispiel #15
0
 /// <summary>
 /// Lists the entries.
 /// </summary>
 /// <param name="ftpClient">The FTP client.</param>
 /// <param name="path">The path.</param>
 /// <returns></returns>
 public static IEnumerable <FtpEntry> StatEntries(this FtpClient ftpClient, FtpPath path)
 {
     return(EnumerateEntries(ftpClient, path, Stat(ftpClient, path)));
 }
Beispiel #16
0
        /// <summary>
        /// Sends a DELE command (DELEte file).
        /// </summary>
        /// <param name="ftpClient">The FTP client.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static bool Dele(this FtpClient ftpClient, FtpPath path)
        {
            var reply = ftpClient.Process(session => session.Expect(session.SendCommand("DELE", path.ToString()), 250, 550));

            return(reply.Code.IsSuccess);
        }
Beispiel #17
0
 /// <summary>
 /// Send STOR command.
 /// </summary>
 /// <param name="ftpClient">The FTP client.</param>
 /// <param name="path">The path.</param>
 /// <param name="mode">The mode.</param>
 /// <returns></returns>
 public static Stream Stor(this FtpClient ftpClient, FtpPath path, FtpTransferMode mode = FtpTransferMode.Binary)
 {
     return(ftpClient.Process(handle => ProcessStor(handle, path, mode)));
 }
Beispiel #18
0
 /// <summary>
 /// Sends MLSD command, parses result.
 /// </summary>
 /// <param name="ftpClient">The FTP client.</param>
 /// <param name="path">The path.</param>
 /// <returns></returns>
 public static IEnumerable <FtpEntry> MlsdEntries(this FtpClient ftpClient, FtpPath path)
 {
     return(Mlsd(ftpClient, path).Select(m => ParseMlsx(m, path)));
 }
Beispiel #19
0
 /// <summary>
 /// Deletes the specified path.
 /// </summary>
 /// <param name="ftpClient">The FTP client.</param>
 /// <param name="path">The path.</param>
 /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
 /// <returns></returns>
 public static bool Delete(this FtpClient ftpClient, FtpPath path, bool isDirectory)
 {
     return(Delete(ftpClient, path, (bool?)isDirectory));
 }
Beispiel #20
0
 /// <summary>
 /// Sends LIST command.
 /// </summary>
 /// <param name="ftpClient">The FTP client.</param>
 /// <param name="path">The path.</param>
 /// <returns></returns>
 public static IList <string> List(this FtpClient ftpClient, FtpPath path)
 {
     return(ftpClient.Process(handle => ProcessList(handle, path)));
 }
Beispiel #21
0
 /// <summary>
 /// Deletes the specified path.
 /// </summary>
 /// <param name="ftpClient">The FTP client.</param>
 /// <param name="path">The path.</param>
 /// <returns></returns>
 public static bool Delete(this FtpClient ftpClient, FtpPath path)
 {
     return(Delete(ftpClient, path, null));
 }
Beispiel #22
-1
 /// <summary>
 /// Initializes a new instance of the <see cref="FtpEntry"/> class.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="size">The size.</param>
 /// <param name="type">The type.</param>
 /// <param name="date">The date.</param>
 /// <param name="target">The target.</param>
 public FtpEntry(FtpPath path, long? size, FtpEntryType type, DateTime date, FtpPath target)
 {
     Path = path;
     Name = path.GetFileName();
     Date = date;
     Type = type;
     Target = target;
     Size = size;
 }