using System; using System.Net; using System.IO; namespace mooftpserv { /// /// File system entry as returned by List. /// public struct FileSystemEntry { public string Name; public bool IsDirectory; public long Size; public DateTime LastModifiedTimeUtc; public string Mode; } /// /// Wrapper that either contains a value or an error string. /// public class ResultOrError { private T result; private string error; private ResultOrError(T result, string error) { this.result = result; this.error = error; } public static ResultOrError MakeResult(T result) { return new ResultOrError(result, null); } public static ResultOrError MakeError(string error) { if (error == null) throw new ArgumentNullException(); return new ResultOrError(default(T), error.Replace(Environment.NewLine, " ")); } public bool HasError { get { return error != null; } } public string Error { get { return error; } } public T Result { get { if (HasError) throw new InvalidOperationException(String.Format("No result available, error: {0}", error)); return result; } } }; /// /// Interface for file system access from FTP. /// public interface IFileSystemHandler { /// /// Make a new instance for a new session with the given peer. /// Each FTP session uses a separate, cloned instance. /// IFileSystemHandler Clone(IPEndPoint peer); /// /// PWD: Returns the path of the current working directory. /// /// /// The absolute path of the current directory or an error string. /// ResultOrError GetCurrentDirectory(); /// /// CWD: Changes the current directory. /// CDUP: Changes to parent directory (called with "..") /// /// /// The new absolute path or an error string. /// /// /// A relative or absolute path to which to change. /// ResultOrError ChangeDirectory(string path); /// /// MKD: Create a directory. /// /// /// The absolute path of the created directory or an error string. /// /// /// A relative or absolute path for the new directory. /// ResultOrError CreateDirectory(string path); /// /// RMD: Remove a directory. /// /// /// A bool or an error string. The bool is not actually used. /// /// /// A relative or absolute path for the directory. /// ResultOrError RemoveDirectory(string path); /// /// RETR: Open a stream for reading the specified file. /// /// /// An opened stream for reading from the file, or an error string. /// /// /// A relative or absolute path for the file. /// ResultOrError ReadFile(string path); /// /// STOR: Open a stream for writing to the specified file. /// If the file exists, it should be overwritten. /// /// /// An opened stream for writing to the file, or an error string. /// /// /// A relative or absolute path for the file. /// ResultOrError WriteFile(string path); ResultOrError WriteFileFinalize(string path, Stream stream); /// /// DELE: Deletes a file. /// /// /// A bool or an error string. The bool is not actually used. /// /// /// A relative or absolute path for the file. /// ResultOrError RemoveFile(string path); /// /// RNFR, RNTO: Renames or moves a file or directory. /// /// /// A bool or an error string. The bool is not actually used. /// /// /// The relative or absolute path of an existing file or directory. /// /// /// A relative or absolute non-existing path to which the file will be renamed or moved. /// ResultOrError RenameFile(string fromPath, string toPath); /// /// LIST: Return a list of files and folders in a directory, or for a file (like 'ls'). /// /// /// The relative or absolute path of an existing directory or file. /// Can be null or empty to return the current directory. /// /// /// An array of file system entries or an error string. /// ResultOrError ListEntries(string path); /// /// LIST: Return a raw output of 'ls' command. /// /// /// The relative or absolute path of an existing directory or file. /// Can be null or empty to return the current directory. /// /// /// Raw output of 'ls' command /// ResultOrError ListEntriesRaw(string path); /// /// SIZE: Gets the size of a file in bytes. /// /// /// The file size, or -1 on error. /// /// /// A relative or absolute path. /// ResultOrError GetFileSize(string path); /// /// MDTM: Gets the last modified timestamp of a file. /// /// /// The last modified time in UTC, or an error string. /// /// /// A relative or absolute path. /// ResultOrError GetLastModifiedTimeUtc(string path); ResultOrError SetLastModifiedTimeUtc(string path, DateTime time); ResultOrError ChmodFile(string mode, string path); } }