2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
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
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
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.
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
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.
48
using NGit.Storage.File;
52
namespace NGit.Storage.File
54
/// <summary>The configuration file that is stored in the file of the file system.</summary>
55
/// <remarks>The configuration file that is stored in the file of the file system.</remarks>
56
public class FileBasedConfig : StoredConfig
58
private readonly FilePath configFile;
60
private readonly FS fs;
62
private volatile FileSnapshot snapshot;
64
private volatile ObjectId hash;
66
/// <summary>Create a configuration with no default fallback.</summary>
67
/// <remarks>Create a configuration with no default fallback.</remarks>
68
/// <param name="cfgLocation">the location of the configuration file on the file system
71
/// the file system abstraction which will be necessary to perform
72
/// certain file system operations.
74
public FileBasedConfig(FilePath cfgLocation, FS fs) : this(null, cfgLocation, fs)
78
/// <summary>The constructor</summary>
79
/// <param name="base">the base configuration file</param>
80
/// <param name="cfgLocation">the location of the configuration file on the file system
83
/// the file system abstraction which will be necessary to perform
84
/// certain file system operations.
86
public FileBasedConfig(Config @base, FilePath cfgLocation, FS fs) : base(@base)
88
configFile = cfgLocation;
90
this.snapshot = FileSnapshot.DIRTY;
91
this.hash = ObjectId.ZeroId;
94
protected internal override bool NotifyUponTransientChanges()
96
// we will notify listeners upon save()
100
/// <returns>location of the configuration file on disk</returns>
101
public FilePath GetFile()
106
/// <summary>Load the configuration as a Git text style configuration file.</summary>
108
/// Load the configuration as a Git text style configuration file.
110
/// If the file does not exist, this configuration is cleared, and thus
111
/// behaves the same as though the file exists, but is empty.
113
/// <exception cref="System.IO.IOException">the file could not be read (but does exist).
115
/// <exception cref="NGit.Errors.ConfigInvalidException">the file is not a properly formatted configuration file.
117
public override void Load()
119
FileSnapshot oldSnapshot = snapshot;
120
FileSnapshot newSnapshot = FileSnapshot.Save(GetFile());
123
byte[] @in = IOUtil.ReadFully(GetFile());
124
ObjectId newHash = Hash(@in);
125
if (hash.Equals(newHash))
127
if (oldSnapshot.Equals(newSnapshot))
129
oldSnapshot.SetClean(newSnapshot);
133
snapshot = newSnapshot;
138
FromText(RawParseUtils.Decode(@in));
139
snapshot = newSnapshot;
143
catch (FileNotFoundException)
146
snapshot = newSnapshot;
148
catch (IOException e)
150
IOException e2 = new IOException(MessageFormat.Format(JGitText.Get().cannotReadFile
152
Sharpen.Extensions.InitCause(e2, e);
155
catch (ConfigInvalidException e)
157
throw new ConfigInvalidException(MessageFormat.Format(JGitText.Get().cannotReadFile
162
/// <summary>Save the configuration as a Git text style configuration file.</summary>
164
/// Save the configuration as a Git text style configuration file.
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.
172
/// <exception cref="System.IO.IOException">the file could not be written.</exception>
173
public override void Save()
175
byte[] @out = Constants.Encode(ToText());
176
LockFile lf = new LockFile(GetFile(), fs);
179
throw new LockFailedException(GetFile());
183
lf.SetNeedSnapshot(true);
187
throw new IOException(MessageFormat.Format(JGitText.Get().cannotCommitWriteTo, GetFile
195
snapshot = lf.GetCommitSnapshot();
197
// notify the listeners
198
FireConfigChangedEvent();
201
protected internal override void Clear()
203
hash = Hash(new byte[0]);
207
private static ObjectId Hash(byte[] rawText)
209
return ObjectId.FromRaw(Constants.NewMessageDigest().Digest(rawText));
212
public override string ToString()
214
return GetType().Name + "[" + GetFile().GetPath() + "]";
218
/// returns true if the currently loaded configuration file is older
219
/// than the file on disk
221
public virtual bool IsOutdated()
223
return snapshot.IsModified(GetFile());