TarBuffer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // TarBuffer.cs
  2. // Copyright (C) 2001 Mike Krueger
  3. //
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU General Public License
  6. // as published by the Free Software Foundation; either version 2
  7. // of the License, or (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. //
  18. // Linking this library statically or dynamically with other modules is
  19. // making a combined work based on this library. Thus, the terms and
  20. // conditions of the GNU General Public License cover the whole
  21. // combination.
  22. //
  23. // As a special exception, the copyright holders of this library give you
  24. // permission to link this library with independent modules to produce an
  25. // executable, regardless of the license terms of these independent
  26. // modules, and to copy and distribute the resulting executable under
  27. // terms of your choice, provided that you also meet, for each linked
  28. // independent module, the terms and conditions of the license of that
  29. // module. An independent module is a module which is not derived from
  30. // or based on this library. If you modify this library, you may extend
  31. // this exception to your version of the library, but you are not
  32. // obligated to do so. If you do not wish to do so, delete this
  33. // exception statement from your version.
  34. using System;
  35. using System.IO;
  36. using System.Text;
  37. namespace ICSharpCode.SharpZipLib.Tar
  38. {
  39. /// <summary>
  40. /// The TarBuffer class implements the tar archive concept
  41. /// of a buffered input stream. This concept goes back to the
  42. /// days of blocked tape drives and special io devices. In the
  43. /// C# universe, the only real function that this class
  44. /// performs is to ensure that files have the correct "record"
  45. /// size, or other tars will complain.
  46. /// <p>
  47. /// You should never have a need to access this class directly.
  48. /// TarBuffers are created by Tar IO Streams.
  49. /// </p>
  50. /// </summary>
  51. public class TarBuffer
  52. {
  53. /* A quote from GNU tar man file on blocking and records
  54. A `tar' archive file contains a series of blocks. Each block
  55. contains `BLOCKSIZE' bytes. Although this format may be thought of as
  56. being on magnetic tape, other media are often used.
  57. Each file archived is represented by a header block which describes
  58. the file, followed by zero or more blocks which give the contents of
  59. the file. At the end of the archive file there may be a block filled
  60. with binary zeros as an end-of-file marker. A reasonable system should
  61. write a block of zeros at the end, but must not assume that such a
  62. block exists when reading an archive.
  63. The blocks may be "blocked" for physical I/O operations. Each
  64. record of N blocks (where N is set by the `--blocking-factor=512-SIZE'
  65. (`-b 512-SIZE') option to `tar') is written with a single `write ()'
  66. operation. On magnetic tapes, the result of such a write is a single
  67. record. When writing an archive, the last record of blocks should be
  68. written at the full size, with blocks after the zero block containing
  69. all zeros. When reading an archive, a reasonable system should
  70. properly handle an archive whose last record is shorter than the rest,
  71. or which contains garbage records after a zero block.
  72. */
  73. // public static readonly int DEFAULT_RCDSIZE = 512;
  74. // public const int DEFAULT_BLOCKFACTOR = 20;
  75. // public static readonly int DEFAULT_BLKSIZE = DEFAULT_RCDSIZE * DEFAULT_BLOCKFACTOR;
  76. public static readonly int BlockSize = 512;
  77. public static readonly int DefaultBlockFactor = 20;
  78. public static readonly int DefaultRecordSize = BlockSize * DefaultBlockFactor;
  79. Stream inputStream;
  80. Stream outputStream;
  81. byte[] recordBuffer;
  82. int currentBlockIndex;
  83. int currentRecordIndex;
  84. int recordSize = DefaultRecordSize;
  85. public int RecordSize
  86. {
  87. get { return recordSize; }
  88. }
  89. int blockFactor = DefaultBlockFactor;
  90. public int BlockFactor
  91. {
  92. get { return blockFactor; }
  93. }
  94. bool debug = false;
  95. /// <summary>
  96. /// Set the debugging flag for the buffer.
  97. /// </summary>
  98. public void SetDebug(bool debug)
  99. {
  100. this.debug = debug;
  101. }
  102. protected TarBuffer()
  103. {
  104. }
  105. public static TarBuffer CreateInputTarBuffer(Stream inputStream)
  106. {
  107. return CreateInputTarBuffer(inputStream, TarBuffer.DefaultBlockFactor);
  108. }
  109. public static TarBuffer CreateInputTarBuffer(Stream inputStream, int blockFactor)
  110. {
  111. TarBuffer tarBuffer = new TarBuffer();
  112. tarBuffer.inputStream = inputStream;
  113. tarBuffer.outputStream = null;
  114. tarBuffer.Initialize(blockFactor);
  115. return tarBuffer;
  116. }
  117. public static TarBuffer CreateOutputTarBuffer(Stream outputStream)
  118. {
  119. return CreateOutputTarBuffer(outputStream, TarBuffer.DefaultBlockFactor);
  120. }
  121. public static TarBuffer CreateOutputTarBuffer(Stream outputStream, int blockFactor)
  122. {
  123. TarBuffer tarBuffer = new TarBuffer();
  124. tarBuffer.inputStream = null;
  125. tarBuffer.outputStream = outputStream;
  126. tarBuffer.Initialize(blockFactor);
  127. return tarBuffer;
  128. }
  129. /// <summary>
  130. /// Initialization common to all constructors.
  131. /// </summary>
  132. void Initialize(int blockFactor)
  133. {
  134. this.debug = false;
  135. this.blockFactor = blockFactor;
  136. this.recordSize = blockFactor * BlockSize;
  137. this.recordBuffer = new byte[RecordSize];
  138. if (inputStream != null)
  139. {
  140. this.currentRecordIndex = -1;
  141. this.currentBlockIndex = BlockFactor;
  142. }
  143. else
  144. {
  145. this.currentRecordIndex = 0;
  146. this.currentBlockIndex = 0;
  147. }
  148. }
  149. /// <summary>
  150. /// Get the TAR Buffer's block factor
  151. /// </summary>
  152. public int GetBlockFactor()
  153. {
  154. return this.blockFactor;
  155. }
  156. /// <summary>
  157. /// Get the TAR Buffer's record size.
  158. /// </summary>
  159. public int GetRecordSize()
  160. {
  161. return this.recordSize;
  162. }
  163. /// <summary>
  164. /// Determine if an archive block indicates End of Archive. End of
  165. /// archive is indicated by a block that consists entirely of null bytes.
  166. /// All remaining blocks for the record should also be null's
  167. /// However some older tars only do a couple of null blocks (Old GNU tar for one)
  168. /// and also partial records
  169. /// </summary>
  170. /// <param name = "block">
  171. /// The block data to check.
  172. /// </param>
  173. public bool IsEOFBlock(byte[] block)
  174. {
  175. for (int i = 0, sz = BlockSize; i < sz; ++i)
  176. {
  177. if (block[i] != 0)
  178. {
  179. return false;
  180. }
  181. }
  182. return true;
  183. }
  184. /// <summary>
  185. /// Skip over a block on the input stream.
  186. /// </summary>
  187. public void SkipBlock()
  188. {
  189. if (this.debug)
  190. {
  191. //Console.WriteLine.WriteLine("SkipBlock: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex);
  192. }
  193. if (this.inputStream == null)
  194. {
  195. throw new System.IO.IOException("no input stream defined");
  196. }
  197. if (this.currentBlockIndex >= this.BlockFactor)
  198. {
  199. if (!this.ReadRecord())
  200. {
  201. return; // UNDONE
  202. }
  203. }
  204. this.currentBlockIndex++;
  205. }
  206. /// <summary>
  207. /// Read a block from the input stream and return the data.
  208. /// </summary>
  209. /// <returns>
  210. /// The block data.
  211. /// </returns>
  212. public byte[] ReadBlock()
  213. {
  214. if (this.debug)
  215. {
  216. //Console.WriteLine.WriteLine( "ReadBlock: blockIndex = " + this.currentBlockIndex + " recordIndex = " + this.currentRecordIndex );
  217. }
  218. if (this.inputStream == null)
  219. {
  220. throw new ApplicationException("TarBuffer.ReadBlock - no input stream defined");
  221. }
  222. if (this.currentBlockIndex >= this.BlockFactor)
  223. {
  224. if (!this.ReadRecord())
  225. {
  226. return null;
  227. }
  228. }
  229. byte[] result = new byte[BlockSize];
  230. Array.Copy(this.recordBuffer, (this.currentBlockIndex * BlockSize), result, 0, BlockSize );
  231. this.currentBlockIndex++;
  232. return result;
  233. }
  234. /// <returns>
  235. /// false if End-Of-File, else true
  236. /// </returns>
  237. bool ReadRecord()
  238. {
  239. if (this.debug)
  240. {
  241. //Console.WriteLine.WriteLine("ReadRecord: recordIndex = " + this.currentRecordIndex);
  242. }
  243. if (this.inputStream == null)
  244. {
  245. throw new System.IO.IOException("no input stream stream defined");
  246. }
  247. this.currentBlockIndex = 0;
  248. int offset = 0;
  249. int bytesNeeded = RecordSize;
  250. while (bytesNeeded > 0)
  251. {
  252. long numBytes = this.inputStream.Read(this.recordBuffer, offset, bytesNeeded);
  253. //
  254. // NOTE
  255. // We have found EOF, and the record is not full!
  256. //
  257. // This is a broken archive. It does not follow the standard
  258. // blocking algorithm. However, because we are generous, and
  259. // it requires little effort, we will simply ignore the error
  260. // and continue as if the entire record were read. This does
  261. // not appear to break anything upstream. We used to return
  262. // false in this case.
  263. //
  264. // Thanks to '[email protected]' for this fix.
  265. //
  266. if (numBytes <= 0)
  267. {
  268. break;
  269. }
  270. offset += (int)numBytes;
  271. bytesNeeded -= (int)numBytes;
  272. if (numBytes != RecordSize)
  273. {
  274. if (this.debug)
  275. {
  276. //Console.WriteLine.WriteLine("ReadRecord: INCOMPLETE READ " + numBytes + " of " + this.blockSize + " bytes read.");
  277. }
  278. }
  279. }
  280. this.currentRecordIndex++;
  281. return true;
  282. }
  283. /// <summary>
  284. /// Get the current block number, within the current record, zero based.
  285. /// </summary>
  286. /// <returns>
  287. /// The current zero based block number.
  288. /// </returns>
  289. public int GetCurrentBlockNum()
  290. {
  291. return this.currentBlockIndex;
  292. }
  293. /// <summary>
  294. /// Get the current record number
  295. /// Absolute block number in file = (currentRecordNum * block factor) + currentBlockNum.
  296. /// </summary>
  297. /// <returns>
  298. /// The current zero based record number.
  299. /// </returns>
  300. public int GetCurrentRecordNum()
  301. {
  302. return this.currentRecordIndex;
  303. }
  304. /// <summary>
  305. /// Write an archive block to the archive.
  306. /// </summary>
  307. /// <param name="block">
  308. /// The data to write to the archive.
  309. /// </param>
  310. ///
  311. public void WriteBlock(byte[] block)
  312. {
  313. if (this.debug)
  314. {
  315. //Console.WriteLine.WriteLine("WriteRecord: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex );
  316. }
  317. if (this.outputStream == null)
  318. {
  319. throw new ApplicationException("TarBuffer.WriteBlock - no output stream defined");
  320. }
  321. if (block.Length != BlockSize)
  322. {
  323. throw new ApplicationException("TarBuffer.WriteBlock - block to write has length '" + block.Length + "' which is not the block size of '" + BlockSize + "'" );
  324. }
  325. if (this.currentBlockIndex >= BlockFactor)
  326. {
  327. this.WriteRecord();
  328. }
  329. Array.Copy(block, 0, this.recordBuffer, (this.currentBlockIndex * BlockSize), BlockSize);
  330. this.currentBlockIndex++;
  331. }
  332. /// <summary>
  333. /// Write an archive record to the archive, where the record may be
  334. /// inside of a larger array buffer. The buffer must be "offset plus
  335. /// record size" long.
  336. /// </summary>
  337. /// <param name="buf">
  338. /// The buffer containing the record data to write.
  339. /// </param>
  340. /// <param name="offset">
  341. /// The offset of the record data within buf.
  342. /// </param>
  343. public void WriteBlock(byte[] buf, int offset)
  344. {
  345. if (this.debug)
  346. {
  347. //Console.WriteLine.WriteLine("WriteBlock: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex );
  348. }
  349. if (this.outputStream == null)
  350. {
  351. throw new ApplicationException("TarBuffer.WriteBlock - no output stream stream defined");
  352. }
  353. if ((offset + BlockSize) > buf.Length)
  354. {
  355. throw new ApplicationException("TarBuffer.WriteBlock - record has length '" + buf.Length + "' with offset '" + offset + "' which is less than the record size of '" + this.recordSize + "'" );
  356. }
  357. if (this.currentBlockIndex >= this.BlockFactor)
  358. {
  359. this.WriteRecord();
  360. }
  361. Array.Copy(buf, offset, this.recordBuffer, (this.currentBlockIndex * BlockSize), BlockSize);
  362. this.currentBlockIndex++;
  363. }
  364. /// <summary>
  365. /// Write a TarBuffer record to the archive.
  366. /// </summary>
  367. void WriteRecord()
  368. {
  369. if (this.debug)
  370. {
  371. //Console.WriteLine.WriteLine("Writerecord: record index = " + this.currentRecordIndex);
  372. }
  373. if (this.outputStream == null)
  374. {
  375. throw new ApplicationException("TarBuffer.WriteRecord no output stream defined");
  376. }
  377. this.outputStream.Write(this.recordBuffer, 0, RecordSize);
  378. this.outputStream.Flush();
  379. this.currentBlockIndex = 0;
  380. this.currentRecordIndex++;
  381. }
  382. /// <summary>
  383. /// Flush the current data block if it has any data in it.
  384. /// </summary>
  385. void Flush()
  386. {
  387. if (this.debug)
  388. {
  389. //Console.WriteLine.WriteLine("TarBuffer.FlushBlock() called.");
  390. }
  391. if (this.outputStream == null)
  392. {
  393. throw new ApplicationException("TarBuffer.Flush no output stream defined");
  394. }
  395. if (this.currentBlockIndex > 0)
  396. {
  397. this.WriteRecord();
  398. }
  399. outputStream.Flush();
  400. }
  401. /// <summary>
  402. /// Close the TarBuffer. If this is an output buffer, also flush the
  403. /// current block before closing.
  404. /// </summary>
  405. public void Close()
  406. {
  407. if (this.debug)
  408. {
  409. //Console.WriteLine.WriteLine("TarBuffer.Close().");
  410. }
  411. if (outputStream != null)
  412. {
  413. Flush();
  414. outputStream.Close();
  415. outputStream = null;
  416. }
  417. else if (inputStream != null)
  418. {
  419. inputStream.Close();
  420. inputStream = null;
  421. }
  422. }
  423. }
  424. }
  425. /* The original Java file had this header:
  426. *
  427. ** Authored by Timothy Gerard Endres
  428. ** <mailto:[email protected]> <http://www.trustice.com>
  429. **
  430. ** This work has been placed into the public domain.
  431. ** You may use this work in any way and for any purpose you wish.
  432. **
  433. ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
  434. ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
  435. ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
  436. ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
  437. ** REDISTRIBUTION OF THIS SOFTWARE.
  438. **
  439. */