2 // Copyright (C) 2001 Mike Krueger
\r
4 // This program is free software; you can redistribute it and/or
\r
5 // modify it under the terms of the GNU General Public License
\r
6 // as published by the Free Software Foundation; either version 2
\r
7 // of the License, or (at your option) any later version.
\r
9 // This program is distributed in the hope that it will be useful,
\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 // GNU General Public License for more details.
\r
14 // You should have received a copy of the GNU General Public License
\r
15 // along with this program; if not, write to the Free Software
\r
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
18 // Linking this library statically or dynamically with other modules is
\r
19 // making a combined work based on this library. Thus, the terms and
\r
20 // conditions of the GNU General Public License cover the whole
\r
23 // As a special exception, the copyright holders of this library give you
\r
24 // permission to link this library with independent modules to produce an
\r
25 // executable, regardless of the license terms of these independent
\r
26 // modules, and to copy and distribute the resulting executable under
\r
27 // terms of your choice, provided that you also meet, for each linked
\r
28 // independent module, the terms and conditions of the license of that
\r
29 // module. An independent module is a module which is not derived from
\r
30 // or based on this library. If you modify this library, you may extend
\r
31 // this exception to your version of the library, but you are not
\r
32 // obligated to do so. If you do not wish to do so, delete this
\r
33 // exception statement from your version.
\r
39 namespace ICSharpCode.SharpZipLib.Tar
\r
43 /// The TarBuffer class implements the tar archive concept
\r
44 /// of a buffered input stream. This concept goes back to the
\r
45 /// days of blocked tape drives and special io devices. In the
\r
46 /// C# universe, the only real function that this class
\r
47 /// performs is to ensure that files have the correct "record"
\r
48 /// size, or other tars will complain.
\r
50 /// You should never have a need to access this class directly.
\r
51 /// TarBuffers are created by Tar IO Streams.
\r
54 public class TarBuffer
\r
57 /* A quote from GNU tar man file on blocking and records
\r
58 A `tar' archive file contains a series of blocks. Each block
\r
59 contains `BLOCKSIZE' bytes. Although this format may be thought of as
\r
60 being on magnetic tape, other media are often used.
\r
62 Each file archived is represented by a header block which describes
\r
63 the file, followed by zero or more blocks which give the contents of
\r
64 the file. At the end of the archive file there may be a block filled
\r
65 with binary zeros as an end-of-file marker. A reasonable system should
\r
66 write a block of zeros at the end, but must not assume that such a
\r
67 block exists when reading an archive.
\r
69 The blocks may be "blocked" for physical I/O operations. Each
\r
70 record of N blocks (where N is set by the `--blocking-factor=512-SIZE'
\r
71 (`-b 512-SIZE') option to `tar') is written with a single `write ()'
\r
72 operation. On magnetic tapes, the result of such a write is a single
\r
73 record. When writing an archive, the last record of blocks should be
\r
74 written at the full size, with blocks after the zero block containing
\r
75 all zeros. When reading an archive, a reasonable system should
\r
76 properly handle an archive whose last record is shorter than the rest,
\r
77 or which contains garbage records after a zero block.
\r
80 // public static readonly int DEFAULT_RCDSIZE = 512;
\r
81 // public const int DEFAULT_BLOCKFACTOR = 20;
\r
82 // public static readonly int DEFAULT_BLKSIZE = DEFAULT_RCDSIZE * DEFAULT_BLOCKFACTOR;
\r
84 public static readonly int BlockSize = 512;
\r
85 public static readonly int DefaultBlockFactor = 20;
\r
86 public static readonly int DefaultRecordSize = BlockSize * DefaultBlockFactor;
\r
89 Stream outputStream;
\r
91 byte[] recordBuffer;
\r
92 int currentBlockIndex;
\r
93 int currentRecordIndex;
\r
95 int recordSize = DefaultRecordSize;
\r
96 public int RecordSize
\r
98 get { return recordSize; }
\r
101 int blockFactor = DefaultBlockFactor;
\r
103 public int BlockFactor
\r
105 get { return blockFactor; }
\r
108 bool debug = false;
\r
111 /// Set the debugging flag for the buffer.
\r
113 public void SetDebug(bool debug)
\r
115 this.debug = debug;
\r
119 protected TarBuffer()
\r
123 public static TarBuffer CreateInputTarBuffer(Stream inputStream)
\r
125 return CreateInputTarBuffer(inputStream, TarBuffer.DefaultBlockFactor);
\r
128 public static TarBuffer CreateInputTarBuffer(Stream inputStream, int blockFactor)
\r
130 TarBuffer tarBuffer = new TarBuffer();
\r
131 tarBuffer.inputStream = inputStream;
\r
132 tarBuffer.outputStream = null;
\r
133 tarBuffer.Initialize(blockFactor);
\r
138 public static TarBuffer CreateOutputTarBuffer(Stream outputStream)
\r
140 return CreateOutputTarBuffer(outputStream, TarBuffer.DefaultBlockFactor);
\r
143 public static TarBuffer CreateOutputTarBuffer(Stream outputStream, int blockFactor)
\r
145 TarBuffer tarBuffer = new TarBuffer();
\r
146 tarBuffer.inputStream = null;
\r
147 tarBuffer.outputStream = outputStream;
\r
148 tarBuffer.Initialize(blockFactor);
\r
154 /// Initialization common to all constructors.
\r
156 void Initialize(int blockFactor)
\r
158 this.debug = false;
\r
159 this.blockFactor = blockFactor;
\r
160 this.recordSize = blockFactor * BlockSize;
\r
162 this.recordBuffer = new byte[RecordSize];
\r
164 if (inputStream != null)
\r
166 this.currentRecordIndex = -1;
\r
167 this.currentBlockIndex = BlockFactor;
\r
171 this.currentRecordIndex = 0;
\r
172 this.currentBlockIndex = 0;
\r
177 /// Get the TAR Buffer's block factor
\r
179 public int GetBlockFactor()
\r
181 return this.blockFactor;
\r
185 /// Get the TAR Buffer's record size.
\r
187 public int GetRecordSize()
\r
189 return this.recordSize;
\r
193 /// Determine if an archive block indicates End of Archive. End of
\r
194 /// archive is indicated by a block that consists entirely of null bytes.
\r
195 /// All remaining blocks for the record should also be null's
\r
196 /// However some older tars only do a couple of null blocks (Old GNU tar for one)
\r
197 /// and also partial records
\r
199 /// <param name = "block">
\r
200 /// The block data to check.
\r
202 public bool IsEOFBlock(byte[] block)
\r
204 for (int i = 0, sz = BlockSize; i < sz; ++i)
\r
206 if (block[i] != 0)
\r
216 /// Skip over a block on the input stream.
\r
218 public void SkipBlock()
\r
222 //Console.WriteLine.WriteLine("SkipBlock: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex);
\r
225 if (this.inputStream == null)
\r
227 throw new System.IO.IOException("no input stream defined");
\r
230 if (this.currentBlockIndex >= this.BlockFactor)
\r
232 if (!this.ReadRecord())
\r
238 this.currentBlockIndex++;
\r
242 /// Read a block from the input stream and return the data.
\r
245 /// The block data.
\r
247 public byte[] ReadBlock()
\r
251 //Console.WriteLine.WriteLine( "ReadBlock: blockIndex = " + this.currentBlockIndex + " recordIndex = " + this.currentRecordIndex );
\r
254 if (this.inputStream == null)
\r
256 throw new ApplicationException("TarBuffer.ReadBlock - no input stream defined");
\r
259 if (this.currentBlockIndex >= this.BlockFactor)
\r
261 if (!this.ReadRecord())
\r
267 byte[] result = new byte[BlockSize];
\r
269 Array.Copy(this.recordBuffer, (this.currentBlockIndex * BlockSize), result, 0, BlockSize );
\r
270 this.currentBlockIndex++;
\r
275 /// false if End-Of-File, else true
\r
281 //Console.WriteLine.WriteLine("ReadRecord: recordIndex = " + this.currentRecordIndex);
\r
284 if (this.inputStream == null)
\r
286 throw new System.IO.IOException("no input stream stream defined");
\r
289 this.currentBlockIndex = 0;
\r
292 int bytesNeeded = RecordSize;
\r
294 while (bytesNeeded > 0)
\r
296 long numBytes = this.inputStream.Read(this.recordBuffer, offset, bytesNeeded);
\r
300 // We have found EOF, and the record is not full!
\r
302 // This is a broken archive. It does not follow the standard
\r
303 // blocking algorithm. However, because we are generous, and
\r
304 // it requires little effort, we will simply ignore the error
\r
305 // and continue as if the entire record were read. This does
\r
306 // not appear to break anything upstream. We used to return
\r
307 // false in this case.
\r
309 // Thanks to 'Yohann.Roussel@alcatel.fr' for this fix.
\r
311 if (numBytes <= 0)
\r
316 offset += (int)numBytes;
\r
317 bytesNeeded -= (int)numBytes;
\r
318 if (numBytes != RecordSize)
\r
322 //Console.WriteLine.WriteLine("ReadRecord: INCOMPLETE READ " + numBytes + " of " + this.blockSize + " bytes read.");
\r
327 this.currentRecordIndex++;
\r
332 /// Get the current block number, within the current record, zero based.
\r
335 /// The current zero based block number.
\r
337 public int GetCurrentBlockNum()
\r
339 return this.currentBlockIndex;
\r
343 /// Get the current record number
\r
344 /// Absolute block number in file = (currentRecordNum * block factor) + currentBlockNum.
\r
347 /// The current zero based record number.
\r
349 public int GetCurrentRecordNum()
\r
351 return this.currentRecordIndex;
\r
355 /// Write an archive block to the archive.
\r
357 /// <param name="block">
\r
358 /// The data to write to the archive.
\r
361 public void WriteBlock(byte[] block)
\r
365 //Console.WriteLine.WriteLine("WriteRecord: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex );
\r
368 if (this.outputStream == null)
\r
370 throw new ApplicationException("TarBuffer.WriteBlock - no output stream defined");
\r
373 if (block.Length != BlockSize)
\r
375 throw new ApplicationException("TarBuffer.WriteBlock - block to write has length '" + block.Length + "' which is not the block size of '" + BlockSize + "'" );
\r
378 if (this.currentBlockIndex >= BlockFactor)
\r
380 this.WriteRecord();
\r
383 Array.Copy(block, 0, this.recordBuffer, (this.currentBlockIndex * BlockSize), BlockSize);
\r
384 this.currentBlockIndex++;
\r
388 /// Write an archive record to the archive, where the record may be
\r
389 /// inside of a larger array buffer. The buffer must be "offset plus
\r
390 /// record size" long.
\r
392 /// <param name="buf">
\r
393 /// The buffer containing the record data to write.
\r
395 /// <param name="offset">
\r
396 /// The offset of the record data within buf.
\r
398 public void WriteBlock(byte[] buf, int offset)
\r
402 //Console.WriteLine.WriteLine("WriteBlock: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex );
\r
405 if (this.outputStream == null)
\r
407 throw new ApplicationException("TarBuffer.WriteBlock - no output stream stream defined");
\r
410 if ((offset + BlockSize) > buf.Length)
\r
412 throw new ApplicationException("TarBuffer.WriteBlock - record has length '" + buf.Length + "' with offset '" + offset + "' which is less than the record size of '" + this.recordSize + "'" );
\r
415 if (this.currentBlockIndex >= this.BlockFactor)
\r
417 this.WriteRecord();
\r
420 Array.Copy(buf, offset, this.recordBuffer, (this.currentBlockIndex * BlockSize), BlockSize);
\r
422 this.currentBlockIndex++;
\r
426 /// Write a TarBuffer record to the archive.
\r
432 //Console.WriteLine.WriteLine("Writerecord: record index = " + this.currentRecordIndex);
\r
435 if (this.outputStream == null)
\r
437 throw new ApplicationException("TarBuffer.WriteRecord no output stream defined");
\r
440 this.outputStream.Write(this.recordBuffer, 0, RecordSize);
\r
441 this.outputStream.Flush();
\r
443 this.currentBlockIndex = 0;
\r
444 this.currentRecordIndex++;
\r
448 /// Flush the current data block if it has any data in it.
\r
454 //Console.WriteLine.WriteLine("TarBuffer.FlushBlock() called.");
\r
457 if (this.outputStream == null)
\r
459 throw new ApplicationException("TarBuffer.Flush no output stream defined");
\r
462 if (this.currentBlockIndex > 0)
\r
464 this.WriteRecord();
\r
466 outputStream.Flush();
\r
470 /// Close the TarBuffer. If this is an output buffer, also flush the
\r
471 /// current block before closing.
\r
473 public void Close()
\r
477 //Console.WriteLine.WriteLine("TarBuffer.Close().");
\r
480 if (outputStream != null)
\r
484 outputStream.Close();
\r
485 outputStream = null;
\r
487 else if (inputStream != null)
\r
489 inputStream.Close();
\r
490 inputStream = null;
\r
496 /* The original Java file had this header:
\r
498 ** Authored by Timothy Gerard Endres
\r
499 ** <mailto:time@gjt.org> <http://www.trustice.com>
\r
501 ** This work has been placed into the public domain.
\r
502 ** You may use this work in any way and for any purpose you wish.
\r
504 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
\r
505 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
\r
506 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
\r
507 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
\r
508 ** REDISTRIBUTION OF THIS SOFTWARE.
\r