Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jgit-dev] isOutdated() and lastModified()

11 dec 2010 kl. 17:02 skrev Dmitry Pavlenko:

> Hello all,
> 
> I seems that FileBasedConfig (and loose-refs-related classes) relies on 
> java.io.File.lastModified() call (at least in isOutdated() method).
> 
> For my Sun Java 1.6.0.22 and OpenJDK 1.6.0 (Debian squeeze/sid)
> java.io.File.lastModified() can be the same after changes in the file.
> 
> I don't know is it expected, is it JDK bug, or JGit problem. Do you?

This is expected. If the changes are quick enough, the clock will not step forward in
between. Furthermore, Java on Linux and MacOS X has a bug that only expose only one second
resolution for lastModified, even if the file system has a lower granularity. Traditionally
Unix file system have had a one second resolution. On Windows NTFS the resolution is 1/100th
of a seconds and for FAT it is 2 seconds. Not sure what MacOS has, but through Java at least, 
only seconds are visible.

> Yes, I've read at the main page that JGit promises to work on Java 5, but 
> anyway I think this will be interesting for JGit developers.

There is a workaround for this in JGit and the original C Git, aka "racy git", which means
that we sometimes need to read the file to check if it has changed.

> 
> The following piece of code outputs

> lm1 = 1292082965000
> lm2 = 1292082965000
> They are equal!

Your code is likely to do its work in much less than a millisecond so you'd get the same
timestamp even if lastModified worked as it should.

-- robin


> If I set a breakpoint somewhere, and run line-by-line,  lm1 != lm2.
> 
> import java.io.*;
> public class Last {
> 	public  static void main(String[] args) {
> 	try {
> 		File someFile = new File("someFile");
> 
> 		FileWriter writer;
> 		
> 		writer = new FileWriter(someFile);
> 		try {
> 			writer.write("some contents");
> 		} finally {
> 			writer.close();
> 		}
> 
> 		final long lm1 = someFile.lastModified();
> 
> 		writer = new FileWriter(someFile);
> 		try {
> 			writer.write("some another contents");
> 		} finally {
> 			writer.close();
> 		}
> 
> 		final long lm2 = someFile.lastModified();
> 
> 		System.out.println("lm1 = " + lm1);
> 		System.out.println("lm2 = " + lm2);
> 
> 		if (lm1 == lm2) {
> 			System.out.println("They are equal!");
> 		}
> 	} catch (IOException e) {
> 	}
> 	}
> }
> _______________________________________________
> jgit-dev mailing list
> jgit-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/jgit-dev



Back to the top