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.

FileBasedConfig.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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>
  9. * and other copyright owners as documented in the project's IP log.
  10. *
  11. * This program and the accompanying materials are made available
  12. * under the terms of the Eclipse Distribution License v1.0 which
  13. * accompanies this distribution, is reproduced below, and is
  14. * available at http://www.eclipse.org/org/documents/edl-v10.php
  15. *
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or
  19. * without modification, are permitted provided that the following
  20. * conditions are met:
  21. *
  22. * - Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. *
  25. * - Redistributions in binary form must reproduce the above
  26. * copyright notice, this list of conditions and the following
  27. * disclaimer in the documentation and/or other materials provided
  28. * with the distribution.
  29. *
  30. * - Neither the name of the Eclipse Foundation, Inc. nor the
  31. * names of its contributors may be used to endorse or promote
  32. * products derived from this software without specific prior
  33. * written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  40. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  47. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48. */
  49. package org.eclipse.jgit.storage.file;
  50. import java.io.ByteArrayOutputStream;
  51. import java.io.File;
  52. import java.io.FileNotFoundException;
  53. import java.io.IOException;
  54. import java.text.MessageFormat;
  55. import org.eclipse.jgit.errors.ConfigInvalidException;
  56. import org.eclipse.jgit.errors.LockFailedException;
  57. import org.eclipse.jgit.internal.JGitText;
  58. import org.eclipse.jgit.internal.storage.file.FileSnapshot;
  59. import org.eclipse.jgit.internal.storage.file.LockFile;
  60. import org.eclipse.jgit.lib.Config;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.StoredConfig;
  64. import org.eclipse.jgit.util.FS;
  65. import org.eclipse.jgit.util.IO;
  66. import org.eclipse.jgit.util.RawParseUtils;
  67. /**
  68. * The configuration file that is stored in the file of the file system.
  69. */
  70. public class FileBasedConfig extends StoredConfig {
  71. private final File configFile;
  72. private boolean utf8Bom;
  73. private volatile FileSnapshot snapshot;
  74. private volatile ObjectId hash;
  75. /**
  76. * Create a configuration with no default fallback.
  77. *
  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(File cfgLocation, FS fs) {
  85. this(null, cfgLocation, fs);
  86. }
  87. /**
  88. * The constructor
  89. *
  90. * @param base
  91. * the base configuration file
  92. * @param cfgLocation
  93. * the location of the configuration file on the file system
  94. * @param fs
  95. * the file system abstraction which will be necessary to perform
  96. * certain file system operations.
  97. */
  98. public FileBasedConfig(Config base, File cfgLocation, FS fs) {
  99. super(base);
  100. configFile = cfgLocation;
  101. this.snapshot = FileSnapshot.DIRTY;
  102. this.hash = ObjectId.zeroId();
  103. }
  104. @Override
  105. protected boolean notifyUponTransientChanges() {
  106. // we will notify listeners upon save()
  107. return false;
  108. }
  109. /** @return location of the configuration file on disk */
  110. public final File getFile() {
  111. return configFile;
  112. }
  113. /**
  114. * Load the configuration as a Git text style configuration file.
  115. * <p>
  116. * If the file does not exist, this configuration is cleared, and thus
  117. * behaves the same as though the file exists, but is empty.
  118. *
  119. * @throws IOException
  120. * the file could not be read (but does exist).
  121. * @throws ConfigInvalidException
  122. * the file is not a properly formatted configuration file.
  123. */
  124. @Override
  125. public void load() throws IOException, ConfigInvalidException {
  126. final FileSnapshot oldSnapshot = snapshot;
  127. final FileSnapshot newSnapshot = FileSnapshot.save(getFile());
  128. try {
  129. final byte[] in = IO.readFully(getFile());
  130. final ObjectId newHash = hash(in);
  131. if (hash.equals(newHash)) {
  132. if (oldSnapshot.equals(newSnapshot))
  133. oldSnapshot.setClean(newSnapshot);
  134. else
  135. snapshot = newSnapshot;
  136. } else {
  137. final String decoded;
  138. if (isUtf8(in)) {
  139. decoded = RawParseUtils.decode(RawParseUtils.UTF8_CHARSET,
  140. in, 3, in.length);
  141. utf8Bom = true;
  142. } else {
  143. decoded = RawParseUtils.decode(in);
  144. }
  145. fromText(decoded);
  146. snapshot = newSnapshot;
  147. hash = newHash;
  148. }
  149. } catch (FileNotFoundException noFile) {
  150. if (configFile.exists()) {
  151. throw noFile;
  152. }
  153. clear();
  154. snapshot = newSnapshot;
  155. } catch (IOException e) {
  156. final IOException e2 = new IOException(MessageFormat.format(JGitText.get().cannotReadFile, getFile()));
  157. e2.initCause(e);
  158. throw e2;
  159. } catch (ConfigInvalidException e) {
  160. throw new ConfigInvalidException(MessageFormat.format(JGitText.get().cannotReadFile, getFile()), e);
  161. }
  162. }
  163. /**
  164. * Save the configuration as a Git text style configuration file.
  165. * <p>
  166. * <b>Warning:</b> Although this method uses the traditional Git file
  167. * locking approach to protect against concurrent writes of the
  168. * configuration file, it does not ensure that the file has not been
  169. * modified since the last read, which means updates performed by other
  170. * objects accessing the same backing file may be lost.
  171. *
  172. * @throws IOException
  173. * the file could not be written.
  174. */
  175. public void save() throws IOException {
  176. final byte[] out;
  177. final String text = toText();
  178. if (utf8Bom) {
  179. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  180. bos.write(0xEF);
  181. bos.write(0xBB);
  182. bos.write(0xBF);
  183. bos.write(text.getBytes(RawParseUtils.UTF8_CHARSET.name()));
  184. out = bos.toByteArray();
  185. } else {
  186. out = Constants.encode(text);
  187. }
  188. final LockFile lf = new LockFile(getFile());
  189. if (!lf.lock())
  190. throw new LockFailedException(getFile());
  191. try {
  192. lf.setNeedSnapshot(true);
  193. lf.write(out);
  194. if (!lf.commit())
  195. throw new IOException(MessageFormat.format(JGitText.get().cannotCommitWriteTo, getFile()));
  196. } finally {
  197. lf.unlock();
  198. }
  199. snapshot = lf.getCommitSnapshot();
  200. hash = hash(out);
  201. // notify the listeners
  202. fireConfigChangedEvent();
  203. }
  204. @Override
  205. public void clear() {
  206. hash = hash(new byte[0]);
  207. super.clear();
  208. }
  209. private static ObjectId hash(final byte[] rawText) {
  210. return ObjectId.fromRaw(Constants.newMessageDigest().digest(rawText));
  211. }
  212. @SuppressWarnings("nls")
  213. @Override
  214. public String toString() {
  215. return getClass().getSimpleName() + "[" + getFile().getPath() + "]";
  216. }
  217. /**
  218. * @return returns true if the currently loaded configuration file is older
  219. * than the file on disk
  220. */
  221. public boolean isOutdated() {
  222. return snapshot.isModified(getFile());
  223. }
  224. }