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