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.
46
namespace NGit.Storage.File
48
/// <summary>Caches when a file was last read, making it possible to detect future edits.
51
/// Caches when a file was last read, making it possible to detect future edits.
53
/// This object tracks the last modified time of a file. Later during an
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.
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>
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.
69
public class FileSnapshot
71
/// <summary>A FileSnapshot that is considered to always be modified.</summary>
73
/// A FileSnapshot that is considered to always be modified.
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.
81
public static readonly NGit.Storage.File.FileSnapshot DIRTY = new NGit.Storage.File.FileSnapshot
84
private sealed class _FileSnapshot_81 : NGit.Storage.File.FileSnapshot
86
public _FileSnapshot_81(long baseArg1, long baseArg2) : base(baseArg1, baseArg2)
90
public override bool IsModified(FilePath path)
96
/// <summary>A FileSnapshot that is clean if the file does not exist.</summary>
98
/// A FileSnapshot that is clean if the file does not exist.
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.
106
public static readonly NGit.Storage.File.FileSnapshot MISSING_FILE = new _FileSnapshot_81
109
/// <summary>Record a snapshot for a specific file path.</summary>
111
/// Record a snapshot for a specific file path.
113
/// This method should be invoked before the file is accessed.
115
/// <param name="path">
116
/// the path to later remember. The path's current status
117
/// information is saved.
119
/// <returns>the snapshot.</returns>
120
public static NGit.Storage.File.FileSnapshot Save(FilePath path)
122
long read = Runtime.CurrentTimeMillis();
123
long modified = path.LastModified();
124
return new NGit.Storage.File.FileSnapshot(read, modified);
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;
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;
137
/// <see cref="lastRead">lastRead</see>
138
/// is far later than
139
/// <see cref="lastModified">lastModified</see>
142
private bool cannotBeRacilyClean;
144
private FileSnapshot(long read, long modified)
146
this.lastRead = read;
147
this.lastModified = modified;
148
this.cannotBeRacilyClean = NotRacyClean(read);
151
/// <returns>time of last snapshot update</returns>
152
public virtual long LastModified()
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)
163
return IsModified(path.LastModified());
166
/// <summary>Update this snapshot when the content hasn't changed.</summary>
168
/// Update this snapshot when the content hasn't changed.
170
/// If the caller gets true from
171
/// <see cref="IsModified(Sharpen.FilePath)">IsModified(Sharpen.FilePath)</see>
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>
178
/// <see cref="IsModified(Sharpen.FilePath)">IsModified(Sharpen.FilePath)</see>
179
/// return false. The logic goes something like
182
/// if (snapshot.isModified(path)) {
183
/// FileSnapshot other = FileSnapshot.save(path);
184
/// Content newContent = ...;
185
/// if (oldContent.equals(newContent) && snapshot.equals(other))
186
/// snapshot.setClean(other);
190
/// <param name="other">the other snapshot.</param>
191
public virtual void SetClean(NGit.Storage.File.FileSnapshot other)
193
long now = other.lastRead;
194
if (NotRacyClean(now))
196
cannotBeRacilyClean = true;
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)
207
return lastModified == other.lastModified;
210
public override bool Equals(object other)
212
if (other is NGit.Storage.File.FileSnapshot)
214
return Equals((NGit.Storage.File.FileSnapshot)other);
219
public override int GetHashCode()
221
// This is pretty pointless, but override hashCode to ensure that
222
// x.hashCode() == y.hashCode() when x.equals(y) is true.
224
return (int)lastModified;
227
private bool NotRacyClean(long read)
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.
233
return read - lastModified > 2500;
236
private bool IsModified(long currLastModified)
238
// Any difference indicates the path was modified.
240
if (lastModified != currLastModified)
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.
248
if (cannotBeRacilyClean)
252
if (NotRacyClean(lastRead))
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.
260
// Our lastRead flag may be old, refresh and retry
261
lastRead = Runtime.CurrentTimeMillis();
262
if (NotRacyClean(lastRead))
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.