View Javadoc
1   /*
2    * Copyright (C) 2008-2011, Google Inc.
3    * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44   */
45  
46  package org.eclipse.jgit.internal.storage.dfs;
47  
48  import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.UNREACHABLE_GARBAGE;
49  import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;
50  import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
51  import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
52  
53  import java.io.BufferedInputStream;
54  import java.io.EOFException;
55  import java.io.IOException;
56  import java.io.InputStream;
57  import java.nio.ByteBuffer;
58  import java.nio.channels.Channels;
59  import java.text.MessageFormat;
60  import java.util.Set;
61  import java.util.zip.CRC32;
62  import java.util.zip.DataFormatException;
63  import java.util.zip.Inflater;
64  
65  import org.eclipse.jgit.errors.CorruptObjectException;
66  import org.eclipse.jgit.errors.LargeObjectException;
67  import org.eclipse.jgit.errors.MissingObjectException;
68  import org.eclipse.jgit.errors.PackInvalidException;
69  import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
70  import org.eclipse.jgit.internal.JGitText;
71  import org.eclipse.jgit.internal.storage.file.PackBitmapIndex;
72  import org.eclipse.jgit.internal.storage.file.PackIndex;
73  import org.eclipse.jgit.internal.storage.file.PackReverseIndex;
74  import org.eclipse.jgit.internal.storage.pack.BinaryDelta;
75  import org.eclipse.jgit.internal.storage.pack.PackExt;
76  import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
77  import org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation;
78  import org.eclipse.jgit.lib.AbbreviatedObjectId;
79  import org.eclipse.jgit.lib.AnyObjectId;
80  import org.eclipse.jgit.lib.Constants;
81  import org.eclipse.jgit.lib.ObjectId;
82  import org.eclipse.jgit.lib.ObjectLoader;
83  import org.eclipse.jgit.lib.Repository;
84  import org.eclipse.jgit.util.LongList;
85  
86  /**
87   * A Git version 2 pack file representation. A pack file contains Git objects in
88   * delta packed format yielding high compression of lots of object where some
89   * objects are similar.
90   */
91  public final class DfsPackFile {
92  	/**
93  	 * File offset used to cache {@link #index} in {@link DfsBlockCache}.
94  	 * <p>
95  	 * To better manage memory, the forward index is stored as a single block in
96  	 * the block cache under this file position. A negative value is used
97  	 * because it cannot occur in a normal pack file, and it is less likely to
98  	 * collide with a valid data block from the file as the high bits will all
99  	 * be set when treated as an unsigned long by the cache code.
100 	 */
101 	private static final long POS_INDEX = -1;
102 
103 	/** Offset used to cache {@link #reverseIndex}. See {@link #POS_INDEX}. */
104 	private static final long POS_REVERSE_INDEX = -2;
105 
106 	/** Offset used to cache {@link #bitmapIndex}. See {@link #POS_INDEX}. */
107 	private static final long POS_BITMAP_INDEX = -3;
108 
109 	/** Cache that owns this pack file and its data. */
110 	private final DfsBlockCache cache;
111 
112 	/** Description of the pack file's storage. */
113 	private final DfsPackDescription packDesc;
114 
115 	/** Unique identity of this pack while in-memory. */
116 	final DfsPackKey key;
117 
118 	/**
119 	 * Total number of bytes in this pack file.
120 	 * <p>
121 	 * This field initializes to -1 and gets populated when a block is loaded.
122 	 */
123 	volatile long length;
124 
125 	/**
126 	 * Preferred alignment for loading blocks from the backing file.
127 	 * <p>
128 	 * It is initialized to 0 and filled in on the first read made from the
129 	 * file. Block sizes may be odd, e.g. 4091, caused by the underling DFS
130 	 * storing 4091 user bytes and 5 bytes block metadata into a lower level
131 	 * 4096 byte block on disk.
132 	 */
133 	private volatile int blockSize;
134 
135 	/** True once corruption has been detected that cannot be worked around. */
136 	private volatile boolean invalid;
137 
138 	/**
139 	 * Lock for initialization of {@link #index} and {@link #corruptObjects}.
140 	 * <p>
141 	 * This lock ensures only one thread can perform the initialization work.
142 	 */
143 	private final Object initLock = new Object();
144 
145 	/** Index mapping {@link ObjectId} to position within the pack stream. */
146 	private volatile DfsBlockCache.Ref<PackIndex> index;
147 
148 	/** Reverse version of {@link #index} mapping position to {@link ObjectId}. */
149 	private volatile DfsBlockCache.Ref<PackReverseIndex> reverseIndex;
150 
151 	/** Index of compressed bitmap mapping entire object graph. */
152 	private volatile DfsBlockCache.Ref<PackBitmapIndex> bitmapIndex;
153 
154 	/**
155 	 * Objects we have tried to read, and discovered to be corrupt.
156 	 * <p>
157 	 * The list is allocated after the first corruption is found, and filled in
158 	 * as more entries are discovered. Typically this list is never used, as
159 	 * pack files do not usually contain corrupt objects.
160 	 */
161 	private volatile LongList corruptObjects;
162 
163 	/**
164 	 * Construct a reader for an existing, packfile.
165 	 *
166 	 * @param cache
167 	 *            cache that owns the pack data.
168 	 * @param desc
169 	 *            description of the pack within the DFS.
170 	 * @param key
171 	 *            interned key used to identify blocks in the block cache.
172 	 */
173 	DfsPackFile(DfsBlockCache cache, DfsPackDescription desc, DfsPackKey key) {
174 		this.cache = cache;
175 		this.packDesc = desc;
176 		this.key = key;
177 
178 		length = desc.getFileSize(PACK);
179 		if (length <= 0)
180 			length = -1;
181 	}
182 
183 	/** @return description that was originally used to configure this pack file. */
184 	public DfsPackDescription getPackDescription() {
185 		return packDesc;
186 	}
187 
188 	/**
189 	 * @return whether the pack index file is loaded and cached in memory.
190 	 */
191 	public boolean isIndexLoaded() {
192 		DfsBlockCache.Ref<PackIndex> idxref = index;
193 		return idxref != null && idxref.has();
194 	}
195 
196 	/** @return bytes cached in memory for this pack, excluding the index. */
197 	public long getCachedSize() {
198 		return key.cachedSize.get();
199 	}
200 
201 	String getPackName() {
202 		return packDesc.getFileName(PACK);
203 	}
204 
205 	void setBlockSize(int newSize) {
206 		blockSize = newSize;
207 	}
208 
209 	void setPackIndex(PackIndex idx) {
210 		long objCnt = idx.getObjectCount();
211 		int recSize = Constants.OBJECT_ID_LENGTH + 8;
212 		int sz = (int) Math.min(objCnt * recSize, Integer.MAX_VALUE);
213 		index = cache.put(key, POS_INDEX, sz, idx);
214 	}
215 
216 	/**
217 	 * Get the PackIndex for this PackFile.
218 	 *
219 	 * @param ctx
220 	 *            reader context to support reading from the backing store if
221 	 *            the index is not already loaded in memory.
222 	 * @return the PackIndex.
223 	 * @throws IOException
224 	 *             the pack index is not available, or is corrupt.
225 	 */
226 	public PackIndex getPackIndex(DfsReader ctx) throws IOException {
227 		return idx(ctx);
228 	}
229 
230 	private PackIndex idx(DfsReader ctx) throws IOException {
231 		DfsBlockCache.Ref<PackIndex> idxref = index;
232 		if (idxref != null) {
233 			PackIndex idx = idxref.get();
234 			if (idx != null)
235 				return idx;
236 		}
237 
238 		if (invalid)
239 			throw new PackInvalidException(getPackName());
240 
241 		Repository.getGlobalListenerList()
242 				.dispatch(new BeforeDfsPackIndexLoadedEvent(this));
243 
244 		synchronized (initLock) {
245 			idxref = index;
246 			if (idxref != null) {
247 				PackIndex idx = idxref.get();
248 				if (idx != null)
249 					return idx;
250 			}
251 
252 			PackIndex idx;
253 			try {
254 				ctx.stats.readIdx++;
255 				long start = System.nanoTime();
256 				ReadableChannel rc = ctx.db.openFile(packDesc, INDEX);
257 				try {
258 					InputStream in = Channels.newInputStream(rc);
259 					int wantSize = 8192;
260 					int bs = rc.blockSize();
261 					if (0 < bs && bs < wantSize)
262 						bs = (wantSize / bs) * bs;
263 					else if (bs <= 0)
264 						bs = wantSize;
265 					idx = PackIndex.read(new BufferedInputStream(in, bs));
266 					ctx.stats.readIdxBytes += rc.position();
267 				} finally {
268 					rc.close();
269 					ctx.stats.readIdxMicros += elapsedMicros(start);
270 				}
271 			} catch (EOFException e) {
272 				invalid = true;
273 				IOException e2 = new IOException(MessageFormat.format(
274 						DfsText.get().shortReadOfIndex,
275 						packDesc.getFileName(INDEX)));
276 				e2.initCause(e);
277 				throw e2;
278 			} catch (IOException e) {
279 				invalid = true;
280 				IOException e2 = new IOException(MessageFormat.format(
281 						DfsText.get().cannotReadIndex,
282 						packDesc.getFileName(INDEX)));
283 				e2.initCause(e);
284 				throw e2;
285 			}
286 
287 			setPackIndex(idx);
288 			return idx;
289 		}
290 	}
291 
292 	private static long elapsedMicros(long start) {
293 		return (System.nanoTime() - start) / 1000L;
294 	}
295 
296 	final boolean isGarbage() {
297 		return packDesc.getPackSource() == UNREACHABLE_GARBAGE;
298 	}
299 
300 	PackBitmapIndex getBitmapIndex(DfsReader ctx) throws IOException {
301 		if (invalid || isGarbage())
302 			return null;
303 		DfsBlockCache.Ref<PackBitmapIndex> idxref = bitmapIndex;
304 		if (idxref != null) {
305 			PackBitmapIndex idx = idxref.get();
306 			if (idx != null)
307 				return idx;
308 		}
309 
310 		if (!packDesc.hasFileExt(PackExt.BITMAP_INDEX))
311 			return null;
312 
313 		synchronized (initLock) {
314 			idxref = bitmapIndex;
315 			if (idxref != null) {
316 				PackBitmapIndex idx = idxref.get();
317 				if (idx != null)
318 					return idx;
319 			}
320 
321 			long size;
322 			PackBitmapIndex idx;
323 			try {
324 				ctx.stats.readBitmap++;
325 				long start = System.nanoTime();
326 				ReadableChannel rc = ctx.db.openFile(packDesc, BITMAP_INDEX);
327 				try {
328 					InputStream in = Channels.newInputStream(rc);
329 					int wantSize = 8192;
330 					int bs = rc.blockSize();
331 					if (0 < bs && bs < wantSize)
332 						bs = (wantSize / bs) * bs;
333 					else if (bs <= 0)
334 						bs = wantSize;
335 					in = new BufferedInputStream(in, bs);
336 					idx = PackBitmapIndex.read(
337 							in, idx(ctx), getReverseIdx(ctx));
338 				} finally {
339 					size = rc.position();
340 					rc.close();
341 					ctx.stats.readIdxBytes += size;
342 					ctx.stats.readIdxMicros += elapsedMicros(start);
343 				}
344 			} catch (EOFException e) {
345 				IOException e2 = new IOException(MessageFormat.format(
346 						DfsText.get().shortReadOfIndex,
347 						packDesc.getFileName(BITMAP_INDEX)));
348 				e2.initCause(e);
349 				throw e2;
350 			} catch (IOException e) {
351 				IOException e2 = new IOException(MessageFormat.format(
352 						DfsText.get().cannotReadIndex,
353 						packDesc.getFileName(BITMAP_INDEX)));
354 				e2.initCause(e);
355 				throw e2;
356 			}
357 
358 			bitmapIndex = cache.put(key, POS_BITMAP_INDEX,
359 					(int) Math.min(size, Integer.MAX_VALUE), idx);
360 			return idx;
361 		}
362 	}
363 
364 	PackReverseIndex getReverseIdx(DfsReader ctx) throws IOException {
365 		DfsBlockCache.Ref<PackReverseIndex> revref = reverseIndex;
366 		if (revref != null) {
367 			PackReverseIndex revidx = revref.get();
368 			if (revidx != null)
369 				return revidx;
370 		}
371 
372 		synchronized (initLock) {
373 			revref = reverseIndex;
374 			if (revref != null) {
375 				PackReverseIndex revidx = revref.get();
376 				if (revidx != null)
377 					return revidx;
378 			}
379 
380 			PackIndex idx = idx(ctx);
381 			PackReverseIndex revidx = new PackReverseIndex(idx);
382 			int sz = (int) Math.min(
383 					idx.getObjectCount() * 8, Integer.MAX_VALUE);
384 			reverseIndex = cache.put(key, POS_REVERSE_INDEX, sz, revidx);
385 			return revidx;
386 		}
387 	}
388 
389 	/**
390 	 * Check if an object is stored within this pack.
391 	 *
392 	 * @param ctx
393 	 *            reader context to support reading from the backing store if
394 	 *            the index is not already loaded in memory.
395 	 * @param id
396 	 *            object to be located.
397 	 * @return true if the object exists in this pack; false if it does not.
398 	 * @throws IOException
399 	 *             the pack index is not available, or is corrupt.
400 	 */
401 	public boolean hasObject(DfsReader ctx, AnyObjectId id) throws IOException {
402 		final long offset = idx(ctx).findOffset(id);
403 		return 0 < offset && !isCorrupt(offset);
404 	}
405 
406 	/**
407 	 * Get an object from this pack.
408 	 *
409 	 * @param ctx
410 	 *            temporary working space associated with the calling thread.
411 	 * @param id
412 	 *            the object to obtain from the pack. Must not be null.
413 	 * @return the object loader for the requested object if it is contained in
414 	 *         this pack; null if the object was not found.
415 	 * @throws IOException
416 	 *             the pack file or the index could not be read.
417 	 */
418 	ObjectLoader get(DfsReader ctx, AnyObjectId id)
419 			throws IOException {
420 		long offset = idx(ctx).findOffset(id);
421 		return 0 < offset && !isCorrupt(offset) ? load(ctx, offset) : null;
422 	}
423 
424 	long findOffset(DfsReader ctx, AnyObjectId id) throws IOException {
425 		return idx(ctx).findOffset(id);
426 	}
427 
428 	void resolve(DfsReader ctx, Set<ObjectId> matches, AbbreviatedObjectId id,
429 			int matchLimit) throws IOException {
430 		idx(ctx).resolve(matches, id, matchLimit);
431 	}
432 
433 	/** Release all memory used by this DfsPackFile instance. */
434 	public void close() {
435 		cache.remove(this);
436 		index = null;
437 		reverseIndex = null;
438 	}
439 
440 	/**
441 	 * Obtain the total number of objects available in this pack. This method
442 	 * relies on pack index, giving number of effectively available objects.
443 	 *
444 	 * @param ctx
445 	 *            current reader for the calling thread.
446 	 * @return number of objects in index of this pack, likewise in this pack
447 	 * @throws IOException
448 	 *             the index file cannot be loaded into memory.
449 	 */
450 	long getObjectCount(DfsReader ctx) throws IOException {
451 		return idx(ctx).getObjectCount();
452 	}
453 
454 	private byte[] decompress(long position, int sz, DfsReader ctx)
455 			throws IOException, DataFormatException {
456 		byte[] dstbuf;
457 		try {
458 			dstbuf = new byte[sz];
459 		} catch (OutOfMemoryError noMemory) {
460 			// The size may be larger than our heap allows, return null to
461 			// let the caller know allocation isn't possible and it should
462 			// use the large object streaming approach instead.
463 			//
464 			// For example, this can occur when sz is 640 MB, and JRE
465 			// maximum heap size is only 256 MB. Even if the JRE has
466 			// 200 MB free, it cannot allocate a 640 MB byte array.
467 			return null;
468 		}
469 
470 		if (ctx.inflate(this, position, dstbuf, false) != sz)
471 			throw new EOFException(MessageFormat.format(
472 					JGitText.get().shortCompressedStreamAt,
473 					Long.valueOf(position)));
474 		return dstbuf;
475 	}
476 
477 	void copyPackAsIs(PackOutputStream out, DfsReader ctx)
478 			throws IOException {
479 		// If the length hasn't been determined yet, pin to set it.
480 		if (length == -1) {
481 			ctx.pin(this, 0);
482 			ctx.unpin();
483 		}
484 		if (cache.shouldCopyThroughCache(length))
485 			copyPackThroughCache(out, ctx);
486 		else
487 			copyPackBypassCache(out, ctx);
488 	}
489 
490 	private void copyPackThroughCache(PackOutputStream out, DfsReader ctx)
491 			throws IOException {
492 		long position = 12;
493 		long remaining = length - (12 + 20);
494 		while (0 < remaining) {
495 			DfsBlock b = cache.getOrLoad(this, position, ctx);
496 			int ptr = (int) (position - b.start);
497 			int n = (int) Math.min(b.size() - ptr, remaining);
498 			b.write(out, position, n);
499 			position += n;
500 			remaining -= n;
501 		}
502 	}
503 
504 	private long copyPackBypassCache(PackOutputStream out, DfsReader ctx)
505 			throws IOException {
506 		try (ReadableChannel rc = ctx.db.openFile(packDesc, PACK)) {
507 			ByteBuffer buf = newCopyBuffer(out, rc);
508 			if (ctx.getOptions().getStreamPackBufferSize() > 0)
509 				rc.setReadAheadBytes(ctx.getOptions().getStreamPackBufferSize());
510 			long position = 12;
511 			long remaining = length - (12 + 20);
512 			boolean packHeadSkipped = false;
513 			while (0 < remaining) {
514 				DfsBlock b = cache.get(key, alignToBlock(position));
515 				if (b != null) {
516 					int ptr = (int) (position - b.start);
517 					int n = (int) Math.min(b.size() - ptr, remaining);
518 					b.write(out, position, n);
519 					position += n;
520 					remaining -= n;
521 					rc.position(position);
522 					packHeadSkipped = true;
523 					continue;
524 				}
525 
526 				buf.position(0);
527 				int n = read(rc, buf);
528 				if (n <= 0)
529 					throw packfileIsTruncated();
530 				else if (n > remaining)
531 					n = (int) remaining;
532 
533 				if (!packHeadSkipped) {
534 					// Need skip the 'PACK' header for the first read
535 					out.write(buf.array(), 12, n - 12);
536 					packHeadSkipped = true;
537 				} else {
538 					out.write(buf.array(), 0, n);
539 				}
540 				position += n;
541 				remaining -= n;
542 			}
543 			return position;
544 		}
545 	}
546 
547 	private ByteBuffer newCopyBuffer(PackOutputStream out, ReadableChannel rc) {
548 		int bs = blockSize(rc);
549 		byte[] copyBuf = out.getCopyBuffer();
550 		if (bs > copyBuf.length)
551 			copyBuf = new byte[bs];
552 		return ByteBuffer.wrap(copyBuf, 0, bs);
553 	}
554 
555 	void copyAsIs(PackOutputStream out, DfsObjectToPack src,
556 			boolean validate, DfsReader ctx) throws IOException,
557 			StoredObjectRepresentationNotAvailableException {
558 		final CRC32 crc1 = validate ? new CRC32() : null;
559 		final CRC32 crc2 = validate ? new CRC32() : null;
560 		final byte[] buf = out.getCopyBuffer();
561 
562 		// Rip apart the header so we can discover the size.
563 		//
564 		try {
565 			readFully(src.offset, buf, 0, 20, ctx);
566 		} catch (IOException ioError) {
567 			StoredObjectRepresentationNotAvailableException gone;
568 			gone = new StoredObjectRepresentationNotAvailableException(src);
569 			gone.initCause(ioError);
570 			throw gone;
571 		}
572 		int c = buf[0] & 0xff;
573 		final int typeCode = (c >> 4) & 7;
574 		long inflatedLength = c & 15;
575 		int shift = 4;
576 		int headerCnt = 1;
577 		while ((c & 0x80) != 0) {
578 			c = buf[headerCnt++] & 0xff;
579 			inflatedLength += ((long) (c & 0x7f)) << shift;
580 			shift += 7;
581 		}
582 
583 		if (typeCode == Constants.OBJ_OFS_DELTA) {
584 			do {
585 				c = buf[headerCnt++] & 0xff;
586 			} while ((c & 128) != 0);
587 			if (validate) {
588 				assert(crc1 != null && crc2 != null);
589 				crc1.update(buf, 0, headerCnt);
590 				crc2.update(buf, 0, headerCnt);
591 			}
592 		} else if (typeCode == Constants.OBJ_REF_DELTA) {
593 			if (validate) {
594 				assert(crc1 != null && crc2 != null);
595 				crc1.update(buf, 0, headerCnt);
596 				crc2.update(buf, 0, headerCnt);
597 			}
598 
599 			readFully(src.offset + headerCnt, buf, 0, 20, ctx);
600 			if (validate) {
601 				assert(crc1 != null && crc2 != null);
602 				crc1.update(buf, 0, 20);
603 				crc2.update(buf, 0, 20);
604 			}
605 			headerCnt += 20;
606 		} else if (validate) {
607 			assert(crc1 != null && crc2 != null);
608 			crc1.update(buf, 0, headerCnt);
609 			crc2.update(buf, 0, headerCnt);
610 		}
611 
612 		final long dataOffset = src.offset + headerCnt;
613 		final long dataLength = src.length;
614 		final long expectedCRC;
615 		final DfsBlock quickCopy;
616 
617 		// Verify the object isn't corrupt before sending. If it is,
618 		// we report it missing instead.
619 		//
620 		try {
621 			quickCopy = ctx.quickCopy(this, dataOffset, dataLength);
622 
623 			if (validate && idx(ctx).hasCRC32Support()) {
624 				assert(crc1 != null);
625 				// Index has the CRC32 code cached, validate the object.
626 				//
627 				expectedCRC = idx(ctx).findCRC32(src);
628 				if (quickCopy != null) {
629 					quickCopy.crc32(crc1, dataOffset, (int) dataLength);
630 				} else {
631 					long pos = dataOffset;
632 					long cnt = dataLength;
633 					while (cnt > 0) {
634 						final int n = (int) Math.min(cnt, buf.length);
635 						readFully(pos, buf, 0, n, ctx);
636 						crc1.update(buf, 0, n);
637 						pos += n;
638 						cnt -= n;
639 					}
640 				}
641 				if (crc1.getValue() != expectedCRC) {
642 					setCorrupt(src.offset);
643 					throw new CorruptObjectException(MessageFormat.format(
644 							JGitText.get().objectAtHasBadZlibStream,
645 							Long.valueOf(src.offset), getPackName()));
646 				}
647 			} else if (validate) {
648 				assert(crc1 != null);
649 				// We don't have a CRC32 code in the index, so compute it
650 				// now while inflating the raw data to get zlib to tell us
651 				// whether or not the data is safe.
652 				//
653 				Inflater inf = ctx.inflater();
654 				byte[] tmp = new byte[1024];
655 				if (quickCopy != null) {
656 					quickCopy.check(inf, tmp, dataOffset, (int) dataLength);
657 				} else {
658 					long pos = dataOffset;
659 					long cnt = dataLength;
660 					while (cnt > 0) {
661 						final int n = (int) Math.min(cnt, buf.length);
662 						readFully(pos, buf, 0, n, ctx);
663 						crc1.update(buf, 0, n);
664 						inf.setInput(buf, 0, n);
665 						while (inf.inflate(tmp, 0, tmp.length) > 0)
666 							continue;
667 						pos += n;
668 						cnt -= n;
669 					}
670 				}
671 				if (!inf.finished() || inf.getBytesRead() != dataLength) {
672 					setCorrupt(src.offset);
673 					throw new EOFException(MessageFormat.format(
674 							JGitText.get().shortCompressedStreamAt,
675 							Long.valueOf(src.offset)));
676 				}
677 				expectedCRC = crc1.getValue();
678 			} else {
679 				expectedCRC = -1;
680 			}
681 		} catch (DataFormatException dataFormat) {
682 			setCorrupt(src.offset);
683 
684 			CorruptObjectException corruptObject = new CorruptObjectException(
685 					MessageFormat.format(
686 							JGitText.get().objectAtHasBadZlibStream,
687 							Long.valueOf(src.offset), getPackName()));
688 			corruptObject.initCause(dataFormat);
689 
690 			StoredObjectRepresentationNotAvailableException gone;
691 			gone = new StoredObjectRepresentationNotAvailableException(src);
692 			gone.initCause(corruptObject);
693 			throw gone;
694 
695 		} catch (IOException ioError) {
696 			StoredObjectRepresentationNotAvailableException gone;
697 			gone = new StoredObjectRepresentationNotAvailableException(src);
698 			gone.initCause(ioError);
699 			throw gone;
700 		}
701 
702 		if (quickCopy != null) {
703 			// The entire object fits into a single byte array window slice,
704 			// and we have it pinned.  Write this out without copying.
705 			//
706 			out.writeHeader(src, inflatedLength);
707 			quickCopy.write(out, dataOffset, (int) dataLength);
708 
709 		} else if (dataLength <= buf.length) {
710 			// Tiny optimization: Lots of objects are very small deltas or
711 			// deflated commits that are likely to fit in the copy buffer.
712 			//
713 			if (!validate) {
714 				long pos = dataOffset;
715 				long cnt = dataLength;
716 				while (cnt > 0) {
717 					final int n = (int) Math.min(cnt, buf.length);
718 					readFully(pos, buf, 0, n, ctx);
719 					pos += n;
720 					cnt -= n;
721 				}
722 			}
723 			out.writeHeader(src, inflatedLength);
724 			out.write(buf, 0, (int) dataLength);
725 		} else {
726 			// Now we are committed to sending the object. As we spool it out,
727 			// check its CRC32 code to make sure there wasn't corruption between
728 			// the verification we did above, and us actually outputting it.
729 			//
730 			out.writeHeader(src, inflatedLength);
731 			long pos = dataOffset;
732 			long cnt = dataLength;
733 			while (cnt > 0) {
734 				final int n = (int) Math.min(cnt, buf.length);
735 				readFully(pos, buf, 0, n, ctx);
736 				if (validate) {
737 					assert(crc2 != null);
738 					crc2.update(buf, 0, n);
739 				}
740 				out.write(buf, 0, n);
741 				pos += n;
742 				cnt -= n;
743 			}
744 			if (validate) {
745 				assert(crc2 != null);
746 				if (crc2.getValue() != expectedCRC) {
747 					throw new CorruptObjectException(MessageFormat.format(
748 							JGitText.get().objectAtHasBadZlibStream,
749 							Long.valueOf(src.offset), getPackName()));
750 				}
751 			}
752 		}
753 	}
754 
755 	boolean invalid() {
756 		return invalid;
757 	}
758 
759 	void setInvalid() {
760 		invalid = true;
761 	}
762 
763 	private IOException packfileIsTruncated() {
764 		invalid = true;
765 		return new IOException(MessageFormat.format(
766 				JGitText.get().packfileIsTruncated, getPackName()));
767 	}
768 
769 	private void readFully(long position, byte[] dstbuf, int dstoff, int cnt,
770 			DfsReader ctx) throws IOException {
771 		if (ctx.copy(this, position, dstbuf, dstoff, cnt) != cnt)
772 			throw new EOFException();
773 	}
774 
775 	long alignToBlock(long pos) {
776 		int size = blockSize;
777 		if (size == 0)
778 			size = cache.getBlockSize();
779 		return (pos / size) * size;
780 	}
781 
782 	DfsBlock getOrLoadBlock(long pos, DfsReader ctx) throws IOException {
783 		return cache.getOrLoad(this, pos, ctx);
784 	}
785 
786 	DfsBlock readOneBlock(long pos, DfsReader ctx)
787 			throws IOException {
788 		if (invalid)
789 			throw new PackInvalidException(getPackName());
790 
791 		ctx.stats.readBlock++;
792 		long start = System.nanoTime();
793 		ReadableChannel rc = ctx.db.openFile(packDesc, PACK);
794 		try {
795 			int size = blockSize(rc);
796 			pos = (pos / size) * size;
797 
798 			// If the size of the file is not yet known, try to discover it.
799 			// Channels may choose to return -1 to indicate they don't
800 			// know the length yet, in this case read up to the size unit
801 			// given by the caller, then recheck the length.
802 			long len = length;
803 			if (len < 0) {
804 				len = rc.size();
805 				if (0 <= len)
806 					length = len;
807 			}
808 
809 			if (0 <= len && len < pos + size)
810 				size = (int) (len - pos);
811 			if (size <= 0)
812 				throw new EOFException(MessageFormat.format(
813 						DfsText.get().shortReadOfBlock, Long.valueOf(pos),
814 						getPackName(), Long.valueOf(0), Long.valueOf(0)));
815 
816 			byte[] buf = new byte[size];
817 			rc.position(pos);
818 			int cnt = read(rc, ByteBuffer.wrap(buf, 0, size));
819 			ctx.stats.readBlockBytes += cnt;
820 			if (cnt != size) {
821 				if (0 <= len) {
822 					throw new EOFException(MessageFormat.format(
823 						    DfsText.get().shortReadOfBlock,
824 						    Long.valueOf(pos),
825 						    getPackName(),
826 						    Integer.valueOf(size),
827 						    Integer.valueOf(cnt)));
828 				}
829 
830 				// Assume the entire thing was read in a single shot, compact
831 				// the buffer to only the space required.
832 				byte[] n = new byte[cnt];
833 				System.arraycopy(buf, 0, n, 0, n.length);
834 				buf = n;
835 			} else if (len < 0) {
836 				// With no length at the start of the read, the channel should
837 				// have the length available at the end.
838 				length = len = rc.size();
839 			}
840 
841 			return new DfsBlock(key, pos, buf);
842 		} finally {
843 			rc.close();
844 			ctx.stats.readBlockMicros += elapsedMicros(start);
845 		}
846 	}
847 
848 	private int blockSize(ReadableChannel rc) {
849 		// If the block alignment is not yet known, discover it. Prefer the
850 		// larger size from either the cache or the file itself.
851 		int size = blockSize;
852 		if (size == 0) {
853 			size = rc.blockSize();
854 			if (size <= 0)
855 				size = cache.getBlockSize();
856 			else if (size < cache.getBlockSize())
857 				size = (cache.getBlockSize() / size) * size;
858 			blockSize = size;
859 		}
860 		return size;
861 	}
862 
863 	private static int read(ReadableChannel rc, ByteBuffer buf)
864 			throws IOException {
865 		int n;
866 		do {
867 			n = rc.read(buf);
868 		} while (0 < n && buf.hasRemaining());
869 		return buf.position();
870 	}
871 
872 	ObjectLoader load(DfsReader ctx, long pos)
873 			throws IOException {
874 		try {
875 			final byte[] ib = ctx.tempId;
876 			Delta delta = null;
877 			byte[] data = null;
878 			int type = Constants.OBJ_BAD;
879 			boolean cached = false;
880 
881 			SEARCH: for (;;) {
882 				readFully(pos, ib, 0, 20, ctx);
883 				int c = ib[0] & 0xff;
884 				final int typeCode = (c >> 4) & 7;
885 				long sz = c & 15;
886 				int shift = 4;
887 				int p = 1;
888 				while ((c & 0x80) != 0) {
889 					c = ib[p++] & 0xff;
890 					sz += ((long) (c & 0x7f)) << shift;
891 					shift += 7;
892 				}
893 
894 				switch (typeCode) {
895 				case Constants.OBJ_COMMIT:
896 				case Constants.OBJ_TREE:
897 				case Constants.OBJ_BLOB:
898 				case Constants.OBJ_TAG: {
899 					if (delta != null) {
900 						data = decompress(pos + p, (int) sz, ctx);
901 						type = typeCode;
902 						break SEARCH;
903 					}
904 
905 					if (sz < ctx.getStreamFileThreshold()) {
906 						data = decompress(pos + p, (int) sz, ctx);
907 						if (data != null)
908 							return new ObjectLoader.SmallObject(typeCode, data);
909 					}
910 					return new LargePackedWholeObject(typeCode, sz, pos, p, this, ctx.db);
911 				}
912 
913 				case Constants.OBJ_OFS_DELTA: {
914 					c = ib[p++] & 0xff;
915 					long base = c & 127;
916 					while ((c & 128) != 0) {
917 						base += 1;
918 						c = ib[p++] & 0xff;
919 						base <<= 7;
920 						base += (c & 127);
921 					}
922 					base = pos - base;
923 					delta = new Delta(delta, pos, (int) sz, p, base);
924 					if (sz != delta.deltaSize)
925 						break SEARCH;
926 
927 					DeltaBaseCache.Entry e = ctx.getDeltaBaseCache().get(key, base);
928 					if (e != null) {
929 						type = e.type;
930 						data = e.data;
931 						cached = true;
932 						break SEARCH;
933 					}
934 					pos = base;
935 					continue SEARCH;
936 				}
937 
938 				case Constants.OBJ_REF_DELTA: {
939 					readFully(pos + p, ib, 0, 20, ctx);
940 					long base = findDeltaBase(ctx, ObjectId.fromRaw(ib));
941 					delta = new Delta(delta, pos, (int) sz, p + 20, base);
942 					if (sz != delta.deltaSize)
943 						break SEARCH;
944 
945 					DeltaBaseCache.Entry e = ctx.getDeltaBaseCache().get(key, base);
946 					if (e != null) {
947 						type = e.type;
948 						data = e.data;
949 						cached = true;
950 						break SEARCH;
951 					}
952 					pos = base;
953 					continue SEARCH;
954 				}
955 
956 				default:
957 					throw new IOException(MessageFormat.format(
958 							JGitText.get().unknownObjectType, Integer.valueOf(typeCode)));
959 				}
960 			}
961 
962 			// At this point there is at least one delta to apply to data.
963 			// (Whole objects with no deltas to apply return early above.)
964 
965 			if (data == null)
966 				throw new LargeObjectException();
967 
968 			assert(delta != null);
969 			do {
970 				// Cache only the base immediately before desired object.
971 				if (cached)
972 					cached = false;
973 				else if (delta.next == null)
974 					ctx.getDeltaBaseCache().put(key, delta.basePos, type, data);
975 
976 				pos = delta.deltaPos;
977 
978 				byte[] cmds = decompress(pos + delta.hdrLen, delta.deltaSize, ctx);
979 				if (cmds == null) {
980 					data = null; // Discard base in case of OutOfMemoryError
981 					throw new LargeObjectException();
982 				}
983 
984 				final long sz = BinaryDelta.getResultSize(cmds);
985 				if (Integer.MAX_VALUE <= sz)
986 					throw new LargeObjectException.ExceedsByteArrayLimit();
987 
988 				final byte[] result;
989 				try {
990 					result = new byte[(int) sz];
991 				} catch (OutOfMemoryError tooBig) {
992 					data = null; // Discard base in case of OutOfMemoryError
993 					cmds = null;
994 					throw new LargeObjectException.OutOfMemory(tooBig);
995 				}
996 
997 				BinaryDelta.apply(data, cmds, result);
998 				data = result;
999 				delta = delta.next;
1000 			} while (delta != null);
1001 
1002 			return new ObjectLoader.SmallObject(type, data);
1003 
1004 		} catch (DataFormatException dfe) {
1005 			CorruptObjectException coe = new CorruptObjectException(
1006 					MessageFormat.format(
1007 							JGitText.get().objectAtHasBadZlibStream, Long.valueOf(pos),
1008 							getPackName()));
1009 			coe.initCause(dfe);
1010 			throw coe;
1011 		}
1012 	}
1013 
1014 	private long findDeltaBase(DfsReader ctx, ObjectId baseId)
1015 			throws IOException, MissingObjectException {
1016 		long ofs = idx(ctx).findOffset(baseId);
1017 		if (ofs < 0)
1018 			throw new MissingObjectException(baseId,
1019 					JGitText.get().missingDeltaBase);
1020 		return ofs;
1021 	}
1022 
1023 	private static class Delta {
1024 		/** Child that applies onto this object. */
1025 		final Delta next;
1026 
1027 		/** Offset of the delta object. */
1028 		final long deltaPos;
1029 
1030 		/** Size of the inflated delta stream. */
1031 		final int deltaSize;
1032 
1033 		/** Total size of the delta's pack entry header (including base). */
1034 		final int hdrLen;
1035 
1036 		/** Offset of the base object this delta applies onto. */
1037 		final long basePos;
1038 
1039 		Delta(Delta next, long ofs, int sz, int hdrLen, long baseOffset) {
1040 			this.next = next;
1041 			this.deltaPos = ofs;
1042 			this.deltaSize = sz;
1043 			this.hdrLen = hdrLen;
1044 			this.basePos = baseOffset;
1045 		}
1046 	}
1047 
1048 	byte[] getDeltaHeader(DfsReader wc, long pos)
1049 			throws IOException, DataFormatException {
1050 		// The delta stream starts as two variable length integers. If we
1051 		// assume they are 64 bits each, we need 16 bytes to encode them,
1052 		// plus 2 extra bytes for the variable length overhead. So 18 is
1053 		// the longest delta instruction header.
1054 		//
1055 		final byte[] hdr = new byte[32];
1056 		wc.inflate(this, pos, hdr, true /* header only */);
1057 		return hdr;
1058 	}
1059 
1060 	int getObjectType(DfsReader ctx, long pos) throws IOException {
1061 		final byte[] ib = ctx.tempId;
1062 		for (;;) {
1063 			readFully(pos, ib, 0, 20, ctx);
1064 			int c = ib[0] & 0xff;
1065 			final int type = (c >> 4) & 7;
1066 
1067 			switch (type) {
1068 			case Constants.OBJ_COMMIT:
1069 			case Constants.OBJ_TREE:
1070 			case Constants.OBJ_BLOB:
1071 			case Constants.OBJ_TAG:
1072 				return type;
1073 
1074 			case Constants.OBJ_OFS_DELTA: {
1075 				int p = 1;
1076 				while ((c & 0x80) != 0)
1077 					c = ib[p++] & 0xff;
1078 				c = ib[p++] & 0xff;
1079 				long ofs = c & 127;
1080 				while ((c & 128) != 0) {
1081 					ofs += 1;
1082 					c = ib[p++] & 0xff;
1083 					ofs <<= 7;
1084 					ofs += (c & 127);
1085 				}
1086 				pos = pos - ofs;
1087 				continue;
1088 			}
1089 
1090 			case Constants.OBJ_REF_DELTA: {
1091 				int p = 1;
1092 				while ((c & 0x80) != 0)
1093 					c = ib[p++] & 0xff;
1094 				readFully(pos + p, ib, 0, 20, ctx);
1095 				pos = findDeltaBase(ctx, ObjectId.fromRaw(ib));
1096 				continue;
1097 			}
1098 
1099 			default:
1100 				throw new IOException(MessageFormat.format(
1101 						JGitText.get().unknownObjectType, Integer.valueOf(type)));
1102 			}
1103 		}
1104 	}
1105 
1106 	long getObjectSize(DfsReader ctx, AnyObjectId id) throws IOException {
1107 		final long offset = idx(ctx).findOffset(id);
1108 		return 0 < offset ? getObjectSize(ctx, offset) : -1;
1109 	}
1110 
1111 	long getObjectSize(DfsReader ctx, long pos)
1112 			throws IOException {
1113 		final byte[] ib = ctx.tempId;
1114 		readFully(pos, ib, 0, 20, ctx);
1115 		int c = ib[0] & 0xff;
1116 		final int type = (c >> 4) & 7;
1117 		long sz = c & 15;
1118 		int shift = 4;
1119 		int p = 1;
1120 		while ((c & 0x80) != 0) {
1121 			c = ib[p++] & 0xff;
1122 			sz += ((long) (c & 0x7f)) << shift;
1123 			shift += 7;
1124 		}
1125 
1126 		long deltaAt;
1127 		switch (type) {
1128 		case Constants.OBJ_COMMIT:
1129 		case Constants.OBJ_TREE:
1130 		case Constants.OBJ_BLOB:
1131 		case Constants.OBJ_TAG:
1132 			return sz;
1133 
1134 		case Constants.OBJ_OFS_DELTA:
1135 			c = ib[p++] & 0xff;
1136 			while ((c & 128) != 0)
1137 				c = ib[p++] & 0xff;
1138 			deltaAt = pos + p;
1139 			break;
1140 
1141 		case Constants.OBJ_REF_DELTA:
1142 			deltaAt = pos + p + 20;
1143 			break;
1144 
1145 		default:
1146 			throw new IOException(MessageFormat.format(
1147 					JGitText.get().unknownObjectType, Integer.valueOf(type)));
1148 		}
1149 
1150 		try {
1151 			return BinaryDelta.getResultSize(getDeltaHeader(ctx, deltaAt));
1152 		} catch (DataFormatException dfe) {
1153 			CorruptObjectException coe = new CorruptObjectException(
1154 					MessageFormat.format(
1155 							JGitText.get().objectAtHasBadZlibStream, Long.valueOf(pos),
1156 							getPackName()));
1157 			coe.initCause(dfe);
1158 			throw coe;
1159 		}
1160 	}
1161 
1162 	void representation(DfsObjectRepresentation r, final long pos,
1163 			DfsReader ctx, PackReverseIndex rev)
1164 			throws IOException {
1165 		r.offset = pos;
1166 		final byte[] ib = ctx.tempId;
1167 		readFully(pos, ib, 0, 20, ctx);
1168 		int c = ib[0] & 0xff;
1169 		int p = 1;
1170 		final int typeCode = (c >> 4) & 7;
1171 		while ((c & 0x80) != 0)
1172 			c = ib[p++] & 0xff;
1173 
1174 		long len = rev.findNextOffset(pos, length - 20) - pos;
1175 		switch (typeCode) {
1176 		case Constants.OBJ_COMMIT:
1177 		case Constants.OBJ_TREE:
1178 		case Constants.OBJ_BLOB:
1179 		case Constants.OBJ_TAG:
1180 			r.format = StoredObjectRepresentation.PACK_WHOLE;
1181 			r.baseId = null;
1182 			r.length = len - p;
1183 			return;
1184 
1185 		case Constants.OBJ_OFS_DELTA: {
1186 			c = ib[p++] & 0xff;
1187 			long ofs = c & 127;
1188 			while ((c & 128) != 0) {
1189 				ofs += 1;
1190 				c = ib[p++] & 0xff;
1191 				ofs <<= 7;
1192 				ofs += (c & 127);
1193 			}
1194 			r.format = StoredObjectRepresentation.PACK_DELTA;
1195 			r.baseId = rev.findObject(pos - ofs);
1196 			r.length = len - p;
1197 			return;
1198 		}
1199 
1200 		case Constants.OBJ_REF_DELTA: {
1201 			readFully(pos + p, ib, 0, 20, ctx);
1202 			r.format = StoredObjectRepresentation.PACK_DELTA;
1203 			r.baseId = ObjectId.fromRaw(ib);
1204 			r.length = len - p - 20;
1205 			return;
1206 		}
1207 
1208 		default:
1209 			throw new IOException(MessageFormat.format(
1210 					JGitText.get().unknownObjectType, Integer.valueOf(typeCode)));
1211 		}
1212 	}
1213 
1214 	boolean isCorrupt(long offset) {
1215 		LongList list = corruptObjects;
1216 		if (list == null)
1217 			return false;
1218 		synchronized (list) {
1219 			return list.contains(offset);
1220 		}
1221 	}
1222 
1223 	private void setCorrupt(long offset) {
1224 		LongList list = corruptObjects;
1225 		if (list == null) {
1226 			synchronized (initLock) {
1227 				list = corruptObjects;
1228 				if (list == null) {
1229 					list = new LongList();
1230 					corruptObjects = list;
1231 				}
1232 			}
1233 		}
1234 		synchronized (list) {
1235 			list.add(offset);
1236 		}
1237 	}
1238 }