]> source.dussan.org Git - jgit.git/blob
372a978
[jgit.git] /
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.storage.file;
45
46 import java.io.BufferedReader;
47 import java.io.File;
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;
60 import java.util.Map;
61 import java.util.Set;
62 import java.util.concurrent.atomic.AtomicReference;
63
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;
78
79 /**
80  * Traditional file system based {@link ObjectDatabase}.
81  * <p>
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
85  * {@link PackFile}s.
86  * <p>
87  * Optionally an object database can reference one or more alternates; other
88  * ObjectDatabase instances that are searched in addition to the current
89  * database.
90  * <p>
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
95  * considered.
96  */
97 public class ObjectDirectory extends FileObjectDatabase {
98         private static final PackList NO_PACKS = new PackList(-1, -1, new PackFile[0]);
99
100         /** Maximum number of candidates offered as resolutions of abbreviation. */
101         private static final int RESOLVE_ABBREV_LIMIT = 256;
102
103         private final Config config;
104
105         private final File objects;
106
107         private final File infoDirectory;
108
109         private final File packDirectory;
110
111         private final File alternatesFile;
112
113         private final AtomicReference<PackList> packList;
114
115         private final FS fs;
116
117         private final AtomicReference<AlternateHandle[]> alternates;
118
119         private final UnpackedObjectCache unpackedObjectCache;
120
121         /**
122          * Initialize a reference to an on-disk object directory.
123          *
124          * @param cfg
125          *            configuration this directory consults for write settings.
126          * @param dir
127          *            the location of the <code>objects</code> directory.
128          * @param alternatePaths
129          *            a list of alternate object directories
130          * @param fs
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.
135          */
136         public ObjectDirectory(final Config cfg, final File dir,
137                         File[] alternatePaths, FS fs) throws IOException {
138                 config = cfg;
139                 objects = dir;
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();
145                 this.fs = fs;
146
147                 alternates = new AtomicReference<AlternateHandle[]>();
148                 if (alternatePaths != null) {
149                         AlternateHandle[] alt;
150
151                         alt = new AlternateHandle[alternatePaths.length];
152                         for (int i = 0; i < alternatePaths.length; i++)
153                                 alt[i] = openAlternate(alternatePaths[i]);
154                         alternates.set(alt);
155                 }
156         }
157
158         /**
159          * @return the location of the <code>objects</code> directory.
160          */
161         public final File getDirectory() {
162                 return objects;
163         }
164
165         @Override
166         public boolean exists() {
167                 return objects.exists();
168         }
169
170         @Override
171         public void create() throws IOException {
172                 objects.mkdirs();
173                 infoDirectory.mkdir();
174                 packDirectory.mkdir();
175         }
176
177         @Override
178         public ObjectDirectoryInserter newInserter() {
179                 return new ObjectDirectoryInserter(this, config);
180         }
181
182         @Override
183         public void close() {
184                 unpackedObjectCache.clear();
185
186                 final PackList packs = packList.get();
187                 packList.set(NO_PACKS);
188                 for (final PackFile p : packs.packs)
189                         p.close();
190
191                 // Fully close all loaded alternates and clear the alternate list.
192                 AlternateHandle[] alt = alternates.get();
193                 if (alt != null) {
194                         alternates.set(null);
195                         for(final AlternateHandle od : alt)
196                                 od.close();
197                 }
198         }
199
200         /**
201          * Compute the location of a loose object file.
202          *
203          * @param objectId
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.
206          */
207         public File fileFor(final AnyObjectId objectId) {
208                 return fileFor(objectId.name());
209         }
210
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);
215         }
216
217         /**
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.
223          */
224         public Collection<PackFile> getPacks() {
225                 final PackFile[] packs = packList.get().packs;
226                 return Collections.unmodifiableCollection(Arrays.asList(packs));
227         }
228
229         /**
230          * Add a single existing pack to the list of available pack files.
231          *
232          * @param pack
233          *            path of the pack file to open.
234          * @param idx
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.
239          */
240         public void openPack(final File pack, final File idx) throws IOException {
241                 final String p = pack.getName();
242                 final String i = idx.getName();
243
244                 if (p.length() != 50 || !p.startsWith("pack-") || !p.endsWith(".pack"))
245                         throw new IOException(MessageFormat.format(JGitText.get().notAValidPack, pack));
246
247                 if (i.length() != 49 || !i.startsWith("pack-") || !i.endsWith(".idx"))
248                         throw new IOException(MessageFormat.format(JGitText.get().notAValidPack, idx));
249
250                 if (!p.substring(0, 45).equals(i.substring(0, 45)))
251                         throw new IOException(MessageFormat.format(JGitText.get().packDoesNotMatchIndex, pack));
252
253                 insertPack(new PackFile(idx, pack));
254         }
255
256         @Override
257         public String toString() {
258                 return "ObjectDirectory[" + getDirectory() + "]";
259         }
260
261         boolean hasObject1(final AnyObjectId objectId) {
262                 if (unpackedObjectCache.isUnpacked(objectId))
263                         return true;
264                 for (final PackFile p : packList.get().packs) {
265                         try {
266                                 if (p.hasObject(objectId)) {
267                                         return true;
268                                 }
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.
273                                 //
274                                 removePack(p);
275                                 continue;
276                         }
277                 }
278                 return false;
279         }
280
281         void resolve(Set<ObjectId> matches, AbbreviatedObjectId id)
282                         throws IOException {
283                 // Go through the packs once. If we didn't find any resolutions
284                 // scan for new packs and check once more.
285                 //
286                 int oldSize = matches.size();
287                 PackList pList = packList.get();
288                 for (;;) {
289                         for (PackFile p : pList.packs) {
290                                 try {
291                                         p.resolve(matches, id, RESOLVE_ABBREV_LIMIT);
292                                 } catch (IOException e) {
293                                         // Assume the pack is corrupted.
294                                         //
295                                         removePack(p);
296                                 }
297                                 if (matches.size() > RESOLVE_ABBREV_LIMIT)
298                                         return;
299                         }
300                         if (matches.size() == oldSize) {
301                                 PackList nList = scanPacks(pList);
302                                 if (nList == pList || nList.packs.length == 0)
303                                         break;
304                                 pList = nList;
305                                 continue;
306                         }
307                         break;
308                 }
309
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)
315                                         continue;
316                                 try {
317                                         ObjectId entId = ObjectId.fromString(fanOut + e);
318                                         if (id.prefixCompare(entId) == 0)
319                                                 matches.add(entId);
320                                 } catch (IllegalArgumentException notId) {
321                                         continue;
322                                 }
323                                 if (matches.size() > RESOLVE_ABBREV_LIMIT)
324                                         return;
325                         }
326                 }
327
328                 for (AlternateHandle alt : myAlternates()) {
329                         alt.db.resolve(matches, id);
330                         if (matches.size() > RESOLVE_ABBREV_LIMIT)
331                                 return;
332                 }
333         }
334
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);
339                         if (ldr != null)
340                                 return ldr;
341                         else
342                                 unpackedObjectCache.remove(objectId);
343                 }
344
345                 PackList pList = packList.get();
346                 SEARCH: for (;;) {
347                         for (final PackFile p : pList.packs) {
348                                 try {
349                                         final ObjectLoader ldr = p.get(curs, objectId);
350                                         if (ldr != null)
351                                                 return ldr;
352                                 } catch (PackMismatchException e) {
353                                         // Pack was modified; refresh the entire pack list.
354                                         //
355                                         pList = scanPacks(pList);
356                                         continue SEARCH;
357                                 } catch (IOException e) {
358                                         // Assume the pack is corrupted.
359                                         //
360                                         removePack(p);
361                                 }
362                         }
363                         return null;
364                 }
365         }
366
367         long getObjectSize1(final WindowCursor curs, final AnyObjectId objectId)
368                         throws IOException {
369                 PackList pList = packList.get();
370                 SEARCH: for (;;) {
371                         for (final PackFile p : pList.packs) {
372                                 try {
373                                         long sz = p.getObjectSize(curs, objectId);
374                                         if (0 <= sz)
375                                                 return sz;
376                                 } catch (PackMismatchException e) {
377                                         // Pack was modified; refresh the entire pack list.
378                                         //
379                                         pList = scanPacks(pList);
380                                         continue SEARCH;
381                                 } catch (IOException e) {
382                                         // Assume the pack is corrupted.
383                                         //
384                                         removePack(p);
385                                 }
386                         }
387                         return -1;
388                 }
389         }
390
391         @Override
392         long getObjectSize2(WindowCursor curs, String objectName,
393                         AnyObjectId objectId) throws IOException {
394                 try {
395                         File path = fileFor(objectName);
396                         FileInputStream in = new FileInputStream(path);
397                         try {
398                                 return UnpackedObject.getSize(in, objectId, curs);
399                         } finally {
400                                 in.close();
401                         }
402                 } catch (FileNotFoundException noFile) {
403                         return -1;
404                 }
405         }
406
407         @Override
408         void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
409                         WindowCursor curs) throws IOException {
410                 PackList pList = packList.get();
411                 SEARCH: for (;;) {
412                         for (final PackFile p : pList.packs) {
413                                 try {
414                                         LocalObjectRepresentation rep = p.representation(curs, otp);
415                                         if (rep != null)
416                                                 packer.select(otp, rep);
417                                 } catch (PackMismatchException e) {
418                                         // Pack was modified; refresh the entire pack list.
419                                         //
420                                         pList = scanPacks(pList);
421                                         continue SEARCH;
422                                 } catch (IOException e) {
423                                         // Assume the pack is corrupted.
424                                         //
425                                         removePack(p);
426                                 }
427                         }
428                         break SEARCH;
429                 }
430
431                 for (AlternateHandle h : myAlternates())
432                         h.db.selectObjectRepresentation(packer, otp, curs);
433         }
434
435         boolean hasObject2(final String objectName) {
436                 return fileFor(objectName).exists();
437         }
438
439         ObjectLoader openObject2(final WindowCursor curs,
440                         final String objectName, final AnyObjectId objectId)
441                         throws IOException {
442                 try {
443                         File path = fileFor(objectName);
444                         FileInputStream in = new FileInputStream(path);
445                         try {
446                                 unpackedObjectCache.add(objectId);
447                                 return UnpackedObject.open(in, path, objectId, curs);
448                         } finally {
449                                 in.close();
450                         }
451                 } catch (FileNotFoundException noFile) {
452                         unpackedObjectCache.remove(objectId);
453                         return null;
454                 }
455         }
456
457         @Override
458         boolean insertUnpackedObject(File tmp, ObjectId id, boolean force) {
459                 if (!force && has(id)) {
460                         // Object is already in the repository, remove temporary file.
461                         //
462                         tmp.delete();
463                         return true;
464                 }
465                 tmp.setReadOnly();
466
467                 final File dst = fileFor(id);
468                 if (force && dst.exists()) {
469                         tmp.delete();
470                         return true;
471                 }
472                 if (tmp.renameTo(dst)) {
473                         unpackedObjectCache.add(id);
474                         return true;
475                 }
476
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.
480                 //
481                 dst.getParentFile().mkdir();
482                 if (tmp.renameTo(dst)) {
483                         unpackedObjectCache.add(id);
484                         return true;
485                 }
486
487                 if (!force && has(id)) {
488                         tmp.delete();
489                         return true;
490                 }
491
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
495                 // fail.
496                 //
497                 tmp.delete();
498                 return false;
499         }
500
501         boolean tryAgain1() {
502                 final PackList old = packList.get();
503                 if (old.tryAgain(packDirectory.lastModified()))
504                         return old != scanPacks(old);
505                 return false;
506         }
507
508         private void insertPack(final PackFile pf) {
509                 PackList o, n;
510                 do {
511                         o = packList.get();
512
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.
516                         //
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)
521                                         break;
522                                 if (name.equals(p.getPackFile().getName()))
523                                         return;
524                         }
525
526                         final PackFile[] newList = new PackFile[1 + oldList.length];
527                         newList[0] = pf;
528                         System.arraycopy(oldList, 0, newList, 1, oldList.length);
529                         n = new PackList(o.lastRead, o.lastModified, newList);
530                 } while (!packList.compareAndSet(o, n));
531         }
532
533         private void removePack(final PackFile deadPack) {
534                 PackList o, n;
535                 do {
536                         o = packList.get();
537
538                         final PackFile[] oldList = o.packs;
539                         final int j = indexOf(oldList, deadPack);
540                         if (j < 0)
541                                 break;
542
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));
548                 deadPack.close();
549         }
550
551         private static int indexOf(final PackFile[] list, final PackFile pack) {
552                 for (int i = 0; i < list.length; i++) {
553                         if (list[i] == pack)
554                                 return i;
555                 }
556                 return -1;
557         }
558
559         private PackList scanPacks(final PackList original) {
560                 synchronized (packList) {
561                         PackList o, n;
562                         do {
563                                 o = packList.get();
564                                 if (o != original) {
565                                         // Another thread did the scan for us, while we
566                                         // were blocked on the monitor above.
567                                         //
568                                         return o;
569                                 }
570                                 n = scanPacksImpl(o);
571                                 if (n == o)
572                                         return n;
573                         } while (!packList.compareAndSet(o, n));
574                         return n;
575                 }
576         }
577
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.
587                         //
588                         if (indexName.length() != 49 || !indexName.endsWith(".idx"))
589                                 continue;
590
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.
597                                 //
598                                 continue;
599                         }
600
601                         final PackFile oldPack = forReuse.remove(packName);
602                         if (oldPack != null) {
603                                 list.add(oldPack);
604                                 continue;
605                         }
606
607                         final File packFile = new File(packDirectory, packName);
608                         final File idxFile = new File(packDirectory, indexName);
609                         list.add(new PackFile(idxFile, packFile));
610                         foundNew = true;
611                 }
612
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.
617                 //
618                 if (!foundNew && lastModified == old.lastModified && forReuse.isEmpty())
619                         return old.updateLastRead(lastRead);
620
621                 for (final PackFile p : forReuse.values()) {
622                         p.close();
623                 }
624
625                 if (list.isEmpty())
626                         return new PackList(lastRead, lastModified, NO_PACKS.packs);
627
628                 final PackFile[] r = list.toArray(new PackFile[list.size()]);
629                 Arrays.sort(r, PackFile.SORT);
630                 return new PackList(lastRead, lastModified, r);
631         }
632
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) {
636                         if (p.invalid()) {
637                                 // The pack instance is corrupted, and cannot be safely used
638                                 // again. Do not include it in our reuse map.
639                                 //
640                                 p.close();
641                                 continue;
642                         }
643
644                         final PackFile prior = forReuse.put(p.getPackFile().getName(), p);
645                         if (prior != null) {
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.
651                                 //
652                                 forReuse.put(prior.getPackFile().getName(), prior);
653                                 p.close();
654                         }
655                 }
656                 return forReuse;
657         }
658
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-"))
666                                 nameSet.add(name);
667                 }
668                 return nameSet;
669         }
670
671         AlternateHandle[] myAlternates() {
672                 AlternateHandle[] alt = alternates.get();
673                 if (alt == null) {
674                         synchronized (alternates) {
675                                 alt = alternates.get();
676                                 if (alt == null) {
677                                         try {
678                                                 alt = loadAlternates();
679                                         } catch (IOException e) {
680                                                 alt = new AlternateHandle[0];
681                                         }
682                                         alternates.set(alt);
683                                 }
684                         }
685                 }
686                 return alt;
687         }
688
689         private AlternateHandle[] loadAlternates() throws IOException {
690                 final List<AlternateHandle> l = new ArrayList<AlternateHandle>(4);
691                 final BufferedReader br = open(alternatesFile);
692                 try {
693                         String line;
694                         while ((line = br.readLine()) != null) {
695                                 l.add(openAlternate(line));
696                         }
697                 } finally {
698                         br.close();
699                 }
700                 return l.toArray(new AlternateHandle[l.size()]);
701         }
702
703         private static BufferedReader open(final File f)
704                         throws FileNotFoundException {
705                 return new BufferedReader(new FileReader(f));
706         }
707
708         private AlternateHandle openAlternate(final String location)
709                         throws IOException {
710                 final File objdir = fs.resolve(objects, location);
711                 return openAlternate(objdir);
712         }
713
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);
720                 }
721
722                 ObjectDirectory db = new ObjectDirectory(config, objdir, null, fs);
723                 return new AlternateHandle(db);
724         }
725
726         private static final class PackList {
727                 /** Last wall-clock time the directory was read. */
728                 volatile long lastRead;
729
730                 /** Last modification time of {@link ObjectDirectory#packDirectory}. */
731                 final long lastModified;
732
733                 /** All known packs, sorted by {@link PackFile#SORT}. */
734                 final PackFile[] packs;
735
736                 private boolean cannotBeRacilyClean;
737
738                 PackList(final long lastRead, final long lastModified,
739                                 final PackFile[] packs) {
740                         this.lastRead = lastRead;
741                         this.lastModified = lastModified;
742                         this.packs = packs;
743                         this.cannotBeRacilyClean = notRacyClean(lastRead);
744                 }
745
746                 private boolean notRacyClean(final long read) {
747                         return read - lastModified > 2 * 60 * 1000L;
748                 }
749
750                 PackList updateLastRead(final long now) {
751                         if (notRacyClean(now))
752                                 cannotBeRacilyClean = true;
753                         lastRead = now;
754                         return this;
755                 }
756
757                 boolean tryAgain(final long currLastModified) {
758                         // Any difference indicates the directory was modified.
759                         //
760                         if (lastModified != currLastModified)
761                                 return true;
762
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.
766                         //
767                         if (cannotBeRacilyClean)
768                                 return false;
769
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.
774                                 //
775                                 return false;
776                         }
777
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.
781                         //
782                         return true;
783                 }
784         }
785
786         @Override
787         public ObjectDatabase newCachedDatabase() {
788                 return newCachedFileObjectDatabase();
789         }
790
791         FileObjectDatabase newCachedFileObjectDatabase() {
792                 return new CachedObjectDirectory(this);
793         }
794 }