TarEntry.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // TarEntry.cs
  2. //
  3. // Copyright (C) 2001 Mike Krueger
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. //
  19. // Linking this library statically or dynamically with other modules is
  20. // making a combined work based on this library. Thus, the terms and
  21. // conditions of the GNU General Public License cover the whole
  22. // combination.
  23. //
  24. // As a special exception, the copyright holders of this library give you
  25. // permission to link this library with independent modules to produce an
  26. // executable, regardless of the license terms of these independent
  27. // modules, and to copy and distribute the resulting executable under
  28. // terms of your choice, provided that you also meet, for each linked
  29. // independent module, the terms and conditions of the license of that
  30. // module. An independent module is a module which is not derived from
  31. // or based on this library. If you modify this library, you may extend
  32. // this exception to your version of the library, but you are not
  33. // obligated to do so. If you do not wish to do so, delete this
  34. // exception statement from your version.
  35. using System;
  36. using System.IO;
  37. namespace CommonMPQ.SharpZipLib.Tar
  38. {
  39. /// <summary>
  40. /// This class represents an entry in a Tar archive. It consists
  41. /// of the entry's header, as well as the entry's File. Entries
  42. /// can be instantiated in one of three ways, depending on how
  43. /// they are to be used.
  44. /// <p>
  45. /// TarEntries that are created from the header bytes read from
  46. /// an archive are instantiated with the TarEntry( byte[] )
  47. /// constructor. These entries will be used when extracting from
  48. /// or listing the contents of an archive. These entries have their
  49. /// header filled in using the header bytes. They also set the File
  50. /// to null, since they reference an archive entry not a file.</p>
  51. /// <p>
  52. /// TarEntries that are created from files that are to be written
  53. /// into an archive are instantiated with the CreateEntryFromFile(string)
  54. /// pseudo constructor. These entries have their header filled in using
  55. /// the File's information. They also keep a reference to the File
  56. /// for convenience when writing entries.</p>
  57. /// <p>
  58. /// Finally, TarEntries can be constructed from nothing but a name.
  59. /// This allows the programmer to construct the entry by hand, for
  60. /// instance when only an InputStream is available for writing to
  61. /// the archive, and the header information is constructed from
  62. /// other information. In this case the header fields are set to
  63. /// defaults and the File is set to null.</p>
  64. /// <see cref="TarHeader"/>
  65. /// </summary>
  66. public class TarEntry : ICloneable
  67. {
  68. #region Constructors
  69. /// <summary>
  70. /// Initialise a default instance of <see cref="TarEntry"/>.
  71. /// </summary>
  72. private TarEntry()
  73. {
  74. header = new TarHeader();
  75. }
  76. /// <summary>
  77. /// Construct an entry from an archive's header bytes. File is set
  78. /// to null.
  79. /// </summary>
  80. /// <param name = "headerBuffer">
  81. /// The header bytes from a tar archive entry.
  82. /// </param>
  83. public TarEntry(byte[] headerBuffer)
  84. {
  85. header = new TarHeader();
  86. header.ParseBuffer(headerBuffer);
  87. }
  88. /// <summary>
  89. /// Construct a TarEntry using the <paramref name="header">header</paramref> provided
  90. /// </summary>
  91. /// <param name="header">Header details for entry</param>
  92. public TarEntry(TarHeader header)
  93. {
  94. if ( header == null )
  95. {
  96. throw new ArgumentNullException("header");
  97. }
  98. this.header = (TarHeader)header.Clone();
  99. }
  100. #endregion
  101. #region ICloneable Members
  102. /// <summary>
  103. /// Clone this tar entry.
  104. /// </summary>
  105. /// <returns>Returns a clone of this entry.</returns>
  106. public object Clone()
  107. {
  108. TarEntry entry = new TarEntry();
  109. entry.file = file;
  110. entry.header = (TarHeader)header.Clone();
  111. entry.Name = Name;
  112. return entry;
  113. }
  114. #endregion
  115. /// <summary>
  116. /// Construct an entry with only a <paramref name="name">name</paramref>.
  117. /// This allows the programmer to construct the entry's header "by hand".
  118. /// </summary>
  119. /// <param name="name">The name to use for the entry</param>
  120. /// <returns>Returns the newly created <see cref="TarEntry"/></returns>
  121. public static TarEntry CreateTarEntry(string name)
  122. {
  123. TarEntry entry = new TarEntry();
  124. TarEntry.NameTarHeader(entry.header, name);
  125. return entry;
  126. }
  127. /// <summary>
  128. /// Construct an entry for a file. File is set to file, and the
  129. /// header is constructed from information from the file.
  130. /// </summary>
  131. /// <param name = "fileName">The file name that the entry represents.</param>
  132. /// <returns>Returns the newly created <see cref="TarEntry"/></returns>
  133. public static TarEntry CreateEntryFromFile(string fileName)
  134. {
  135. TarEntry entry = new TarEntry();
  136. entry.GetFileTarHeader(entry.header, fileName);
  137. return entry;
  138. }
  139. /// <summary>
  140. /// Determine if the two entries are equal. Equality is determined
  141. /// by the header names being equal.
  142. /// </summary>
  143. /// <param name="obj">The <see cref="Object"/> to compare with the current Object.</param>
  144. /// <returns>
  145. /// True if the entries are equal; false if not.
  146. /// </returns>
  147. public override bool Equals(object obj)
  148. {
  149. TarEntry localEntry = obj as TarEntry;
  150. if ( localEntry != null )
  151. {
  152. return Name.Equals(localEntry.Name);
  153. }
  154. return false;
  155. }
  156. /// <summary>
  157. /// Derive a Hash value for the current <see cref="Object"/>
  158. /// </summary>
  159. /// <returns>A Hash code for the current <see cref="Object"/></returns>
  160. public override int GetHashCode()
  161. {
  162. return Name.GetHashCode();
  163. }
  164. /// <summary>
  165. /// Determine if the given entry is a descendant of this entry.
  166. /// Descendancy is determined by the name of the descendant
  167. /// starting with this entry's name.
  168. /// </summary>
  169. /// <param name = "toTest">
  170. /// Entry to be checked as a descendent of this.
  171. /// </param>
  172. /// <returns>
  173. /// True if entry is a descendant of this.
  174. /// </returns>
  175. public bool IsDescendent(TarEntry toTest)
  176. {
  177. if ( toTest == null ) {
  178. throw new ArgumentNullException("toTest");
  179. }
  180. return toTest.Name.StartsWith(Name);
  181. }
  182. /// <summary>
  183. /// Get this entry's header.
  184. /// </summary>
  185. /// <returns>
  186. /// This entry's TarHeader.
  187. /// </returns>
  188. public TarHeader TarHeader
  189. {
  190. get {
  191. return header;
  192. }
  193. }
  194. /// <summary>
  195. /// Get/Set this entry's name.
  196. /// </summary>
  197. public string Name
  198. {
  199. get {
  200. return header.Name;
  201. }
  202. set {
  203. header.Name = value;
  204. }
  205. }
  206. /// <summary>
  207. /// Get/set this entry's user id.
  208. /// </summary>
  209. public int UserId
  210. {
  211. get {
  212. return header.UserId;
  213. }
  214. set {
  215. header.UserId = value;
  216. }
  217. }
  218. /// <summary>
  219. /// Get/set this entry's group id.
  220. /// </summary>
  221. public int GroupId
  222. {
  223. get {
  224. return header.GroupId;
  225. }
  226. set {
  227. header.GroupId = value;
  228. }
  229. }
  230. /// <summary>
  231. /// Get/set this entry's user name.
  232. /// </summary>
  233. public string UserName
  234. {
  235. get {
  236. return header.UserName;
  237. }
  238. set {
  239. header.UserName = value;
  240. }
  241. }
  242. /// <summary>
  243. /// Get/set this entry's group name.
  244. /// </summary>
  245. public string GroupName
  246. {
  247. get {
  248. return header.GroupName;
  249. }
  250. set {
  251. header.GroupName = value;
  252. }
  253. }
  254. /// <summary>
  255. /// Convenience method to set this entry's group and user ids.
  256. /// </summary>
  257. /// <param name="userId">
  258. /// This entry's new user id.
  259. /// </param>
  260. /// <param name="groupId">
  261. /// This entry's new group id.
  262. /// </param>
  263. public void SetIds(int userId, int groupId)
  264. {
  265. UserId = userId;
  266. GroupId = groupId;
  267. }
  268. /// <summary>
  269. /// Convenience method to set this entry's group and user names.
  270. /// </summary>
  271. /// <param name="userName">
  272. /// This entry's new user name.
  273. /// </param>
  274. /// <param name="groupName">
  275. /// This entry's new group name.
  276. /// </param>
  277. public void SetNames(string userName, string groupName)
  278. {
  279. UserName = userName;
  280. GroupName = groupName;
  281. }
  282. /// <summary>
  283. /// Get/Set the modification time for this entry
  284. /// </summary>
  285. public DateTime ModTime {
  286. get {
  287. return header.ModTime;
  288. }
  289. set {
  290. header.ModTime = value;
  291. }
  292. }
  293. /// <summary>
  294. /// Get this entry's file.
  295. /// </summary>
  296. /// <returns>
  297. /// This entry's file.
  298. /// </returns>
  299. public string File {
  300. get {
  301. return file;
  302. }
  303. }
  304. /// <summary>
  305. /// Get/set this entry's recorded file size.
  306. /// </summary>
  307. public long Size {
  308. get {
  309. return header.Size;
  310. }
  311. set {
  312. header.Size = value;
  313. }
  314. }
  315. /// <summary>
  316. /// Return true if this entry represents a directory, false otherwise
  317. /// </summary>
  318. /// <returns>
  319. /// True if this entry is a directory.
  320. /// </returns>
  321. public bool IsDirectory {
  322. get {
  323. if (file != null) {
  324. return Directory.Exists(file);
  325. }
  326. if (header != null) {
  327. if ((header.TypeFlag == TarHeader.LF_DIR) || Name.EndsWith( "/" )) {
  328. return true;
  329. }
  330. }
  331. return false;
  332. }
  333. }
  334. /// <summary>
  335. /// Fill in a TarHeader with information from a File.
  336. /// </summary>
  337. /// <param name="header">
  338. /// The TarHeader to fill in.
  339. /// </param>
  340. /// <param name="file">
  341. /// The file from which to get the header information.
  342. /// </param>
  343. public void GetFileTarHeader(TarHeader header, string file)
  344. {
  345. if ( header == null ) {
  346. throw new ArgumentNullException("header");
  347. }
  348. if ( file == null ) {
  349. throw new ArgumentNullException("file");
  350. }
  351. this.file = file;
  352. // bugfix from torhovl from #D forum:
  353. string name = file;
  354. #if !NETCF_1_0 && !NETCF_2_0
  355. // 23-Jan-2004 GnuTar allows device names in path where the name is not local to the current directory
  356. if (name.IndexOf(Environment.CurrentDirectory) == 0) {
  357. name = name.Substring(Environment.CurrentDirectory.Length);
  358. }
  359. #endif
  360. /*
  361. if (Path.DirectorySeparatorChar == '\\')
  362. {
  363. // check if the OS is Windows
  364. // Strip off drive letters!
  365. if (name.Length > 2)
  366. {
  367. char ch1 = name[0];
  368. char ch2 = name[1];
  369. if (ch2 == ':' && Char.IsLetter(ch1))
  370. {
  371. name = name.Substring(2);
  372. }
  373. }
  374. }
  375. */
  376. name = name.Replace(Path.DirectorySeparatorChar, '/');
  377. // No absolute pathnames
  378. // Windows (and Posix?) paths can start with UNC style "\\NetworkDrive\",
  379. // so we loop on starting /'s.
  380. while (name.StartsWith("/")) {
  381. name = name.Substring(1);
  382. }
  383. header.LinkName = String.Empty;
  384. header.Name = name;
  385. if (Directory.Exists(file)) {
  386. header.Mode = 1003; // Magic number for security access for a UNIX filesystem
  387. header.TypeFlag = TarHeader.LF_DIR;
  388. if ( (header.Name.Length == 0) || header.Name[header.Name.Length - 1] != '/') {
  389. header.Name = header.Name + "/";
  390. }
  391. header.Size = 0;
  392. } else {
  393. header.Mode = 33216; // Magic number for security access for a UNIX filesystem
  394. header.TypeFlag = TarHeader.LF_NORMAL;
  395. header.Size = new FileInfo(file.Replace('/', Path.DirectorySeparatorChar)).Length;
  396. }
  397. header.ModTime = System.IO.File.GetLastWriteTime(file.Replace('/', Path.DirectorySeparatorChar)).ToUniversalTime();
  398. header.DevMajor = 0;
  399. header.DevMinor = 0;
  400. }
  401. /// <summary>
  402. /// Get entries for all files present in this entries directory.
  403. /// If this entry doesnt represent a directory zero entries are returned.
  404. /// </summary>
  405. /// <returns>
  406. /// An array of TarEntry's for this entry's children.
  407. /// </returns>
  408. public TarEntry[] GetDirectoryEntries()
  409. {
  410. if ( (file == null) || !Directory.Exists(file)) {
  411. return new TarEntry[0];
  412. }
  413. string[] list = Directory.GetFileSystemEntries(file);
  414. TarEntry[] result = new TarEntry[list.Length];
  415. for (int i = 0; i < list.Length; ++i) {
  416. result[i] = TarEntry.CreateEntryFromFile(list[i]);
  417. }
  418. return result;
  419. }
  420. /// <summary>
  421. /// Write an entry's header information to a header buffer.
  422. /// </summary>
  423. /// <param name = "outBuffer">
  424. /// The tar entry header buffer to fill in.
  425. /// </param>
  426. public void WriteEntryHeader(byte[] outBuffer)
  427. {
  428. header.WriteHeader(outBuffer);
  429. }
  430. /// <summary>
  431. /// Convenience method that will modify an entry's name directly
  432. /// in place in an entry header buffer byte array.
  433. /// </summary>
  434. /// <param name="buffer">
  435. /// The buffer containing the entry header to modify.
  436. /// </param>
  437. /// <param name="newName">
  438. /// The new name to place into the header buffer.
  439. /// </param>
  440. static public void AdjustEntryName(byte[] buffer, string newName)
  441. {
  442. TarHeader.GetNameBytes(newName, buffer, 0, TarHeader.NAMELEN);
  443. }
  444. /// <summary>
  445. /// Fill in a TarHeader given only the entry's name.
  446. /// </summary>
  447. /// <param name="header">
  448. /// The TarHeader to fill in.
  449. /// </param>
  450. /// <param name="name">
  451. /// The tar entry name.
  452. /// </param>
  453. static public void NameTarHeader(TarHeader header, string name)
  454. {
  455. if ( header == null ) {
  456. throw new ArgumentNullException("header");
  457. }
  458. if ( name == null ) {
  459. throw new ArgumentNullException("name");
  460. }
  461. bool isDir = name.EndsWith("/");
  462. header.Name = name;
  463. header.Mode = isDir ? 1003 : 33216;
  464. header.UserId = 0;
  465. header.GroupId = 0;
  466. header.Size = 0;
  467. header.ModTime = DateTime.UtcNow;
  468. header.TypeFlag = isDir ? TarHeader.LF_DIR : TarHeader.LF_NORMAL;
  469. header.LinkName = String.Empty;
  470. header.UserName = String.Empty;
  471. header.GroupName = String.Empty;
  472. header.DevMajor = 0;
  473. header.DevMinor = 0;
  474. }
  475. #region Instance Fields
  476. /// <summary>
  477. /// The name of the file this entry represents or null if the entry is not based on a file.
  478. /// </summary>
  479. string file;
  480. /// <summary>
  481. /// The entry's header information.
  482. /// </summary>
  483. TarHeader header;
  484. #endregion
  485. }
  486. }
  487. /* The original Java file had this header:
  488. *
  489. ** Authored by Timothy Gerard Endres
  490. ** <mailto:time@gjt.org> <http://www.trustice.com>
  491. **
  492. ** This work has been placed into the public domain.
  493. ** You may use this work in any way and for any purpose you wish.
  494. **
  495. ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
  496. ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
  497. ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
  498. ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
  499. ** REDISTRIBUTION OF THIS SOFTWARE.
  500. **
  501. */