2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org
.spearce
.jgit
.transport
;
41 import java
.io
.EOFException
;
43 import java
.io
.FileOutputStream
;
44 import java
.io
.IOException
;
45 import java
.io
.InputStream
;
46 import java
.io
.RandomAccessFile
;
47 import java
.security
.MessageDigest
;
48 import java
.util
.ArrayList
;
49 import java
.util
.Arrays
;
50 import java
.util
.HashMap
;
51 import java
.util
.List
;
52 import java
.util
.zip
.CRC32
;
53 import java
.util
.zip
.DataFormatException
;
54 import java
.util
.zip
.Deflater
;
55 import java
.util
.zip
.Inflater
;
57 import org
.spearce
.jgit
.errors
.CorruptObjectException
;
58 import org
.spearce
.jgit
.errors
.MissingObjectException
;
59 import org
.spearce
.jgit
.lib
.BinaryDelta
;
60 import org
.spearce
.jgit
.lib
.Constants
;
61 import org
.spearce
.jgit
.lib
.InflaterCache
;
62 import org
.spearce
.jgit
.lib
.MutableObjectId
;
63 import org
.spearce
.jgit
.lib
.ObjectId
;
64 import org
.spearce
.jgit
.lib
.ObjectIdMap
;
65 import org
.spearce
.jgit
.lib
.ObjectLoader
;
66 import org
.spearce
.jgit
.lib
.PackIndexWriter
;
67 import org
.spearce
.jgit
.lib
.ProgressMonitor
;
68 import org
.spearce
.jgit
.lib
.Repository
;
69 import org
.spearce
.jgit
.util
.NB
;
71 /** Indexes Git pack files for local use. */
72 public class IndexPack
{
73 /** Progress message when reading raw data from the pack. */
74 public static final String PROGRESS_DOWNLOAD
= "Receiving objects";
76 /** Progress message when computing names of delta compressed objects. */
77 public static final String PROGRESS_RESOLVE_DELTA
= "Resolving deltas";
80 * Size of the internal stream buffer.
82 * If callers are going to be supplying IndexPack a BufferedInputStream they
83 * should use this buffer size as the size of the buffer for that
84 * BufferedInputStream, and any other its may be wrapping. This way the
85 * buffers will cascade efficiently and only the IndexPack buffer will be
86 * receiving the bulk of the data stream.
88 public static final int BUFFER_SIZE
= 8192;
91 * Create an index pack instance to load a new pack into a repository.
93 * The received pack data and generated index will be saved to temporary
94 * files within the repository's <code>objects</code> directory. To use the
95 * data contained within them call {@link #renameAndOpenPack()} once the
96 * indexing is complete.
99 * the repository that will receive the new pack.
101 * stream to read the pack data from. If the stream is buffered
102 * use {@link #BUFFER_SIZE} as the buffer size for the stream.
103 * @return a new index pack instance.
104 * @throws IOException
105 * a temporary file could not be created.
107 public static IndexPack
create(final Repository db
, final InputStream is
)
109 final String suffix
= ".pack";
110 final File objdir
= db
.getObjectsDirectory();
111 final File tmp
= File
.createTempFile("incoming_", suffix
, objdir
);
112 final String n
= tmp
.getName();
115 base
= new File(objdir
, n
.substring(0, n
.length() - suffix
.length()));
116 final IndexPack ip
= new IndexPack(db
, is
, base
);
117 ip
.setIndexVersion(db
.getConfig().getCore().getPackIndexVersion());
121 private final Repository repo
;
123 private Inflater inflater
;
125 private final MessageDigest objectDigest
;
127 private final MutableObjectId tempObjectId
;
129 private InputStream in
;
139 private boolean fixThin
;
141 private int outputVersion
;
143 private final File dstPack
;
145 private final File dstIdx
;
147 private long objectCount
;
149 private PackedObjectInfo
[] entries
;
151 private int deltaCount
;
153 private int entryCount
;
155 private final CRC32 crc
= new CRC32();
157 private ObjectIdMap
<ArrayList
<UnresolvedDelta
>> baseById
;
159 private HashMap
<Long
, ArrayList
<UnresolvedDelta
>> baseByPos
;
161 private byte[] objectData
;
163 private MessageDigest packDigest
;
165 private RandomAccessFile packOut
;
167 private byte[] packcsum
;
169 /** If {@link #fixThin} this is the last byte of the original checksum. */
170 private long originalEOF
;
173 * Create a new pack indexer utility.
177 * stream to read the pack data from. If the stream is buffered
178 * use {@link #BUFFER_SIZE} as the buffer size for the stream.
180 * @throws IOException
181 * the output packfile could not be created.
183 public IndexPack(final Repository db
, final InputStream src
,
184 final File dstBase
) throws IOException
{
187 inflater
= InflaterCache
.get();
188 buf
= new byte[BUFFER_SIZE
];
189 objectData
= new byte[BUFFER_SIZE
];
190 objectDigest
= Constants
.newMessageDigest();
191 tempObjectId
= new MutableObjectId();
192 packDigest
= Constants
.newMessageDigest();
194 if (dstBase
!= null) {
195 final File dir
= dstBase
.getParentFile();
196 final String nam
= dstBase
.getName();
197 dstPack
= new File(dir
, nam
+ ".pack");
198 dstIdx
= new File(dir
, nam
+ ".idx");
199 packOut
= new RandomAccessFile(dstPack
, "rw");
200 packOut
.setLength(0);
208 * Set the pack index file format version this instance will create.
211 * the version to write. The special version 0 designates the
212 * oldest (most compatible) format available for the objects.
213 * @see PackIndexWriter
215 public void setIndexVersion(final int version
) {
216 outputVersion
= version
;
220 * Configure this index pack instance to make a thin pack complete.
222 * Thin packs are sometimes used during network transfers to allow a delta
223 * to be sent without a base object. Such packs are not permitted on disk.
224 * They can be fixed by copying the base object onto the end of the pack.
227 * true to enable fixing a thin pack.
229 public void setFixThin(final boolean fix
) {
234 * Consume data from the input stream until the packfile is indexed.
239 * @throws IOException
241 public void index(final ProgressMonitor progress
) throws IOException
{
242 progress
.start(2 /* tasks */);
247 entries
= new PackedObjectInfo
[(int) objectCount
];
248 baseById
= new ObjectIdMap
<ArrayList
<UnresolvedDelta
>>();
249 baseByPos
= new HashMap
<Long
, ArrayList
<UnresolvedDelta
>>();
251 progress
.beginTask(PROGRESS_DOWNLOAD
, (int) objectCount
);
252 for (int done
= 0; done
< objectCount
; done
++) {
255 if (progress
.isCancelled())
256 throw new IOException("Download cancelled");
261 if (deltaCount
> 0) {
263 throw new IOException("need packOut");
264 resolveDeltas(progress
);
265 if (entryCount
< objectCount
) {
267 throw new IOException("pack has "
268 + (objectCount
- entryCount
)
269 + " unresolved deltas");
271 fixThinPack(progress
);
275 packOut
.getChannel().force(true);
285 final Inflater inf
= inflater
;
287 InflaterCache
.release(inf
);
295 dstPack
.setReadOnly();
297 dstIdx
.setReadOnly();
298 } catch (IOException err
) {
307 private void resolveDeltas(final ProgressMonitor progress
)
309 progress
.beginTask(PROGRESS_RESOLVE_DELTA
, deltaCount
);
310 final int last
= entryCount
;
311 for (int i
= 0; i
< last
; i
++) {
312 final int before
= entryCount
;
313 resolveDeltas(entries
[i
]);
314 progress
.update(entryCount
- before
);
315 if (progress
.isCancelled())
316 throw new IOException("Download cancelled during indexing");
321 private void resolveDeltas(final PackedObjectInfo oe
) throws IOException
{
322 final int oldCRC
= oe
.getCRC();
323 if (baseById
.containsKey(oe
)
324 || baseByPos
.containsKey(new Long(oe
.getOffset())))
325 resolveDeltas(oe
.getOffset(), oldCRC
, Constants
.OBJ_BAD
, null, oe
);
328 private void resolveDeltas(final long pos
, final int oldCRC
, int type
,
329 byte[] data
, PackedObjectInfo oe
) throws IOException
{
332 int c
= readFromFile();
333 final int typeCode
= (c
>> 4) & 7;
336 while ((c
& 0x80) != 0) {
338 sz
+= (c
& 0x7f) << shift
;
343 case Constants
.OBJ_COMMIT
:
344 case Constants
.OBJ_TREE
:
345 case Constants
.OBJ_BLOB
:
346 case Constants
.OBJ_TAG
:
348 data
= inflateFromFile((int) sz
);
350 case Constants
.OBJ_OFS_DELTA
: {
351 c
= readFromFile() & 0xff;
352 while ((c
& 128) != 0)
353 c
= readFromFile() & 0xff;
354 data
= BinaryDelta
.apply(data
, inflateFromFile((int) sz
));
357 case Constants
.OBJ_REF_DELTA
: {
358 crc
.update(buf
, fillFromFile(20), 20);
360 data
= BinaryDelta
.apply(data
, inflateFromFile((int) sz
));
364 throw new IOException("Unknown object type " + typeCode
+ ".");
367 final int crc32
= (int) crc
.getValue();
369 throw new IOException("Corruption detected re-reading at " + pos
);
371 objectDigest
.update(Constants
.encodedTypeString(type
));
372 objectDigest
.update((byte) ' ');
373 objectDigest
.update(Constants
.encodeASCII(data
.length
));
374 objectDigest
.update((byte) 0);
375 objectDigest
.update(data
);
376 tempObjectId
.fromRaw(objectDigest
.digest(), 0);
378 verifyNoCollision(type
, data
);
379 oe
= new PackedObjectInfo(pos
, crc32
, tempObjectId
);
380 entries
[entryCount
++] = oe
;
383 resolveChildDeltas(pos
, type
, data
, oe
);
386 private void resolveChildDeltas(final long pos
, int type
, byte[] data
,
387 PackedObjectInfo oe
) throws IOException
{
388 final ArrayList
<UnresolvedDelta
> a
= baseById
.remove(oe
);
389 final ArrayList
<UnresolvedDelta
> b
= baseByPos
.remove(new Long(pos
));
391 if (a
!= null && b
!= null) {
392 while (ai
< a
.size() && bi
< b
.size()) {
393 final UnresolvedDelta ad
= a
.get(ai
);
394 final UnresolvedDelta bd
= b
.get(bi
);
395 if (ad
.position
< bd
.position
) {
396 resolveDeltas(ad
.position
, ad
.crc
, type
, data
, null);
399 resolveDeltas(bd
.position
, bd
.crc
, type
, data
, null);
405 while (ai
< a
.size()) {
406 final UnresolvedDelta ad
= a
.get(ai
++);
407 resolveDeltas(ad
.position
, ad
.crc
, type
, data
, null);
410 while (bi
< b
.size()) {
411 final UnresolvedDelta bd
= b
.get(bi
++);
412 resolveDeltas(bd
.position
, bd
.crc
, type
, data
, null);
416 private void fixThinPack(final ProgressMonitor progress
) throws IOException
{
420 originalEOF
= packOut
.length() - 20;
421 final Deflater def
= new Deflater(Deflater
.DEFAULT_COMPRESSION
, false);
422 long end
= originalEOF
;
423 for (final ObjectId baseId
: new ArrayList
<ObjectId
>(baseById
.keySet())) {
424 final ObjectLoader ldr
= repo
.openObject(baseId
);
427 final byte[] data
= ldr
.getBytes();
428 final int typeCode
= ldr
.getType();
429 final PackedObjectInfo oe
;
433 writeWhole(def
, typeCode
, data
);
434 oe
= new PackedObjectInfo(end
, (int) crc
.getValue(), baseId
);
435 entries
[entryCount
++] = oe
;
436 end
= packOut
.getFilePointer();
438 resolveChildDeltas(oe
.getOffset(), typeCode
, data
, oe
);
439 if (progress
.isCancelled())
440 throw new IOException("Download cancelled during indexing");
444 if (!baseById
.isEmpty()) {
445 final ObjectId need
= baseById
.keySet().iterator().next();
446 throw new MissingObjectException(need
, "delta base");
449 fixHeaderFooter(packcsum
, packDigest
.digest());
452 private void writeWhole(final Deflater def
, final int typeCode
,
453 final byte[] data
) throws IOException
{
454 int sz
= data
.length
;
456 buf
[hdrlen
++] = (byte) ((typeCode
<< 4) | sz
& 15);
459 buf
[hdrlen
- 1] |= 0x80;
460 buf
[hdrlen
++] = (byte) (sz
& 0x7f);
463 packDigest
.update(buf
, 0, hdrlen
);
464 crc
.update(buf
, 0, hdrlen
);
465 packOut
.write(buf
, 0, hdrlen
);
469 while (!def
.finished()) {
470 final int datlen
= def
.deflate(buf
);
471 packDigest
.update(buf
, 0, datlen
);
472 crc
.update(buf
, 0, datlen
);
473 packOut
.write(buf
, 0, datlen
);
477 private void fixHeaderFooter(final byte[] origcsum
, final byte[] tailcsum
)
479 final MessageDigest origDigest
= Constants
.newMessageDigest();
480 final MessageDigest tailDigest
= Constants
.newMessageDigest();
481 long origRemaining
= originalEOF
;
489 final int origCnt
= (int) Math
.min(bAvail
, origRemaining
);
490 origDigest
.update(buf
, 0, origCnt
);
491 origRemaining
-= origCnt
;
492 if (origRemaining
== 0)
493 tailDigest
.update(buf
, origCnt
, bAvail
- origCnt
);
496 NB
.encodeInt32(buf
, 8, entryCount
);
498 packOut
.write(buf
, 0, 12);
499 packOut
.seek(bAvail
);
502 packDigest
.update(buf
, 0, bAvail
);
504 final int n
= packOut
.read(buf
);
507 if (origRemaining
!= 0) {
508 final int origCnt
= (int) Math
.min(n
, origRemaining
);
509 origDigest
.update(buf
, 0, origCnt
);
510 origRemaining
-= origCnt
;
511 if (origRemaining
== 0)
512 tailDigest
.update(buf
, origCnt
, n
- origCnt
);
514 tailDigest
.update(buf
, 0, n
);
516 packDigest
.update(buf
, 0, n
);
519 if (!Arrays
.equals(origDigest
.digest(), origcsum
)
520 || !Arrays
.equals(tailDigest
.digest(), tailcsum
))
521 throw new IOException("Pack corrupted while writing to filesystem");
523 packcsum
= packDigest
.digest();
524 packOut
.write(packcsum
);
527 private void growEntries() {
528 final PackedObjectInfo
[] ne
;
530 ne
= new PackedObjectInfo
[(int) objectCount
+ baseById
.size()];
531 System
.arraycopy(entries
, 0, ne
, 0, entryCount
);
535 private void writeIdx() throws IOException
{
536 Arrays
.sort(entries
, 0, entryCount
);
537 List
<PackedObjectInfo
> list
= Arrays
.asList(entries
);
538 if (entryCount
< entries
.length
)
539 list
= list
.subList(0, entryCount
);
541 final FileOutputStream os
= new FileOutputStream(dstIdx
);
543 final PackIndexWriter iw
;
544 if (outputVersion
<= 0)
545 iw
= PackIndexWriter
.createOldestPossible(os
, list
);
547 iw
= PackIndexWriter
.createVersion(os
, outputVersion
);
548 iw
.write(list
, packcsum
);
549 os
.getChannel().force(true);
555 private void readPackHeader() throws IOException
{
556 final int hdrln
= Constants
.PACK_SIGNATURE
.length
+ 4 + 4;
557 final int p
= fillFromInput(hdrln
);
558 for (int k
= 0; k
< Constants
.PACK_SIGNATURE
.length
; k
++)
559 if (buf
[p
+ k
] != Constants
.PACK_SIGNATURE
[k
])
560 throw new IOException("Not a PACK file.");
562 final long vers
= NB
.decodeUInt32(buf
, p
+ 4);
563 if (vers
!= 2 && vers
!= 3)
564 throw new IOException("Unsupported pack version " + vers
+ ".");
565 objectCount
= NB
.decodeUInt32(buf
, p
+ 8);
569 private void readPackFooter() throws IOException
{
571 final byte[] cmpcsum
= packDigest
.digest();
572 final int c
= fillFromInput(20);
573 packcsum
= new byte[20];
574 System
.arraycopy(buf
, c
, packcsum
, 0, 20);
577 packOut
.write(packcsum
);
579 if (!Arrays
.equals(cmpcsum
, packcsum
))
580 throw new CorruptObjectException("Packfile checksum incorrect.");
583 // Cleanup all resources associated with our input parsing.
584 private void endInput() {
589 // Read one entire object or delta from the input.
590 private void indexOneObject() throws IOException
{
591 final long pos
= position();
594 int c
= readFromInput();
595 final int typeCode
= (c
>> 4) & 7;
598 while ((c
& 0x80) != 0) {
600 sz
+= (c
& 0x7f) << shift
;
605 case Constants
.OBJ_COMMIT
:
606 case Constants
.OBJ_TREE
:
607 case Constants
.OBJ_BLOB
:
608 case Constants
.OBJ_TAG
:
609 whole(typeCode
, pos
, sz
);
611 case Constants
.OBJ_OFS_DELTA
: {
614 while ((c
& 128) != 0) {
620 final Long base
= new Long(pos
- ofs
);
621 ArrayList
<UnresolvedDelta
> r
= baseByPos
.get(base
);
623 r
= new ArrayList
<UnresolvedDelta
>(8);
624 baseByPos
.put(base
, r
);
626 skipInflateFromInput(sz
);
627 r
.add(new UnresolvedDelta(pos
, (int) crc
.getValue()));
631 case Constants
.OBJ_REF_DELTA
: {
632 c
= fillFromInput(20);
633 crc
.update(buf
, c
, 20);
634 final ObjectId base
= ObjectId
.fromRaw(buf
, c
);
636 ArrayList
<UnresolvedDelta
> r
= baseById
.get(base
);
638 r
= new ArrayList
<UnresolvedDelta
>(8);
639 baseById
.put(base
, r
);
641 skipInflateFromInput(sz
);
642 r
.add(new UnresolvedDelta(pos
, (int) crc
.getValue()));
647 throw new IOException("Unknown object type " + typeCode
+ ".");
651 private void whole(final int type
, final long pos
, final long sz
)
653 final byte[] data
= inflateFromInput(sz
);
654 objectDigest
.update(Constants
.encodedTypeString(type
));
655 objectDigest
.update((byte) ' ');
656 objectDigest
.update(Constants
.encodeASCII(sz
));
657 objectDigest
.update((byte) 0);
658 objectDigest
.update(data
);
659 tempObjectId
.fromRaw(objectDigest
.digest(), 0);
661 verifyNoCollision(type
, data
);
662 final int crc32
= (int) crc
.getValue();
663 entries
[entryCount
++] = new PackedObjectInfo(pos
, crc32
, tempObjectId
);
666 private void verifyNoCollision(final int type
, final byte[] data
)
668 final ObjectLoader ldr
= repo
.openObject(tempObjectId
);
670 final byte[] existingData
= ldr
.getCachedBytes();
671 if (ldr
.getType() != type
|| !Arrays
.equals(data
, existingData
)) {
672 throw new IOException("collision in " + tempObjectId
.name());
677 // Current position of {@link #bOffset} within the entire file.
678 private long position() {
679 return bBase
+ bOffset
;
682 private void position(final long pos
) throws IOException
{
689 // Consume exactly one byte from the buffer and return it.
690 private int readFromInput() throws IOException
{
694 final int b
= buf
[bOffset
++] & 0xff;
699 // Consume exactly one byte from the buffer and return it.
700 private int readFromFile() throws IOException
{
704 final int b
= buf
[bOffset
++] & 0xff;
709 // Consume cnt bytes from the buffer.
710 private void use(final int cnt
) {
715 // Ensure at least need bytes are available in in {@link #buf}.
716 private int fillFromInput(final int need
) throws IOException
{
717 while (bAvail
< need
) {
718 int next
= bOffset
+ bAvail
;
719 int free
= buf
.length
- next
;
720 if (free
+ bAvail
< need
) {
723 free
= buf
.length
- next
;
725 next
= in
.read(buf
, next
, free
);
727 throw new EOFException("Packfile is truncated.");
733 // Ensure at least need bytes are available in in {@link #buf}.
734 private int fillFromFile(final int need
) throws IOException
{
736 int next
= bOffset
+ bAvail
;
737 int free
= buf
.length
- next
;
738 if (free
+ bAvail
< need
) {
740 System
.arraycopy(buf
, bOffset
, buf
, 0, bAvail
);
743 free
= buf
.length
- next
;
745 next
= packOut
.read(buf
, next
, free
);
747 throw new EOFException("Packfile is truncated.");
753 // Store consumed bytes in {@link #buf} up to {@link #bOffset}.
754 private void sync() throws IOException
{
755 packDigest
.update(buf
, 0, bOffset
);
757 packOut
.write(buf
, 0, bOffset
);
759 System
.arraycopy(buf
, bOffset
, buf
, 0, bAvail
);
764 private void skipInflateFromInput(long sz
) throws IOException
{
765 final Inflater inf
= inflater
;
767 final byte[] dst
= objectData
;
770 while (!inf
.finished()) {
771 if (inf
.needsInput()) {
773 crc
.update(buf
, p
, bAvail
);
776 p
= fillFromInput(1);
777 inf
.setInput(buf
, p
, bAvail
);
780 int free
= dst
.length
- n
;
786 n
+= inf
.inflate(dst
, n
, free
);
789 throw new DataFormatException("wrong decompressed length");
790 n
= bAvail
- inf
.getRemaining();
792 crc
.update(buf
, p
, n
);
795 } catch (DataFormatException dfe
) {
802 private byte[] inflateFromInput(final long sz
) throws IOException
{
803 final byte[] dst
= new byte[(int) sz
];
804 final Inflater inf
= inflater
;
808 while (!inf
.finished()) {
809 if (inf
.needsInput()) {
811 crc
.update(buf
, p
, bAvail
);
814 p
= fillFromInput(1);
815 inf
.setInput(buf
, p
, bAvail
);
818 n
+= inf
.inflate(dst
, n
, dst
.length
- n
);
821 throw new DataFormatException("wrong decompressed length");
822 n
= bAvail
- inf
.getRemaining();
824 crc
.update(buf
, p
, n
);
828 } catch (DataFormatException dfe
) {
835 private byte[] inflateFromFile(final int sz
) throws IOException
{
836 final Inflater inf
= inflater
;
838 final byte[] dst
= new byte[sz
];
841 while (!inf
.finished()) {
842 if (inf
.needsInput()) {
844 crc
.update(buf
, p
, bAvail
);
848 inf
.setInput(buf
, p
, bAvail
);
850 n
+= inf
.inflate(dst
, n
, sz
- n
);
852 n
= bAvail
- inf
.getRemaining();
854 crc
.update(buf
, p
, n
);
858 } catch (DataFormatException dfe
) {
865 private static CorruptObjectException
corrupt(final DataFormatException dfe
) {
866 return new CorruptObjectException("Packfile corruption detected: "
870 private static class UnresolvedDelta
{
875 UnresolvedDelta(final long headerOffset
, final int crc32
) {
876 position
= headerOffset
;
882 * Rename the pack to it's final name and location and open it.
884 * If the call completes successfully the repository this IndexPack instance
885 * was created with will have the objects in the pack available for reading
886 * and use, without needing to scan for packs.
888 * @throws IOException
889 * The pack could not be inserted into the repository's objects
890 * directory. The pack no longer exists on disk, as it was
891 * removed prior to throwing the exception to the caller.
893 public void renameAndOpenPack() throws IOException
{
894 final MessageDigest d
= Constants
.newMessageDigest();
895 final byte[] oeBytes
= new byte[Constants
.OBJECT_ID_LENGTH
];
896 for (int i
= 0; i
< entryCount
; i
++) {
897 final PackedObjectInfo oe
= entries
[i
];
898 oe
.copyRawTo(oeBytes
, 0);
902 final String name
= ObjectId
.fromRaw(d
.digest()).name();
903 final File packDir
= new File(repo
.getObjectsDirectory(), "pack");
904 final File finalPack
= new File(packDir
, "pack-" + name
+ ".pack");
905 final File finalIdx
= new File(packDir
, "pack-" + name
+ ".idx");
907 if (finalPack
.exists()) {
908 // If the pack is already present we should never replace it.
910 cleanupTemporaryFiles();
914 if (!dstPack
.renameTo(finalPack
)) {
915 cleanupTemporaryFiles();
916 throw new IOException("Cannot move pack to " + finalPack
);
919 if (!dstIdx
.renameTo(finalIdx
)) {
920 cleanupTemporaryFiles();
921 if (!finalPack
.delete())
922 finalPack
.deleteOnExit();
923 throw new IOException("Cannot move index to " + finalIdx
);
927 repo
.openPack(finalPack
, finalIdx
);
928 } catch (IOException err
) {
935 private void cleanupTemporaryFiles() {
936 if (!dstIdx
.delete())
937 dstIdx
.deleteOnExit();
938 if (!dstPack
.delete())
939 dstPack
.deleteOnExit();