| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Linq; |
| | | 5 | | |
| | | 6 | | using Renci.SshNet.Common; |
| | | 7 | | |
| | | 8 | | namespace Renci.SshNet.Sftp |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Contains SFTP file attributes. |
| | | 12 | | /// </summary> |
| | | 13 | | public class SftpFileAttributes |
| | | 14 | | { |
| | | 15 | | #pragma warning disable IDE1006 // Naming Styles |
| | | 16 | | #pragma warning disable SA1310 // Field names should not contain underscore |
| | | 17 | | private const uint S_IFMT = 0xF000; // bitmask for the file type bitfields |
| | | 18 | | private const uint S_IFSOCK = 0xC000; // socket |
| | | 19 | | private const uint S_IFLNK = 0xA000; // symbolic link |
| | | 20 | | private const uint S_IFREG = 0x8000; // regular file |
| | | 21 | | private const uint S_IFBLK = 0x6000; // block device |
| | | 22 | | private const uint S_IFDIR = 0x4000; // directory |
| | | 23 | | private const uint S_IFCHR = 0x2000; // character device |
| | | 24 | | private const uint S_IFIFO = 0x1000; // FIFO |
| | | 25 | | private const uint S_ISUID = 0x0800; // set UID bit |
| | | 26 | | private const uint S_ISGID = 0x0400; // set-group-ID bit (see below) |
| | | 27 | | private const uint S_ISVTX = 0x0200; // sticky bit (see below) |
| | | 28 | | private const uint S_IRUSR = 0x0100; // owner has read permission |
| | | 29 | | private const uint S_IWUSR = 0x0080; // owner has write permission |
| | | 30 | | private const uint S_IXUSR = 0x0040; // owner has execute permission |
| | | 31 | | private const uint S_IRGRP = 0x0020; // group has read permission |
| | | 32 | | private const uint S_IWGRP = 0x0010; // group has write permission |
| | | 33 | | private const uint S_IXGRP = 0x0008; // group has execute permission |
| | | 34 | | private const uint S_IROTH = 0x0004; // others have read permission |
| | | 35 | | private const uint S_IWOTH = 0x0002; // others have write permission |
| | | 36 | | private const uint S_IXOTH = 0x0001; // others have execute permission |
| | | 37 | | #pragma warning restore SA1310 // Field names should not contain underscore |
| | | 38 | | #pragma warning restore IDE1006 // Naming Styles |
| | | 39 | | |
| | | 40 | | private readonly DateTime _originalLastAccessTimeUtc; |
| | | 41 | | private readonly DateTime _originalLastWriteTimeUtc; |
| | | 42 | | private readonly long _originalSize; |
| | | 43 | | private readonly int _originalUserId; |
| | | 44 | | private readonly int _originalGroupId; |
| | | 45 | | private readonly uint _originalPermissions; |
| | | 46 | | private readonly IDictionary<string, string> _originalExtensions; |
| | | 47 | | |
| | | 48 | | private bool _isBitFiledsBitSet; |
| | | 49 | | private bool _isUIDBitSet; |
| | | 50 | | private bool _isGroupIDBitSet; |
| | | 51 | | private bool _isStickyBitSet; |
| | | 52 | | |
| | | 53 | | internal bool IsLastAccessTimeChanged |
| | | 54 | | { |
| | 63372 | 55 | | get { return _originalLastAccessTimeUtc != LastAccessTimeUtc; } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | internal bool IsLastWriteTimeChanged |
| | | 59 | | { |
| | 62982 | 60 | | get { return _originalLastWriteTimeUtc != LastWriteTimeUtc; } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | internal bool IsSizeChanged |
| | | 64 | | { |
| | 63372 | 65 | | get { return _originalSize != Size; } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | internal bool IsUserIdChanged |
| | | 69 | | { |
| | 63372 | 70 | | get { return _originalUserId != UserId; } |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | internal bool IsGroupIdChanged |
| | | 74 | | { |
| | 62994 | 75 | | get { return _originalGroupId != GroupId; } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | internal bool IsPermissionsChanged |
| | | 79 | | { |
| | 63372 | 80 | | get { return _originalPermissions != Permissions; } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | internal bool IsExtensionsChanged |
| | | 84 | | { |
| | 63372 | 85 | | get { return _originalExtensions != null && Extensions != null && !_originalExtensions.SequenceEqual(Extensi |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets or sets the local time the current file or directory was last accessed. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <value> |
| | | 92 | | /// The local time that the current file or directory was last accessed. |
| | | 93 | | /// </value> |
| | | 94 | | public DateTime LastAccessTime |
| | | 95 | | { |
| | | 96 | | get |
| | 28 | 97 | | { |
| | 28 | 98 | | return ToLocalTime(LastAccessTimeUtc); |
| | 28 | 99 | | } |
| | | 100 | | |
| | | 101 | | set |
| | 4 | 102 | | { |
| | 4 | 103 | | LastAccessTimeUtc = ToUniversalTime(value); |
| | 4 | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Gets or sets the local time when the current file or directory was last written to. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <value> |
| | | 111 | | /// The local time the current file was last written. |
| | | 112 | | /// </value> |
| | | 113 | | public DateTime LastWriteTime |
| | | 114 | | { |
| | | 115 | | get |
| | 40 | 116 | | { |
| | 40 | 117 | | return ToLocalTime(LastWriteTimeUtc); |
| | 40 | 118 | | } |
| | | 119 | | |
| | | 120 | | set |
| | 4 | 121 | | { |
| | 4 | 122 | | LastWriteTimeUtc = ToUniversalTime(value); |
| | 4 | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Gets or sets the UTC time the current file or directory was last accessed. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <value> |
| | | 130 | | /// The UTC time that the current file or directory was last accessed. |
| | | 131 | | /// </value> |
| | 43816 | 132 | | public DateTime LastAccessTimeUtc { get; set; } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets or sets the UTC time when the current file or directory was last written to. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <value> |
| | | 138 | | /// The UTC time the current file was last written. |
| | | 139 | | /// </value> |
| | 43713 | 140 | | public DateTime LastWriteTimeUtc { get; set; } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Gets or sets the size, in bytes, of the current file. |
| | | 144 | | /// </summary> |
| | | 145 | | /// <value> |
| | | 146 | | /// The size of the current file in bytes. |
| | | 147 | | /// </value> |
| | 44279 | 148 | | public long Size { get; set; } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Gets or sets file user id. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <value> |
| | | 154 | | /// File user id. |
| | | 155 | | /// </value> |
| | 43805 | 156 | | public int UserId { get; set; } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Gets or sets file group id. |
| | | 160 | | /// </summary> |
| | | 161 | | /// <value> |
| | | 162 | | /// File group id. |
| | | 163 | | /// </value> |
| | 43679 | 164 | | public int GroupId { get; set; } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Gets a value indicating whether file represents a socket. |
| | | 168 | | /// </summary> |
| | | 169 | | /// <value> |
| | | 170 | | /// <see langword="true"/> if file represents a socket; otherwise, <see langword="false"/>. |
| | | 171 | | /// </value> |
| | 43734 | 172 | | public bool IsSocket { get; private set; } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Gets a value indicating whether file represents a symbolic link. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <value> |
| | | 178 | | /// <see langword="true"/> if file represents a symbolic link; otherwise, <see langword="false"/>. |
| | | 179 | | /// </value> |
| | 43734 | 180 | | public bool IsSymbolicLink { get; private set; } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Gets a value indicating whether file represents a regular file. |
| | | 184 | | /// </summary> |
| | | 185 | | /// <value> |
| | | 186 | | /// <see langword="true"/> if file represents a regular file; otherwise, <see langword="false"/>. |
| | | 187 | | /// </value> |
| | 43997 | 188 | | public bool IsRegularFile { get; private set; } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Gets a value indicating whether file represents a block device. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <value> |
| | | 194 | | /// <see langword="true"/> if file represents a block device; otherwise, <see langword="false"/>. |
| | | 195 | | /// </value> |
| | 43734 | 196 | | public bool IsBlockDevice { get; private set; } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Gets a value indicating whether file represents a directory. |
| | | 200 | | /// </summary> |
| | | 201 | | /// <value> |
| | | 202 | | /// <see langword="true"/> if file represents a directory; otherwise, <see langword="false"/>. |
| | | 203 | | /// </value> |
| | 43781 | 204 | | public bool IsDirectory { get; private set; } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Gets a value indicating whether file represents a character device. |
| | | 208 | | /// </summary> |
| | | 209 | | /// <value> |
| | | 210 | | /// <see langword="true"/> if file represents a character device; otherwise, <see langword="false"/>. |
| | | 211 | | /// </value> |
| | 43734 | 212 | | public bool IsCharacterDevice { get; private set; } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Gets a value indicating whether file represents a named pipe. |
| | | 216 | | /// </summary> |
| | | 217 | | /// <value> |
| | | 218 | | /// <see langword="true"/> if file represents a named pipe; otherwise, <see langword="false"/>. |
| | | 219 | | /// </value> |
| | 43734 | 220 | | public bool IsNamedPipe { get; private set; } |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// Gets or sets a value indicating whether the owner can read from this file. |
| | | 224 | | /// </summary> |
| | | 225 | | /// <value> |
| | | 226 | | /// <see langword="true"/> if owner can read from this file; otherwise, <see langword="false"/>. |
| | | 227 | | /// </value> |
| | 43735 | 228 | | public bool OwnerCanRead { get; set; } |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Gets or sets a value indicating whether the owner can write into this file. |
| | | 232 | | /// </summary> |
| | | 233 | | /// <value> |
| | | 234 | | /// <see langword="true"/> if owner can write into this file; otherwise, <see langword="false"/>. |
| | | 235 | | /// </value> |
| | 43735 | 236 | | public bool OwnerCanWrite { get; set; } |
| | | 237 | | |
| | | 238 | | /// <summary> |
| | | 239 | | /// Gets or sets a value indicating whether the owner can execute this file. |
| | | 240 | | /// </summary> |
| | | 241 | | /// <value> |
| | | 242 | | /// <see langword="true"/> if owner can execute this file; otherwise, <see langword="false"/>. |
| | | 243 | | /// </value> |
| | 43735 | 244 | | public bool OwnerCanExecute { get; set; } |
| | | 245 | | |
| | | 246 | | /// <summary> |
| | | 247 | | /// Gets or sets a value indicating whether the group members can read from this file. |
| | | 248 | | /// </summary> |
| | | 249 | | /// <value> |
| | | 250 | | /// <see langword="true"/> if group members can read from this file; otherwise, <see langword="false"/>. |
| | | 251 | | /// </value> |
| | 43735 | 252 | | public bool GroupCanRead { get; set; } |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// Gets or sets a value indicating whether the group members can write into this file. |
| | | 256 | | /// </summary> |
| | | 257 | | /// <value> |
| | | 258 | | /// <see langword="true"/> if group members can write into this file; otherwise, <see langword="false"/>. |
| | | 259 | | /// </value> |
| | 43735 | 260 | | public bool GroupCanWrite { get; set; } |
| | | 261 | | |
| | | 262 | | /// <summary> |
| | | 263 | | /// Gets or sets a value indicating whether the group members can execute this file. |
| | | 264 | | /// </summary> |
| | | 265 | | /// <value> |
| | | 266 | | /// <see langword="true"/> if group members can execute this file; otherwise, <see langword="false"/>. |
| | | 267 | | /// </value> |
| | 43735 | 268 | | public bool GroupCanExecute { get; set; } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Gets or sets a value indicating whether the others can read from this file. |
| | | 272 | | /// </summary> |
| | | 273 | | /// <value> |
| | | 274 | | /// <see langword="true"/> if others can read from this file; otherwise, <see langword="false"/>. |
| | | 275 | | /// </value> |
| | 43735 | 276 | | public bool OthersCanRead { get; set; } |
| | | 277 | | |
| | | 278 | | /// <summary> |
| | | 279 | | /// Gets or sets a value indicating whether the others can write into this file. |
| | | 280 | | /// </summary> |
| | | 281 | | /// <value> |
| | | 282 | | /// <see langword="true"/> if others can write into this file; otherwise, <see langword="false"/>. |
| | | 283 | | /// </value> |
| | 43735 | 284 | | public bool OthersCanWrite { get; set; } |
| | | 285 | | |
| | | 286 | | /// <summary> |
| | | 287 | | /// Gets or sets a value indicating whether the others can execute this file. |
| | | 288 | | /// </summary> |
| | | 289 | | /// <value> |
| | | 290 | | /// <see langword="true"/> if others can execute this file; otherwise, <see langword="false"/>. |
| | | 291 | | /// </value> |
| | 43735 | 292 | | public bool OthersCanExecute { get; set; } |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Gets the extensions. |
| | | 296 | | /// </summary> |
| | | 297 | | /// <value> |
| | | 298 | | /// The extensions. |
| | | 299 | | /// </value> |
| | 22873 | 300 | | public IDictionary<string, string> Extensions { get; private set; } |
| | | 301 | | |
| | | 302 | | internal uint Permissions |
| | | 303 | | { |
| | | 304 | | get |
| | 21281 | 305 | | { |
| | 21281 | 306 | | uint permission = 0; |
| | | 307 | | |
| | 21281 | 308 | | if (_isBitFiledsBitSet) |
| | 6 | 309 | | { |
| | 6 | 310 | | permission |= S_IFMT; |
| | 6 | 311 | | } |
| | | 312 | | |
| | 21281 | 313 | | if (IsSocket) |
| | 38 | 314 | | { |
| | 38 | 315 | | permission |= S_IFSOCK; |
| | 38 | 316 | | } |
| | | 317 | | |
| | 21281 | 318 | | if (IsSymbolicLink) |
| | 44 | 319 | | { |
| | 44 | 320 | | permission |= S_IFLNK; |
| | 44 | 321 | | } |
| | | 322 | | |
| | 21281 | 323 | | if (IsRegularFile) |
| | 89 | 324 | | { |
| | 89 | 325 | | permission |= S_IFREG; |
| | 89 | 326 | | } |
| | | 327 | | |
| | 21281 | 328 | | if (IsBlockDevice) |
| | 48 | 329 | | { |
| | 48 | 330 | | permission |= S_IFBLK; |
| | 48 | 331 | | } |
| | | 332 | | |
| | 21281 | 333 | | if (IsDirectory) |
| | 84 | 334 | | { |
| | 84 | 335 | | permission |= S_IFDIR; |
| | 84 | 336 | | } |
| | | 337 | | |
| | 21281 | 338 | | if (IsCharacterDevice) |
| | 100 | 339 | | { |
| | 100 | 340 | | permission |= S_IFCHR; |
| | 100 | 341 | | } |
| | | 342 | | |
| | 21281 | 343 | | if (IsNamedPipe) |
| | 74 | 344 | | { |
| | 74 | 345 | | permission |= S_IFIFO; |
| | 74 | 346 | | } |
| | | 347 | | |
| | 21281 | 348 | | if (_isUIDBitSet) |
| | 82 | 349 | | { |
| | 82 | 350 | | permission |= S_ISUID; |
| | 82 | 351 | | } |
| | | 352 | | |
| | 21281 | 353 | | if (_isGroupIDBitSet) |
| | 72 | 354 | | { |
| | 72 | 355 | | permission |= S_ISGID; |
| | 72 | 356 | | } |
| | | 357 | | |
| | 21281 | 358 | | if (_isStickyBitSet) |
| | 80 | 359 | | { |
| | 80 | 360 | | permission |= S_ISVTX; |
| | 80 | 361 | | } |
| | | 362 | | |
| | 21281 | 363 | | if (OwnerCanRead) |
| | 93 | 364 | | { |
| | 93 | 365 | | permission |= S_IRUSR; |
| | 93 | 366 | | } |
| | | 367 | | |
| | 21281 | 368 | | if (OwnerCanWrite) |
| | 102 | 369 | | { |
| | 102 | 370 | | permission |= S_IWUSR; |
| | 102 | 371 | | } |
| | | 372 | | |
| | 21281 | 373 | | if (OwnerCanExecute) |
| | 85 | 374 | | { |
| | 85 | 375 | | permission |= S_IXUSR; |
| | 85 | 376 | | } |
| | | 377 | | |
| | 21281 | 378 | | if (GroupCanRead) |
| | 102 | 379 | | { |
| | 102 | 380 | | permission |= S_IRGRP; |
| | 102 | 381 | | } |
| | | 382 | | |
| | 21281 | 383 | | if (GroupCanWrite) |
| | 82 | 384 | | { |
| | 82 | 385 | | permission |= S_IWGRP; |
| | 82 | 386 | | } |
| | | 387 | | |
| | 21281 | 388 | | if (GroupCanExecute) |
| | 79 | 389 | | { |
| | 79 | 390 | | permission |= S_IXGRP; |
| | 79 | 391 | | } |
| | | 392 | | |
| | 21281 | 393 | | if (OthersCanRead) |
| | 116 | 394 | | { |
| | 116 | 395 | | permission |= S_IROTH; |
| | 116 | 396 | | } |
| | | 397 | | |
| | 21281 | 398 | | if (OthersCanWrite) |
| | 66 | 399 | | { |
| | 66 | 400 | | permission |= S_IWOTH; |
| | 66 | 401 | | } |
| | | 402 | | |
| | 21281 | 403 | | if (OthersCanExecute) |
| | 73 | 404 | | { |
| | 73 | 405 | | permission |= S_IXOTH; |
| | 73 | 406 | | } |
| | | 407 | | |
| | 21281 | 408 | | return permission; |
| | 21281 | 409 | | } |
| | | 410 | | private set |
| | 22453 | 411 | | { |
| | 22453 | 412 | | _isBitFiledsBitSet = (value & S_IFMT) == S_IFMT; |
| | | 413 | | |
| | 22453 | 414 | | IsSocket = (value & S_IFSOCK) == S_IFSOCK; |
| | | 415 | | |
| | 22453 | 416 | | IsSymbolicLink = (value & S_IFLNK) == S_IFLNK; |
| | | 417 | | |
| | 22453 | 418 | | IsRegularFile = (value & S_IFREG) == S_IFREG; |
| | | 419 | | |
| | 22453 | 420 | | IsBlockDevice = (value & S_IFBLK) == S_IFBLK; |
| | | 421 | | |
| | 22453 | 422 | | IsDirectory = (value & S_IFDIR) == S_IFDIR; |
| | | 423 | | |
| | 22453 | 424 | | IsCharacterDevice = (value & S_IFCHR) == S_IFCHR; |
| | | 425 | | |
| | 22453 | 426 | | IsNamedPipe = (value & S_IFIFO) == S_IFIFO; |
| | | 427 | | |
| | 22453 | 428 | | _isUIDBitSet = (value & S_ISUID) == S_ISUID; |
| | | 429 | | |
| | 22453 | 430 | | _isGroupIDBitSet = (value & S_ISGID) == S_ISGID; |
| | | 431 | | |
| | 22453 | 432 | | _isStickyBitSet = (value & S_ISVTX) == S_ISVTX; |
| | | 433 | | |
| | 22453 | 434 | | OwnerCanRead = (value & S_IRUSR) == S_IRUSR; |
| | | 435 | | |
| | 22453 | 436 | | OwnerCanWrite = (value & S_IWUSR) == S_IWUSR; |
| | | 437 | | |
| | 22453 | 438 | | OwnerCanExecute = (value & S_IXUSR) == S_IXUSR; |
| | | 439 | | |
| | 22453 | 440 | | GroupCanRead = (value & S_IRGRP) == S_IRGRP; |
| | | 441 | | |
| | 22453 | 442 | | GroupCanWrite = (value & S_IWGRP) == S_IWGRP; |
| | | 443 | | |
| | 22453 | 444 | | GroupCanExecute = (value & S_IXGRP) == S_IXGRP; |
| | | 445 | | |
| | 22453 | 446 | | OthersCanRead = (value & S_IROTH) == S_IROTH; |
| | | 447 | | |
| | 22453 | 448 | | OthersCanWrite = (value & S_IWOTH) == S_IWOTH; |
| | | 449 | | |
| | 22453 | 450 | | OthersCanExecute = (value & S_IXOTH) == S_IXOTH; |
| | 22453 | 451 | | } |
| | | 452 | | } |
| | | 453 | | |
| | 4 | 454 | | private SftpFileAttributes() |
| | 4 | 455 | | { |
| | 4 | 456 | | } |
| | | 457 | | |
| | 22453 | 458 | | internal SftpFileAttributes(DateTime lastAccessTimeUtc, DateTime lastWriteTimeUtc, long size, int userId, int gr |
| | 22453 | 459 | | { |
| | 22453 | 460 | | LastAccessTimeUtc = _originalLastAccessTimeUtc = lastAccessTimeUtc; |
| | 22453 | 461 | | LastWriteTimeUtc = _originalLastWriteTimeUtc = lastWriteTimeUtc; |
| | 22453 | 462 | | Size = _originalSize = size; |
| | 22453 | 463 | | UserId = _originalUserId = userId; |
| | 22453 | 464 | | GroupId = _originalGroupId = groupId; |
| | 22453 | 465 | | Permissions = _originalPermissions = permissions; |
| | 22453 | 466 | | Extensions = _originalExtensions = extensions; |
| | 22453 | 467 | | } |
| | | 468 | | |
| | | 469 | | /// <summary> |
| | | 470 | | /// Sets the permissions. |
| | | 471 | | /// </summary> |
| | | 472 | | /// <param name="mode">The mode.</param> |
| | | 473 | | public void SetPermissions(short mode) |
| | 1 | 474 | | { |
| | 1 | 475 | | if (mode is < 0 or > 999) |
| | 0 | 476 | | { |
| | 0 | 477 | | throw new ArgumentOutOfRangeException(nameof(mode)); |
| | | 478 | | } |
| | | 479 | | |
| | 1 | 480 | | var modeBytes = mode.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0').ToCharArray(); |
| | | 481 | | |
| | 1 | 482 | | var permission = ((modeBytes[0] & 0x0F) * 8 * 8) + ((modeBytes[1] & 0x0F) * 8) + (modeBytes[2] & 0x0F); |
| | | 483 | | |
| | 1 | 484 | | OwnerCanRead = (permission & S_IRUSR) == S_IRUSR; |
| | 1 | 485 | | OwnerCanWrite = (permission & S_IWUSR) == S_IWUSR; |
| | 1 | 486 | | OwnerCanExecute = (permission & S_IXUSR) == S_IXUSR; |
| | | 487 | | |
| | 1 | 488 | | GroupCanRead = (permission & S_IRGRP) == S_IRGRP; |
| | 1 | 489 | | GroupCanWrite = (permission & S_IWGRP) == S_IWGRP; |
| | 1 | 490 | | GroupCanExecute = (permission & S_IXGRP) == S_IXGRP; |
| | | 491 | | |
| | 1 | 492 | | OthersCanRead = (permission & S_IROTH) == S_IROTH; |
| | 1 | 493 | | OthersCanWrite = (permission & S_IWOTH) == S_IWOTH; |
| | 1 | 494 | | OthersCanExecute = (permission & S_IXOTH) == S_IXOTH; |
| | 1 | 495 | | } |
| | | 496 | | |
| | | 497 | | /// <summary> |
| | | 498 | | /// Returns a byte array representing the current <see cref="SftpFileAttributes"/>. |
| | | 499 | | /// </summary> |
| | | 500 | | /// <returns> |
| | | 501 | | /// A byte array representing the current <see cref="SftpFileAttributes"/>. |
| | | 502 | | /// </returns> |
| | | 503 | | public byte[] GetBytes() |
| | 10562 | 504 | | { |
| | 10562 | 505 | | using (var stream = new SshDataStream(4)) |
| | 10562 | 506 | | { |
| | 10562 | 507 | | uint flag = 0; |
| | | 508 | | |
| | 10562 | 509 | | if (IsSizeChanged && IsRegularFile) |
| | 4 | 510 | | { |
| | 4 | 511 | | flag |= 0x00000001; |
| | 4 | 512 | | } |
| | | 513 | | |
| | 10562 | 514 | | if (IsUserIdChanged || IsGroupIdChanged) |
| | 63 | 515 | | { |
| | 63 | 516 | | flag |= 0x00000002; |
| | 63 | 517 | | } |
| | | 518 | | |
| | 10562 | 519 | | if (IsPermissionsChanged) |
| | 1 | 520 | | { |
| | 1 | 521 | | flag |= 0x00000004; |
| | 1 | 522 | | } |
| | | 523 | | |
| | 10562 | 524 | | if (IsLastAccessTimeChanged || IsLastWriteTimeChanged) |
| | 67 | 525 | | { |
| | 67 | 526 | | flag |= 0x00000008; |
| | 67 | 527 | | } |
| | | 528 | | |
| | 10562 | 529 | | if (IsExtensionsChanged) |
| | 0 | 530 | | { |
| | 0 | 531 | | flag |= 0x80000000; |
| | 0 | 532 | | } |
| | | 533 | | |
| | 10562 | 534 | | stream.Write(flag); |
| | | 535 | | |
| | 10562 | 536 | | if (IsSizeChanged && IsRegularFile) |
| | 4 | 537 | | { |
| | 4 | 538 | | stream.Write((ulong) Size); |
| | 4 | 539 | | } |
| | | 540 | | |
| | 10562 | 541 | | if (IsUserIdChanged || IsGroupIdChanged) |
| | 63 | 542 | | { |
| | 63 | 543 | | stream.Write((uint) UserId); |
| | 63 | 544 | | stream.Write((uint) GroupId); |
| | 63 | 545 | | } |
| | | 546 | | |
| | 10562 | 547 | | if (IsPermissionsChanged) |
| | 1 | 548 | | { |
| | 1 | 549 | | stream.Write(Permissions); |
| | 1 | 550 | | } |
| | | 551 | | |
| | 10562 | 552 | | if (IsLastAccessTimeChanged || IsLastWriteTimeChanged) |
| | 67 | 553 | | { |
| | 67 | 554 | | var time = (uint) ((LastAccessTimeUtc.ToFileTimeUtc() / 10000000) - 11644473600); |
| | 67 | 555 | | stream.Write(time); |
| | 67 | 556 | | time = (uint) ((LastWriteTimeUtc.ToFileTimeUtc() / 10000000) - 11644473600); |
| | 67 | 557 | | stream.Write(time); |
| | 67 | 558 | | } |
| | | 559 | | |
| | 10562 | 560 | | if (IsExtensionsChanged) |
| | 0 | 561 | | { |
| | 0 | 562 | | foreach (var item in Extensions) |
| | 0 | 563 | | { |
| | | 564 | | /* |
| | | 565 | | * TODO: we write as ASCII but read as UTF8 !!! |
| | | 566 | | */ |
| | | 567 | | |
| | 0 | 568 | | stream.Write(item.Key, SshData.Ascii); |
| | 0 | 569 | | stream.Write(item.Value, SshData.Ascii); |
| | 0 | 570 | | } |
| | 0 | 571 | | } |
| | | 572 | | |
| | 10562 | 573 | | return stream.ToArray(); |
| | | 574 | | } |
| | 10562 | 575 | | } |
| | | 576 | | |
| | 4 | 577 | | internal static readonly SftpFileAttributes Empty = new SftpFileAttributes(); |
| | | 578 | | |
| | | 579 | | internal static SftpFileAttributes FromBytes(SshDataStream stream) |
| | 22150 | 580 | | { |
| | | 581 | | const uint SSH_FILEXFER_ATTR_SIZE = 0x00000001; |
| | | 582 | | const uint SSH_FILEXFER_ATTR_UIDGID = 0x00000002; |
| | | 583 | | const uint SSH_FILEXFER_ATTR_PERMISSIONS = 0x00000004; |
| | | 584 | | const uint SSH_FILEXFER_ATTR_ACMODTIME = 0x00000008; |
| | | 585 | | const uint SSH_FILEXFER_ATTR_EXTENDED = 0x80000000; |
| | | 586 | | |
| | 22150 | 587 | | var flag = stream.ReadUInt32(); |
| | | 588 | | |
| | 22150 | 589 | | long size = -1; |
| | 22150 | 590 | | var userId = -1; |
| | 22150 | 591 | | var groupId = -1; |
| | 22150 | 592 | | uint permissions = 0; |
| | | 593 | | DateTime accessTime; |
| | | 594 | | DateTime modifyTime; |
| | 22150 | 595 | | Dictionary<string, string> extensions = null; |
| | | 596 | | |
| | 22150 | 597 | | if ((flag & SSH_FILEXFER_ATTR_SIZE) == SSH_FILEXFER_ATTR_SIZE) |
| | 10628 | 598 | | { |
| | 10628 | 599 | | size = (long) stream.ReadUInt64(); |
| | 10628 | 600 | | } |
| | | 601 | | |
| | 22150 | 602 | | if ((flag & SSH_FILEXFER_ATTR_UIDGID) == SSH_FILEXFER_ATTR_UIDGID) |
| | 10631 | 603 | | { |
| | 10631 | 604 | | userId = (int) stream.ReadUInt32(); |
| | | 605 | | |
| | 10631 | 606 | | groupId = (int) stream.ReadUInt32(); |
| | 10631 | 607 | | } |
| | | 608 | | |
| | 22150 | 609 | | if ((flag & SSH_FILEXFER_ATTR_PERMISSIONS) == SSH_FILEXFER_ATTR_PERMISSIONS) |
| | 10628 | 610 | | { |
| | 10628 | 611 | | permissions = stream.ReadUInt32(); |
| | 10628 | 612 | | } |
| | | 613 | | |
| | 22150 | 614 | | if ((flag & SSH_FILEXFER_ATTR_ACMODTIME) == SSH_FILEXFER_ATTR_ACMODTIME) |
| | 10631 | 615 | | { |
| | | 616 | | // The incoming times are "Unix times", so they're already in UTC. We need to preserve that |
| | | 617 | | // to avoid losing information in a local time conversion during the "fall back" hour in DST. |
| | 10631 | 618 | | var time = stream.ReadUInt32(); |
| | 10631 | 619 | | accessTime = DateTime.FromFileTimeUtc((time + 11644473600) * 10000000); |
| | 10631 | 620 | | time = stream.ReadUInt32(); |
| | 10631 | 621 | | modifyTime = DateTime.FromFileTimeUtc((time + 11644473600) * 10000000); |
| | 10631 | 622 | | } |
| | | 623 | | else |
| | 11519 | 624 | | { |
| | 11519 | 625 | | accessTime = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| | 11519 | 626 | | modifyTime = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| | 11519 | 627 | | } |
| | | 628 | | |
| | 22150 | 629 | | if ((flag & SSH_FILEXFER_ATTR_EXTENDED) == SSH_FILEXFER_ATTR_EXTENDED) |
| | 0 | 630 | | { |
| | 0 | 631 | | var extendedCount = (int) stream.ReadUInt32(); |
| | 0 | 632 | | extensions = new Dictionary<string, string>(extendedCount); |
| | 0 | 633 | | for (var i = 0; i < extendedCount; i++) |
| | 0 | 634 | | { |
| | 0 | 635 | | var extensionName = stream.ReadString(SshData.Utf8); |
| | 0 | 636 | | var extensionData = stream.ReadString(SshData.Utf8); |
| | 0 | 637 | | extensions.Add(extensionName, extensionData); |
| | 0 | 638 | | } |
| | 0 | 639 | | } |
| | | 640 | | |
| | 22150 | 641 | | return new SftpFileAttributes(accessTime, modifyTime, size, userId, groupId, permissions, extensions); |
| | 22150 | 642 | | } |
| | | 643 | | |
| | | 644 | | internal static SftpFileAttributes FromBytes(byte[] buffer) |
| | 0 | 645 | | { |
| | 0 | 646 | | using (var stream = new SshDataStream(buffer)) |
| | 0 | 647 | | { |
| | 0 | 648 | | return FromBytes(stream); |
| | | 649 | | } |
| | 0 | 650 | | } |
| | | 651 | | |
| | | 652 | | private static DateTime ToLocalTime(DateTime value) |
| | 68 | 653 | | { |
| | | 654 | | DateTime result; |
| | | 655 | | |
| | 68 | 656 | | if (value == DateTime.MinValue) |
| | 0 | 657 | | { |
| | 0 | 658 | | result = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Local); |
| | 0 | 659 | | } |
| | | 660 | | else |
| | 68 | 661 | | { |
| | 68 | 662 | | result = value.ToLocalTime(); |
| | 68 | 663 | | } |
| | | 664 | | |
| | 68 | 665 | | return result; |
| | 68 | 666 | | } |
| | | 667 | | |
| | | 668 | | private static DateTime ToUniversalTime(DateTime value) |
| | 8 | 669 | | { |
| | | 670 | | DateTime result; |
| | | 671 | | |
| | 8 | 672 | | if (value == DateTime.MinValue) |
| | 0 | 673 | | { |
| | 0 | 674 | | result = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| | 0 | 675 | | } |
| | | 676 | | else |
| | 8 | 677 | | { |
| | 8 | 678 | | result = value.ToUniversalTime(); |
| | 8 | 679 | | } |
| | | 680 | | |
| | 8 | 681 | | return result; |
| | 8 | 682 | | } |
| | | 683 | | } |
| | | 684 | | } |