| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using Renci.SshNet.Common; |
| | | 4 | | |
| | | 5 | | namespace 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> |
| | 10390 | 17 | | 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 |
| | 10269 | 26 | | internal SftpFile(ISftpSession sftpSession, string fullName, SftpFileAttributes attributes) |
| | 10269 | 27 | | { |
| | 10269 | 28 | | if (sftpSession is null) |
| | 0 | 29 | | { |
| | 0 | 30 | | throw new SshConnectionException("Client not connected."); |
| | | 31 | | } |
| | | 32 | | |
| | 10269 | 33 | | if (attributes is null) |
| | 0 | 34 | | { |
| | 0 | 35 | | throw new ArgumentNullException(nameof(attributes)); |
| | | 36 | | } |
| | | 37 | | |
| | 10269 | 38 | | if (fullName is null) |
| | 0 | 39 | | { |
| | 0 | 40 | | throw new ArgumentNullException(nameof(fullName)); |
| | | 41 | | } |
| | | 42 | | |
| | 10269 | 43 | | _sftpSession = sftpSession; |
| | 10269 | 44 | | Attributes = attributes; |
| | 10269 | 45 | | Name = fullName.Substring(fullName.LastIndexOf('/') + 1); |
| | 10269 | 46 | | FullName = fullName; |
| | 10269 | 47 | | } |
| | | 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> |
| | 10423 | 55 | | 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> |
| | 10283 | 67 | | 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 |
| | 4 | 78 | | { |
| | 4 | 79 | | return Attributes.LastAccessTime; |
| | 4 | 80 | | } |
| | | 81 | | set |
| | 0 | 82 | | { |
| | 0 | 83 | | Attributes.LastAccessTime = value; |
| | 0 | 84 | | } |
| | | 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 |
| | 4 | 96 | | { |
| | 4 | 97 | | return Attributes.LastWriteTime; |
| | 4 | 98 | | } |
| | | 99 | | set |
| | 0 | 100 | | { |
| | 0 | 101 | | Attributes.LastWriteTime = value; |
| | 0 | 102 | | } |
| | | 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 |
| | 7 | 114 | | { |
| | 7 | 115 | | return Attributes.LastAccessTimeUtc; |
| | 7 | 116 | | } |
| | | 117 | | set |
| | 0 | 118 | | { |
| | 0 | 119 | | Attributes.LastAccessTimeUtc = value; |
| | 0 | 120 | | } |
| | | 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 |
| | 22 | 132 | | { |
| | 22 | 133 | | return Attributes.LastWriteTimeUtc; |
| | 22 | 134 | | } |
| | | 135 | | set |
| | 0 | 136 | | { |
| | 0 | 137 | | Attributes.LastWriteTimeUtc = value; |
| | 0 | 138 | | } |
| | | 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 |
| | 11 | 150 | | { |
| | 11 | 151 | | return Attributes.Size; |
| | 11 | 152 | | } |
| | | 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 |
| | 0 | 164 | | { |
| | 0 | 165 | | return Attributes.UserId; |
| | 0 | 166 | | } |
| | | 167 | | set |
| | 0 | 168 | | { |
| | 0 | 169 | | Attributes.UserId = value; |
| | 0 | 170 | | } |
| | | 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 |
| | 0 | 182 | | { |
| | 0 | 183 | | return Attributes.GroupId; |
| | 0 | 184 | | } |
| | | 185 | | set |
| | 0 | 186 | | { |
| | 0 | 187 | | Attributes.GroupId = value; |
| | 0 | 188 | | } |
| | | 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 |
| | 0 | 200 | | { |
| | 0 | 201 | | return Attributes.IsSocket; |
| | 0 | 202 | | } |
| | | 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 |
| | 0 | 214 | | { |
| | 0 | 215 | | return Attributes.IsSymbolicLink; |
| | 0 | 216 | | } |
| | | 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 |
| | 24 | 228 | | { |
| | 24 | 229 | | return Attributes.IsRegularFile; |
| | 24 | 230 | | } |
| | | 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 |
| | 0 | 242 | | { |
| | 0 | 243 | | return Attributes.IsBlockDevice; |
| | 0 | 244 | | } |
| | | 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 |
| | 47 | 256 | | { |
| | 47 | 257 | | return Attributes.IsDirectory; |
| | 47 | 258 | | } |
| | | 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 |
| | 0 | 270 | | { |
| | 0 | 271 | | return Attributes.IsCharacterDevice; |
| | 0 | 272 | | } |
| | | 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 |
| | 0 | 284 | | { |
| | 0 | 285 | | return Attributes.IsNamedPipe; |
| | 0 | 286 | | } |
| | | 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 |
| | 0 | 298 | | { |
| | 0 | 299 | | return Attributes.OwnerCanRead; |
| | 0 | 300 | | } |
| | | 301 | | set |
| | 0 | 302 | | { |
| | 0 | 303 | | Attributes.OwnerCanRead = value; |
| | 0 | 304 | | } |
| | | 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 |
| | 0 | 316 | | { |
| | 0 | 317 | | return Attributes.OwnerCanWrite; |
| | 0 | 318 | | } |
| | | 319 | | set |
| | 0 | 320 | | { |
| | 0 | 321 | | Attributes.OwnerCanWrite = value; |
| | 0 | 322 | | } |
| | | 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 |
| | 0 | 334 | | { |
| | 0 | 335 | | return Attributes.OwnerCanExecute; |
| | 0 | 336 | | } |
| | | 337 | | set |
| | 0 | 338 | | { |
| | 0 | 339 | | Attributes.OwnerCanExecute = value; |
| | 0 | 340 | | } |
| | | 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 |
| | 0 | 352 | | { |
| | 0 | 353 | | return Attributes.GroupCanRead; |
| | 0 | 354 | | } |
| | | 355 | | set |
| | 0 | 356 | | { |
| | 0 | 357 | | Attributes.GroupCanRead = value; |
| | 0 | 358 | | } |
| | | 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 |
| | 0 | 370 | | { |
| | 0 | 371 | | return Attributes.GroupCanWrite; |
| | 0 | 372 | | } |
| | | 373 | | set |
| | 0 | 374 | | { |
| | 0 | 375 | | Attributes.GroupCanWrite = value; |
| | 0 | 376 | | } |
| | | 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 |
| | 0 | 388 | | { |
| | 0 | 389 | | return Attributes.GroupCanExecute; |
| | 0 | 390 | | } |
| | | 391 | | set |
| | 0 | 392 | | { |
| | 0 | 393 | | Attributes.GroupCanExecute = value; |
| | 0 | 394 | | } |
| | | 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 |
| | 0 | 406 | | { |
| | 0 | 407 | | return Attributes.OthersCanRead; |
| | 0 | 408 | | } |
| | | 409 | | set |
| | 0 | 410 | | { |
| | 0 | 411 | | Attributes.OthersCanRead = value; |
| | 0 | 412 | | } |
| | | 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 |
| | 0 | 424 | | { |
| | 0 | 425 | | return Attributes.OthersCanWrite; |
| | 0 | 426 | | } |
| | | 427 | | set |
| | 0 | 428 | | { |
| | 0 | 429 | | Attributes.OthersCanWrite = value; |
| | 0 | 430 | | } |
| | | 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 |
| | 0 | 442 | | { |
| | 0 | 443 | | return Attributes.OthersCanExecute; |
| | 0 | 444 | | } |
| | | 445 | | set |
| | 0 | 446 | | { |
| | 0 | 447 | | Attributes.OthersCanExecute = value; |
| | 0 | 448 | | } |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | /// <summary> |
| | | 452 | | /// Sets file permissions. |
| | | 453 | | /// </summary> |
| | | 454 | | /// <param name="mode">The mode.</param> |
| | | 455 | | public void SetPermissions(short mode) |
| | 1 | 456 | | { |
| | 1 | 457 | | Attributes.SetPermissions(mode); |
| | | 458 | | |
| | 1 | 459 | | UpdateStatus(); |
| | 1 | 460 | | } |
| | | 461 | | |
| | | 462 | | /// <summary> |
| | | 463 | | /// Permanently deletes a file on remote machine. |
| | | 464 | | /// </summary> |
| | | 465 | | public void Delete() |
| | 10 | 466 | | { |
| | 10 | 467 | | if (IsDirectory) |
| | 0 | 468 | | { |
| | 0 | 469 | | _sftpSession.RequestRmDir(FullName); |
| | 0 | 470 | | } |
| | | 471 | | else |
| | 10 | 472 | | { |
| | 10 | 473 | | _sftpSession.RequestRemove(FullName); |
| | 10 | 474 | | } |
| | 10 | 475 | | } |
| | | 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) |
| | 1 | 483 | | { |
| | 1 | 484 | | if (destFileName is null) |
| | 0 | 485 | | { |
| | 0 | 486 | | throw new ArgumentNullException(nameof(destFileName)); |
| | | 487 | | } |
| | | 488 | | |
| | 1 | 489 | | _sftpSession.RequestRename(FullName, destFileName); |
| | | 490 | | |
| | 1 | 491 | | var fullPath = _sftpSession.GetCanonicalPath(destFileName); |
| | | 492 | | |
| | 1 | 493 | | Name = fullPath.Substring(fullPath.LastIndexOf('/') + 1); |
| | | 494 | | |
| | 1 | 495 | | FullName = fullPath; |
| | 1 | 496 | | } |
| | | 497 | | |
| | | 498 | | /// <summary> |
| | | 499 | | /// Updates file status on the server. |
| | | 500 | | /// </summary> |
| | | 501 | | public void UpdateStatus() |
| | 1 | 502 | | { |
| | 1 | 503 | | _sftpSession.RequestSetStat(FullName, Attributes); |
| | 1 | 504 | | } |
| | | 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() |
| | 0 | 513 | | { |
| | 0 | 514 | | return string.Format(CultureInfo.CurrentCulture, |
| | 0 | 515 | | "Name {0}, Length {1}, User ID {2}, Group ID {3}, Accessed {4}, Modified {5}", |
| | 0 | 516 | | Name, |
| | 0 | 517 | | Length, |
| | 0 | 518 | | UserId, |
| | 0 | 519 | | GroupId, |
| | 0 | 520 | | LastAccessTime, |
| | 0 | 521 | | LastWriteTime); |
| | 0 | 522 | | } |
| | | 523 | | } |
| | | 524 | | } |