< Summary

Information
Class: Renci.SshNet.Sftp.SftpFile
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Sftp\SftpFile.cs
Line coverage
32%
Covered lines: 54
Uncovered lines: 114
Coverable lines: 168
Total lines: 524
Line coverage: 32.1%
Branch coverage
50%
Covered branches: 5
Total branches: 10
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Sftp\SftpFile.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using Renci.SshNet.Common;
 4
 5namespace Renci.SshNet.Sftp
 6{
 7    /// <summary>
 8    /// Represents SFTP file information.
 9    /// </summary>
 10    public sealed class SftpFile : ISftpFile
 11    {
 12        private readonly ISftpSession _sftpSession;
 13
 14        /// <summary>
 15        /// Gets the file attributes.
 16        /// </summary>
 1039017        public SftpFileAttributes Attributes { get; private set; }
 18
 19        /// <summary>
 20        /// Initializes a new instance of the <see cref="SftpFile"/> class.
 21        /// </summary>
 22        /// <param name="sftpSession">The SFTP session.</param>
 23        /// <param name="fullName">Full path of the directory or file.</param>
 24        /// <param name="attributes">Attributes of the directory or file.</param>
 25        /// <exception cref="ArgumentNullException"><paramref name="sftpSession"/> or <paramref name="fullName"/> is <se
 1026926        internal SftpFile(ISftpSession sftpSession, string fullName, SftpFileAttributes attributes)
 1026927        {
 1026928            if (sftpSession is null)
 029            {
 030                throw new SshConnectionException("Client not connected.");
 31            }
 32
 1026933            if (attributes is null)
 034            {
 035                throw new ArgumentNullException(nameof(attributes));
 36            }
 37
 1026938            if (fullName is null)
 039            {
 040                throw new ArgumentNullException(nameof(fullName));
 41            }
 42
 1026943            _sftpSession = sftpSession;
 1026944            Attributes = attributes;
 1026945            Name = fullName.Substring(fullName.LastIndexOf('/') + 1);
 1026946            FullName = fullName;
 1026947        }
 48
 49        /// <summary>
 50        /// Gets the full path of the file or directory.
 51        /// </summary>
 52        /// <value>
 53        /// The full path of the file or directory.
 54        /// </value>
 1042355        public string FullName { get; private set; }
 56
 57        /// <summary>
 58        /// Gets the name of the file or directory.
 59        /// </summary>
 60        /// <value>
 61        /// The name of the file or directory.
 62        /// </value>
 63        /// <remarks>
 64        /// For directories, this is the name of the last directory in the hierarchy if a hierarchy exists;
 65        /// otherwise, the name of the directory.
 66        /// </remarks>
 1028367        public string Name { get; private set; }
 68
 69        /// <summary>
 70        /// Gets or sets the time the current file or directory was last accessed.
 71        /// </summary>
 72        /// <value>
 73        /// The time that the current file or directory was last accessed.
 74        /// </value>
 75        public DateTime LastAccessTime
 76        {
 77            get
 478            {
 479                return Attributes.LastAccessTime;
 480            }
 81            set
 082            {
 083                Attributes.LastAccessTime = value;
 084            }
 85        }
 86
 87        /// <summary>
 88        /// Gets or sets the time when the current file or directory was last written to.
 89        /// </summary>
 90        /// <value>
 91        /// The time the current file was last written.
 92        /// </value>
 93        public DateTime LastWriteTime
 94        {
 95            get
 496            {
 497                return Attributes.LastWriteTime;
 498            }
 99            set
 0100            {
 0101                Attributes.LastWriteTime = value;
 0102            }
 103        }
 104
 105        /// <summary>
 106        /// Gets or sets the time, in coordinated universal time (UTC), the current file or directory was last accessed.
 107        /// </summary>
 108        /// <value>
 109        /// The time that the current file or directory was last accessed.
 110        /// </value>
 111        public DateTime LastAccessTimeUtc
 112        {
 113            get
 7114            {
 7115                return Attributes.LastAccessTimeUtc;
 7116            }
 117            set
 0118            {
 0119                Attributes.LastAccessTimeUtc = value;
 0120            }
 121        }
 122
 123        /// <summary>
 124        /// Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last writ
 125        /// </summary>
 126        /// <value>
 127        /// The time the current file was last written.
 128        /// </value>
 129        public DateTime LastWriteTimeUtc
 130        {
 131            get
 22132            {
 22133                return Attributes.LastWriteTimeUtc;
 22134            }
 135            set
 0136            {
 0137                Attributes.LastWriteTimeUtc = value;
 0138            }
 139        }
 140
 141        /// <summary>
 142        /// Gets the size, in bytes, of the current file.
 143        /// </summary>
 144        /// <value>
 145        /// The size of the current file in bytes.
 146        /// </value>
 147        public long Length
 148        {
 149            get
 11150            {
 11151                return Attributes.Size;
 11152            }
 153        }
 154
 155        /// <summary>
 156        /// Gets or sets file user id.
 157        /// </summary>
 158        /// <value>
 159        /// File user id.
 160        /// </value>
 161        public int UserId
 162        {
 163            get
 0164            {
 0165                return Attributes.UserId;
 0166            }
 167            set
 0168            {
 0169                Attributes.UserId = value;
 0170            }
 171        }
 172
 173        /// <summary>
 174        /// Gets or sets file group id.
 175        /// </summary>
 176        /// <value>
 177        /// File group id.
 178        /// </value>
 179        public int GroupId
 180        {
 181            get
 0182            {
 0183                return Attributes.GroupId;
 0184            }
 185            set
 0186            {
 0187                Attributes.GroupId = value;
 0188            }
 189        }
 190
 191        /// <summary>
 192        /// Gets a value indicating whether file represents a socket.
 193        /// </summary>
 194        /// <value>
 195        /// <see langword="true"/> if file represents a socket; otherwise, <see langword="false"/>.
 196        /// </value>
 197        public bool IsSocket
 198        {
 199            get
 0200            {
 0201                return Attributes.IsSocket;
 0202            }
 203        }
 204
 205        /// <summary>
 206        /// Gets a value indicating whether file represents a symbolic link.
 207        /// </summary>
 208        /// <value>
 209        /// <see langword="true"/> if file represents a symbolic link; otherwise, <see langword="false"/>.
 210        /// </value>
 211        public bool IsSymbolicLink
 212        {
 213            get
 0214            {
 0215                return Attributes.IsSymbolicLink;
 0216            }
 217        }
 218
 219        /// <summary>
 220        /// Gets a value indicating whether file represents a regular file.
 221        /// </summary>
 222        /// <value>
 223        /// <see langword="true"/> if file represents a regular file; otherwise, <see langword="false"/>.
 224        /// </value>
 225        public bool IsRegularFile
 226        {
 227            get
 24228            {
 24229                return Attributes.IsRegularFile;
 24230            }
 231        }
 232
 233        /// <summary>
 234        /// Gets a value indicating whether file represents a block device.
 235        /// </summary>
 236        /// <value>
 237        /// <see langword="true"/> if file represents a block device; otherwise, <see langword="false"/>.
 238        /// </value>
 239        public bool IsBlockDevice
 240        {
 241            get
 0242            {
 0243                return Attributes.IsBlockDevice;
 0244            }
 245        }
 246
 247        /// <summary>
 248        /// Gets a value indicating whether file represents a directory.
 249        /// </summary>
 250        /// <value>
 251        /// <see langword="true"/> if file represents a directory; otherwise, <see langword="false"/>.
 252        /// </value>
 253        public bool IsDirectory
 254        {
 255            get
 47256            {
 47257                return Attributes.IsDirectory;
 47258            }
 259        }
 260
 261        /// <summary>
 262        /// Gets a value indicating whether file represents a character device.
 263        /// </summary>
 264        /// <value>
 265        /// <see langword="true"/> if file represents a character device; otherwise, <see langword="false"/>.
 266        /// </value>
 267        public bool IsCharacterDevice
 268        {
 269            get
 0270            {
 0271                return Attributes.IsCharacterDevice;
 0272            }
 273        }
 274
 275        /// <summary>
 276        /// Gets a value indicating whether file represents a named pipe.
 277        /// </summary>
 278        /// <value>
 279        /// <see langword="true"/> if file represents a named pipe; otherwise, <see langword="false"/>.
 280        /// </value>
 281        public bool IsNamedPipe
 282        {
 283            get
 0284            {
 0285                return Attributes.IsNamedPipe;
 0286            }
 287        }
 288
 289        /// <summary>
 290        /// Gets or sets a value indicating whether the owner can read from this file.
 291        /// </summary>
 292        /// <value>
 293        /// <see langword="true"/> if owner can read from this file; otherwise, <see langword="false"/>.
 294        /// </value>
 295        public bool OwnerCanRead
 296        {
 297            get
 0298            {
 0299                return Attributes.OwnerCanRead;
 0300            }
 301            set
 0302            {
 0303                Attributes.OwnerCanRead = value;
 0304            }
 305        }
 306
 307        /// <summary>
 308        /// Gets or sets a value indicating whether the owner can write into this file.
 309        /// </summary>
 310        /// <value>
 311        /// <see langword="true"/> if owner can write into this file; otherwise, <see langword="false"/>.
 312        /// </value>
 313        public bool OwnerCanWrite
 314        {
 315            get
 0316            {
 0317                return Attributes.OwnerCanWrite;
 0318            }
 319            set
 0320            {
 0321                Attributes.OwnerCanWrite = value;
 0322            }
 323        }
 324
 325        /// <summary>
 326        /// Gets or sets a value indicating whether the owner can execute this file.
 327        /// </summary>
 328        /// <value>
 329        /// <see langword="true"/> if owner can execute this file; otherwise, <see langword="false"/>.
 330        /// </value>
 331        public bool OwnerCanExecute
 332        {
 333            get
 0334            {
 0335                return Attributes.OwnerCanExecute;
 0336            }
 337            set
 0338            {
 0339                Attributes.OwnerCanExecute = value;
 0340            }
 341        }
 342
 343        /// <summary>
 344        /// Gets or sets a value indicating whether the group members can read from this file.
 345        /// </summary>
 346        /// <value>
 347        /// <see langword="true"/> if group members can read from this file; otherwise, <see langword="false"/>.
 348        /// </value>
 349        public bool GroupCanRead
 350        {
 351            get
 0352            {
 0353                return Attributes.GroupCanRead;
 0354            }
 355            set
 0356            {
 0357                Attributes.GroupCanRead = value;
 0358            }
 359        }
 360
 361        /// <summary>
 362        /// Gets or sets a value indicating whether the group members can write into this file.
 363        /// </summary>
 364        /// <value>
 365        /// <see langword="true"/> if group members can write into this file; otherwise, <see langword="false"/>.
 366        /// </value>
 367        public bool GroupCanWrite
 368        {
 369            get
 0370            {
 0371                return Attributes.GroupCanWrite;
 0372            }
 373            set
 0374            {
 0375                Attributes.GroupCanWrite = value;
 0376            }
 377        }
 378
 379        /// <summary>
 380        /// Gets or sets a value indicating whether the group members can execute this file.
 381        /// </summary>
 382        /// <value>
 383        /// <see langword="true"/> if group members can execute this file; otherwise, <see langword="false"/>.
 384        /// </value>
 385        public bool GroupCanExecute
 386        {
 387            get
 0388            {
 0389                return Attributes.GroupCanExecute;
 0390            }
 391            set
 0392            {
 0393                Attributes.GroupCanExecute = value;
 0394            }
 395        }
 396
 397        /// <summary>
 398        /// Gets or sets a value indicating whether the others can read from this file.
 399        /// </summary>
 400        /// <value>
 401        /// <see langword="true"/> if others can read from this file; otherwise, <see langword="false"/>.
 402        /// </value>
 403        public bool OthersCanRead
 404        {
 405            get
 0406            {
 0407                return Attributes.OthersCanRead;
 0408            }
 409            set
 0410            {
 0411                Attributes.OthersCanRead = value;
 0412            }
 413        }
 414
 415        /// <summary>
 416        /// Gets or sets a value indicating whether the others can write into this file.
 417        /// </summary>
 418        /// <value>
 419        /// <see langword="true"/> if others can write into this file; otherwise, <see langword="false"/>.
 420        /// </value>
 421        public bool OthersCanWrite
 422        {
 423            get
 0424            {
 0425                return Attributes.OthersCanWrite;
 0426            }
 427            set
 0428            {
 0429                Attributes.OthersCanWrite = value;
 0430            }
 431        }
 432
 433        /// <summary>
 434        /// Gets or sets a value indicating whether the others can execute this file.
 435        /// </summary>
 436        /// <value>
 437        /// <see langword="true"/> if others can execute this file; otherwise, <see langword="false"/>.
 438        /// </value>
 439        public bool OthersCanExecute
 440        {
 441            get
 0442            {
 0443                return Attributes.OthersCanExecute;
 0444            }
 445            set
 0446            {
 0447                Attributes.OthersCanExecute = value;
 0448            }
 449        }
 450
 451        /// <summary>
 452        /// Sets file permissions.
 453        /// </summary>
 454        /// <param name="mode">The mode.</param>
 455        public void SetPermissions(short mode)
 1456        {
 1457            Attributes.SetPermissions(mode);
 458
 1459            UpdateStatus();
 1460        }
 461
 462        /// <summary>
 463        /// Permanently deletes a file on remote machine.
 464        /// </summary>
 465        public void Delete()
 10466        {
 10467            if (IsDirectory)
 0468            {
 0469                _sftpSession.RequestRmDir(FullName);
 0470            }
 471            else
 10472            {
 10473                _sftpSession.RequestRemove(FullName);
 10474            }
 10475        }
 476
 477        /// <summary>
 478        /// Moves a specified file to a new location on remote machine, providing the option to specify a new file name.
 479        /// </summary>
 480        /// <param name="destFileName">The path to move the file to, which can specify a different file name.</param>
 481        /// <exception cref="ArgumentNullException"><paramref name="destFileName"/> is <see langword="null"/>.</exceptio
 482        public void MoveTo(string destFileName)
 1483        {
 1484            if (destFileName is null)
 0485            {
 0486                throw new ArgumentNullException(nameof(destFileName));
 487            }
 488
 1489            _sftpSession.RequestRename(FullName, destFileName);
 490
 1491            var fullPath = _sftpSession.GetCanonicalPath(destFileName);
 492
 1493            Name = fullPath.Substring(fullPath.LastIndexOf('/') + 1);
 494
 1495            FullName = fullPath;
 1496        }
 497
 498        /// <summary>
 499        /// Updates file status on the server.
 500        /// </summary>
 501        public void UpdateStatus()
 1502        {
 1503            _sftpSession.RequestSetStat(FullName, Attributes);
 1504        }
 505
 506        /// <summary>
 507        /// Returns a <see cref="string"/> that represents this instance.
 508        /// </summary>
 509        /// <returns>
 510        /// A <see cref="string"/> that represents this instance.
 511        /// </returns>
 512        public override string ToString()
 0513        {
 0514            return string.Format(CultureInfo.CurrentCulture,
 0515                                 "Name {0}, Length {1}, User ID {2}, Group ID {3}, Accessed {4}, Modified {5}",
 0516                                 Name,
 0517                                 Length,
 0518                                 UserId,
 0519                                 GroupId,
 0520                                 LastAccessTime,
 0521                                 LastWriteTime);
 0522        }
 523    }
 524}