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 32KB

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