View Javadoc
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  
44  package org.eclipse.jgit.internal.storage.dfs;
45  
46  import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
47  import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
48  
49  import java.io.FileNotFoundException;
50  import java.io.IOException;
51  
52  import org.eclipse.jgit.errors.CorruptPackIndexException;
53  import org.eclipse.jgit.errors.MissingObjectException;
54  import org.eclipse.jgit.internal.JGitText;
55  import org.eclipse.jgit.internal.fsck.FsckError;
56  import org.eclipse.jgit.internal.fsck.FsckError.CorruptIndex;
57  import org.eclipse.jgit.internal.fsck.FsckPackParser;
58  import org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource;
59  import org.eclipse.jgit.lib.Constants;
60  import org.eclipse.jgit.lib.NullProgressMonitor;
61  import org.eclipse.jgit.lib.ObjectChecker;
62  import org.eclipse.jgit.lib.ObjectId;
63  import org.eclipse.jgit.lib.ProgressMonitor;
64  import org.eclipse.jgit.lib.Ref;
65  import org.eclipse.jgit.revwalk.ObjectWalk;
66  import org.eclipse.jgit.revwalk.RevObject;
67  
68  /**
69   * Verify the validity and connectivity of a DFS repository.
70   */
71  public class DfsFsck {
72  	private final DfsRepository repo;
73  	private final DfsObjDatabase objdb;
74  	private ObjectChecker objChecker = new ObjectChecker();
75  	private boolean connectivityOnly;
76  
77  	/**
78  	 * Initialize DFS fsck.
79  	 *
80  	 * @param repository
81  	 *            the dfs repository to check.
82  	 */
83  	public DfsFsck(DfsRepository repository) {
84  		repo = repository;
85  		objdb = repo.getObjectDatabase();
86  	}
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 
103 		FsckError errors = new FsckError();
104 		if (!connectivityOnly) {
105 			checkPacks(pm, errors);
106 		}
107 		checkConnectivity(pm, errors);
108 		return errors;
109 	}
110 
111 	private void checkPacks(ProgressMonitor pm, FsckError errors)
112 			throws IOException, FileNotFoundException {
113 		try (DfsReader ctx = objdb.newReader()) {
114 			for (DfsPackFile pack : objdb.getPacks()) {
115 				DfsPackDescription packDesc = pack.getPackDescription();
116 				if (packDesc.getPackSource()
117 						== PackSource.UNREACHABLE_GARBAGE) {
118 					continue;
119 				}
120 				try (ReadableChannel rc = objdb.openFile(packDesc, PACK)) {
121 					verifyPack(pm, errors, ctx, pack, rc);
122 				} catch (MissingObjectException e) {
123 					errors.getMissingObjects().add(e.getObjectId());
124 				} catch (CorruptPackIndexException e) {
125 					errors.getCorruptIndices().add(new CorruptIndex(
126 							pack.getPackDescription().getFileName(INDEX),
127 							e.getErrorType()));
128 				}
129 			}
130 		}
131 	}
132 
133 	private void verifyPack(ProgressMonitor pm, FsckError errors, DfsReader ctx,
134 			DfsPackFile pack, ReadableChannel ch)
135 					throws IOException, CorruptPackIndexException {
136 		FsckPackParser fpp = new FsckPackParser(objdb, ch);
137 		fpp.setObjectChecker(objChecker);
138 		fpp.overwriteObjectCount(pack.getPackDescription().getObjectCount());
139 		fpp.parse(pm);
140 		errors.getCorruptObjects().addAll(fpp.getCorruptObjects());
141 
142 		fpp.verifyIndex(pack.getPackIndex(ctx));
143 	}
144 
145 	private void checkConnectivity(ProgressMonitor pm, FsckError errors)
146 			throws IOException {
147 		pm.beginTask(JGitText.get().countingObjects, ProgressMonitor.UNKNOWN);
148 		try (ObjectWalk ow = new ObjectWalk(repo)) {
149 			for (Ref r : repo.getRefDatabase().getRefs()) {
150 				ObjectId objectId = r.getObjectId();
151 				if (objectId == null) {
152 					// skip unborn branch
153 					continue;
154 				}
155 				RevObject tip;
156 				try {
157 					tip = ow.parseAny(objectId);
158 					if (r.getLeaf().getName().startsWith(Constants.R_HEADS)
159 							&& tip.getType() != Constants.OBJ_COMMIT) {
160 						// heads should only point to a commit object
161 						errors.getNonCommitHeads().add(r.getLeaf().getName());
162 					}
163 					ow.markStart(tip);
164 				} catch (MissingObjectException e) {
165 					errors.getMissingObjects().add(e.getObjectId());
166 					continue;
167 				}
168 			}
169 			try {
170 				ow.checkConnectivity();
171 			} catch (MissingObjectException e) {
172 				errors.getMissingObjects().add(e.getObjectId());
173 			}
174 		}
175 		pm.endTask();
176 	}
177 
178 	/**
179 	 * Use a customized object checker instead of the default one. Caller can
180 	 * specify a skip list to ignore some errors.
181 	 *
182 	 * @param objChecker
183 	 *            A customized object checker.
184 	 */
185 	public void setObjectChecker(ObjectChecker objChecker) {
186 		this.objChecker = objChecker;
187 	}
188 
189 	/**
190 	 * Whether fsck should bypass object validity and integrity checks and only
191 	 * check connectivity.
192 	 *
193 	 * @param connectivityOnly
194 	 *            whether fsck should bypass object validity and integrity
195 	 *            checks and only check connectivity. The default is
196 	 *            {@code false}, meaning to run all checks.
197 	 */
198 	public void setConnectivityOnly(boolean connectivityOnly) {
199 		this.connectivityOnly = connectivityOnly;
200 	}
201 }