~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Storage.File/FileSnapshot.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

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 Sharpen;
 
45
 
 
46
namespace NGit.Storage.File
 
47
{
 
48
        /// <summary>Caches when a file was last read, making it possible to detect future edits.
 
49
        ///     </summary>
 
50
        /// <remarks>
 
51
        /// Caches when a file was last read, making it possible to detect future edits.
 
52
        /// <p>
 
53
        /// This object tracks the last modified time of a file. Later during an
 
54
        /// invocation of
 
55
        /// <see cref="IsModified(Sharpen.FilePath)">IsModified(Sharpen.FilePath)</see>
 
56
        /// the object will return true if the
 
57
        /// file may have been modified and should be re-read from disk.
 
58
        /// <p>
 
59
        /// A snapshot does not "live update" when the underlying filesystem changes.
 
60
        /// Callers must poll for updates by periodically invoking
 
61
        /// <see cref="IsModified(Sharpen.FilePath)">IsModified(Sharpen.FilePath)</see>
 
62
        /// .
 
63
        /// <p>
 
64
        /// To work around the "racy git" problem (where a file may be modified multiple
 
65
        /// times within the granularity of the filesystem modification clock) this class
 
66
        /// may return true from isModified(File) if the last modification time of the
 
67
        /// file is less than 3 seconds ago.
 
68
        /// </remarks>
 
69
        public class FileSnapshot
 
70
        {
 
71
                /// <summary>A FileSnapshot that is considered to always be modified.</summary>
 
72
                /// <remarks>
 
73
                /// A FileSnapshot that is considered to always be modified.
 
74
                /// <p>
 
75
                /// This instance is useful for application code that wants to lazily read a
 
76
                /// file, but only after
 
77
                /// <see cref="IsModified(Sharpen.FilePath)">IsModified(Sharpen.FilePath)</see>
 
78
                /// gets invoked. The returned
 
79
                /// snapshot contains only invalid status information.
 
80
                /// </remarks>
 
81
                public static readonly NGit.Storage.File.FileSnapshot DIRTY = new NGit.Storage.File.FileSnapshot
 
82
                        (-1, -1);
 
83
 
 
84
                private sealed class _FileSnapshot_81 : NGit.Storage.File.FileSnapshot
 
85
                {
 
86
                        public _FileSnapshot_81(long baseArg1, long baseArg2) : base(baseArg1, baseArg2)
 
87
                        {
 
88
                        }
 
89
 
 
90
                        public override bool IsModified(FilePath path)
 
91
                        {
 
92
                                return path.Exists();
 
93
                        }
 
94
                }
 
95
 
 
96
                /// <summary>A FileSnapshot that is clean if the file does not exist.</summary>
 
97
                /// <remarks>
 
98
                /// A FileSnapshot that is clean if the file does not exist.
 
99
                /// <p>
 
100
                /// This instance is useful if the application wants to consider a missing
 
101
                /// file to be clean.
 
102
                /// <see cref="IsModified(Sharpen.FilePath)">IsModified(Sharpen.FilePath)</see>
 
103
                /// will return false if the file
 
104
                /// path does not exist.
 
105
                /// </remarks>
 
106
                public static readonly NGit.Storage.File.FileSnapshot MISSING_FILE = new _FileSnapshot_81
 
107
                        (0, 0);
 
108
 
 
109
                /// <summary>Record a snapshot for a specific file path.</summary>
 
110
                /// <remarks>
 
111
                /// Record a snapshot for a specific file path.
 
112
                /// <p>
 
113
                /// This method should be invoked before the file is accessed.
 
114
                /// </remarks>
 
115
                /// <param name="path">
 
116
                /// the path to later remember. The path's current status
 
117
                /// information is saved.
 
118
                /// </param>
 
119
                /// <returns>the snapshot.</returns>
 
120
                public static NGit.Storage.File.FileSnapshot Save(FilePath path)
 
121
                {
 
122
                        long read = Runtime.CurrentTimeMillis();
 
123
                        long modified = path.LastModified();
 
124
                        return new NGit.Storage.File.FileSnapshot(read, modified);
 
125
                }
 
126
 
 
127
                /// <summary>Last observed modification time of the path.</summary>
 
128
                /// <remarks>Last observed modification time of the path.</remarks>
 
129
                private readonly long lastModified;
 
130
 
 
131
                /// <summary>Last wall-clock time the path was read.</summary>
 
132
                /// <remarks>Last wall-clock time the path was read.</remarks>
 
133
                private long lastRead;
 
134
 
 
135
                /// <summary>
 
136
                /// True once
 
137
                /// <see cref="lastRead">lastRead</see>
 
138
                /// is far later than
 
139
                /// <see cref="lastModified">lastModified</see>
 
140
                /// .
 
141
                /// </summary>
 
142
                private bool cannotBeRacilyClean;
 
143
 
 
144
                private FileSnapshot(long read, long modified)
 
145
                {
 
146
                        this.lastRead = read;
 
147
                        this.lastModified = modified;
 
148
                        this.cannotBeRacilyClean = NotRacyClean(read);
 
149
                }
 
150
 
 
151
                /// <returns>time of last snapshot update</returns>
 
152
                public virtual long LastModified()
 
153
                {
 
154
                        return lastModified;
 
155
                }
 
156
 
 
157
                /// <summary>Check if the path may have been modified since the snapshot was saved.</summary>
 
158
                /// <remarks>Check if the path may have been modified since the snapshot was saved.</remarks>
 
159
                /// <param name="path">the path the snapshot describes.</param>
 
160
                /// <returns>true if the path needs to be read again.</returns>
 
161
                public virtual bool IsModified(FilePath path)
 
162
                {
 
163
                        return IsModified(path.LastModified());
 
164
                }
 
165
 
 
166
                /// <summary>Update this snapshot when the content hasn't changed.</summary>
 
167
                /// <remarks>
 
168
                /// Update this snapshot when the content hasn't changed.
 
169
                /// <p>
 
170
                /// If the caller gets true from
 
171
                /// <see cref="IsModified(Sharpen.FilePath)">IsModified(Sharpen.FilePath)</see>
 
172
                /// , re-reads the
 
173
                /// content, discovers the content is identical, and
 
174
                /// <see cref="Equals(FileSnapshot)">Equals(FileSnapshot)</see>
 
175
                /// is true, it can use
 
176
                /// <see cref="SetClean(FileSnapshot)">SetClean(FileSnapshot)</see>
 
177
                /// to make a future
 
178
                /// <see cref="IsModified(Sharpen.FilePath)">IsModified(Sharpen.FilePath)</see>
 
179
                /// return false. The logic goes something like
 
180
                /// this:
 
181
                /// <pre>
 
182
                /// if (snapshot.isModified(path)) {
 
183
                /// FileSnapshot other = FileSnapshot.save(path);
 
184
                /// Content newContent = ...;
 
185
                /// if (oldContent.equals(newContent) &amp;&amp; snapshot.equals(other))
 
186
                /// snapshot.setClean(other);
 
187
                /// }
 
188
                /// </pre>
 
189
                /// </remarks>
 
190
                /// <param name="other">the other snapshot.</param>
 
191
                public virtual void SetClean(NGit.Storage.File.FileSnapshot other)
 
192
                {
 
193
                        long now = other.lastRead;
 
194
                        if (NotRacyClean(now))
 
195
                        {
 
196
                                cannotBeRacilyClean = true;
 
197
                        }
 
198
                        lastRead = now;
 
199
                }
 
200
 
 
201
                /// <summary>Compare two snapshots to see if they cache the same information.</summary>
 
202
                /// <remarks>Compare two snapshots to see if they cache the same information.</remarks>
 
203
                /// <param name="other">the other snapshot.</param>
 
204
                /// <returns>true if the two snapshots share the same information.</returns>
 
205
                public virtual bool Equals(NGit.Storage.File.FileSnapshot other)
 
206
                {
 
207
                        return lastModified == other.lastModified;
 
208
                }
 
209
 
 
210
                public override bool Equals(object other)
 
211
                {
 
212
                        if (other is NGit.Storage.File.FileSnapshot)
 
213
                        {
 
214
                                return Equals((NGit.Storage.File.FileSnapshot)other);
 
215
                        }
 
216
                        return false;
 
217
                }
 
218
 
 
219
                public override int GetHashCode()
 
220
                {
 
221
                        // This is pretty pointless, but override hashCode to ensure that
 
222
                        // x.hashCode() == y.hashCode() when x.equals(y) is true.
 
223
                        //
 
224
                        return (int)lastModified;
 
225
                }
 
226
 
 
227
                private bool NotRacyClean(long read)
 
228
                {
 
229
                        // The last modified time granularity of FAT filesystems is 2 seconds.
 
230
                        // Using 2.5 seconds here provides a reasonably high assurance that
 
231
                        // a modification was not missed.
 
232
                        //
 
233
                        return read - lastModified > 2500;
 
234
                }
 
235
 
 
236
                private bool IsModified(long currLastModified)
 
237
                {
 
238
                        // Any difference indicates the path was modified.
 
239
                        //
 
240
                        if (lastModified != currLastModified)
 
241
                        {
 
242
                                return true;
 
243
                        }
 
244
                        // We have already determined the last read was far enough
 
245
                        // after the last modification that any new modifications
 
246
                        // are certain to change the last modified time.
 
247
                        //
 
248
                        if (cannotBeRacilyClean)
 
249
                        {
 
250
                                return false;
 
251
                        }
 
252
                        if (NotRacyClean(lastRead))
 
253
                        {
 
254
                                // Our last read should have marked cannotBeRacilyClean,
 
255
                                // but this thread may not have seen the change. The read
 
256
                                // of the volatile field lastRead should have fixed that.
 
257
                                //
 
258
                                return false;
 
259
                        }
 
260
                        // Our lastRead flag may be old, refresh and retry
 
261
                        lastRead = Runtime.CurrentTimeMillis();
 
262
                        if (NotRacyClean(lastRead))
 
263
                        {
 
264
                                return false;
 
265
                        }
 
266
                        // We last read this path too close to its last observed
 
267
                        // modification time. We may have missed a modification.
 
268
                        // Scan again, to ensure we still see the same state.
 
269
                        //
 
270
                        return true;
 
271
                }
 
272
        }
 
273
}