View Javadoc
1   /*
2    * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
3    * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
4    * Copyright (C) 2009, Google Inc.
5    * Copyright (C) 2009, JetBrains s.r.o.
6    * Copyright (C) 2008-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
7    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
8    * Copyright (C) 2008, Thad Hughes <thadh@thad.corp.google.com> and others
9    *
10   * This program and the accompanying materials are made available under the
11   * terms of the Eclipse Distribution License v. 1.0 which is available at
12   * https://www.eclipse.org/org/documents/edl-v10.php.
13   *
14   * SPDX-License-Identifier: BSD-3-Clause
15   */
16  
17  package org.eclipse.jgit.storage.file;
18  
19  import static java.nio.charset.StandardCharsets.UTF_8;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.File;
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.text.MessageFormat;
26  
27  import org.eclipse.jgit.errors.ConfigInvalidException;
28  import org.eclipse.jgit.errors.LockFailedException;
29  import org.eclipse.jgit.internal.JGitText;
30  import org.eclipse.jgit.internal.storage.file.FileSnapshot;
31  import org.eclipse.jgit.internal.storage.file.LockFile;
32  import org.eclipse.jgit.lib.Config;
33  import org.eclipse.jgit.lib.Constants;
34  import org.eclipse.jgit.lib.ObjectId;
35  import org.eclipse.jgit.lib.StoredConfig;
36  import org.eclipse.jgit.util.FS;
37  import org.eclipse.jgit.util.FileUtils;
38  import org.eclipse.jgit.util.IO;
39  import org.eclipse.jgit.util.RawParseUtils;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  /**
44   * The configuration file that is stored in the file of the file system.
45   */
46  public class FileBasedConfig extends StoredConfig {
47  	private static final Logger LOG = LoggerFactory
48  			.getLogger(FileBasedConfig.class);
49  
50  	private final File configFile;
51  
52  	private final FS fs;
53  
54  	private boolean utf8Bom;
55  
56  	private volatile FileSnapshot snapshot;
57  
58  	private volatile ObjectId hash;
59  
60  	/**
61  	 * Create a configuration with no default fallback.
62  	 *
63  	 * @param cfgLocation
64  	 *            the location of the configuration file on the file system
65  	 * @param fs
66  	 *            the file system abstraction which will be necessary to perform
67  	 *            certain file system operations.
68  	 */
69  	public FileBasedConfig(File cfgLocation, FS fs) {
70  		this(null, cfgLocation, fs);
71  	}
72  
73  	/**
74  	 * The constructor
75  	 *
76  	 * @param base
77  	 *            the base configuration file
78  	 * @param cfgLocation
79  	 *            the location of the configuration file on the file system
80  	 * @param fs
81  	 *            the file system abstraction which will be necessary to perform
82  	 *            certain file system operations.
83  	 */
84  	public FileBasedConfig(Config base, File cfgLocation, FS fs) {
85  		super(base);
86  		configFile = cfgLocation;
87  		this.fs = fs;
88  		this.snapshot = FileSnapshot.DIRTY;
89  		this.hash = ObjectId.zeroId();
90  	}
91  
92  	/** {@inheritDoc} */
93  	@Override
94  	protected boolean notifyUponTransientChanges() {
95  		// we will notify listeners upon save()
96  		return false;
97  	}
98  
99  	/**
100 	 * Get location of the configuration file on disk
101 	 *
102 	 * @return location of the configuration file on disk
103 	 */
104 	public final File getFile() {
105 		return configFile;
106 	}
107 
108 	/**
109 	 * {@inheritDoc}
110 	 * <p>
111 	 * Load the configuration as a Git text style configuration file.
112 	 * <p>
113 	 * If the file does not exist, this configuration is cleared, and thus
114 	 * behaves the same as though the file exists, but is empty.
115 	 */
116 	@Override
117 	public void load() throws IOException, ConfigInvalidException {
118 		final int maxRetries = 5;
119 		int retryDelayMillis = 20;
120 		int retries = 0;
121 		while (true) {
122 			final FileSnapshot oldSnapshot = snapshot;
123 			final FileSnapshot newSnapshot;
124 			// don't use config in this snapshot to avoid endless recursion
125 			newSnapshot = FileSnapshot.saveNoConfig(getFile());
126 			try {
127 				final byte[] in = IO.readFully(getFile());
128 				final ObjectId newHash = hash(in);
129 				if (hash.equals(newHash)) {
130 					if (oldSnapshot.equals(newSnapshot)) {
131 						oldSnapshot.setClean(newSnapshot);
132 					} else {
133 						snapshot = newSnapshot;
134 					}
135 				} else {
136 					final String decoded;
137 					if (isUtf8(in)) {
138 						decoded = RawParseUtils.decode(UTF_8,
139 								in, 3, in.length);
140 						utf8Bom = true;
141 					} else {
142 						decoded = RawParseUtils.decode(in);
143 					}
144 					fromText(decoded);
145 					snapshot = newSnapshot;
146 					hash = newHash;
147 				}
148 				return;
149 			} catch (FileNotFoundException noFile) {
150 				// might be locked by another process (see exception Javadoc)
151 				if (retries < maxRetries && configFile.exists()) {
152 					if (LOG.isDebugEnabled()) {
153 						LOG.debug(MessageFormat.format(
154 								JGitText.get().configHandleMayBeLocked,
155 								Integer.valueOf(retries)), noFile);
156 					}
157 					try {
158 						Thread.sleep(retryDelayMillis);
159 					} catch (InterruptedException e) {
160 						Thread.currentThread().interrupt();
161 					}
162 					retries++;
163 					retryDelayMillis *= 2; // max wait 1260 ms
164 					continue;
165 				}
166 				if (configFile.exists()) {
167 					throw noFile;
168 				}
169 				clear();
170 				snapshot = newSnapshot;
171 				return;
172 			} catch (IOException e) {
173 				if (FileUtils.isStaleFileHandle(e)
174 						&& retries < maxRetries) {
175 					if (LOG.isDebugEnabled()) {
176 						LOG.debug(MessageFormat.format(
177 								JGitText.get().configHandleIsStale,
178 								Integer.valueOf(retries)), e);
179 					}
180 					retries++;
181 					continue;
182 				}
183 				throw new IOException(MessageFormat
184 						.format(JGitText.get().cannotReadFile, getFile()), e);
185 			} catch (ConfigInvalidException e) {
186 				throw new ConfigInvalidException(MessageFormat
187 						.format(JGitText.get().cannotReadFile, getFile()), e);
188 			}
189 		}
190 	}
191 
192 	/**
193 	 * {@inheritDoc}
194 	 * <p>
195 	 * Save the configuration as a Git text style configuration file.
196 	 * <p>
197 	 * <b>Warning:</b> Although this method uses the traditional Git file
198 	 * locking approach to protect against concurrent writes of the
199 	 * configuration file, it does not ensure that the file has not been
200 	 * modified since the last read, which means updates performed by other
201 	 * objects accessing the same backing file may be lost.
202 	 */
203 	@Override
204 	public void save() throws IOException {
205 		final byte[] out;
206 		final String text = toText();
207 		if (utf8Bom) {
208 			final ByteArrayOutputStream bos = new ByteArrayOutputStream();
209 			bos.write(0xEF);
210 			bos.write(0xBB);
211 			bos.write(0xBF);
212 			bos.write(text.getBytes(UTF_8));
213 			out = bos.toByteArray();
214 		} else {
215 			out = Constants.encode(text);
216 		}
217 
218 		final LockFileal/storage/file/LockFile.html#LockFile">LockFile lf = new LockFile(getFile());
219 		if (!lf.lock())
220 			throw new LockFailedException(getFile());
221 		try {
222 			lf.setNeedSnapshot(true);
223 			lf.write(out);
224 			if (!lf.commit())
225 				throw new IOException(MessageFormat.format(JGitText.get().cannotCommitWriteTo, getFile()));
226 		} finally {
227 			lf.unlock();
228 		}
229 		snapshot = lf.getCommitSnapshot();
230 		hash = hash(out);
231 		// notify the listeners
232 		fireConfigChangedEvent();
233 	}
234 
235 	/** {@inheritDoc} */
236 	@Override
237 	public void clear() {
238 		hash = hash(new byte[0]);
239 		super.clear();
240 	}
241 
242 	private static ObjectId hash(byte[] rawText) {
243 		return ObjectId.fromRaw(Constants.newMessageDigest().digest(rawText));
244 	}
245 
246 	/** {@inheritDoc} */
247 	@SuppressWarnings("nls")
248 	@Override
249 	public String toString() {
250 		return getClass().getSimpleName() + "[" + getFile().getPath() + "]";
251 	}
252 
253 	/**
254 	 * Whether the currently loaded configuration file is outdated
255 	 *
256 	 * @return returns true if the currently loaded configuration file is older
257 	 *         than the file on disk
258 	 */
259 	public boolean isOutdated() {
260 		return snapshot.isModified(getFile());
261 	}
262 
263 	/**
264 	 * {@inheritDoc}
265 	 *
266 	 * @since 4.10
267 	 */
268 	@Override
269 	protected byte[] readIncludedConfig(String relPath)
270 			throws ConfigInvalidException {
271 		final File file;
272 		if (relPath.startsWith("~/")) { //$NON-NLS-1$
273 			file = fs.resolve(fs.userHome(), relPath.substring(2));
274 		} else {
275 			file = fs.resolve(configFile.getParentFile(), relPath);
276 		}
277 
278 		if (!file.exists()) {
279 			return null;
280 		}
281 
282 		try {
283 			return IO.readFully(file);
284 		} catch (IOException ioe) {
285 			throw new ConfigInvalidException(MessageFormat
286 					.format(JGitText.get().cannotReadFile, relPath), ioe);
287 		}
288 	}
289 }