View Javadoc
1   /*
2    * Copyright (C) 2008-2011, Google Inc.
3    * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 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.file;
47  
48  import java.io.File;
49  import java.io.FileOutputStream;
50  import java.io.IOException;
51  import java.io.InputStream;
52  import java.io.RandomAccessFile;
53  import java.nio.file.StandardCopyOption;
54  import java.security.MessageDigest;
55  import java.text.MessageFormat;
56  import java.util.Arrays;
57  import java.util.List;
58  import java.util.zip.CRC32;
59  import java.util.zip.Deflater;
60  
61  import org.eclipse.jgit.errors.LockFailedException;
62  import org.eclipse.jgit.internal.JGitText;
63  import org.eclipse.jgit.lib.AnyObjectId;
64  import org.eclipse.jgit.lib.Constants;
65  import org.eclipse.jgit.lib.CoreConfig;
66  import org.eclipse.jgit.lib.ObjectId;
67  import org.eclipse.jgit.lib.ObjectInserter;
68  import org.eclipse.jgit.lib.ProgressMonitor;
69  import org.eclipse.jgit.transport.PackParser;
70  import org.eclipse.jgit.transport.PackedObjectInfo;
71  import org.eclipse.jgit.util.FileUtils;
72  import org.eclipse.jgit.util.NB;
73  
74  /**
75   * Consumes a pack stream and stores as a pack file in {@link ObjectDirectory}.
76   * <p>
77   * To obtain an instance of a parser, applications should use
78   * {@link ObjectInserter#newPackParser(InputStream)}.
79   */
80  public class ObjectDirectoryPackParser extends PackParser {
81  	private final FileObjectDatabase db;
82  
83  	/** CRC-32 computation for objects that are appended onto the pack. */
84  	private final CRC32 crc;
85  
86  	/** Running SHA-1 of any base objects appended after {@link #origEnd}. */
87  	private final MessageDigest tailDigest;
88  
89  	/** Preferred format version of the pack-*.idx file to generate. */
90  	private int indexVersion;
91  
92  	/** If true, pack with 0 objects will be stored. Usually these are deleted. */
93  	private boolean keepEmpty;
94  
95  	/** Path of the temporary file holding the pack data. */
96  	private File tmpPack;
97  
98  	/**
99  	 * Path of the index created for the pack, to find objects quickly at read
100 	 * time.
101 	 */
102 	private File tmpIdx;
103 
104 	/** Read/write handle to {@link #tmpPack} while it is being parsed. */
105 	private RandomAccessFile out;
106 
107 	/** Length of the original pack stream, before missing bases were appended. */
108 	private long origEnd;
109 
110 	/** The original checksum of data up to {@link #origEnd}. */
111 	private byte[] origHash;
112 
113 	/** Current end of the pack file. */
114 	private long packEnd;
115 
116 	/** Checksum of the entire pack file. */
117 	private byte[] packHash;
118 
119 	/** Compresses delta bases when completing a thin pack. */
120 	private Deflater def;
121 
122 	/** The pack that was created, if parsing was successful. */
123 	private PackFile newPack;
124 
125 	ObjectDirectoryPackParser(FileObjectDatabase odb, InputStream src) {
126 		super(odb, src);
127 		this.db = odb;
128 		this.crc = new CRC32();
129 		this.tailDigest = Constants.newMessageDigest();
130 
131 		indexVersion = db.getConfig().get(CoreConfig.KEY).getPackIndexVersion();
132 	}
133 
134 	/**
135 	 * Set the pack index file format version this instance will create.
136 	 *
137 	 * @param version
138 	 *            the version to write. The special version 0 designates the
139 	 *            oldest (most compatible) format available for the objects.
140 	 * @see PackIndexWriter
141 	 */
142 	public void setIndexVersion(int version) {
143 		indexVersion = version;
144 	}
145 
146 	/**
147 	 * Configure this index pack instance to keep an empty pack.
148 	 * <p>
149 	 * By default an empty pack (a pack with no objects) is not kept, as doi so
150 	 * is completely pointless. With no objects in the pack there is no d stored
151 	 * by it, so the pack is unnecessary.
152 	 *
153 	 * @param empty
154 	 *            true to enable keeping an empty pack.
155 	 */
156 	public void setKeepEmpty(final boolean empty) {
157 		keepEmpty = empty;
158 	}
159 
160 	/**
161 	 * Get the imported {@link PackFile}.
162 	 * <p>
163 	 * This method is supplied only to support testing; applications shouldn't
164 	 * be using it directly to access the imported data.
165 	 *
166 	 * @return the imported PackFile, if parsing was successful.
167 	 */
168 	public PackFile getPackFile() {
169 		return newPack;
170 	}
171 
172 	@Override
173 	public long getPackSize() {
174 		if (newPack == null)
175 			return super.getPackSize();
176 
177 		File pack = newPack.getPackFile();
178 		long size = pack.length();
179 		String p = pack.getAbsolutePath();
180 		String i = p.substring(0, p.length() - ".pack".length()) + ".idx"; //$NON-NLS-1$ //$NON-NLS-2$
181 		File idx = new File(i);
182 		if (idx.exists() && idx.isFile())
183 			size += idx.length();
184 		return size;
185 	}
186 
187 	@Override
188 	public PackLock parse(ProgressMonitor receiving, ProgressMonitor resolving)
189 			throws IOException {
190 		tmpPack = File.createTempFile("incoming_", ".pack", db.getDirectory()); //$NON-NLS-1$ //$NON-NLS-2$
191 		tmpIdx = new File(db.getDirectory(), baseName(tmpPack) + ".idx"); //$NON-NLS-1$
192 		try {
193 			out = new RandomAccessFile(tmpPack, "rw"); //$NON-NLS-1$
194 
195 			super.parse(receiving, resolving);
196 
197 			out.seek(packEnd);
198 			out.write(packHash);
199 			out.getChannel().force(true);
200 			out.close();
201 
202 			writeIdx();
203 
204 			tmpPack.setReadOnly();
205 			tmpIdx.setReadOnly();
206 
207 			return renameAndOpenPack(getLockMessage());
208 		} finally {
209 			if (def != null)
210 				def.end();
211 			try {
212 				if (out != null && out.getChannel().isOpen())
213 					out.close();
214 			} catch (IOException closeError) {
215 				// Ignored. We want to delete the file.
216 			}
217 			cleanupTemporaryFiles();
218 		}
219 	}
220 
221 	@Override
222 	protected void onPackHeader(long objectCount) throws IOException {
223 		// Ignored, the count is not required.
224 	}
225 
226 	@Override
227 	protected void onBeginWholeObject(long streamPosition, int type,
228 			long inflatedSize) throws IOException {
229 		crc.reset();
230 	}
231 
232 	@Override
233 	protected void onEndWholeObject(PackedObjectInfo info) throws IOException {
234 		info.setCRC((int) crc.getValue());
235 	}
236 
237 	@Override
238 	protected void onBeginOfsDelta(long streamPosition,
239 			long baseStreamPosition, long inflatedSize) throws IOException {
240 		crc.reset();
241 	}
242 
243 	@Override
244 	protected void onBeginRefDelta(long streamPosition, AnyObjectId baseId,
245 			long inflatedSize) throws IOException {
246 		crc.reset();
247 	}
248 
249 	@Override
250 	protected UnresolvedDelta onEndDelta() throws IOException {
251 		UnresolvedDelta delta = new UnresolvedDelta();
252 		delta.setCRC((int) crc.getValue());
253 		return delta;
254 	}
255 
256 	@Override
257 	protected void onInflatedObjectData(PackedObjectInfo obj, int typeCode,
258 			byte[] data) throws IOException {
259 		// ObjectDirectory ignores this event.
260 	}
261 
262 	@Override
263 	protected void onObjectHeader(Source src, byte[] raw, int pos, int len)
264 			throws IOException {
265 		crc.update(raw, pos, len);
266 	}
267 
268 	@Override
269 	protected void onObjectData(Source src, byte[] raw, int pos, int len)
270 			throws IOException {
271 		crc.update(raw, pos, len);
272 	}
273 
274 	@Override
275 	protected void onStoreStream(byte[] raw, int pos, int len)
276 			throws IOException {
277 		out.write(raw, pos, len);
278 	}
279 
280 	@Override
281 	protected void onPackFooter(byte[] hash) throws IOException {
282 		packEnd = out.getFilePointer();
283 		origEnd = packEnd;
284 		origHash = hash;
285 		packHash = hash;
286 	}
287 
288 	@Override
289 	protected ObjectTypeAndSize seekDatabase(UnresolvedDelta delta,
290 			ObjectTypeAndSize info) throws IOException {
291 		out.seek(delta.getOffset());
292 		crc.reset();
293 		return readObjectHeader(info);
294 	}
295 
296 	@Override
297 	protected ObjectTypeAndSize seekDatabase(PackedObjectInfo obj,
298 			ObjectTypeAndSize info) throws IOException {
299 		out.seek(obj.getOffset());
300 		crc.reset();
301 		return readObjectHeader(info);
302 	}
303 
304 	@Override
305 	protected int readDatabase(byte[] dst, int pos, int cnt) throws IOException {
306 		return out.read(dst, pos, cnt);
307 	}
308 
309 	@Override
310 	protected boolean checkCRC(int oldCRC) {
311 		return oldCRC == (int) crc.getValue();
312 	}
313 
314 	private static String baseName(File tmpPack) {
315 		String name = tmpPack.getName();
316 		return name.substring(0, name.lastIndexOf('.'));
317 	}
318 
319 	private void cleanupTemporaryFiles() {
320 		if (tmpIdx != null && !tmpIdx.delete() && tmpIdx.exists())
321 			tmpIdx.deleteOnExit();
322 		if (tmpPack != null && !tmpPack.delete() && tmpPack.exists())
323 			tmpPack.deleteOnExit();
324 	}
325 
326 	@Override
327 	protected boolean onAppendBase(final int typeCode, final byte[] data,
328 			final PackedObjectInfo info) throws IOException {
329 		info.setOffset(packEnd);
330 
331 		final byte[] buf = buffer();
332 		int sz = data.length;
333 		int len = 0;
334 		buf[len++] = (byte) ((typeCode << 4) | sz & 15);
335 		sz >>>= 4;
336 		while (sz > 0) {
337 			buf[len - 1] |= 0x80;
338 			buf[len++] = (byte) (sz & 0x7f);
339 			sz >>>= 7;
340 		}
341 
342 		tailDigest.update(buf, 0, len);
343 		crc.reset();
344 		crc.update(buf, 0, len);
345 		out.seek(packEnd);
346 		out.write(buf, 0, len);
347 		packEnd += len;
348 
349 		if (def == null)
350 			def = new Deflater(Deflater.DEFAULT_COMPRESSION, false);
351 		else
352 			def.reset();
353 		def.setInput(data);
354 		def.finish();
355 
356 		while (!def.finished()) {
357 			len = def.deflate(buf);
358 			tailDigest.update(buf, 0, len);
359 			crc.update(buf, 0, len);
360 			out.write(buf, 0, len);
361 			packEnd += len;
362 		}
363 
364 		info.setCRC((int) crc.getValue());
365 		return true;
366 	}
367 
368 	@Override
369 	protected void onEndThinPack() throws IOException {
370 		final byte[] buf = buffer();
371 
372 		final MessageDigest origDigest = Constants.newMessageDigest();
373 		final MessageDigest tailDigest2 = Constants.newMessageDigest();
374 		final MessageDigest packDigest = Constants.newMessageDigest();
375 
376 		long origRemaining = origEnd;
377 		out.seek(0);
378 		out.readFully(buf, 0, 12);
379 		origDigest.update(buf, 0, 12);
380 		origRemaining -= 12;
381 
382 		NB.encodeInt32(buf, 8, getObjectCount());
383 		out.seek(0);
384 		out.write(buf, 0, 12);
385 		packDigest.update(buf, 0, 12);
386 
387 		for (;;) {
388 			final int n = out.read(buf);
389 			if (n < 0)
390 				break;
391 			if (origRemaining != 0) {
392 				final int origCnt = (int) Math.min(n, origRemaining);
393 				origDigest.update(buf, 0, origCnt);
394 				origRemaining -= origCnt;
395 				if (origRemaining == 0)
396 					tailDigest2.update(buf, origCnt, n - origCnt);
397 			} else
398 				tailDigest2.update(buf, 0, n);
399 
400 			packDigest.update(buf, 0, n);
401 		}
402 
403 		if (!Arrays.equals(origDigest.digest(), origHash) || !Arrays
404 				.equals(tailDigest2.digest(), this.tailDigest.digest()))
405 			throw new IOException(
406 					JGitText.get().packCorruptedWhileWritingToFilesystem);
407 
408 		packHash = packDigest.digest();
409 	}
410 
411 	private void writeIdx() throws IOException {
412 		List<PackedObjectInfo> list = getSortedObjectList(null /* by ObjectId */);
413 		final FileOutputStream os = new FileOutputStream(tmpIdx);
414 		try {
415 			final PackIndexWriter iw;
416 			if (indexVersion <= 0)
417 				iw = PackIndexWriter.createOldestPossible(os, list);
418 			else
419 				iw = PackIndexWriter.createVersion(os, indexVersion);
420 			iw.write(list, packHash);
421 			os.getChannel().force(true);
422 		} finally {
423 			os.close();
424 		}
425 	}
426 
427 	private PackLock renameAndOpenPack(final String lockMessage)
428 			throws IOException {
429 		if (!keepEmpty && getObjectCount() == 0) {
430 			cleanupTemporaryFiles();
431 			return null;
432 		}
433 
434 		final MessageDigest d = Constants.newMessageDigest();
435 		final byte[] oeBytes = new byte[Constants.OBJECT_ID_LENGTH];
436 		for (int i = 0; i < getObjectCount(); i++) {
437 			final PackedObjectInfo oe = getObject(i);
438 			oe.copyRawTo(oeBytes, 0);
439 			d.update(oeBytes);
440 		}
441 
442 		final String name = ObjectId.fromRaw(d.digest()).name();
443 		final File packDir = new File(db.getDirectory(), "pack"); //$NON-NLS-1$
444 		final File finalPack = new File(packDir, "pack-" + name + ".pack"); //$NON-NLS-1$ //$NON-NLS-2$
445 		final File finalIdx = new File(packDir, "pack-" + name + ".idx"); //$NON-NLS-1$ //$NON-NLS-2$
446 		final PackLock keep = new PackLock(finalPack, db.getFS());
447 
448 		if (!packDir.exists() && !packDir.mkdir() && !packDir.exists()) {
449 			// The objects/pack directory isn't present, and we are unable
450 			// to create it. There is no way to move this pack in.
451 			//
452 			cleanupTemporaryFiles();
453 			throw new IOException(MessageFormat.format(
454 					JGitText.get().cannotCreateDirectory, packDir
455 							.getAbsolutePath()));
456 		}
457 
458 		if (finalPack.exists()) {
459 			// If the pack is already present we should never replace it.
460 			//
461 			cleanupTemporaryFiles();
462 			return null;
463 		}
464 
465 		if (lockMessage != null) {
466 			// If we have a reason to create a keep file for this pack, do
467 			// so, or fail fast and don't put the pack in place.
468 			//
469 			try {
470 				if (!keep.lock(lockMessage))
471 					throw new LockFailedException(finalPack,
472 							MessageFormat.format(
473 									JGitText.get().cannotLockPackIn, finalPack));
474 			} catch (IOException e) {
475 				cleanupTemporaryFiles();
476 				throw e;
477 			}
478 		}
479 
480 		try {
481 			FileUtils.rename(tmpPack, finalPack,
482 					StandardCopyOption.ATOMIC_MOVE);
483 		} catch (IOException e) {
484 			cleanupTemporaryFiles();
485 			keep.unlock();
486 			throw new IOException(MessageFormat.format(
487 					JGitText.get().cannotMovePackTo, finalPack), e);
488 		}
489 
490 		try {
491 			FileUtils.rename(tmpIdx, finalIdx, StandardCopyOption.ATOMIC_MOVE);
492 		} catch (IOException e) {
493 			cleanupTemporaryFiles();
494 			keep.unlock();
495 			if (!finalPack.delete())
496 				finalPack.deleteOnExit();
497 			throw new IOException(MessageFormat.format(
498 					JGitText.get().cannotMoveIndexTo, finalIdx), e);
499 		}
500 
501 		try {
502 			newPack = db.openPack(finalPack);
503 		} catch (IOException err) {
504 			keep.unlock();
505 			if (finalPack.exists())
506 				FileUtils.delete(finalPack);
507 			if (finalIdx.exists())
508 				FileUtils.delete(finalIdx);
509 			throw err;
510 		}
511 
512 		return lockMessage != null ? keep : null;
513 	}
514 }