View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.internal.storage.file;
45  
46  import static java.nio.charset.StandardCharsets.UTF_8;
47  import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
48  import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
49  
50  import java.io.BufferedReader;
51  import java.io.File;
52  import java.io.FileInputStream;
53  import java.io.FileNotFoundException;
54  import java.io.IOException;
55  import java.nio.file.AtomicMoveNotSupportedException;
56  import java.nio.file.Files;
57  import java.nio.file.StandardCopyOption;
58  import java.text.MessageFormat;
59  import java.util.ArrayList;
60  import java.util.Arrays;
61  import java.util.Collection;
62  import java.util.Collections;
63  import java.util.HashMap;
64  import java.util.HashSet;
65  import java.util.List;
66  import java.util.Map;
67  import java.util.Objects;
68  import java.util.Set;
69  import java.util.concurrent.atomic.AtomicReference;
70  
71  import org.eclipse.jgit.errors.CorruptObjectException;
72  import org.eclipse.jgit.errors.PackInvalidException;
73  import org.eclipse.jgit.errors.PackMismatchException;
74  import org.eclipse.jgit.internal.JGitText;
75  import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
76  import org.eclipse.jgit.internal.storage.pack.PackExt;
77  import org.eclipse.jgit.internal.storage.pack.PackWriter;
78  import org.eclipse.jgit.lib.AbbreviatedObjectId;
79  import org.eclipse.jgit.lib.AnyObjectId;
80  import org.eclipse.jgit.lib.Config;
81  import org.eclipse.jgit.lib.ConfigConstants;
82  import org.eclipse.jgit.lib.Constants;
83  import org.eclipse.jgit.lib.ObjectDatabase;
84  import org.eclipse.jgit.lib.ObjectId;
85  import org.eclipse.jgit.lib.ObjectLoader;
86  import org.eclipse.jgit.lib.RepositoryCache;
87  import org.eclipse.jgit.lib.RepositoryCache.FileKey;
88  import org.eclipse.jgit.util.FS;
89  import org.eclipse.jgit.util.FileUtils;
90  import org.slf4j.Logger;
91  import org.slf4j.LoggerFactory;
92  
93  /**
94   * Traditional file system based {@link org.eclipse.jgit.lib.ObjectDatabase}.
95   * <p>
96   * This is the classical object database representation for a Git repository,
97   * where objects are stored loose by hashing them into directories by their
98   * {@link org.eclipse.jgit.lib.ObjectId}, or are stored in compressed containers
99   * known as {@link org.eclipse.jgit.internal.storage.file.PackFile}s.
100  * <p>
101  * Optionally an object database can reference one or more alternates; other
102  * ObjectDatabase instances that are searched in addition to the current
103  * database.
104  * <p>
105  * Databases are divided into two halves: a half that is considered to be fast
106  * to search (the {@code PackFile}s), and a half that is considered to be slow
107  * to search (loose objects). When alternates are present the fast half is fully
108  * searched (recursively through all alternates) before the slow half is
109  * considered.
110  */
111 public class ObjectDirectory extends FileObjectDatabase {
112 	private final static Logger LOG = LoggerFactory
113 			.getLogger(ObjectDirectory.class);
114 
115 	private static final PackList NO_PACKS = new PackList(
116 			FileSnapshot.DIRTY, new PackFile[0]);
117 
118 	/** Maximum number of candidates offered as resolutions of abbreviation. */
119 	private static final int RESOLVE_ABBREV_LIMIT = 256;
120 
121 	private final AlternateHandle handle = new AlternateHandle(this);
122 
123 	private final Config config;
124 
125 	private final File objects;
126 
127 	private final File infoDirectory;
128 
129 	private final File packDirectory;
130 
131 	private final File preservedDirectory;
132 
133 	private final File alternatesFile;
134 
135 	private final FS fs;
136 
137 	private final AtomicReference<AlternateHandle[]> alternates;
138 
139 	private final UnpackedObjectCache unpackedObjectCache;
140 
141 	private final File shallowFile;
142 
143 	private FileSnapshot shallowFileSnapshot = FileSnapshot.DIRTY;
144 
145 	private Set<ObjectId> shallowCommitsIds;
146 
147 	final AtomicReference<PackList> packList;
148 
149 	/**
150 	 * Initialize a reference to an on-disk object directory.
151 	 *
152 	 * @param cfg
153 	 *            configuration this directory consults for write settings.
154 	 * @param dir
155 	 *            the location of the <code>objects</code> directory.
156 	 * @param alternatePaths
157 	 *            a list of alternate object directories
158 	 * @param fs
159 	 *            the file system abstraction which will be necessary to perform
160 	 *            certain file system operations.
161 	 * @param shallowFile
162 	 *            file which contains IDs of shallow commits, null if shallow
163 	 *            commits handling should be turned off
164 	 * @throws java.io.IOException
165 	 *             an alternate object cannot be opened.
166 	 */
167 	public ObjectDirectory(final Config cfg, final File dir,
168 			File[] alternatePaths, FS fs, File shallowFile) throws IOException {
169 		config = cfg;
170 		objects = dir;
171 		infoDirectory = new File(objects, "info"); //$NON-NLS-1$
172 		packDirectory = new File(objects, "pack"); //$NON-NLS-1$
173 		preservedDirectory = new File(packDirectory, "preserved"); //$NON-NLS-1$
174 		alternatesFile = new File(objects, Constants.INFO_ALTERNATES);
175 		packList = new AtomicReference<>(NO_PACKS);
176 		unpackedObjectCache = new UnpackedObjectCache();
177 		this.fs = fs;
178 		this.shallowFile = shallowFile;
179 
180 		alternates = new AtomicReference<>();
181 		if (alternatePaths != null) {
182 			AlternateHandle[] alt;
183 
184 			alt = new AlternateHandle[alternatePaths.length];
185 			for (int i = 0; i < alternatePaths.length; i++)
186 				alt[i] = openAlternate(alternatePaths[i]);
187 			alternates.set(alt);
188 		}
189 	}
190 
191 	/** {@inheritDoc} */
192 	@Override
193 	public final File getDirectory() {
194 		return objects;
195 	}
196 
197 	/**
198 	 * <p>Getter for the field <code>packDirectory</code>.</p>
199 	 *
200 	 * @return the location of the <code>pack</code> directory.
201 	 */
202 	public final File getPackDirectory() {
203 		return packDirectory;
204 	}
205 
206 	/**
207 	 * <p>Getter for the field <code>preservedDirectory</code>.</p>
208 	 *
209 	 * @return the location of the <code>preserved</code> directory.
210 	 */
211 	public final File getPreservedDirectory() {
212 		return preservedDirectory;
213 	}
214 
215 	/** {@inheritDoc} */
216 	@Override
217 	public boolean exists() {
218 		return fs.exists(objects);
219 	}
220 
221 	/** {@inheritDoc} */
222 	@Override
223 	public void create() throws IOException {
224 		FileUtils.mkdirs(objects);
225 		FileUtils.mkdir(infoDirectory);
226 		FileUtils.mkdir(packDirectory);
227 	}
228 
229 	/** {@inheritDoc} */
230 	@Override
231 	public ObjectDirectoryInserter newInserter() {
232 		return new ObjectDirectoryInserter(this, config);
233 	}
234 
235 	/**
236 	 * Create a new inserter that inserts all objects as pack files, not loose
237 	 * objects.
238 	 *
239 	 * @return new inserter.
240 	 */
241 	public PackInserter newPackInserter() {
242 		return new PackInserter(this);
243 	}
244 
245 	/** {@inheritDoc} */
246 	@Override
247 	public void close() {
248 		unpackedObjectCache.clear();
249 
250 		final PackList packs = packList.get();
251 		if (packs != NO_PACKS && packList.compareAndSet(packs, NO_PACKS)) {
252 			for (PackFile p : packs.packs)
253 				p.close();
254 		}
255 
256 		// Fully close all loaded alternates and clear the alternate list.
257 		AlternateHandle[] alt = alternates.get();
258 		if (alt != null && alternates.compareAndSet(alt, null)) {
259 			for(AlternateHandle od : alt)
260 				od.close();
261 		}
262 	}
263 
264 	/** {@inheritDoc} */
265 	@Override
266 	public Collection<PackFile> getPacks() {
267 		PackList list = packList.get();
268 		if (list == NO_PACKS)
269 			list = scanPacks(list);
270 		PackFile[] packs = list.packs;
271 		return Collections.unmodifiableCollection(Arrays.asList(packs));
272 	}
273 
274 	/**
275 	 * {@inheritDoc}
276 	 * <p>
277 	 * Add a single existing pack to the list of available pack files.
278 	 */
279 	@Override
280 	public PackFile openPack(File pack)
281 			throws IOException {
282 		final String p = pack.getName();
283 		if (p.length() != 50 || !p.startsWith("pack-") || !p.endsWith(".pack")) //$NON-NLS-1$ //$NON-NLS-2$
284 			throw new IOException(MessageFormat.format(JGitText.get().notAValidPack, pack));
285 
286 		// The pack and index are assumed to exist. The existence of other
287 		// extensions needs to be explicitly checked.
288 		//
289 		int extensions = PACK.getBit() | INDEX.getBit();
290 		final String base = p.substring(0, p.length() - 4);
291 		for (PackExt ext : PackExt.values()) {
292 			if ((extensions & ext.getBit()) == 0) {
293 				final String name = base + ext.getExtension();
294 				if (new File(pack.getParentFile(), name).exists())
295 					extensions |= ext.getBit();
296 			}
297 		}
298 
299 		PackFile res = new PackFile(pack, extensions);
300 		insertPack(res);
301 		return res;
302 	}
303 
304 	/** {@inheritDoc} */
305 	@Override
306 	public String toString() {
307 		return "ObjectDirectory[" + getDirectory() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
308 	}
309 
310 	/** {@inheritDoc} */
311 	@Override
312 	public boolean has(AnyObjectId objectId) {
313 		return unpackedObjectCache.isUnpacked(objectId)
314 				|| hasPackedInSelfOrAlternate(objectId, null)
315 				|| hasLooseInSelfOrAlternate(objectId, null);
316 	}
317 
318 	private boolean hasPackedInSelfOrAlternate(AnyObjectId objectId,
319 			Set<AlternateHandle.Id> skips) {
320 		if (hasPackedObject(objectId)) {
321 			return true;
322 		}
323 		skips = addMe(skips);
324 		for (AlternateHandle alt : myAlternates()) {
325 			if (!skips.contains(alt.getId())) {
326 				if (alt.db.hasPackedInSelfOrAlternate(objectId, skips)) {
327 					return true;
328 				}
329 			}
330 		}
331 		return false;
332 	}
333 
334 	private boolean hasLooseInSelfOrAlternate(AnyObjectId objectId,
335 			Set<AlternateHandle.Id> skips) {
336 		if (fileFor(objectId).exists()) {
337 			return true;
338 		}
339 		skips = addMe(skips);
340 		for (AlternateHandle alt : myAlternates()) {
341 			if (!skips.contains(alt.getId())) {
342 				if (alt.db.hasLooseInSelfOrAlternate(objectId, skips)) {
343 					return true;
344 				}
345 			}
346 		}
347 		return false;
348 	}
349 
350 	boolean hasPackedObject(AnyObjectId objectId) {
351 		PackList pList;
352 		do {
353 			pList = packList.get();
354 			for (PackFile p : pList.packs) {
355 				try {
356 					if (p.hasObject(objectId))
357 						return true;
358 				} catch (IOException e) {
359 					// The hasObject call should have only touched the index,
360 					// so any failure here indicates the index is unreadable
361 					// by this process, and the pack is likewise not readable.
362 					LOG.warn(MessageFormat.format(
363 							JGitText.get().unableToReadPackfile,
364 							p.getPackFile().getAbsolutePath()), e);
365 					removePack(p);
366 				}
367 			}
368 		} while (searchPacksAgain(pList));
369 		return false;
370 	}
371 
372 	@Override
373 	void resolve(Set<ObjectId> matches, AbbreviatedObjectId id)
374 			throws IOException {
375 		resolve(matches, id, null);
376 	}
377 
378 	private void resolve(Set<ObjectId> matches, AbbreviatedObjectId id,
379 			Set<AlternateHandle.Id> skips)
380 			throws IOException {
381 		// Go through the packs once. If we didn't find any resolutions
382 		// scan for new packs and check once more.
383 		int oldSize = matches.size();
384 		PackList pList;
385 		do {
386 			pList = packList.get();
387 			for (PackFile p : pList.packs) {
388 				try {
389 					p.resolve(matches, id, RESOLVE_ABBREV_LIMIT);
390 					p.resetTransientErrorCount();
391 				} catch (IOException e) {
392 					handlePackError(e, p);
393 				}
394 				if (matches.size() > RESOLVE_ABBREV_LIMIT)
395 					return;
396 			}
397 		} while (matches.size() == oldSize && searchPacksAgain(pList));
398 
399 		String fanOut = id.name().substring(0, 2);
400 		String[] entries = new File(getDirectory(), fanOut).list();
401 		if (entries != null) {
402 			for (String e : entries) {
403 				if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
404 					continue;
405 				try {
406 					ObjectId entId = ObjectId.fromString(fanOut + e);
407 					if (id.prefixCompare(entId) == 0)
408 						matches.add(entId);
409 				} catch (IllegalArgumentException notId) {
410 					continue;
411 				}
412 				if (matches.size() > RESOLVE_ABBREV_LIMIT)
413 					return;
414 			}
415 		}
416 
417 		skips = addMe(skips);
418 		for (AlternateHandle alt : myAlternates()) {
419 			if (!skips.contains(alt.getId())) {
420 				alt.db.resolve(matches, id, skips);
421 				if (matches.size() > RESOLVE_ABBREV_LIMIT) {
422 					return;
423 				}
424 			}
425 		}
426 	}
427 
428 	@Override
429 	ObjectLoader openObject(WindowCursor curs, AnyObjectId objectId)
430 			throws IOException {
431 		if (unpackedObjectCache.isUnpacked(objectId)) {
432 			ObjectLoader ldr = openLooseObject(curs, objectId);
433 			if (ldr != null) {
434 				return ldr;
435 			}
436 		}
437 		ObjectLoader ldr = openPackedFromSelfOrAlternate(curs, objectId, null);
438 		if (ldr != null) {
439 			return ldr;
440 		}
441 		return openLooseFromSelfOrAlternate(curs, objectId, null);
442 	}
443 
444 	private ObjectLoader openPackedFromSelfOrAlternate(WindowCursor curs,
445 			AnyObjectId objectId, Set<AlternateHandle.Id> skips) {
446 		ObjectLoader ldr = openPackedObject(curs, objectId);
447 		if (ldr != null) {
448 			return ldr;
449 		}
450 		skips = addMe(skips);
451 		for (AlternateHandle alt : myAlternates()) {
452 			if (!skips.contains(alt.getId())) {
453 				ldr = alt.db.openPackedFromSelfOrAlternate(curs, objectId, skips);
454 				if (ldr != null) {
455 					return ldr;
456 				}
457 			}
458 		}
459 		return null;
460 	}
461 
462 	private ObjectLoader openLooseFromSelfOrAlternate(WindowCursor curs,
463 			AnyObjectId objectId, Set<AlternateHandle.Id> skips)
464 					throws IOException {
465 		ObjectLoader ldr = openLooseObject(curs, objectId);
466 		if (ldr != null) {
467 			return ldr;
468 		}
469 		skips = addMe(skips);
470 		for (AlternateHandle alt : myAlternates()) {
471 			if (!skips.contains(alt.getId())) {
472 				ldr = alt.db.openLooseFromSelfOrAlternate(curs, objectId, skips);
473 				if (ldr != null) {
474 					return ldr;
475 				}
476 			}
477 		}
478 		return null;
479 	}
480 
481 	ObjectLoader openPackedObject(WindowCursor curs, AnyObjectId objectId) {
482 		PackList pList;
483 		do {
484 			SEARCH: for (;;) {
485 				pList = packList.get();
486 				for (PackFile p : pList.packs) {
487 					try {
488 						ObjectLoader ldr = p.get(curs, objectId);
489 						p.resetTransientErrorCount();
490 						if (ldr != null)
491 							return ldr;
492 					} catch (PackMismatchException e) {
493 						// Pack was modified; refresh the entire pack list.
494 						if (searchPacksAgain(pList))
495 							continue SEARCH;
496 					} catch (IOException e) {
497 						handlePackError(e, p);
498 					}
499 				}
500 				break SEARCH;
501 			}
502 		} while (searchPacksAgain(pList));
503 		return null;
504 	}
505 
506 	@Override
507 	ObjectLoader openLooseObject(WindowCursor curs, AnyObjectId id)
508 			throws IOException {
509 		File path = fileFor(id);
510 		try (FileInputStream in = new FileInputStream(path)) {
511 			unpackedObjectCache.add(id);
512 			return UnpackedObject.open(in, path, id, curs);
513 		} catch (FileNotFoundException noFile) {
514 			if (path.exists()) {
515 				throw noFile;
516 			}
517 			unpackedObjectCache.remove(id);
518 			return null;
519 		}
520 	}
521 
522 	@Override
523 	long getObjectSize(WindowCursor curs, AnyObjectId id)
524 			throws IOException {
525 		if (unpackedObjectCache.isUnpacked(id)) {
526 			long len = getLooseObjectSize(curs, id);
527 			if (0 <= len) {
528 				return len;
529 			}
530 		}
531 		long len = getPackedSizeFromSelfOrAlternate(curs, id, null);
532 		if (0 <= len) {
533 			return len;
534 		}
535 		return getLooseSizeFromSelfOrAlternate(curs, id, null);
536 	}
537 
538 	private long getPackedSizeFromSelfOrAlternate(WindowCursor curs,
539 			AnyObjectId id, Set<AlternateHandle.Id> skips) {
540 		long len = getPackedObjectSize(curs, id);
541 		if (0 <= len) {
542 			return len;
543 		}
544 		skips = addMe(skips);
545 		for (AlternateHandle alt : myAlternates()) {
546 			if (!skips.contains(alt.getId())) {
547 				len = alt.db.getPackedSizeFromSelfOrAlternate(curs, id, skips);
548 				if (0 <= len) {
549 					return len;
550 				}
551 			}
552 		}
553 		return -1;
554 	}
555 
556 	private long getLooseSizeFromSelfOrAlternate(WindowCursor curs,
557 			AnyObjectId id, Set<AlternateHandle.Id> skips) throws IOException {
558 		long len = getLooseObjectSize(curs, id);
559 		if (0 <= len) {
560 			return len;
561 		}
562 		skips = addMe(skips);
563 		for (AlternateHandle alt : myAlternates()) {
564 			if (!skips.contains(alt.getId())) {
565 				len = alt.db.getLooseSizeFromSelfOrAlternate(curs, id, skips);
566 				if (0 <= len) {
567 					return len;
568 				}
569 			}
570 		}
571 		return -1;
572 	}
573 
574 	private long getPackedObjectSize(WindowCursor curs, AnyObjectId id) {
575 		PackList pList;
576 		do {
577 			SEARCH: for (;;) {
578 				pList = packList.get();
579 				for (PackFile p : pList.packs) {
580 					try {
581 						long len = p.getObjectSize(curs, id);
582 						p.resetTransientErrorCount();
583 						if (0 <= len)
584 							return len;
585 					} catch (PackMismatchException e) {
586 						// Pack was modified; refresh the entire pack list.
587 						if (searchPacksAgain(pList))
588 							continue SEARCH;
589 					} catch (IOException e) {
590 						handlePackError(e, p);
591 					}
592 				}
593 				break SEARCH;
594 			}
595 		} while (searchPacksAgain(pList));
596 		return -1;
597 	}
598 
599 	private long getLooseObjectSize(WindowCursor curs, AnyObjectId id)
600 			throws IOException {
601 		File f = fileFor(id);
602 		try (FileInputStream in = new FileInputStream(f)) {
603 			unpackedObjectCache.add(id);
604 			return UnpackedObject.getSize(in, id, curs);
605 		} catch (FileNotFoundException noFile) {
606 			if (f.exists()) {
607 				throw noFile;
608 			}
609 			unpackedObjectCache.remove(id);
610 			return -1;
611 		}
612 	}
613 
614 	@Override
615 	void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
616 																	WindowCursor curs) throws IOException {
617 		selectObjectRepresentation(packer, otp, curs, null);
618 	}
619 
620 	private void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
621 			WindowCursor curs, Set<AlternateHandle.Id> skips) throws IOException {
622 		PackList pList = packList.get();
623 		SEARCH: for (;;) {
624 			for (PackFile p : pList.packs) {
625 				try {
626 					LocalObjectRepresentation rep = p.representation(curs, otp);
627 					p.resetTransientErrorCount();
628 					if (rep != null)
629 						packer.select(otp, rep);
630 				} catch (PackMismatchException e) {
631 					// Pack was modified; refresh the entire pack list.
632 					//
633 					pList = scanPacks(pList);
634 					continue SEARCH;
635 				} catch (IOException e) {
636 					handlePackError(e, p);
637 				}
638 			}
639 			break SEARCH;
640 		}
641 
642 		skips = addMe(skips);
643 		for (AlternateHandle h : myAlternates()) {
644 			if (!skips.contains(h.getId())) {
645 				h.db.selectObjectRepresentation(packer, otp, curs, skips);
646 			}
647 		}
648 	}
649 
650 	private void handlePackError(IOException e, PackFile p) {
651 		String warnTmpl = null;
652 		int transientErrorCount = 0;
653 		String errTmpl = JGitText.get().exceptionWhileReadingPack;
654 		if ((e instanceof CorruptObjectException)
655 				|| (e instanceof PackInvalidException)) {
656 			warnTmpl = JGitText.get().corruptPack;
657 			LOG.warn(MessageFormat.format(warnTmpl,
658 					p.getPackFile().getAbsolutePath()), e);
659 			// Assume the pack is corrupted, and remove it from the list.
660 			removePack(p);
661 		} else if (e instanceof FileNotFoundException) {
662 			if (p.getPackFile().exists()) {
663 				errTmpl = JGitText.get().packInaccessible;
664 				transientErrorCount = p.incrementTransientErrorCount();
665 			} else {
666 				warnTmpl = JGitText.get().packWasDeleted;
667 				removePack(p);
668 			}
669 		} else if (FileUtils.isStaleFileHandleInCausalChain(e)) {
670 			warnTmpl = JGitText.get().packHandleIsStale;
671 			removePack(p);
672 		} else {
673 			transientErrorCount = p.incrementTransientErrorCount();
674 		}
675 		if (warnTmpl != null) {
676 			LOG.warn(MessageFormat.format(warnTmpl,
677 					p.getPackFile().getAbsolutePath()), e);
678 		} else {
679 			if (doLogExponentialBackoff(transientErrorCount)) {
680 				// Don't remove the pack from the list, as the error may be
681 				// transient.
682 				LOG.error(MessageFormat.format(errTmpl,
683 						p.getPackFile().getAbsolutePath(),
684 						Integer.valueOf(transientErrorCount)), e);
685 			}
686 		}
687 	}
688 
689 	/**
690 	 * @param n
691 	 *            count of consecutive failures
692 	 * @return @{code true} if i is a power of 2
693 	 */
694 	private boolean doLogExponentialBackoff(int n) {
695 		return (n & (n - 1)) == 0;
696 	}
697 
698 	@Override
699 	InsertLooseObjectResult insertUnpackedObject(File tmp, ObjectId id,
700 			boolean createDuplicate) throws IOException {
701 		// If the object is already in the repository, remove temporary file.
702 		//
703 		if (unpackedObjectCache.isUnpacked(id)) {
704 			FileUtils.delete(tmp, FileUtils.RETRY);
705 			return InsertLooseObjectResult.EXISTS_LOOSE;
706 		}
707 		if (!createDuplicate && has(id)) {
708 			FileUtils.delete(tmp, FileUtils.RETRY);
709 			return InsertLooseObjectResult.EXISTS_PACKED;
710 		}
711 
712 		final File dst = fileFor(id);
713 		if (dst.exists()) {
714 			// We want to be extra careful and avoid replacing an object
715 			// that already exists. We can't be sure renameTo() would
716 			// fail on all platforms if dst exists, so we check first.
717 			//
718 			FileUtils.delete(tmp, FileUtils.RETRY);
719 			return InsertLooseObjectResult.EXISTS_LOOSE;
720 		}
721 		try {
722 			Files.move(FileUtils.toPath(tmp), FileUtils.toPath(dst),
723 					StandardCopyOption.ATOMIC_MOVE);
724 			dst.setReadOnly();
725 			unpackedObjectCache.add(id);
726 			return InsertLooseObjectResult.INSERTED;
727 		} catch (AtomicMoveNotSupportedException e) {
728 			LOG.error(e.getMessage(), e);
729 		} catch (IOException e) {
730 			// ignore
731 		}
732 
733 		// Maybe the directory doesn't exist yet as the object
734 		// directories are always lazily created. Note that we
735 		// try the rename first as the directory likely does exist.
736 		//
737 		FileUtils.mkdir(dst.getParentFile(), true);
738 		try {
739 			Files.move(FileUtils.toPath(tmp), FileUtils.toPath(dst),
740 					StandardCopyOption.ATOMIC_MOVE);
741 			dst.setReadOnly();
742 			unpackedObjectCache.add(id);
743 			return InsertLooseObjectResult.INSERTED;
744 		} catch (AtomicMoveNotSupportedException e) {
745 			LOG.error(e.getMessage(), e);
746 		} catch (IOException e) {
747 			LOG.debug(e.getMessage(), e);
748 		}
749 
750 		if (!createDuplicate && has(id)) {
751 			FileUtils.delete(tmp, FileUtils.RETRY);
752 			return InsertLooseObjectResult.EXISTS_PACKED;
753 		}
754 
755 		// The object failed to be renamed into its proper
756 		// location and it doesn't exist in the repository
757 		// either. We really don't know what went wrong, so
758 		// fail.
759 		//
760 		FileUtils.delete(tmp, FileUtils.RETRY);
761 		return InsertLooseObjectResult.FAILURE;
762 	}
763 
764 	boolean searchPacksAgain(PackList old) {
765 		// Whether to trust the pack folder's modification time. If set
766 		// to false we will always scan the .git/objects/pack folder to
767 		// check for new pack files. If set to true (default) we use the
768 		// lastmodified attribute of the folder and assume that no new
769 		// pack files can be in this folder if his modification time has
770 		// not changed.
771 		boolean trustFolderStat = config.getBoolean(
772 				ConfigConstants.CONFIG_CORE_SECTION,
773 				ConfigConstants.CONFIG_KEY_TRUSTFOLDERSTAT, true);
774 
775 		return ((!trustFolderStat) || old.snapshot.isModified(packDirectory))
776 				&& old != scanPacks(old);
777 	}
778 
779 	@Override
780 	Config getConfig() {
781 		return config;
782 	}
783 
784 	@Override
785 	FS getFS() {
786 		return fs;
787 	}
788 
789 	@Override
790 	Set<ObjectId> getShallowCommits() throws IOException {
791 		if (shallowFile == null || !shallowFile.isFile())
792 			return Collections.emptySet();
793 
794 		if (shallowFileSnapshot == null
795 				|| shallowFileSnapshot.isModified(shallowFile)) {
796 			shallowCommitsIds = new HashSet<>();
797 
798 			try (BufferedReader reader = open(shallowFile)) {
799 				String line;
800 				while ((line = reader.readLine()) != null) {
801 					try {
802 						shallowCommitsIds.add(ObjectId.fromString(line));
803 					} catch (IllegalArgumentException ex) {
804 						throw new IOException(MessageFormat
805 								.format(JGitText.get().badShallowLine, line));
806 					}
807 				}
808 			}
809 
810 			shallowFileSnapshot = FileSnapshot.save(shallowFile);
811 		}
812 
813 		return shallowCommitsIds;
814 	}
815 
816 	private void insertPack(PackFile pf) {
817 		PackList o, n;
818 		do {
819 			o = packList.get();
820 
821 			// If the pack in question is already present in the list
822 			// (picked up by a concurrent thread that did a scan?) we
823 			// do not want to insert it a second time.
824 			//
825 			final PackFile[] oldList = o.packs;
826 			final String name = pf.getPackFile().getName();
827 			for (PackFile p : oldList) {
828 				if (name.equals(p.getPackFile().getName()))
829 					return;
830 			}
831 
832 			final PackFiletorage/file/PackFile.html#PackFile">PackFile[] newList = new PackFile[1 + oldList.length];
833 			newList[0] = pf;
834 			System.arraycopy(oldList, 0, newList, 1, oldList.length);
835 			n = new PackList(o.snapshot, newList);
836 		} while (!packList.compareAndSet(o, n));
837 	}
838 
839 	private void removePack(PackFile deadPack) {
840 		PackList o, n;
841 		do {
842 			o = packList.get();
843 
844 			final PackFile[] oldList = o.packs;
845 			final int j = indexOf(oldList, deadPack);
846 			if (j < 0)
847 				break;
848 
849 			final PackFiletorage/file/PackFile.html#PackFile">PackFile[] newList = new PackFile[oldList.length - 1];
850 			System.arraycopy(oldList, 0, newList, 0, j);
851 			System.arraycopy(oldList, j + 1, newList, j, newList.length - j);
852 			n = new PackList(o.snapshot, newList);
853 		} while (!packList.compareAndSet(o, n));
854 		deadPack.close();
855 	}
856 
857 	private static int indexOf(PackFile../../../../../../org/eclipse/jgit/internal/storage/file/PackFile.html#PackFile">PackFile[] list, PackFile pack) {
858 		for (int i = 0; i < list.length; i++) {
859 			if (list[i] == pack)
860 				return i;
861 		}
862 		return -1;
863 	}
864 
865 	private PackList scanPacks(PackList original) {
866 		synchronized (packList) {
867 			PackList o, n;
868 			do {
869 				o = packList.get();
870 				if (o != original) {
871 					// Another thread did the scan for us, while we
872 					// were blocked on the monitor above.
873 					//
874 					return o;
875 				}
876 				n = scanPacksImpl(o);
877 				if (n == o)
878 					return n;
879 			} while (!packList.compareAndSet(o, n));
880 			return n;
881 		}
882 	}
883 
884 	private PackList scanPacksImpl(PackList old) {
885 		final Map<String, PackFile> forReuse = reuseMap(old);
886 		final FileSnapshot snapshot = FileSnapshot.save(packDirectory);
887 		final Set<String> names = listPackDirectory();
888 		final List<PackFile> list = new ArrayList<>(names.size() >> 2);
889 		boolean foundNew = false;
890 		for (String indexName : names) {
891 			// Must match "pack-[0-9a-f]{40}.idx" to be an index.
892 			//
893 			if (indexName.length() != 49 || !indexName.endsWith(".idx")) //$NON-NLS-1$
894 				continue;
895 
896 			final String base = indexName.substring(0, indexName.length() - 3);
897 			int extensions = 0;
898 			for (PackExt ext : PackExt.values()) {
899 				if (names.contains(base + ext.getExtension()))
900 					extensions |= ext.getBit();
901 			}
902 
903 			if ((extensions & PACK.getBit()) == 0) {
904 				// Sometimes C Git's HTTP fetch transport leaves a
905 				// .idx file behind and does not download the .pack.
906 				// We have to skip over such useless indexes.
907 				//
908 				continue;
909 			}
910 
911 			final String packName = base + PACK.getExtension();
912 			final File packFile = new File(packDirectory, packName);
913 			final PackFile oldPack = forReuse.get(packName);
914 			if (oldPack != null
915 					&& !oldPack.getFileSnapshot().isModified(packFile)) {
916 				forReuse.remove(packName);
917 				list.add(oldPack);
918 				continue;
919 			}
920 
921 			list.add(new PackFile(packFile, extensions));
922 			foundNew = true;
923 		}
924 
925 		// If we did not discover any new files, the modification time was not
926 		// changed, and we did not remove any files, then the set of files is
927 		// the same as the set we were given. Instead of building a new object
928 		// return the same collection.
929 		//
930 		if (!foundNew && forReuse.isEmpty() && snapshot.equals(old.snapshot)) {
931 			old.snapshot.setClean(snapshot);
932 			return old;
933 		}
934 
935 		for (PackFile p : forReuse.values()) {
936 			p.close();
937 		}
938 
939 		if (list.isEmpty())
940 			return new PackList(snapshot, NO_PACKS.packs);
941 
942 		final PackFilefile/PackFile.html#PackFile">PackFile[] r = list.toArray(new PackFile[0]);
943 		Arrays.sort(r, PackFile.SORT);
944 		return new PackList(snapshot, r);
945 	}
946 
947 	private static Map<String, PackFile> reuseMap(PackList old) {
948 		final Map<String, PackFile> forReuse = new HashMap<>();
949 		for (PackFile p : old.packs) {
950 			if (p.invalid()) {
951 				// The pack instance is corrupted, and cannot be safely used
952 				// again. Do not include it in our reuse map.
953 				//
954 				p.close();
955 				continue;
956 			}
957 
958 			final PackFile prior = forReuse.put(p.getPackFile().getName(), p);
959 			if (prior != null) {
960 				// This should never occur. It should be impossible for us
961 				// to have two pack files with the same name, as all of them
962 				// came out of the same directory. If it does, we promised to
963 				// close any PackFiles we did not reuse, so close the second,
964 				// readers are likely to be actively using the first.
965 				//
966 				forReuse.put(prior.getPackFile().getName(), prior);
967 				p.close();
968 			}
969 		}
970 		return forReuse;
971 	}
972 
973 	private Set<String> listPackDirectory() {
974 		final String[] nameList = packDirectory.list();
975 		if (nameList == null)
976 			return Collections.emptySet();
977 		final Set<String> nameSet = new HashSet<>(nameList.length << 1);
978 		for (String name : nameList) {
979 			if (name.startsWith("pack-")) //$NON-NLS-1$
980 				nameSet.add(name);
981 		}
982 		return nameSet;
983 	}
984 
985 	void closeAllPackHandles(File packFile) {
986 		// if the packfile already exists (because we are rewriting a
987 		// packfile for the same set of objects maybe with different
988 		// PackConfig) then make sure we get rid of all handles on the file.
989 		// Windows will not allow for rename otherwise.
990 		if (packFile.exists()) {
991 			for (PackFile p : getPacks()) {
992 				if (packFile.getPath().equals(p.getPackFile().getPath())) {
993 					p.close();
994 					break;
995 				}
996 			}
997 		}
998 	}
999 
1000 	AlternateHandle[] myAlternates() {
1001 		AlternateHandle[] alt = alternates.get();
1002 		if (alt == null) {
1003 			synchronized (alternates) {
1004 				alt = alternates.get();
1005 				if (alt == null) {
1006 					try {
1007 						alt = loadAlternates();
1008 					} catch (IOException e) {
1009 						alt = new AlternateHandle[0];
1010 					}
1011 					alternates.set(alt);
1012 				}
1013 			}
1014 		}
1015 		return alt;
1016 	}
1017 
1018 	Set<AlternateHandle.Id> addMe(Set<AlternateHandle.Id> skips) {
1019 		if (skips == null) {
1020 			skips = new HashSet<>();
1021 		}
1022 		skips.add(handle.getId());
1023 		return skips;
1024 	}
1025 
1026 	private AlternateHandle[] loadAlternates() throws IOException {
1027 		final List<AlternateHandle> l = new ArrayList<>(4);
1028 		try (BufferedReader br = open(alternatesFile)) {
1029 			String line;
1030 			while ((line = br.readLine()) != null) {
1031 				l.add(openAlternate(line));
1032 			}
1033 		}
1034 		return l.toArray(new AlternateHandle[0]);
1035 	}
1036 
1037 	private static BufferedReader open(File f)
1038 			throws IOException, FileNotFoundException {
1039 		return Files.newBufferedReader(f.toPath(), UTF_8);
1040 	}
1041 
1042 	private AlternateHandle openAlternate(String location)
1043 			throws IOException {
1044 		final File objdir = fs.resolve(objects, location);
1045 		return openAlternate(objdir);
1046 	}
1047 
1048 	private AlternateHandle openAlternate(File objdir) throws IOException {
1049 		final File parent = objdir.getParentFile();
1050 		if (FileKey.isGitRepository(parent, fs)) {
1051 			FileKey key = FileKey.exact(parent, fs);
1052 			FileRepository db = (FileRepository) RepositoryCache.open(key);
1053 			return new AlternateRepository(db);
1054 		}
1055 
1056 		ObjectDirectory db = new ObjectDirectory(config, objdir, null, fs, null);
1057 		return new AlternateHandle(db);
1058 	}
1059 
1060 	/**
1061 	 * {@inheritDoc}
1062 	 * <p>
1063 	 * Compute the location of a loose object file.
1064 	 */
1065 	@Override
1066 	public File fileFor(AnyObjectId objectId) {
1067 		String n = objectId.name();
1068 		String d = n.substring(0, 2);
1069 		String f = n.substring(2);
1070 		return new File(new File(getDirectory(), d), f);
1071 	}
1072 
1073 	static final class PackList {
1074 		/** State just before reading the pack directory. */
1075 		final FileSnapshot snapshot;
1076 
1077 		/** All known packs, sorted by {@link PackFile#SORT}. */
1078 		final PackFile[] packs;
1079 
1080 		PackList(FileSnapshot monitor, PackFile[] packs) {
1081 			this.snapshot = monitor;
1082 			this.packs = packs;
1083 		}
1084 	}
1085 
1086 	static class AlternateHandle {
1087 		static class Id {
1088 			String alternateId;
1089 
1090 			public Id(File object) {
1091 				try {
1092 					this.alternateId = object.getCanonicalPath();
1093 				} catch (Exception e) {
1094 					alternateId = null;
1095 				}
1096 			}
1097 
1098 			@Override
1099 			public boolean equals(Object o) {
1100 				if (o == this) {
1101 					return true;
1102 				}
1103 				if (o == null || !(o instanceof Id)) {
1104 					return false;
1105 				}
1106 				Id aId = (Id) o;
1107 				return Objects.equals(alternateId, aId.alternateId);
1108 			}
1109 
1110 			@Override
1111 			public int hashCode() {
1112 				if (alternateId == null) {
1113 					return 1;
1114 				}
1115 				return alternateId.hashCode();
1116 			}
1117 		}
1118 
1119 		final ObjectDirectory db;
1120 
1121 		AlternateHandle(ObjectDirectory db) {
1122 			this.db = db;
1123 		}
1124 
1125 		void close() {
1126 			db.close();
1127 		}
1128 
1129 		public Id getId(){
1130 			return db.getAlternateId();
1131 		}
1132 	}
1133 
1134 	static class AlternateRepository extends AlternateHandle {
1135 		final FileRepository repository;
1136 
1137 		AlternateRepository(FileRepository r) {
1138 			super(r.getObjectDatabase());
1139 			repository = r;
1140 		}
1141 
1142 		@Override
1143 		void close() {
1144 			repository.close();
1145 		}
1146 	}
1147 
1148 	/** {@inheritDoc} */
1149 	@Override
1150 	public ObjectDatabase newCachedDatabase() {
1151 		return newCachedFileObjectDatabase();
1152 	}
1153 
1154 	CachedObjectDirectory newCachedFileObjectDatabase() {
1155 		return new CachedObjectDirectory(this);
1156 	}
1157 
1158 	AlternateHandle.Id getAlternateId() {
1159 		return new AlternateHandle.Id(objects);
1160 	}
1161 }