TarEntry.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. using System;
  2. using System.IO;
  3. namespace ICSharpCode.SharpZipLib.Tar
  4. {
  5. /// <summary>
  6. /// This class represents an entry in a Tar archive. It consists
  7. /// of the entry's header, as well as the entry's File. Entries
  8. /// can be instantiated in one of three ways, depending on how
  9. /// they are to be used.
  10. /// <p>
  11. /// TarEntries that are created from the header bytes read from
  12. /// an archive are instantiated with the TarEntry( byte[] )
  13. /// constructor. These entries will be used when extracting from
  14. /// or listing the contents of an archive. These entries have their
  15. /// header filled in using the header bytes. They also set the File
  16. /// to null, since they reference an archive entry not a file.</p>
  17. /// <p>
  18. /// TarEntries that are created from files that are to be written
  19. /// into an archive are instantiated with the CreateEntryFromFile(string)
  20. /// pseudo constructor. These entries have their header filled in using
  21. /// the File's information. They also keep a reference to the File
  22. /// for convenience when writing entries.</p>
  23. /// <p>
  24. /// Finally, TarEntries can be constructed from nothing but a name.
  25. /// This allows the programmer to construct the entry by hand, for
  26. /// instance when only an InputStream is available for writing to
  27. /// the archive, and the header information is constructed from
  28. /// other information. In this case the header fields are set to
  29. /// defaults and the File is set to null.</p>
  30. /// <see cref="TarHeader"/>
  31. /// </summary>
  32. public class TarEntry
  33. {
  34. #region Constructors
  35. /// <summary>
  36. /// Initialise a default instance of <see cref="TarEntry"/>.
  37. /// </summary>
  38. private TarEntry()
  39. {
  40. header = new TarHeader();
  41. }
  42. /// <summary>
  43. /// Construct an entry from an archive's header bytes. File is set
  44. /// to null.
  45. /// </summary>
  46. /// <param name = "headerBuffer">
  47. /// The header bytes from a tar archive entry.
  48. /// </param>
  49. public TarEntry(byte[] headerBuffer)
  50. {
  51. header = new TarHeader();
  52. header.ParseBuffer(headerBuffer);
  53. }
  54. /// <summary>
  55. /// Construct a TarEntry using the <paramref name="header">header</paramref> provided
  56. /// </summary>
  57. /// <param name="header">Header details for entry</param>
  58. public TarEntry(TarHeader header)
  59. {
  60. if (header == null) {
  61. throw new ArgumentNullException("nameof(header)");
  62. }
  63. this.header = (TarHeader)header.Clone();
  64. }
  65. #endregion
  66. #region ICloneable Members
  67. /// <summary>
  68. /// Clone this tar entry.
  69. /// </summary>
  70. /// <returns>Returns a clone of this entry.</returns>
  71. public object Clone()
  72. {
  73. var entry = new TarEntry();
  74. entry.file = file;
  75. entry.header = (TarHeader)header.Clone();
  76. entry.Name = Name;
  77. return entry;
  78. }
  79. #endregion
  80. /// <summary>
  81. /// Construct an entry with only a <paramref name="name">name</paramref>.
  82. /// This allows the programmer to construct the entry's header "by hand".
  83. /// </summary>
  84. /// <param name="name">The name to use for the entry</param>
  85. /// <returns>Returns the newly created <see cref="TarEntry"/></returns>
  86. public static TarEntry CreateTarEntry(string name)
  87. {
  88. var entry = new TarEntry();
  89. TarEntry.NameTarHeader(entry.header, name);
  90. return entry;
  91. }
  92. /// <summary>
  93. /// Construct an entry for a file. File is set to file, and the
  94. /// header is constructed from information from the file.
  95. /// </summary>
  96. /// <param name = "fileName">The file name that the entry represents.</param>
  97. /// <returns>Returns the newly created <see cref="TarEntry"/></returns>
  98. public static TarEntry CreateEntryFromFile(string fileName)
  99. {
  100. var entry = new TarEntry();
  101. entry.GetFileTarHeader(entry.header, fileName);
  102. return entry;
  103. }
  104. /// <summary>
  105. /// Determine if the two entries are equal. Equality is determined
  106. /// by the header names being equal.
  107. /// </summary>
  108. /// <param name="obj">The <see cref="Object"/> to compare with the current Object.</param>
  109. /// <returns>
  110. /// True if the entries are equal; false if not.
  111. /// </returns>
  112. public override bool Equals(object obj)
  113. {
  114. var localEntry = obj as TarEntry;
  115. if (localEntry != null) {
  116. return Name.Equals(localEntry.Name);
  117. }
  118. return false;
  119. }
  120. /// <summary>
  121. /// Derive a Hash value for the current <see cref="Object"/>
  122. /// </summary>
  123. /// <returns>A Hash code for the current <see cref="Object"/></returns>
  124. public override int GetHashCode()
  125. {
  126. return Name.GetHashCode();
  127. }
  128. /// <summary>
  129. /// Determine if the given entry is a descendant of this entry.
  130. /// Descendancy is determined by the name of the descendant
  131. /// starting with this entry's name.
  132. /// </summary>
  133. /// <param name = "toTest">
  134. /// Entry to be checked as a descendent of this.
  135. /// </param>
  136. /// <returns>
  137. /// True if entry is a descendant of this.
  138. /// </returns>
  139. public bool IsDescendent(TarEntry toTest)
  140. {
  141. if (toTest == null) {
  142. throw new ArgumentNullException("nameof(toTest)");
  143. }
  144. return toTest.Name.StartsWith(Name, StringComparison.Ordinal);
  145. }
  146. /// <summary>
  147. /// Get this entry's header.
  148. /// </summary>
  149. /// <returns>
  150. /// This entry's TarHeader.
  151. /// </returns>
  152. public TarHeader TarHeader {
  153. get {
  154. return header;
  155. }
  156. }
  157. /// <summary>
  158. /// Get/Set this entry's name.
  159. /// </summary>
  160. public string Name {
  161. get {
  162. return header.Name;
  163. }
  164. set {
  165. header.Name = value;
  166. }
  167. }
  168. /// <summary>
  169. /// Get/set this entry's user id.
  170. /// </summary>
  171. public int UserId {
  172. get {
  173. return header.UserId;
  174. }
  175. set {
  176. header.UserId = value;
  177. }
  178. }
  179. /// <summary>
  180. /// Get/set this entry's group id.
  181. /// </summary>
  182. public int GroupId {
  183. get {
  184. return header.GroupId;
  185. }
  186. set {
  187. header.GroupId = value;
  188. }
  189. }
  190. /// <summary>
  191. /// Get/set this entry's user name.
  192. /// </summary>
  193. public string UserName {
  194. get {
  195. return header.UserName;
  196. }
  197. set {
  198. header.UserName = value;
  199. }
  200. }
  201. /// <summary>
  202. /// Get/set this entry's group name.
  203. /// </summary>
  204. public string GroupName {
  205. get {
  206. return header.GroupName;
  207. }
  208. set {
  209. header.GroupName = value;
  210. }
  211. }
  212. /// <summary>
  213. /// Convenience method to set this entry's group and user ids.
  214. /// </summary>
  215. /// <param name="userId">
  216. /// This entry's new user id.
  217. /// </param>
  218. /// <param name="groupId">
  219. /// This entry's new group id.
  220. /// </param>
  221. public void SetIds(int userId, int groupId)
  222. {
  223. UserId = userId;
  224. GroupId = groupId;
  225. }
  226. /// <summary>
  227. /// Convenience method to set this entry's group and user names.
  228. /// </summary>
  229. /// <param name="userName">
  230. /// This entry's new user name.
  231. /// </param>
  232. /// <param name="groupName">
  233. /// This entry's new group name.
  234. /// </param>
  235. public void SetNames(string userName, string groupName)
  236. {
  237. UserName = userName;
  238. GroupName = groupName;
  239. }
  240. /// <summary>
  241. /// Get/Set the modification time for this entry
  242. /// </summary>
  243. public DateTime ModTime {
  244. get {
  245. return header.ModTime;
  246. }
  247. set {
  248. header.ModTime = value;
  249. }
  250. }
  251. /// <summary>
  252. /// Get this entry's file.
  253. /// </summary>
  254. /// <returns>
  255. /// This entry's file.
  256. /// </returns>
  257. public string File {
  258. get {
  259. return file;
  260. }
  261. }
  262. /// <summary>
  263. /// Get/set this entry's recorded file size.
  264. /// </summary>
  265. public long Size {
  266. get {
  267. return header.Size;
  268. }
  269. set {
  270. header.Size = value;
  271. }
  272. }
  273. /// <summary>
  274. /// Return true if this entry represents a directory, false otherwise
  275. /// </summary>
  276. /// <returns>
  277. /// True if this entry is a directory.
  278. /// </returns>
  279. public bool IsDirectory {
  280. get {
  281. if (file != null) {
  282. return Directory.Exists(file);
  283. }
  284. if (header != null) {
  285. if ((header.TypeFlag == TarHeader.LF_DIR) || Name.EndsWith("/", StringComparison.Ordinal)) {
  286. return true;
  287. }
  288. }
  289. return false;
  290. }
  291. }
  292. /// <summary>
  293. /// Fill in a TarHeader with information from a File.
  294. /// </summary>
  295. /// <param name="header">
  296. /// The TarHeader to fill in.
  297. /// </param>
  298. /// <param name="file">
  299. /// The file from which to get the header information.
  300. /// </param>
  301. public void GetFileTarHeader(TarHeader header, string file)
  302. {
  303. if (header == null) {
  304. throw new ArgumentNullException("nameof(header)");
  305. }
  306. if (file == null) {
  307. throw new ArgumentNullException("nameof(file)");
  308. }
  309. this.file = file;
  310. // bugfix from torhovl from #D forum:
  311. string name = file;
  312. // 23-Jan-2004 GnuTar allows device names in path where the name is not local to the current directory
  313. if (name.IndexOf(Directory.GetCurrentDirectory(), StringComparison.Ordinal) == 0) {
  314. name = name.Substring(Directory.GetCurrentDirectory().Length);
  315. }
  316. /*
  317. if (Path.DirectorySeparatorChar == '\\')
  318. {
  319. // check if the OS is Windows
  320. // Strip off drive letters!
  321. if (name.Length > 2)
  322. {
  323. char ch1 = name[0];
  324. char ch2 = name[1];
  325. if (ch2 == ':' && Char.IsLetter(ch1))
  326. {
  327. name = name.Substring(2);
  328. }
  329. }
  330. }
  331. */
  332. name = name.Replace(Path.DirectorySeparatorChar, '/');
  333. // No absolute pathnames
  334. // Windows (and Posix?) paths can start with UNC style "\\NetworkDrive\",
  335. // so we loop on starting /'s.
  336. while (name.StartsWith("/", StringComparison.Ordinal)) {
  337. name = name.Substring(1);
  338. }
  339. header.LinkName = String.Empty;
  340. header.Name = name;
  341. if (Directory.Exists(file)) {
  342. header.Mode = 1003; // Magic number for security access for a UNIX filesystem
  343. header.TypeFlag = TarHeader.LF_DIR;
  344. if ((header.Name.Length == 0) || header.Name[header.Name.Length - 1] != '/') {
  345. header.Name = header.Name + "/";
  346. }
  347. header.Size = 0;
  348. } else {
  349. header.Mode = 33216; // Magic number for security access for a UNIX filesystem
  350. header.TypeFlag = TarHeader.LF_NORMAL;
  351. header.Size = new FileInfo(file.Replace('/', Path.DirectorySeparatorChar)).Length;
  352. }
  353. header.ModTime = System.IO.File.GetLastWriteTime(file.Replace('/', Path.DirectorySeparatorChar)).ToUniversalTime();
  354. header.DevMajor = 0;
  355. header.DevMinor = 0;
  356. }
  357. /// <summary>
  358. /// Get entries for all files present in this entries directory.
  359. /// If this entry doesnt represent a directory zero entries are returned.
  360. /// </summary>
  361. /// <returns>
  362. /// An array of TarEntry's for this entry's children.
  363. /// </returns>
  364. public TarEntry[] GetDirectoryEntries()
  365. {
  366. if ((file == null) || !Directory.Exists(file)) {
  367. return new TarEntry[0];
  368. }
  369. string[] list = Directory.GetFileSystemEntries(file);
  370. TarEntry[] result = new TarEntry[list.Length];
  371. for (int i = 0; i < list.Length; ++i) {
  372. result[i] = TarEntry.CreateEntryFromFile(list[i]);
  373. }
  374. return result;
  375. }
  376. /// <summary>
  377. /// Write an entry's header information to a header buffer.
  378. /// </summary>
  379. /// <param name = "outBuffer">
  380. /// The tar entry header buffer to fill in.
  381. /// </param>
  382. public void WriteEntryHeader(byte[] outBuffer)
  383. {
  384. header.WriteHeader(outBuffer);
  385. }
  386. /// <summary>
  387. /// Convenience method that will modify an entry's name directly
  388. /// in place in an entry header buffer byte array.
  389. /// </summary>
  390. /// <param name="buffer">
  391. /// The buffer containing the entry header to modify.
  392. /// </param>
  393. /// <param name="newName">
  394. /// The new name to place into the header buffer.
  395. /// </param>
  396. static public void AdjustEntryName(byte[] buffer, string newName)
  397. {
  398. TarHeader.GetNameBytes(newName, buffer, 0, TarHeader.NAMELEN);
  399. }
  400. /// <summary>
  401. /// Fill in a TarHeader given only the entry's name.
  402. /// </summary>
  403. /// <param name="header">
  404. /// The TarHeader to fill in.
  405. /// </param>
  406. /// <param name="name">
  407. /// The tar entry name.
  408. /// </param>
  409. static public void NameTarHeader(TarHeader header, string name)
  410. {
  411. if (header == null) {
  412. throw new ArgumentNullException("nameof(header)");
  413. }
  414. if (name == null) {
  415. throw new ArgumentNullException("nameof(name)");
  416. }
  417. bool isDir = name.EndsWith("/", StringComparison.Ordinal);
  418. header.Name = name;
  419. header.Mode = isDir ? 1003 : 33216;
  420. header.UserId = 0;
  421. header.GroupId = 0;
  422. header.Size = 0;
  423. header.ModTime = DateTime.UtcNow;
  424. header.TypeFlag = isDir ? TarHeader.LF_DIR : TarHeader.LF_NORMAL;
  425. header.LinkName = String.Empty;
  426. header.UserName = String.Empty;
  427. header.GroupName = String.Empty;
  428. header.DevMajor = 0;
  429. header.DevMinor = 0;
  430. }
  431. #region Instance Fields
  432. /// <summary>
  433. /// The name of the file this entry represents or null if the entry is not based on a file.
  434. /// </summary>
  435. string file;
  436. /// <summary>
  437. /// The entry's header information.
  438. /// </summary>
  439. TarHeader header;
  440. #endregion
  441. }
  442. }