| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- using System;
- using System.Globalization;
- using Renci.SshNet.Common;
- namespace Renci.SshNet.Sftp
- {
- /// <summary>
- /// Represents SFTP file information
- /// </summary>
- public class SftpFile
- {
- private readonly ISftpSession _sftpSession;
- /// <summary>
- /// Gets the file attributes.
- /// </summary>
- public SftpFileAttributes Attributes { get; private set; }
- /// <summary>
- /// Initializes a new instance of the <see cref="SftpFile"/> class.
- /// </summary>
- /// <param name="sftpSession">The SFTP session.</param>
- /// <param name="fullName">Full path of the directory or file.</param>
- /// <param name="attributes">Attributes of the directory or file.</param>
- /// <exception cref="ArgumentNullException"><paramref name="sftpSession"/> or <paramref name="fullName"/> is null.</exception>
- internal SftpFile(ISftpSession sftpSession, string fullName, SftpFileAttributes attributes)
- {
- if (sftpSession == null)
- throw new SshConnectionException("Client not connected.");
- if (attributes == null)
- throw new ArgumentNullException("attributes");
- if (fullName == null)
- throw new ArgumentNullException("fullName");
- _sftpSession = sftpSession;
- Attributes = attributes;
- Name = fullName.Substring(fullName.LastIndexOf('/') + 1);
- FullName = fullName;
- }
- /// <summary>
- /// Gets the full path of the directory or file.
- /// </summary>
- public string FullName { get; private set; }
- /// <summary>
- /// For files, gets the name of the file. For directories, gets the name of the last directory in the hierarchy if a hierarchy exists.
- /// Otherwise, the Name property gets the name of the directory.
- /// </summary>
- public string Name { get; private set; }
- /// <summary>
- /// Gets or sets the time the current file or directory was last accessed.
- /// </summary>
- /// <value>
- /// The time that the current file or directory was last accessed.
- /// </value>
- public DateTime LastAccessTime
- {
- get
- {
- return Attributes.LastAccessTime;
- }
- set
- {
- Attributes.LastAccessTime = value;
- }
- }
- /// <summary>
- /// Gets or sets the time when the current file or directory was last written to.
- /// </summary>
- /// <value>
- /// The time the current file was last written.
- /// </value>
- public DateTime LastWriteTime
- {
- get
- {
- return Attributes.LastWriteTime;
- }
- set
- {
- Attributes.LastWriteTime = value;
- }
- }
- /// <summary>
- /// Gets or sets the time, in coordinated universal time (UTC), the current file or directory was last accessed.
- /// </summary>
- /// <value>
- /// The time that the current file or directory was last accessed.
- /// </value>
- public DateTime LastAccessTimeUtc
- {
- get
- {
- return Attributes.LastAccessTime.ToUniversalTime();
- }
- set
- {
- Attributes.LastAccessTime = value.ToLocalTime();
- }
- }
- /// <summary>
- /// Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.
- /// </summary>
- /// <value>
- /// The time the current file was last written.
- /// </value>
- public DateTime LastWriteTimeUtc
- {
- get
- {
- return Attributes.LastWriteTime.ToUniversalTime();
- }
- set
- {
- Attributes.LastWriteTime = value.ToLocalTime();
- }
- }
- /// <summary>
- /// Gets or sets the size, in bytes, of the current file.
- /// </summary>
- /// <value>
- /// The size of the current file in bytes.
- /// </value>
- public long Length
- {
- get
- {
- return Attributes.Size;
- }
- }
- /// <summary>
- /// Gets or sets file user id.
- /// </summary>
- /// <value>
- /// File user id.
- /// </value>
- public int UserId
- {
- get
- {
- return Attributes.UserId;
- }
- set
- {
- Attributes.UserId = value;
- }
- }
- /// <summary>
- /// Gets or sets file group id.
- /// </summary>
- /// <value>
- /// File group id.
- /// </value>
- public int GroupId
- {
- get
- {
- return Attributes.GroupId;
- }
- set
- {
- Attributes.GroupId = value;
- }
- }
- /// <summary>
- /// Gets a value indicating whether file represents a socket.
- /// </summary>
- /// <value>
- /// <c>true</c> if file represents a socket; otherwise, <c>false</c>.
- /// </value>
- public bool IsSocket
- {
- get
- {
- return Attributes.IsSocket;
- }
- }
- /// <summary>
- /// Gets a value indicating whether file represents a symbolic link.
- /// </summary>
- /// <value>
- /// <c>true</c> if file represents a symbolic link; otherwise, <c>false</c>.
- /// </value>
- public bool IsSymbolicLink
- {
- get
- {
- return Attributes.IsSymbolicLink;
- }
- }
- /// <summary>
- /// Gets a value indicating whether file represents a regular file.
- /// </summary>
- /// <value>
- /// <c>true</c> if file represents a regular file; otherwise, <c>false</c>.
- /// </value>
- public bool IsRegularFile
- {
- get
- {
- return Attributes.IsRegularFile;
- }
- }
- /// <summary>
- /// Gets a value indicating whether file represents a block device.
- /// </summary>
- /// <value>
- /// <c>true</c> if file represents a block device; otherwise, <c>false</c>.
- /// </value>
- public bool IsBlockDevice
- {
- get
- {
- return Attributes.IsBlockDevice;
- }
- }
- /// <summary>
- /// Gets a value indicating whether file represents a directory.
- /// </summary>
- /// <value>
- /// <c>true</c> if file represents a directory; otherwise, <c>false</c>.
- /// </value>
- public bool IsDirectory
- {
- get
- {
- return Attributes.IsDirectory;
- }
- }
- /// <summary>
- /// Gets a value indicating whether file represents a character device.
- /// </summary>
- /// <value>
- /// <c>true</c> if file represents a character device; otherwise, <c>false</c>.
- /// </value>
- public bool IsCharacterDevice
- {
- get
- {
- return Attributes.IsCharacterDevice;
- }
- }
- /// <summary>
- /// Gets a value indicating whether file represents a named pipe.
- /// </summary>
- /// <value>
- /// <c>true</c> if file represents a named pipe; otherwise, <c>false</c>.
- /// </value>
- public bool IsNamedPipe
- {
- get
- {
- return Attributes.IsNamedPipe;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the owner can read from this file.
- /// </summary>
- /// <value>
- /// <c>true</c> if owner can read from this file; otherwise, <c>false</c>.
- /// </value>
- public bool OwnerCanRead
- {
- get
- {
- return Attributes.OwnerCanRead;
- }
- set
- {
- Attributes.OwnerCanRead = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the owner can write into this file.
- /// </summary>
- /// <value>
- /// <c>true</c> if owner can write into this file; otherwise, <c>false</c>.
- /// </value>
- public bool OwnerCanWrite
- {
- get
- {
- return Attributes.OwnerCanWrite;
- }
- set
- {
- Attributes.OwnerCanWrite = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the owner can execute this file.
- /// </summary>
- /// <value>
- /// <c>true</c> if owner can execute this file; otherwise, <c>false</c>.
- /// </value>
- public bool OwnerCanExecute
- {
- get
- {
- return Attributes.OwnerCanExecute;
- }
- set
- {
- Attributes.OwnerCanExecute = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the group members can read from this file.
- /// </summary>
- /// <value>
- /// <c>true</c> if group members can read from this file; otherwise, <c>false</c>.
- /// </value>
- public bool GroupCanRead
- {
- get
- {
- return Attributes.GroupCanRead;
- }
- set
- {
- Attributes.GroupCanRead = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the group members can write into this file.
- /// </summary>
- /// <value>
- /// <c>true</c> if group members can write into this file; otherwise, <c>false</c>.
- /// </value>
- public bool GroupCanWrite
- {
- get
- {
- return Attributes.GroupCanWrite;
- }
- set
- {
- Attributes.GroupCanWrite = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the group members can execute this file.
- /// </summary>
- /// <value>
- /// <c>true</c> if group members can execute this file; otherwise, <c>false</c>.
- /// </value>
- public bool GroupCanExecute
- {
- get
- {
- return Attributes.GroupCanExecute;
- }
- set
- {
- Attributes.GroupCanExecute = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the others can read from this file.
- /// </summary>
- /// <value>
- /// <c>true</c> if others can read from this file; otherwise, <c>false</c>.
- /// </value>
- public bool OthersCanRead
- {
- get
- {
- return Attributes.OthersCanRead;
- }
- set
- {
- Attributes.OthersCanRead = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the others can write into this file.
- /// </summary>
- /// <value>
- /// <c>true</c> if others can write into this file; otherwise, <c>false</c>.
- /// </value>
- public bool OthersCanWrite
- {
- get
- {
- return Attributes.OthersCanWrite;
- }
- set
- {
- Attributes.OthersCanWrite = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the others can execute this file.
- /// </summary>
- /// <value>
- /// <c>true</c> if others can execute this file; otherwise, <c>false</c>.
- /// </value>
- public bool OthersCanExecute
- {
- get
- {
- return Attributes.OthersCanExecute;
- }
- set
- {
- Attributes.OthersCanExecute = value;
- }
- }
- /// <summary>
- /// Sets file permissions.
- /// </summary>
- /// <param name="mode">The mode.</param>
- public void SetPermissions(short mode)
- {
- Attributes.SetPermissions(mode);
- UpdateStatus();
- }
- /// <summary>
- /// Permanently deletes a file on remote machine.
- /// </summary>
- public void Delete()
- {
- if (IsDirectory)
- {
- _sftpSession.RequestRmDir(FullName);
- }
- else
- {
- _sftpSession.RequestRemove(FullName);
- }
- }
- /// <summary>
- /// Moves a specified file to a new location on remote machine, providing the option to specify a new file name.
- /// </summary>
- /// <param name="destFileName">The path to move the file to, which can specify a different file name.</param>
- /// <exception cref="ArgumentNullException"><paramref name="destFileName"/> is null.</exception>
- public void MoveTo(string destFileName)
- {
- if (destFileName == null)
- throw new ArgumentNullException("destFileName");
- _sftpSession.RequestRename(FullName, destFileName);
- var fullPath = _sftpSession.GetCanonicalPath(destFileName);
- Name = fullPath.Substring(fullPath.LastIndexOf('/') + 1);
- FullName = fullPath;
- }
- /// <summary>
- /// Updates file status on the server.
- /// </summary>
- public void UpdateStatus()
- {
- _sftpSession.RequestSetStat(FullName, Attributes);
- }
- /// <summary>
- /// Returns a <see cref="System.String"/> that represents this instance.
- /// </summary>
- /// <returns>
- /// A <see cref="System.String"/> that represents this instance.
- /// </returns>
- public override string ToString()
- {
- return string.Format(CultureInfo.CurrentCulture, "Name {0}, Length {1}, User ID {2}, Group ID {3}, Accessed {4}, Modified {5}", Name, Length, UserId, GroupId, LastAccessTime, LastWriteTime);
- }
- }
- }
|