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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.File;
  51. import java.io.FileNotFoundException;
  52. import java.io.IOException;
  53. import java.text.MessageFormat;
  54. import org.eclipse.jgit.JGitText;
  55. import org.eclipse.jgit.errors.ConfigInvalidException;
  56. import org.eclipse.jgit.lib.Config;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.StoredConfig;
  60. import org.eclipse.jgit.util.FS;
  61. import org.eclipse.jgit.util.IO;
  62. import org.eclipse.jgit.util.RawParseUtils;
  63. /**
  64. * The configuration file that is stored in the file of the file system.
  65. */
  66. public class FileBasedConfig extends StoredConfig {
  67. private final File configFile;
  68. private final FS fs;
  69. private volatile FileSnapshot snapshot;
  70. private volatile ObjectId hash;
  71. /**
  72. * Create a configuration with no default fallback.
  73. *
  74. * @param cfgLocation
  75. * the location of the configuration file on the file system
  76. * @param fs
  77. * the file system abstraction which will be necessary to perform
  78. * certain file system operations.
  79. */
  80. public FileBasedConfig(File cfgLocation, FS fs) {
  81. this(null, cfgLocation, fs);
  82. }
  83. /**
  84. * The constructor
  85. *
  86. * @param base
  87. * the base configuration file
  88. * @param cfgLocation
  89. * the location of the configuration file on the file system
  90. * @param fs
  91. * the file system abstraction which will be necessary to perform
  92. * certain file system operations.
  93. */
  94. public FileBasedConfig(Config base, File cfgLocation, FS fs) {
  95. super(base);
  96. configFile = cfgLocation;
  97. this.fs = fs;
  98. this.snapshot = FileSnapshot.DIRTY;
  99. this.hash = ObjectId.zeroId();
  100. }
  101. @Override
  102. protected boolean notifyUponTransientChanges() {
  103. // we will notify listeners upon save()
  104. return false;
  105. }
  106. /** @return location of the configuration file on disk */
  107. public final File getFile() {
  108. return configFile;
  109. }
  110. /**
  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. * @throws IOException
  117. * the file could not be read (but does exist).
  118. * @throws ConfigInvalidException
  119. * the file is not a properly formatted configuration file.
  120. */
  121. @Override
  122. public void load() throws IOException, ConfigInvalidException {
  123. final FileSnapshot oldSnapshot = snapshot;
  124. final FileSnapshot newSnapshot = FileSnapshot.save(getFile());
  125. try {
  126. final byte[] in = IO.readFully(getFile());
  127. final ObjectId newHash = hash(in);
  128. if (hash.equals(newHash)) {
  129. if (oldSnapshot.equals(newSnapshot))
  130. oldSnapshot.setClean(newSnapshot);
  131. else
  132. snapshot = newSnapshot;
  133. } else {
  134. fromText(RawParseUtils.decode(in));
  135. snapshot = newSnapshot;
  136. hash = newHash;
  137. }
  138. } catch (FileNotFoundException noFile) {
  139. clear();
  140. snapshot = newSnapshot;
  141. } catch (IOException e) {
  142. final IOException e2 = new IOException(MessageFormat.format(JGitText.get().cannotReadFile, getFile()));
  143. e2.initCause(e);
  144. throw e2;
  145. } catch (ConfigInvalidException e) {
  146. throw new ConfigInvalidException(MessageFormat.format(JGitText.get().cannotReadFile, getFile()), e);
  147. }
  148. }
  149. /**
  150. * Save the configuration as a Git text style configuration file.
  151. * <p>
  152. * <b>Warning:</b> Although this method uses the traditional Git file
  153. * locking approach to protect against concurrent writes of the
  154. * configuration file, it does not ensure that the file has not been
  155. * modified since the last read, which means updates performed by other
  156. * objects accessing the same backing file may be lost.
  157. *
  158. * @throws IOException
  159. * the file could not be written.
  160. */
  161. public void save() throws IOException {
  162. final byte[] out = Constants.encode(toText());
  163. final LockFile lf = new LockFile(getFile(), fs);
  164. if (!lf.lock())
  165. throw new IOException(MessageFormat.format(JGitText.get().cannotLockFile, getFile()));
  166. try {
  167. lf.setNeedSnapshot(true);
  168. lf.write(out);
  169. if (!lf.commit())
  170. throw new IOException(MessageFormat.format(JGitText.get().cannotCommitWriteTo, getFile()));
  171. } finally {
  172. lf.unlock();
  173. }
  174. snapshot = lf.getCommitSnapshot();
  175. hash = hash(out);
  176. // notify the listeners
  177. fireConfigChangedEvent();
  178. }
  179. @Override
  180. public void clear() {
  181. hash = hash(new byte[0]);
  182. super.clear();
  183. }
  184. private static ObjectId hash(final byte[] rawText) {
  185. return ObjectId.fromRaw(Constants.newMessageDigest().digest(rawText));
  186. }
  187. @Override
  188. public String toString() {
  189. return getClass().getSimpleName() + "[" + getFile().getPath() + "]";
  190. }
  191. /**
  192. * @return returns true if the currently loaded configuration file is older
  193. * than the file on disk
  194. */
  195. public boolean isOutdated() {
  196. return snapshot.isModified(getFile());
  197. }
  198. }