2
0

SftpFile.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using Renci.SshNet.Common;
  5. namespace Renci.SshNet.Sftp
  6. {
  7. /// <summary>
  8. /// Represents SFTP file information
  9. /// </summary>
  10. public class SftpFile
  11. {
  12. private readonly SftpSession _sftpSession;
  13. /// <summary>
  14. /// Gets the file attributes.
  15. /// </summary>
  16. public SftpFileAttributes Attributes { get; private set; }
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="SftpFile"/> class.
  19. /// </summary>
  20. /// <param name="sftpSession">The SFTP session.</param>
  21. /// <param name="fullName">Full path of the directory or file.</param>
  22. /// <param name="attributes">Attributes of the directory or file.</param>
  23. /// <exception cref="ArgumentNullException"><paramref name="sftpSession"/> or <paramref name="fullName"/> is null.</exception>
  24. internal SftpFile(SftpSession sftpSession, string fullName, SftpFileAttributes attributes)
  25. {
  26. if (sftpSession == null)
  27. throw new SshConnectionException("Client not connected.");
  28. if (attributes == null)
  29. throw new ArgumentNullException("attributes");
  30. if (fullName == null)
  31. throw new ArgumentNullException("fullName");
  32. this._sftpSession = sftpSession;
  33. this.Attributes = attributes;
  34. this.Name = fullName.Substring(fullName.LastIndexOf('/') + 1);
  35. this.FullName = fullName;
  36. }
  37. /// <summary>
  38. /// Gets the full path of the directory or file.
  39. /// </summary>
  40. public string FullName { get; private set; }
  41. /// <summary>
  42. /// For files, gets the name of the file. For directories, gets the name of the last directory in the hierarchy if a hierarchy exists.
  43. /// Otherwise, the Name property gets the name of the directory.
  44. /// </summary>
  45. public string Name { get; private set; }
  46. /// <summary>
  47. /// Gets or sets the time the current file or directory was last accessed.
  48. /// </summary>
  49. /// <value>
  50. /// The time that the current file or directory was last accessed.
  51. /// </value>
  52. public DateTime LastAccessTime
  53. {
  54. get
  55. {
  56. return this.Attributes.LastAccessTime;
  57. }
  58. set
  59. {
  60. this.Attributes.LastAccessTime = value;
  61. }
  62. }
  63. /// <summary>
  64. /// Gets or sets the time when the current file or directory was last written to.
  65. /// </summary>
  66. /// <value>
  67. /// The time the current file was last written.
  68. /// </value>
  69. public DateTime LastWriteTime
  70. {
  71. get
  72. {
  73. return this.Attributes.LastWriteTime;
  74. }
  75. set
  76. {
  77. this.Attributes.LastWriteTime = value;
  78. }
  79. }
  80. /// <summary>
  81. /// Gets or sets the time, in coordinated universal time (UTC), the current file or directory was last accessed.
  82. /// </summary>
  83. /// <value>
  84. /// The time that the current file or directory was last accessed.
  85. /// </value>
  86. public DateTime LastAccessTimeUtc
  87. {
  88. get
  89. {
  90. return this.Attributes.LastAccessTime.ToUniversalTime();
  91. }
  92. set
  93. {
  94. this.Attributes.LastAccessTime = value.ToLocalTime();
  95. }
  96. }
  97. /// <summary>
  98. /// Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.
  99. /// </summary>
  100. /// <value>
  101. /// The time the current file was last written.
  102. /// </value>
  103. public DateTime LastWriteTimeUtc
  104. {
  105. get
  106. {
  107. return this.Attributes.LastWriteTime.ToUniversalTime();
  108. }
  109. set
  110. {
  111. this.Attributes.LastWriteTime = value.ToLocalTime();
  112. }
  113. }
  114. /// <summary>
  115. /// Gets or sets the size, in bytes, of the current file.
  116. /// </summary>
  117. /// <value>
  118. /// The size of the current file in bytes.
  119. /// </value>
  120. public long Length
  121. {
  122. get
  123. {
  124. return this.Attributes.Size;
  125. }
  126. }
  127. /// <summary>
  128. /// Gets or sets file user id.
  129. /// </summary>
  130. /// <value>
  131. /// File user id.
  132. /// </value>
  133. public int UserId
  134. {
  135. get
  136. {
  137. return this.Attributes.UserId;
  138. }
  139. set
  140. {
  141. this.Attributes.UserId = value;
  142. }
  143. }
  144. /// <summary>
  145. /// Gets or sets file group id.
  146. /// </summary>
  147. /// <value>
  148. /// File group id.
  149. /// </value>
  150. public int GroupId
  151. {
  152. get
  153. {
  154. return this.Attributes.GroupId;
  155. }
  156. set
  157. {
  158. this.Attributes.GroupId = value;
  159. }
  160. }
  161. /// <summary>
  162. /// Gets a value indicating whether file represents a socket.
  163. /// </summary>
  164. /// <value>
  165. /// <c>true</c> if file represents a socket; otherwise, <c>false</c>.
  166. /// </value>
  167. public bool IsSocket
  168. {
  169. get
  170. {
  171. return this.Attributes.IsSocket;
  172. }
  173. }
  174. /// <summary>
  175. /// Gets a value indicating whether file represents a symbolic link.
  176. /// </summary>
  177. /// <value>
  178. /// <c>true</c> if file represents a symbolic link; otherwise, <c>false</c>.
  179. /// </value>
  180. public bool IsSymbolicLink
  181. {
  182. get
  183. {
  184. return this.Attributes.IsSymbolicLink;
  185. }
  186. }
  187. /// <summary>
  188. /// Gets a value indicating whether file represents a regular file.
  189. /// </summary>
  190. /// <value>
  191. /// <c>true</c> if file represents a regular file; otherwise, <c>false</c>.
  192. /// </value>
  193. public bool IsRegularFile
  194. {
  195. get
  196. {
  197. return this.Attributes.IsRegularFile;
  198. }
  199. }
  200. /// <summary>
  201. /// Gets a value indicating whether file represents a block device.
  202. /// </summary>
  203. /// <value>
  204. /// <c>true</c> if file represents a block device; otherwise, <c>false</c>.
  205. /// </value>
  206. public bool IsBlockDevice
  207. {
  208. get
  209. {
  210. return this.Attributes.IsBlockDevice;
  211. }
  212. }
  213. /// <summary>
  214. /// Gets a value indicating whether file represents a directory.
  215. /// </summary>
  216. /// <value>
  217. /// <c>true</c> if file represents a directory; otherwise, <c>false</c>.
  218. /// </value>
  219. public bool IsDirectory
  220. {
  221. get
  222. {
  223. return this.Attributes.IsDirectory;
  224. }
  225. }
  226. /// <summary>
  227. /// Gets a value indicating whether file represents a character device.
  228. /// </summary>
  229. /// <value>
  230. /// <c>true</c> if file represents a character device; otherwise, <c>false</c>.
  231. /// </value>
  232. public bool IsCharacterDevice
  233. {
  234. get
  235. {
  236. return this.Attributes.IsCharacterDevice;
  237. }
  238. }
  239. /// <summary>
  240. /// Gets a value indicating whether file represents a named pipe.
  241. /// </summary>
  242. /// <value>
  243. /// <c>true</c> if file represents a named pipe; otherwise, <c>false</c>.
  244. /// </value>
  245. public bool IsNamedPipe
  246. {
  247. get
  248. {
  249. return this.Attributes.IsNamedPipe;
  250. }
  251. }
  252. /// <summary>
  253. /// Gets or sets a value indicating whether the owner can read from this file.
  254. /// </summary>
  255. /// <value>
  256. /// <c>true</c> if owner can read from this file; otherwise, <c>false</c>.
  257. /// </value>
  258. public bool OwnerCanRead
  259. {
  260. get
  261. {
  262. return this.Attributes.OwnerCanRead;
  263. }
  264. set
  265. {
  266. this.Attributes.OwnerCanRead = value;
  267. }
  268. }
  269. /// <summary>
  270. /// Gets or sets a value indicating whether the owner can write into this file.
  271. /// </summary>
  272. /// <value>
  273. /// <c>true</c> if owner can write into this file; otherwise, <c>false</c>.
  274. /// </value>
  275. public bool OwnerCanWrite
  276. {
  277. get
  278. {
  279. return this.Attributes.OwnerCanWrite;
  280. }
  281. set
  282. {
  283. this.Attributes.OwnerCanWrite = value;
  284. }
  285. }
  286. /// <summary>
  287. /// Gets or sets a value indicating whether the owner can execute this file.
  288. /// </summary>
  289. /// <value>
  290. /// <c>true</c> if owner can execute this file; otherwise, <c>false</c>.
  291. /// </value>
  292. public bool OwnerCanExecute
  293. {
  294. get
  295. {
  296. return this.Attributes.OwnerCanExecute;
  297. }
  298. set
  299. {
  300. this.Attributes.OwnerCanExecute = value;
  301. }
  302. }
  303. /// <summary>
  304. /// Gets or sets a value indicating whether the group members can read from this file.
  305. /// </summary>
  306. /// <value>
  307. /// <c>true</c> if group members can read from this file; otherwise, <c>false</c>.
  308. /// </value>
  309. public bool GroupCanRead
  310. {
  311. get
  312. {
  313. return this.Attributes.GroupCanRead;
  314. }
  315. set
  316. {
  317. this.Attributes.GroupCanRead = value;
  318. }
  319. }
  320. /// <summary>
  321. /// Gets or sets a value indicating whether the group members can write into this file.
  322. /// </summary>
  323. /// <value>
  324. /// <c>true</c> if group members can write into this file; otherwise, <c>false</c>.
  325. /// </value>
  326. public bool GroupCanWrite
  327. {
  328. get
  329. {
  330. return this.Attributes.GroupCanWrite;
  331. }
  332. set
  333. {
  334. this.Attributes.GroupCanWrite = value;
  335. }
  336. }
  337. /// <summary>
  338. /// Gets or sets a value indicating whether the group members can execute this file.
  339. /// </summary>
  340. /// <value>
  341. /// <c>true</c> if group members can execute this file; otherwise, <c>false</c>.
  342. /// </value>
  343. public bool GroupCanExecute
  344. {
  345. get
  346. {
  347. return this.Attributes.GroupCanExecute;
  348. }
  349. set
  350. {
  351. this.Attributes.GroupCanExecute = value;
  352. }
  353. }
  354. /// <summary>
  355. /// Gets or sets a value indicating whether the others can read from this file.
  356. /// </summary>
  357. /// <value>
  358. /// <c>true</c> if others can read from this file; otherwise, <c>false</c>.
  359. /// </value>
  360. public bool OthersCanRead
  361. {
  362. get
  363. {
  364. return this.Attributes.OthersCanRead;
  365. }
  366. set
  367. {
  368. this.Attributes.OthersCanRead = value;
  369. }
  370. }
  371. /// <summary>
  372. /// Gets or sets a value indicating whether the others can write into this file.
  373. /// </summary>
  374. /// <value>
  375. /// <c>true</c> if others can write into this file; otherwise, <c>false</c>.
  376. /// </value>
  377. public bool OthersCanWrite
  378. {
  379. get
  380. {
  381. return this.Attributes.OthersCanWrite;
  382. }
  383. set
  384. {
  385. this.Attributes.OthersCanWrite = value;
  386. }
  387. }
  388. /// <summary>
  389. /// Gets or sets a value indicating whether the others can execute this file.
  390. /// </summary>
  391. /// <value>
  392. /// <c>true</c> if others can execute this file; otherwise, <c>false</c>.
  393. /// </value>
  394. public bool OthersCanExecute
  395. {
  396. get
  397. {
  398. return this.Attributes.OthersCanExecute;
  399. }
  400. set
  401. {
  402. this.Attributes.OthersCanExecute = value;
  403. }
  404. }
  405. /// <summary>
  406. /// Gets the extension part of the file.
  407. /// </summary>
  408. /// <value>
  409. /// File extensions.
  410. /// </value>
  411. public IDictionary<string, string> Extensions { get; private set; }
  412. /// <summary>
  413. /// Sets file permissions.
  414. /// </summary>
  415. /// <param name="mode">The mode.</param>
  416. public void SetPermissions(short mode)
  417. {
  418. this.Attributes.SetPermissions(mode);
  419. this.UpdateStatus();
  420. }
  421. /// <summary>
  422. /// Permanently deletes a file on remote machine.
  423. /// </summary>
  424. public void Delete()
  425. {
  426. if (this.IsDirectory)
  427. {
  428. this._sftpSession.RequestRmDir(this.FullName);
  429. }
  430. else
  431. {
  432. this._sftpSession.RequestRemove(this.FullName);
  433. }
  434. }
  435. /// <summary>
  436. /// Moves a specified file to a new location on remote machine, providing the option to specify a new file name.
  437. /// </summary>
  438. /// <param name="destFileName">The path to move the file to, which can specify a different file name.</param>
  439. /// <exception cref="ArgumentNullException"><paramref name="destFileName"/> is null.</exception>
  440. public void MoveTo(string destFileName)
  441. {
  442. if (destFileName == null)
  443. throw new ArgumentNullException("destFileName");
  444. this._sftpSession.RequestRename(this.FullName, destFileName);
  445. var fullPath = this._sftpSession.GetCanonicalPath(destFileName);
  446. this.Name = fullPath.Substring(fullPath.LastIndexOf('/') + 1);
  447. this.FullName = fullPath;
  448. }
  449. /// <summary>
  450. /// Updates file status on the server.
  451. /// </summary>
  452. public void UpdateStatus()
  453. {
  454. this._sftpSession.RequestSetStat(this.FullName, this.Attributes);
  455. }
  456. /// <summary>
  457. /// Returns a <see cref="System.String"/> that represents this instance.
  458. /// </summary>
  459. /// <returns>
  460. /// A <see cref="System.String"/> that represents this instance.
  461. /// </returns>
  462. public override string ToString()
  463. {
  464. return string.Format(CultureInfo.CurrentCulture, "Name {0}, Length {1}, User ID {2}, Group ID {3}, Accessed {4}, Modified {5}", this.Name, this.Length, this.UserId, this.GroupId, this.LastAccessTime, this.LastWriteTime);
  465. }
  466. }
  467. }