/**
* Wait until the lock file information differs from the old file.
* <p>
- * This method tests both the length and the last modification date. If both
- * are the same, this method sleeps until it can force the new lock file's
- * modification date to be later than the target file.
+ * This method tests the last modification date. If both are the same, this
+ * method sleeps until it can force the new lock file's modification date to
+ * be later than the target file.
*
* @throws InterruptedException
* the thread was interrupted before the last modified date of
* the target file.
*/
public void waitForStatChange() throws InterruptedException {
- if (ref.length() == lck.length()) {
- long otime = ref.lastModified();
- long ntime = lck.lastModified();
- while (otime == ntime) {
- Thread.sleep(25 /* milliseconds */);
- lck.setLastModified(System.currentTimeMillis());
- ntime = lck.lastModified();
- }
+ FileSnapshot o = FileSnapshot.save(ref);
+ FileSnapshot n = FileSnapshot.save(lck);
+ while (o.equals(n)) {
+ Thread.sleep(25 /* milliseconds */);
+ lck.setLastModified(System.currentTimeMillis());
+ n = FileSnapshot.save(lck);
}
}
throw new IOException(MessageFormat.format(
JGitText.get().cannotLockFile, packedRefsFile));
try {
- PackedRefList cur = readPackedRefs(0, 0);
+ PackedRefList cur = readPackedRefs();
int idx = cur.find(name);
if (0 <= idx)
commitPackedRefs(lck, cur.remove(idx), packed);
}
private PackedRefList getPackedRefs() throws IOException {
- long size = packedRefsFile.length();
- long mtime = size != 0 ? packedRefsFile.lastModified() : 0;
-
final PackedRefList curList = packedRefs.get();
- if (size == curList.lastSize && mtime == curList.lastModified)
+ if (!curList.snapshot.isModified(packedRefsFile))
return curList;
- final PackedRefList newList = readPackedRefs(size, mtime);
+ final PackedRefList newList = readPackedRefs();
if (packedRefs.compareAndSet(curList, newList))
modCnt.incrementAndGet();
return newList;
}
- private PackedRefList readPackedRefs(long size, long mtime)
+ private PackedRefList readPackedRefs()
throws IOException {
+ final FileSnapshot snapshot = FileSnapshot.save(packedRefsFile);
final BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
return PackedRefList.NO_PACKED_REFS;
}
try {
- return new PackedRefList(parsePackedRefs(br), size, mtime);
+ return new PackedRefList(parsePackedRefs(br), snapshot);
} finally {
br.close();
}
protected void writeFile(String name, byte[] content)
throws IOException {
lck.setFSync(true);
- lck.setNeedStatInformation(true);
+ lck.setNeedSnapshot(true);
try {
lck.write(content);
} catch (IOException ioe) {
if (!lck.commit())
throw new ObjectWritingException(MessageFormat.format(JGitText.get().unableToWrite, name));
- packedRefs.compareAndSet(oldPackedList, new PackedRefList(refs,
- content.length, lck.getCommitLastModified()));
+ packedRefs.compareAndSet(oldPackedList, new PackedRefList(
+ refs, lck.getCommitSnapshot()));
}
}.writePackedRefs();
}
}
private static class PackedRefList extends RefList<Ref> {
- static final PackedRefList NO_PACKED_REFS = new PackedRefList(RefList
- .emptyList(), 0, 0);
-
- /** Last length of the packed-refs file when we read it. */
- final long lastSize;
+ static final PackedRefList NO_PACKED_REFS = new PackedRefList(
+ RefList.emptyList(), FileSnapshot.MISSING_FILE);
- /** Last modified time of the packed-refs file when we read it. */
- final long lastModified;
+ final FileSnapshot snapshot;
- PackedRefList(RefList<Ref> src, long size, long mtime) {
+ PackedRefList(RefList<Ref> src, FileSnapshot s) {
super(src);
- lastSize = size;
- lastModified = mtime;
+ snapshot = s;
}
}