~ubuntu-branches/ubuntu/saucy/monodevelop/saucy-proposed

« back to all changes in this revision

Viewing changes to external/ngit/NGit/NGit.Storage.File/FileBasedConfig.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (19.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20120527180820-fydl21qnbnfr8w2t
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit'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
using System.IO;
 
45
using NGit;
 
46
using NGit.Errors;
 
47
using NGit.Internal;
 
48
using NGit.Storage.File;
 
49
using NGit.Util;
 
50
using Sharpen;
 
51
 
 
52
namespace NGit.Storage.File
 
53
{
 
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
 
57
        {
 
58
                private readonly FilePath configFile;
 
59
 
 
60
                private readonly FS fs;
 
61
 
 
62
                private volatile FileSnapshot snapshot;
 
63
 
 
64
                private volatile ObjectId hash;
 
65
 
 
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
 
69
                ///     </param>
 
70
                /// <param name="fs">
 
71
                /// the file system abstraction which will be necessary to perform
 
72
                /// certain file system operations.
 
73
                /// </param>
 
74
                public FileBasedConfig(FilePath cfgLocation, FS fs) : this(null, cfgLocation, fs)
 
75
                {
 
76
                }
 
77
 
 
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
 
81
                ///     </param>
 
82
                /// <param name="fs">
 
83
                /// the file system abstraction which will be necessary to perform
 
84
                /// certain file system operations.
 
85
                /// </param>
 
86
                public FileBasedConfig(Config @base, FilePath cfgLocation, FS fs) : base(@base)
 
87
                {
 
88
                        configFile = cfgLocation;
 
89
                        this.fs = fs;
 
90
                        this.snapshot = FileSnapshot.DIRTY;
 
91
                        this.hash = ObjectId.ZeroId;
 
92
                }
 
93
 
 
94
                protected internal override bool NotifyUponTransientChanges()
 
95
                {
 
96
                        // we will notify listeners upon save()
 
97
                        return false;
 
98
                }
 
99
 
 
100
                /// <returns>location of the configuration file on disk</returns>
 
101
                public FilePath GetFile()
 
102
                {
 
103
                        return configFile;
 
104
                }
 
105
 
 
106
                /// <summary>Load the configuration as a Git text style configuration file.</summary>
 
107
                /// <remarks>
 
108
                /// Load the configuration as a Git text style configuration file.
 
109
                /// <p>
 
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.
 
112
                /// </remarks>
 
113
                /// <exception cref="System.IO.IOException">the file could not be read (but does exist).
 
114
                ///     </exception>
 
115
                /// <exception cref="NGit.Errors.ConfigInvalidException">the file is not a properly formatted configuration file.
 
116
                ///     </exception>
 
117
                public override void Load()
 
118
                {
 
119
                        FileSnapshot oldSnapshot = snapshot;
 
120
                        FileSnapshot newSnapshot = FileSnapshot.Save(GetFile());
 
121
                        try
 
122
                        {
 
123
                                byte[] @in = IOUtil.ReadFully(GetFile());
 
124
                                ObjectId newHash = Hash(@in);
 
125
                                if (hash.Equals(newHash))
 
126
                                {
 
127
                                        if (oldSnapshot.Equals(newSnapshot))
 
128
                                        {
 
129
                                                oldSnapshot.SetClean(newSnapshot);
 
130
                                        }
 
131
                                        else
 
132
                                        {
 
133
                                                snapshot = newSnapshot;
 
134
                                        }
 
135
                                }
 
136
                                else
 
137
                                {
 
138
                                        FromText(RawParseUtils.Decode(@in));
 
139
                                        snapshot = newSnapshot;
 
140
                                        hash = newHash;
 
141
                                }
 
142
                        }
 
143
                        catch (FileNotFoundException)
 
144
                        {
 
145
                                Clear();
 
146
                                snapshot = newSnapshot;
 
147
                        }
 
148
                        catch (IOException e)
 
149
                        {
 
150
                                IOException e2 = new IOException(MessageFormat.Format(JGitText.Get().cannotReadFile
 
151
                                        , GetFile()));
 
152
                                Sharpen.Extensions.InitCause(e2, e);
 
153
                                throw e2;
 
154
                        }
 
155
                        catch (ConfigInvalidException e)
 
156
                        {
 
157
                                throw new ConfigInvalidException(MessageFormat.Format(JGitText.Get().cannotReadFile
 
158
                                        , GetFile()), e);
 
159
                        }
 
160
                }
 
161
 
 
162
                /// <summary>Save the configuration as a Git text style configuration file.</summary>
 
163
                /// <remarks>
 
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
                /// </remarks>
 
172
                /// <exception cref="System.IO.IOException">the file could not be written.</exception>
 
173
                public override void Save()
 
174
                {
 
175
                        byte[] @out = Constants.Encode(ToText());
 
176
                        LockFile lf = new LockFile(GetFile(), fs);
 
177
                        if (!lf.Lock())
 
178
                        {
 
179
                                throw new LockFailedException(GetFile());
 
180
                        }
 
181
                        try
 
182
                        {
 
183
                                lf.SetNeedSnapshot(true);
 
184
                                lf.Write(@out);
 
185
                                if (!lf.Commit())
 
186
                                {
 
187
                                        throw new IOException(MessageFormat.Format(JGitText.Get().cannotCommitWriteTo, GetFile
 
188
                                                ()));
 
189
                                }
 
190
                        }
 
191
                        finally
 
192
                        {
 
193
                                lf.Unlock();
 
194
                        }
 
195
                        snapshot = lf.GetCommitSnapshot();
 
196
                        hash = Hash(@out);
 
197
                        // notify the listeners
 
198
                        FireConfigChangedEvent();
 
199
                }
 
200
 
 
201
                protected internal override void Clear()
 
202
                {
 
203
                        hash = Hash(new byte[0]);
 
204
                        base.Clear();
 
205
                }
 
206
 
 
207
                private static ObjectId Hash(byte[] rawText)
 
208
                {
 
209
                        return ObjectId.FromRaw(Constants.NewMessageDigest().Digest(rawText));
 
210
                }
 
211
 
 
212
                public override string ToString()
 
213
                {
 
214
                        return GetType().Name + "[" + GetFile().GetPath() + "]";
 
215
                }
 
216
 
 
217
                /// <returns>
 
218
                /// returns true if the currently loaded configuration file is older
 
219
                /// than the file on disk
 
220
                /// </returns>
 
221
                public virtual bool IsOutdated()
 
222
                {
 
223
                        return snapshot.IsModified(GetFile());
 
224
                }
 
225
        }
 
226
}