You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectDirectory.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. package org.eclipse.jgit.storage.file;
  44. import java.io.BufferedReader;
  45. import java.io.File;
  46. import java.io.FileInputStream;
  47. import java.io.FileNotFoundException;
  48. import java.io.FileReader;
  49. import java.io.IOException;
  50. import java.text.MessageFormat;
  51. import java.util.ArrayList;
  52. import java.util.Arrays;
  53. import java.util.Collection;
  54. import java.util.Collections;
  55. import java.util.HashMap;
  56. import java.util.HashSet;
  57. import java.util.List;
  58. import java.util.Map;
  59. import java.util.Set;
  60. import java.util.concurrent.atomic.AtomicReference;
  61. import org.eclipse.jgit.JGitText;
  62. import org.eclipse.jgit.errors.PackMismatchException;
  63. import org.eclipse.jgit.events.ConfigChangedEvent;
  64. import org.eclipse.jgit.events.ConfigChangedListener;
  65. import org.eclipse.jgit.lib.AnyObjectId;
  66. import org.eclipse.jgit.lib.Config;
  67. import org.eclipse.jgit.lib.CoreConfig;
  68. import org.eclipse.jgit.lib.ObjectDatabase;
  69. import org.eclipse.jgit.lib.ObjectId;
  70. import org.eclipse.jgit.lib.ObjectInserter;
  71. import org.eclipse.jgit.lib.ObjectLoader;
  72. import org.eclipse.jgit.lib.RepositoryCache;
  73. import org.eclipse.jgit.lib.RepositoryCache.FileKey;
  74. import org.eclipse.jgit.storage.pack.ObjectToPack;
  75. import org.eclipse.jgit.storage.pack.PackWriter;
  76. import org.eclipse.jgit.util.FS;
  77. /**
  78. * Traditional file system based {@link ObjectDatabase}.
  79. * <p>
  80. * This is the classical object database representation for a Git repository,
  81. * where objects are stored loose by hashing them into directories by their
  82. * {@link ObjectId}, or are stored in compressed containers known as
  83. * {@link PackFile}s.
  84. * <p>
  85. * Optionally an object database can reference one or more alternates; other
  86. * ObjectDatabase instances that are searched in addition to the current
  87. * database.
  88. * <p>
  89. * Databases are divided into two halves: a half that is considered to be fast
  90. * to search (the {@code PackFile}s), and a half that is considered to be slow
  91. * to search (loose objects). When alternates are present the fast half is fully
  92. * searched (recursively through all alternates) before the slow half is
  93. * considered.
  94. */
  95. public class ObjectDirectory extends FileObjectDatabase implements
  96. ConfigChangedListener {
  97. private static final PackList NO_PACKS = new PackList(-1, -1, new PackFile[0]);
  98. private final Config config;
  99. private final File objects;
  100. private final File infoDirectory;
  101. private final File packDirectory;
  102. private final File alternatesFile;
  103. private final AtomicReference<PackList> packList;
  104. private final FS fs;
  105. private final AtomicReference<AlternateHandle[]> alternates;
  106. private int streamFileThreshold;
  107. /**
  108. * Initialize a reference to an on-disk object directory.
  109. *
  110. * @param cfg
  111. * configuration this directory consults for write settings.
  112. * @param dir
  113. * the location of the <code>objects</code> directory.
  114. * @param alternatePaths
  115. * a list of alternate object directories
  116. * @param fs
  117. * the file system abstraction which will be necessary to perform
  118. * certain file system operations.
  119. * @throws IOException
  120. * an alternate object cannot be opened.
  121. */
  122. public ObjectDirectory(final Config cfg, final File dir,
  123. File[] alternatePaths, FS fs) throws IOException {
  124. config = cfg;
  125. objects = dir;
  126. infoDirectory = new File(objects, "info");
  127. packDirectory = new File(objects, "pack");
  128. alternatesFile = new File(infoDirectory, "alternates");
  129. packList = new AtomicReference<PackList>(NO_PACKS);
  130. this.fs = fs;
  131. alternates = new AtomicReference<AlternateHandle[]>();
  132. if (alternatePaths != null) {
  133. AlternateHandle[] alt;
  134. alt = new AlternateHandle[alternatePaths.length];
  135. for (int i = 0; i < alternatePaths.length; i++)
  136. alt[i] = openAlternate(alternatePaths[i]);
  137. alternates.set(alt);
  138. }
  139. onConfigChanged(new ConfigChangedEvent());
  140. }
  141. public void onConfigChanged(ConfigChangedEvent event) {
  142. CoreConfig core = config.get(CoreConfig.KEY);
  143. streamFileThreshold = core.getStreamFileThreshold();
  144. }
  145. /**
  146. * @return the location of the <code>objects</code> directory.
  147. */
  148. public final File getDirectory() {
  149. return objects;
  150. }
  151. @Override
  152. public boolean exists() {
  153. return objects.exists();
  154. }
  155. @Override
  156. public void create() throws IOException {
  157. objects.mkdirs();
  158. infoDirectory.mkdir();
  159. packDirectory.mkdir();
  160. }
  161. @Override
  162. public ObjectInserter newInserter() {
  163. return new ObjectDirectoryInserter(this, config);
  164. }
  165. @Override
  166. public void close() {
  167. final PackList packs = packList.get();
  168. packList.set(NO_PACKS);
  169. for (final PackFile p : packs.packs)
  170. p.close();
  171. // Fully close all loaded alternates and clear the alternate list.
  172. AlternateHandle[] alt = alternates.get();
  173. if (alt != null) {
  174. alternates.set(null);
  175. for(final AlternateHandle od : alt)
  176. od.close();
  177. }
  178. }
  179. /**
  180. * Compute the location of a loose object file.
  181. *
  182. * @param objectId
  183. * identity of the loose object to map to the directory.
  184. * @return location of the object, if it were to exist as a loose object.
  185. */
  186. public File fileFor(final AnyObjectId objectId) {
  187. return fileFor(objectId.name());
  188. }
  189. private File fileFor(final String objectName) {
  190. final String d = objectName.substring(0, 2);
  191. final String f = objectName.substring(2);
  192. return new File(new File(objects, d), f);
  193. }
  194. /**
  195. * @return unmodifiable collection of all known pack files local to this
  196. * directory. Most recent packs are presented first. Packs most
  197. * likely to contain more recent objects appear before packs
  198. * containing objects referenced by commits further back in the
  199. * history of the repository.
  200. */
  201. public Collection<PackFile> getPacks() {
  202. final PackFile[] packs = packList.get().packs;
  203. return Collections.unmodifiableCollection(Arrays.asList(packs));
  204. }
  205. /**
  206. * Add a single existing pack to the list of available pack files.
  207. *
  208. * @param pack
  209. * path of the pack file to open.
  210. * @param idx
  211. * path of the corresponding index file.
  212. * @throws IOException
  213. * index file could not be opened, read, or is not recognized as
  214. * a Git pack file index.
  215. */
  216. public void openPack(final File pack, final File idx) throws IOException {
  217. final String p = pack.getName();
  218. final String i = idx.getName();
  219. if (p.length() != 50 || !p.startsWith("pack-") || !p.endsWith(".pack"))
  220. throw new IOException(MessageFormat.format(JGitText.get().notAValidPack, pack));
  221. if (i.length() != 49 || !i.startsWith("pack-") || !i.endsWith(".idx"))
  222. throw new IOException(MessageFormat.format(JGitText.get().notAValidPack, idx));
  223. if (!p.substring(0, 45).equals(i.substring(0, 45)))
  224. throw new IOException(MessageFormat.format(JGitText.get().packDoesNotMatchIndex, pack));
  225. insertPack(new PackFile(idx, pack));
  226. }
  227. @Override
  228. public String toString() {
  229. return "ObjectDirectory[" + getDirectory() + "]";
  230. }
  231. boolean hasObject1(final AnyObjectId objectId) {
  232. for (final PackFile p : packList.get().packs) {
  233. try {
  234. if (p.hasObject(objectId)) {
  235. return true;
  236. }
  237. } catch (IOException e) {
  238. // The hasObject call should have only touched the index,
  239. // so any failure here indicates the index is unreadable
  240. // by this process, and the pack is likewise not readable.
  241. //
  242. removePack(p);
  243. continue;
  244. }
  245. }
  246. return false;
  247. }
  248. ObjectLoader openObject1(final WindowCursor curs,
  249. final AnyObjectId objectId) throws IOException {
  250. PackList pList = packList.get();
  251. SEARCH: for (;;) {
  252. for (final PackFile p : pList.packs) {
  253. try {
  254. final ObjectLoader ldr = p.get(curs, objectId);
  255. if (ldr != null)
  256. return ldr;
  257. } catch (PackMismatchException e) {
  258. // Pack was modified; refresh the entire pack list.
  259. //
  260. pList = scanPacks(pList);
  261. continue SEARCH;
  262. } catch (IOException e) {
  263. // Assume the pack is corrupted.
  264. //
  265. removePack(p);
  266. }
  267. }
  268. return null;
  269. }
  270. }
  271. @Override
  272. void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
  273. WindowCursor curs) throws IOException {
  274. PackList pList = packList.get();
  275. SEARCH: for (;;) {
  276. for (final PackFile p : pList.packs) {
  277. try {
  278. LocalObjectRepresentation rep = p.representation(curs, otp);
  279. if (rep != null)
  280. packer.select(otp, rep);
  281. } catch (PackMismatchException e) {
  282. // Pack was modified; refresh the entire pack list.
  283. //
  284. pList = scanPacks(pList);
  285. continue SEARCH;
  286. } catch (IOException e) {
  287. // Assume the pack is corrupted.
  288. //
  289. removePack(p);
  290. }
  291. }
  292. break SEARCH;
  293. }
  294. for (AlternateHandle h : myAlternates())
  295. h.db.selectObjectRepresentation(packer, otp, curs);
  296. }
  297. boolean hasObject2(final String objectName) {
  298. return fileFor(objectName).exists();
  299. }
  300. ObjectLoader openObject2(final WindowCursor curs,
  301. final String objectName, final AnyObjectId objectId)
  302. throws IOException {
  303. try {
  304. File path = fileFor(objectName);
  305. FileInputStream in = new FileInputStream(path);
  306. try {
  307. return UnpackedObject.open(in, path, objectId, curs);
  308. } finally {
  309. in.close();
  310. }
  311. } catch (FileNotFoundException noFile) {
  312. return null;
  313. }
  314. }
  315. boolean tryAgain1() {
  316. final PackList old = packList.get();
  317. if (old.tryAgain(packDirectory.lastModified()))
  318. return old != scanPacks(old);
  319. return false;
  320. }
  321. private void insertPack(final PackFile pf) {
  322. PackList o, n;
  323. do {
  324. o = packList.get();
  325. // If the pack in question is already present in the list
  326. // (picked up by a concurrent thread that did a scan?) we
  327. // do not want to insert it a second time.
  328. //
  329. final PackFile[] oldList = o.packs;
  330. final String name = pf.getPackFile().getName();
  331. for (PackFile p : oldList) {
  332. if (PackFile.SORT.compare(pf, p) < 0)
  333. break;
  334. if (name.equals(p.getPackFile().getName()))
  335. return;
  336. }
  337. final PackFile[] newList = new PackFile[1 + oldList.length];
  338. newList[0] = pf;
  339. System.arraycopy(oldList, 0, newList, 1, oldList.length);
  340. n = new PackList(o.lastRead, o.lastModified, newList);
  341. } while (!packList.compareAndSet(o, n));
  342. }
  343. private void removePack(final PackFile deadPack) {
  344. PackList o, n;
  345. do {
  346. o = packList.get();
  347. final PackFile[] oldList = o.packs;
  348. final int j = indexOf(oldList, deadPack);
  349. if (j < 0)
  350. break;
  351. final PackFile[] newList = new PackFile[oldList.length - 1];
  352. System.arraycopy(oldList, 0, newList, 0, j);
  353. System.arraycopy(oldList, j + 1, newList, j, newList.length - j);
  354. n = new PackList(o.lastRead, o.lastModified, newList);
  355. } while (!packList.compareAndSet(o, n));
  356. deadPack.close();
  357. }
  358. private static int indexOf(final PackFile[] list, final PackFile pack) {
  359. for (int i = 0; i < list.length; i++) {
  360. if (list[i] == pack)
  361. return i;
  362. }
  363. return -1;
  364. }
  365. private PackList scanPacks(final PackList original) {
  366. synchronized (packList) {
  367. PackList o, n;
  368. do {
  369. o = packList.get();
  370. if (o != original) {
  371. // Another thread did the scan for us, while we
  372. // were blocked on the monitor above.
  373. //
  374. return o;
  375. }
  376. n = scanPacksImpl(o);
  377. if (n == o)
  378. return n;
  379. } while (!packList.compareAndSet(o, n));
  380. return n;
  381. }
  382. }
  383. private PackList scanPacksImpl(final PackList old) {
  384. final Map<String, PackFile> forReuse = reuseMap(old);
  385. final long lastRead = System.currentTimeMillis();
  386. final long lastModified = packDirectory.lastModified();
  387. final Set<String> names = listPackDirectory();
  388. final List<PackFile> list = new ArrayList<PackFile>(names.size() >> 2);
  389. boolean foundNew = false;
  390. for (final String indexName : names) {
  391. // Must match "pack-[0-9a-f]{40}.idx" to be an index.
  392. //
  393. if (indexName.length() != 49 || !indexName.endsWith(".idx"))
  394. continue;
  395. final String base = indexName.substring(0, indexName.length() - 4);
  396. final String packName = base + ".pack";
  397. if (!names.contains(packName)) {
  398. // Sometimes C Git's HTTP fetch transport leaves a
  399. // .idx file behind and does not download the .pack.
  400. // We have to skip over such useless indexes.
  401. //
  402. continue;
  403. }
  404. final PackFile oldPack = forReuse.remove(packName);
  405. if (oldPack != null) {
  406. list.add(oldPack);
  407. continue;
  408. }
  409. final File packFile = new File(packDirectory, packName);
  410. final File idxFile = new File(packDirectory, indexName);
  411. list.add(new PackFile(idxFile, packFile));
  412. foundNew = true;
  413. }
  414. // If we did not discover any new files, the modification time was not
  415. // changed, and we did not remove any files, then the set of files is
  416. // the same as the set we were given. Instead of building a new object
  417. // return the same collection.
  418. //
  419. if (!foundNew && lastModified == old.lastModified && forReuse.isEmpty())
  420. return old.updateLastRead(lastRead);
  421. for (final PackFile p : forReuse.values()) {
  422. p.close();
  423. }
  424. if (list.isEmpty())
  425. return new PackList(lastRead, lastModified, NO_PACKS.packs);
  426. final PackFile[] r = list.toArray(new PackFile[list.size()]);
  427. Arrays.sort(r, PackFile.SORT);
  428. return new PackList(lastRead, lastModified, r);
  429. }
  430. private static Map<String, PackFile> reuseMap(final PackList old) {
  431. final Map<String, PackFile> forReuse = new HashMap<String, PackFile>();
  432. for (final PackFile p : old.packs) {
  433. if (p.invalid()) {
  434. // The pack instance is corrupted, and cannot be safely used
  435. // again. Do not include it in our reuse map.
  436. //
  437. p.close();
  438. continue;
  439. }
  440. final PackFile prior = forReuse.put(p.getPackFile().getName(), p);
  441. if (prior != null) {
  442. // This should never occur. It should be impossible for us
  443. // to have two pack files with the same name, as all of them
  444. // came out of the same directory. If it does, we promised to
  445. // close any PackFiles we did not reuse, so close the second,
  446. // readers are likely to be actively using the first.
  447. //
  448. forReuse.put(prior.getPackFile().getName(), prior);
  449. p.close();
  450. }
  451. }
  452. return forReuse;
  453. }
  454. private Set<String> listPackDirectory() {
  455. final String[] nameList = packDirectory.list();
  456. if (nameList == null)
  457. return Collections.emptySet();
  458. final Set<String> nameSet = new HashSet<String>(nameList.length << 1);
  459. for (final String name : nameList) {
  460. if (name.startsWith("pack-"))
  461. nameSet.add(name);
  462. }
  463. return nameSet;
  464. }
  465. AlternateHandle[] myAlternates() {
  466. AlternateHandle[] alt = alternates.get();
  467. if (alt == null) {
  468. synchronized (alternates) {
  469. alt = alternates.get();
  470. if (alt == null) {
  471. try {
  472. alt = loadAlternates();
  473. } catch (IOException e) {
  474. alt = new AlternateHandle[0];
  475. }
  476. alternates.set(alt);
  477. }
  478. }
  479. }
  480. return alt;
  481. }
  482. private AlternateHandle[] loadAlternates() throws IOException {
  483. final List<AlternateHandle> l = new ArrayList<AlternateHandle>(4);
  484. final BufferedReader br = open(alternatesFile);
  485. try {
  486. String line;
  487. while ((line = br.readLine()) != null) {
  488. l.add(openAlternate(line));
  489. }
  490. } finally {
  491. br.close();
  492. }
  493. return l.toArray(new AlternateHandle[l.size()]);
  494. }
  495. private static BufferedReader open(final File f)
  496. throws FileNotFoundException {
  497. return new BufferedReader(new FileReader(f));
  498. }
  499. private AlternateHandle openAlternate(final String location)
  500. throws IOException {
  501. final File objdir = fs.resolve(objects, location);
  502. return openAlternate(objdir);
  503. }
  504. private AlternateHandle openAlternate(File objdir) throws IOException {
  505. final File parent = objdir.getParentFile();
  506. if (FileKey.isGitRepository(parent, fs)) {
  507. FileKey key = FileKey.exact(parent, fs);
  508. FileRepository db = (FileRepository) RepositoryCache.open(key);
  509. return new AlternateRepository(db);
  510. }
  511. ObjectDirectory db = new ObjectDirectory(config, objdir, null, fs);
  512. return new AlternateHandle(db);
  513. }
  514. private static final class PackList {
  515. /** Last wall-clock time the directory was read. */
  516. volatile long lastRead;
  517. /** Last modification time of {@link ObjectDirectory#packDirectory}. */
  518. final long lastModified;
  519. /** All known packs, sorted by {@link PackFile#SORT}. */
  520. final PackFile[] packs;
  521. private boolean cannotBeRacilyClean;
  522. PackList(final long lastRead, final long lastModified,
  523. final PackFile[] packs) {
  524. this.lastRead = lastRead;
  525. this.lastModified = lastModified;
  526. this.packs = packs;
  527. this.cannotBeRacilyClean = notRacyClean(lastRead);
  528. }
  529. private boolean notRacyClean(final long read) {
  530. return read - lastModified > 2 * 60 * 1000L;
  531. }
  532. PackList updateLastRead(final long now) {
  533. if (notRacyClean(now))
  534. cannotBeRacilyClean = true;
  535. lastRead = now;
  536. return this;
  537. }
  538. boolean tryAgain(final long currLastModified) {
  539. // Any difference indicates the directory was modified.
  540. //
  541. if (lastModified != currLastModified)
  542. return true;
  543. // We have already determined the last read was far enough
  544. // after the last modification that any new modifications
  545. // are certain to change the last modified time.
  546. //
  547. if (cannotBeRacilyClean)
  548. return false;
  549. if (notRacyClean(lastRead)) {
  550. // Our last read should have marked cannotBeRacilyClean,
  551. // but this thread may not have seen the change. The read
  552. // of the volatile field lastRead should have fixed that.
  553. //
  554. return false;
  555. }
  556. // We last read this directory too close to its last observed
  557. // modification time. We may have missed a modification. Scan
  558. // the directory again, to ensure we still see the same state.
  559. //
  560. return true;
  561. }
  562. }
  563. @Override
  564. public ObjectDatabase newCachedDatabase() {
  565. return newCachedFileObjectDatabase();
  566. }
  567. FileObjectDatabase newCachedFileObjectDatabase() {
  568. return new CachedObjectDirectory(this);
  569. }
  570. @Override
  571. int getStreamFileThreshold() {
  572. return streamFileThreshold;
  573. }
  574. }