2 * Copyright (C) 2009, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
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
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
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.
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
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.
44 package org.eclipse.jgit.storage.file;
46 import java.io.BufferedReader;
48 import java.io.FileInputStream;
49 import java.io.FileNotFoundException;
50 import java.io.FileReader;
51 import java.io.IOException;
52 import java.text.MessageFormat;
53 import java.util.ArrayList;
54 import java.util.Arrays;
55 import java.util.Collection;
56 import java.util.Collections;
57 import java.util.HashMap;
58 import java.util.HashSet;
59 import java.util.List;
62 import java.util.concurrent.atomic.AtomicReference;
64 import org.eclipse.jgit.JGitText;
65 import org.eclipse.jgit.errors.PackMismatchException;
66 import org.eclipse.jgit.lib.AbbreviatedObjectId;
67 import org.eclipse.jgit.lib.AnyObjectId;
68 import org.eclipse.jgit.lib.Config;
69 import org.eclipse.jgit.lib.Constants;
70 import org.eclipse.jgit.lib.ObjectDatabase;
71 import org.eclipse.jgit.lib.ObjectId;
72 import org.eclipse.jgit.lib.ObjectLoader;
73 import org.eclipse.jgit.lib.RepositoryCache;
74 import org.eclipse.jgit.lib.RepositoryCache.FileKey;
75 import org.eclipse.jgit.storage.pack.ObjectToPack;
76 import org.eclipse.jgit.storage.pack.PackWriter;
77 import org.eclipse.jgit.util.FS;
80 * Traditional file system based {@link ObjectDatabase}.
82 * This is the classical object database representation for a Git repository,
83 * where objects are stored loose by hashing them into directories by their
84 * {@link ObjectId}, or are stored in compressed containers known as
87 * Optionally an object database can reference one or more alternates; other
88 * ObjectDatabase instances that are searched in addition to the current
91 * Databases are divided into two halves: a half that is considered to be fast
92 * to search (the {@code PackFile}s), and a half that is considered to be slow
93 * to search (loose objects). When alternates are present the fast half is fully
94 * searched (recursively through all alternates) before the slow half is
97 public class ObjectDirectory extends FileObjectDatabase {
98 private static final PackList NO_PACKS = new PackList(-1, -1, new PackFile[0]);
100 /** Maximum number of candidates offered as resolutions of abbreviation. */
101 private static final int RESOLVE_ABBREV_LIMIT = 256;
103 private final Config config;
105 private final File objects;
107 private final File infoDirectory;
109 private final File packDirectory;
111 private final File alternatesFile;
113 private final AtomicReference<PackList> packList;
117 private final AtomicReference<AlternateHandle[]> alternates;
119 private final UnpackedObjectCache unpackedObjectCache;
122 * Initialize a reference to an on-disk object directory.
125 * configuration this directory consults for write settings.
127 * the location of the <code>objects</code> directory.
128 * @param alternatePaths
129 * a list of alternate object directories
131 * the file system abstraction which will be necessary to perform
132 * certain file system operations.
133 * @throws IOException
134 * an alternate object cannot be opened.
136 public ObjectDirectory(final Config cfg, final File dir,
137 File[] alternatePaths, FS fs) throws IOException {
140 infoDirectory = new File(objects, "info");
141 packDirectory = new File(objects, "pack");
142 alternatesFile = new File(infoDirectory, "alternates");
143 packList = new AtomicReference<PackList>(NO_PACKS);
144 unpackedObjectCache = new UnpackedObjectCache();
147 alternates = new AtomicReference<AlternateHandle[]>();
148 if (alternatePaths != null) {
149 AlternateHandle[] alt;
151 alt = new AlternateHandle[alternatePaths.length];
152 for (int i = 0; i < alternatePaths.length; i++)
153 alt[i] = openAlternate(alternatePaths[i]);
159 * @return the location of the <code>objects</code> directory.
161 public final File getDirectory() {
166 public boolean exists() {
167 return objects.exists();
171 public void create() throws IOException {
173 infoDirectory.mkdir();
174 packDirectory.mkdir();
178 public ObjectDirectoryInserter newInserter() {
179 return new ObjectDirectoryInserter(this, config);
183 public void close() {
184 unpackedObjectCache.clear();
186 final PackList packs = packList.get();
187 packList.set(NO_PACKS);
188 for (final PackFile p : packs.packs)
191 // Fully close all loaded alternates and clear the alternate list.
192 AlternateHandle[] alt = alternates.get();
194 alternates.set(null);
195 for(final AlternateHandle od : alt)
201 * Compute the location of a loose object file.
204 * identity of the loose object to map to the directory.
205 * @return location of the object, if it were to exist as a loose object.
207 public File fileFor(final AnyObjectId objectId) {
208 return fileFor(objectId.name());
211 private File fileFor(final String objectName) {
212 final String d = objectName.substring(0, 2);
213 final String f = objectName.substring(2);
214 return new File(new File(objects, d), f);
218 * @return unmodifiable collection of all known pack files local to this
219 * directory. Most recent packs are presented first. Packs most
220 * likely to contain more recent objects appear before packs
221 * containing objects referenced by commits further back in the
222 * history of the repository.
224 public Collection<PackFile> getPacks() {
225 final PackFile[] packs = packList.get().packs;
226 return Collections.unmodifiableCollection(Arrays.asList(packs));
230 * Add a single existing pack to the list of available pack files.
233 * path of the pack file to open.
235 * path of the corresponding index file.
236 * @throws IOException
237 * index file could not be opened, read, or is not recognized as
238 * a Git pack file index.
240 public void openPack(final File pack, final File idx) throws IOException {
241 final String p = pack.getName();
242 final String i = idx.getName();
244 if (p.length() != 50 || !p.startsWith("pack-") || !p.endsWith(".pack"))
245 throw new IOException(MessageFormat.format(JGitText.get().notAValidPack, pack));
247 if (i.length() != 49 || !i.startsWith("pack-") || !i.endsWith(".idx"))
248 throw new IOException(MessageFormat.format(JGitText.get().notAValidPack, idx));
250 if (!p.substring(0, 45).equals(i.substring(0, 45)))
251 throw new IOException(MessageFormat.format(JGitText.get().packDoesNotMatchIndex, pack));
253 insertPack(new PackFile(idx, pack));
257 public String toString() {
258 return "ObjectDirectory[" + getDirectory() + "]";
261 boolean hasObject1(final AnyObjectId objectId) {
262 if (unpackedObjectCache.isUnpacked(objectId))
264 for (final PackFile p : packList.get().packs) {
266 if (p.hasObject(objectId)) {
269 } catch (IOException e) {
270 // The hasObject call should have only touched the index,
271 // so any failure here indicates the index is unreadable
272 // by this process, and the pack is likewise not readable.
281 void resolve(Set<ObjectId> matches, AbbreviatedObjectId id)
283 // Go through the packs once. If we didn't find any resolutions
284 // scan for new packs and check once more.
286 int oldSize = matches.size();
287 PackList pList = packList.get();
289 for (PackFile p : pList.packs) {
291 p.resolve(matches, id, RESOLVE_ABBREV_LIMIT);
292 } catch (IOException e) {
293 // Assume the pack is corrupted.
297 if (matches.size() > RESOLVE_ABBREV_LIMIT)
300 if (matches.size() == oldSize) {
301 PackList nList = scanPacks(pList);
302 if (nList == pList || nList.packs.length == 0)
310 String fanOut = id.name().substring(0, 2);
311 String[] entries = new File(getDirectory(), fanOut).list();
312 if (entries != null) {
313 for (String e : entries) {
314 if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
317 ObjectId entId = ObjectId.fromString(fanOut + e);
318 if (id.prefixCompare(entId) == 0)
320 } catch (IllegalArgumentException notId) {
323 if (matches.size() > RESOLVE_ABBREV_LIMIT)
328 for (AlternateHandle alt : myAlternates()) {
329 alt.db.resolve(matches, id);
330 if (matches.size() > RESOLVE_ABBREV_LIMIT)
335 ObjectLoader openObject1(final WindowCursor curs,
336 final AnyObjectId objectId) throws IOException {
337 if (unpackedObjectCache.isUnpacked(objectId)) {
338 ObjectLoader ldr = openObject2(curs, objectId.name(), objectId);
342 unpackedObjectCache.remove(objectId);
345 PackList pList = packList.get();
347 for (final PackFile p : pList.packs) {
349 final ObjectLoader ldr = p.get(curs, objectId);
352 } catch (PackMismatchException e) {
353 // Pack was modified; refresh the entire pack list.
355 pList = scanPacks(pList);
357 } catch (IOException e) {
358 // Assume the pack is corrupted.
367 long getObjectSize1(final WindowCursor curs, final AnyObjectId objectId)
369 PackList pList = packList.get();
371 for (final PackFile p : pList.packs) {
373 long sz = p.getObjectSize(curs, objectId);
376 } catch (PackMismatchException e) {
377 // Pack was modified; refresh the entire pack list.
379 pList = scanPacks(pList);
381 } catch (IOException e) {
382 // Assume the pack is corrupted.
392 long getObjectSize2(WindowCursor curs, String objectName,
393 AnyObjectId objectId) throws IOException {
395 File path = fileFor(objectName);
396 FileInputStream in = new FileInputStream(path);
398 return UnpackedObject.getSize(in, objectId, curs);
402 } catch (FileNotFoundException noFile) {
408 void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
409 WindowCursor curs) throws IOException {
410 PackList pList = packList.get();
412 for (final PackFile p : pList.packs) {
414 LocalObjectRepresentation rep = p.representation(curs, otp);
416 packer.select(otp, rep);
417 } catch (PackMismatchException e) {
418 // Pack was modified; refresh the entire pack list.
420 pList = scanPacks(pList);
422 } catch (IOException e) {
423 // Assume the pack is corrupted.
431 for (AlternateHandle h : myAlternates())
432 h.db.selectObjectRepresentation(packer, otp, curs);
435 boolean hasObject2(final String objectName) {
436 return fileFor(objectName).exists();
439 ObjectLoader openObject2(final WindowCursor curs,
440 final String objectName, final AnyObjectId objectId)
443 File path = fileFor(objectName);
444 FileInputStream in = new FileInputStream(path);
446 unpackedObjectCache.add(objectId);
447 return UnpackedObject.open(in, path, objectId, curs);
451 } catch (FileNotFoundException noFile) {
452 unpackedObjectCache.remove(objectId);
458 boolean insertUnpackedObject(File tmp, ObjectId id, boolean force) {
459 if (!force && has(id)) {
460 // Object is already in the repository, remove temporary file.
467 final File dst = fileFor(id);
468 if (force && dst.exists()) {
472 if (tmp.renameTo(dst)) {
473 unpackedObjectCache.add(id);
477 // Maybe the directory doesn't exist yet as the object
478 // directories are always lazily created. Note that we
479 // try the rename first as the directory likely does exist.
481 dst.getParentFile().mkdir();
482 if (tmp.renameTo(dst)) {
483 unpackedObjectCache.add(id);
487 if (!force && has(id)) {
492 // The object failed to be renamed into its proper
493 // location and it doesn't exist in the repository
494 // either. We really don't know what went wrong, so
501 boolean tryAgain1() {
502 final PackList old = packList.get();
503 if (old.tryAgain(packDirectory.lastModified()))
504 return old != scanPacks(old);
508 private void insertPack(final PackFile pf) {
513 // If the pack in question is already present in the list
514 // (picked up by a concurrent thread that did a scan?) we
515 // do not want to insert it a second time.
517 final PackFile[] oldList = o.packs;
518 final String name = pf.getPackFile().getName();
519 for (PackFile p : oldList) {
520 if (PackFile.SORT.compare(pf, p) < 0)
522 if (name.equals(p.getPackFile().getName()))
526 final PackFile[] newList = new PackFile[1 + oldList.length];
528 System.arraycopy(oldList, 0, newList, 1, oldList.length);
529 n = new PackList(o.lastRead, o.lastModified, newList);
530 } while (!packList.compareAndSet(o, n));
533 private void removePack(final PackFile deadPack) {
538 final PackFile[] oldList = o.packs;
539 final int j = indexOf(oldList, deadPack);
543 final PackFile[] newList = new PackFile[oldList.length - 1];
544 System.arraycopy(oldList, 0, newList, 0, j);
545 System.arraycopy(oldList, j + 1, newList, j, newList.length - j);
546 n = new PackList(o.lastRead, o.lastModified, newList);
547 } while (!packList.compareAndSet(o, n));
551 private static int indexOf(final PackFile[] list, final PackFile pack) {
552 for (int i = 0; i < list.length; i++) {
559 private PackList scanPacks(final PackList original) {
560 synchronized (packList) {
565 // Another thread did the scan for us, while we
566 // were blocked on the monitor above.
570 n = scanPacksImpl(o);
573 } while (!packList.compareAndSet(o, n));
578 private PackList scanPacksImpl(final PackList old) {
579 final Map<String, PackFile> forReuse = reuseMap(old);
580 final long lastRead = System.currentTimeMillis();
581 final long lastModified = packDirectory.lastModified();
582 final Set<String> names = listPackDirectory();
583 final List<PackFile> list = new ArrayList<PackFile>(names.size() >> 2);
584 boolean foundNew = false;
585 for (final String indexName : names) {
586 // Must match "pack-[0-9a-f]{40}.idx" to be an index.
588 if (indexName.length() != 49 || !indexName.endsWith(".idx"))
591 final String base = indexName.substring(0, indexName.length() - 4);
592 final String packName = base + ".pack";
593 if (!names.contains(packName)) {
594 // Sometimes C Git's HTTP fetch transport leaves a
595 // .idx file behind and does not download the .pack.
596 // We have to skip over such useless indexes.
601 final PackFile oldPack = forReuse.remove(packName);
602 if (oldPack != null) {
607 final File packFile = new File(packDirectory, packName);
608 final File idxFile = new File(packDirectory, indexName);
609 list.add(new PackFile(idxFile, packFile));
613 // If we did not discover any new files, the modification time was not
614 // changed, and we did not remove any files, then the set of files is
615 // the same as the set we were given. Instead of building a new object
616 // return the same collection.
618 if (!foundNew && lastModified == old.lastModified && forReuse.isEmpty())
619 return old.updateLastRead(lastRead);
621 for (final PackFile p : forReuse.values()) {
626 return new PackList(lastRead, lastModified, NO_PACKS.packs);
628 final PackFile[] r = list.toArray(new PackFile[list.size()]);
629 Arrays.sort(r, PackFile.SORT);
630 return new PackList(lastRead, lastModified, r);
633 private static Map<String, PackFile> reuseMap(final PackList old) {
634 final Map<String, PackFile> forReuse = new HashMap<String, PackFile>();
635 for (final PackFile p : old.packs) {
637 // The pack instance is corrupted, and cannot be safely used
638 // again. Do not include it in our reuse map.
644 final PackFile prior = forReuse.put(p.getPackFile().getName(), p);
646 // This should never occur. It should be impossible for us
647 // to have two pack files with the same name, as all of them
648 // came out of the same directory. If it does, we promised to
649 // close any PackFiles we did not reuse, so close the second,
650 // readers are likely to be actively using the first.
652 forReuse.put(prior.getPackFile().getName(), prior);
659 private Set<String> listPackDirectory() {
660 final String[] nameList = packDirectory.list();
661 if (nameList == null)
662 return Collections.emptySet();
663 final Set<String> nameSet = new HashSet<String>(nameList.length << 1);
664 for (final String name : nameList) {
665 if (name.startsWith("pack-"))
671 AlternateHandle[] myAlternates() {
672 AlternateHandle[] alt = alternates.get();
674 synchronized (alternates) {
675 alt = alternates.get();
678 alt = loadAlternates();
679 } catch (IOException e) {
680 alt = new AlternateHandle[0];
689 private AlternateHandle[] loadAlternates() throws IOException {
690 final List<AlternateHandle> l = new ArrayList<AlternateHandle>(4);
691 final BufferedReader br = open(alternatesFile);
694 while ((line = br.readLine()) != null) {
695 l.add(openAlternate(line));
700 return l.toArray(new AlternateHandle[l.size()]);
703 private static BufferedReader open(final File f)
704 throws FileNotFoundException {
705 return new BufferedReader(new FileReader(f));
708 private AlternateHandle openAlternate(final String location)
710 final File objdir = fs.resolve(objects, location);
711 return openAlternate(objdir);
714 private AlternateHandle openAlternate(File objdir) throws IOException {
715 final File parent = objdir.getParentFile();
716 if (FileKey.isGitRepository(parent, fs)) {
717 FileKey key = FileKey.exact(parent, fs);
718 FileRepository db = (FileRepository) RepositoryCache.open(key);
719 return new AlternateRepository(db);
722 ObjectDirectory db = new ObjectDirectory(config, objdir, null, fs);
723 return new AlternateHandle(db);
726 private static final class PackList {
727 /** Last wall-clock time the directory was read. */
728 volatile long lastRead;
730 /** Last modification time of {@link ObjectDirectory#packDirectory}. */
731 final long lastModified;
733 /** All known packs, sorted by {@link PackFile#SORT}. */
734 final PackFile[] packs;
736 private boolean cannotBeRacilyClean;
738 PackList(final long lastRead, final long lastModified,
739 final PackFile[] packs) {
740 this.lastRead = lastRead;
741 this.lastModified = lastModified;
743 this.cannotBeRacilyClean = notRacyClean(lastRead);
746 private boolean notRacyClean(final long read) {
747 return read - lastModified > 2 * 60 * 1000L;
750 PackList updateLastRead(final long now) {
751 if (notRacyClean(now))
752 cannotBeRacilyClean = true;
757 boolean tryAgain(final long currLastModified) {
758 // Any difference indicates the directory was modified.
760 if (lastModified != currLastModified)
763 // We have already determined the last read was far enough
764 // after the last modification that any new modifications
765 // are certain to change the last modified time.
767 if (cannotBeRacilyClean)
770 if (notRacyClean(lastRead)) {
771 // Our last read should have marked cannotBeRacilyClean,
772 // but this thread may not have seen the change. The read
773 // of the volatile field lastRead should have fixed that.
778 // We last read this directory too close to its last observed
779 // modification time. We may have missed a modification. Scan
780 // the directory again, to ensure we still see the same state.
787 public ObjectDatabase newCachedDatabase() {
788 return newCachedFileObjectDatabase();
791 FileObjectDatabase newCachedFileObjectDatabase() {
792 return new CachedObjectDirectory(this);