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.

DfsFsck.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C) 2017, 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.internal.storage.dfs;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
  46. import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
  47. import java.io.FileNotFoundException;
  48. import java.io.IOException;
  49. import org.eclipse.jgit.errors.CorruptPackIndexException;
  50. import org.eclipse.jgit.errors.MissingObjectException;
  51. import org.eclipse.jgit.internal.JGitText;
  52. import org.eclipse.jgit.internal.fsck.FsckError;
  53. import org.eclipse.jgit.internal.fsck.FsckError.CorruptIndex;
  54. import org.eclipse.jgit.internal.fsck.FsckError.CorruptObject;
  55. import org.eclipse.jgit.internal.fsck.FsckPackParser;
  56. import org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource;
  57. import org.eclipse.jgit.internal.submodule.SubmoduleValidator;
  58. import org.eclipse.jgit.internal.submodule.SubmoduleValidator.SubmoduleValidationException;
  59. import org.eclipse.jgit.lib.AnyObjectId;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.GitmoduleEntry;
  62. import org.eclipse.jgit.lib.NullProgressMonitor;
  63. import org.eclipse.jgit.lib.ObjectChecker;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.ObjectLoader;
  66. import org.eclipse.jgit.lib.ProgressMonitor;
  67. import org.eclipse.jgit.lib.Ref;
  68. import org.eclipse.jgit.revwalk.ObjectWalk;
  69. import org.eclipse.jgit.revwalk.RevObject;
  70. /**
  71. * Verify the validity and connectivity of a DFS repository.
  72. */
  73. public class DfsFsck {
  74. private final DfsRepository repo;
  75. private final DfsObjDatabase objdb;
  76. private ObjectChecker objChecker = new ObjectChecker();
  77. private boolean connectivityOnly;
  78. /**
  79. * Initialize DFS fsck.
  80. *
  81. * @param repository
  82. * the dfs repository to check.
  83. */
  84. public DfsFsck(DfsRepository repository) {
  85. repo = repository;
  86. objdb = repo.getObjectDatabase();
  87. }
  88. /**
  89. * Verify the integrity and connectivity of all objects in the object
  90. * database.
  91. *
  92. * @param pm
  93. * callback to provide progress feedback during the check.
  94. * @return all errors about the repository.
  95. * @throws java.io.IOException
  96. * if encounters IO errors during the process.
  97. */
  98. public FsckError check(ProgressMonitor pm) throws IOException {
  99. if (pm == null) {
  100. pm = NullProgressMonitor.INSTANCE;
  101. }
  102. FsckError errors = new FsckError();
  103. if (!connectivityOnly) {
  104. objChecker.reset();
  105. checkPacks(pm, errors);
  106. }
  107. checkConnectivity(pm, errors);
  108. return errors;
  109. }
  110. private void checkPacks(ProgressMonitor pm, FsckError errors)
  111. throws IOException, FileNotFoundException {
  112. try (DfsReader ctx = objdb.newReader()) {
  113. for (DfsPackFile pack : objdb.getPacks()) {
  114. DfsPackDescription packDesc = pack.getPackDescription();
  115. if (packDesc.getPackSource()
  116. == PackSource.UNREACHABLE_GARBAGE) {
  117. continue;
  118. }
  119. try (ReadableChannel rc = objdb.openFile(packDesc, PACK)) {
  120. verifyPack(pm, errors, ctx, pack, rc);
  121. } catch (MissingObjectException e) {
  122. errors.getMissingObjects().add(e.getObjectId());
  123. } catch (CorruptPackIndexException e) {
  124. errors.getCorruptIndices().add(new CorruptIndex(
  125. pack.getPackDescription().getFileName(INDEX),
  126. e.getErrorType()));
  127. }
  128. }
  129. }
  130. checkGitModules(pm, errors);
  131. }
  132. private void verifyPack(ProgressMonitor pm, FsckError errors, DfsReader ctx,
  133. DfsPackFile pack, ReadableChannel ch)
  134. throws IOException, CorruptPackIndexException {
  135. FsckPackParser fpp = new FsckPackParser(objdb, ch);
  136. fpp.setObjectChecker(objChecker);
  137. fpp.overwriteObjectCount(pack.getPackDescription().getObjectCount());
  138. fpp.parse(pm);
  139. errors.getCorruptObjects().addAll(fpp.getCorruptObjects());
  140. fpp.verifyIndex(pack.getPackIndex(ctx));
  141. }
  142. private void checkGitModules(ProgressMonitor pm, FsckError errors)
  143. throws IOException {
  144. pm.beginTask(JGitText.get().validatingGitModules,
  145. objChecker.getGitsubmodules().size());
  146. for (GitmoduleEntry entry : objChecker.getGitsubmodules()) {
  147. AnyObjectId blobId = entry.getBlobId();
  148. ObjectLoader blob = objdb.open(blobId, Constants.OBJ_BLOB);
  149. try {
  150. SubmoduleValidator.assertValidGitModulesFile(
  151. new String(blob.getBytes(), UTF_8));
  152. } catch (SubmoduleValidationException e) {
  153. CorruptObject co = new FsckError.CorruptObject(
  154. blobId.toObjectId(), Constants.OBJ_BLOB,
  155. e.getFsckMessageId());
  156. errors.getCorruptObjects().add(co);
  157. }
  158. pm.update(1);
  159. }
  160. pm.endTask();
  161. }
  162. private void checkConnectivity(ProgressMonitor pm, FsckError errors)
  163. throws IOException {
  164. pm.beginTask(JGitText.get().countingObjects, ProgressMonitor.UNKNOWN);
  165. try (ObjectWalk ow = new ObjectWalk(repo)) {
  166. for (Ref r : repo.getRefDatabase().getRefs()) {
  167. ObjectId objectId = r.getObjectId();
  168. if (objectId == null) {
  169. // skip unborn branch
  170. continue;
  171. }
  172. RevObject tip;
  173. try {
  174. tip = ow.parseAny(objectId);
  175. if (r.getLeaf().getName().startsWith(Constants.R_HEADS)
  176. && tip.getType() != Constants.OBJ_COMMIT) {
  177. // heads should only point to a commit object
  178. errors.getNonCommitHeads().add(r.getLeaf().getName());
  179. }
  180. ow.markStart(tip);
  181. } catch (MissingObjectException e) {
  182. errors.getMissingObjects().add(e.getObjectId());
  183. continue;
  184. }
  185. }
  186. try {
  187. ow.checkConnectivity();
  188. } catch (MissingObjectException e) {
  189. errors.getMissingObjects().add(e.getObjectId());
  190. }
  191. }
  192. pm.endTask();
  193. }
  194. /**
  195. * Use a customized object checker instead of the default one. Caller can
  196. * specify a skip list to ignore some errors.
  197. *
  198. * It will be reset at the start of each {{@link #check(ProgressMonitor)}
  199. * call.
  200. *
  201. * @param objChecker
  202. * A customized object checker.
  203. */
  204. public void setObjectChecker(ObjectChecker objChecker) {
  205. this.objChecker = objChecker;
  206. }
  207. /**
  208. * Whether fsck should bypass object validity and integrity checks and only
  209. * check connectivity.
  210. *
  211. * @param connectivityOnly
  212. * whether fsck should bypass object validity and integrity
  213. * checks and only check connectivity. The default is
  214. * {@code false}, meaning to run all checks.
  215. */
  216. public void setConnectivityOnly(boolean connectivityOnly) {
  217. this.connectivityOnly = connectivityOnly;
  218. }
  219. }