[PATCH 01 of 11 v2] posix, windows: introduce cachestat

Idan Kamara idankk86 at gmail.com
Mon Jul 25 12:09:08 UTC 2011


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1311595382 -10800
# Node ID 43e4a9f7c3176922b7c16c83565261ce7d7fc07d
# Parent  545e00279670423696ed64af8aa9b0c882b44f09
posix, windows: introduce cachestat

This class contains a stat result, and possibly other file info to reliably
determine between two points in time whether a file has changed.

Uniquely identifying a file gives us that reliability because we either
atomic rename or append. So one of two will happen: the file 'id' will change,
or the size of the file will change.

posix implements it simply by calling os.stat() and checking if the result
has st_ino.

For now on Windows we always assume the path is uncacheable. This can be
improved on NTFS due to file IDs: http://msdn.microsoft.com/en-us/library/aa363788(v=vs.85).aspx

So we need to find out if a file path is on an NTFS drive, for that we have:

- GetVolumeInformation, which unfortunately only works with a root path (but is available on XP)
- GetVolumeInformationByHandleW, works on a full file path but requires Vista or higher

diff -r 545e00279670 -r 43e4a9f7c317 mercurial/posix.py
--- a/mercurial/posix.py	Sat Jul 23 06:09:14 2011 +0200
+++ b/mercurial/posix.py	Mon Jul 25 15:03:02 2011 +0300
@@ -348,3 +348,19 @@
     child process under Windows, unneeded on other systems.
     """
     pass
+
+class cachestat(object):
+    def __init__(self, path):
+        self.stat = os.stat(path)
+
+    def cacheable(self):
+        return bool(self.stat.st_ino)
+
+    def __eq__(self, other):
+        try:
+            return self.stat == other.stat
+        except AttributeError:
+            return False
+
+    def __ne__(self, other):
+        return not self == other
diff -r 545e00279670 -r 43e4a9f7c317 mercurial/windows.py
--- a/mercurial/windows.py	Sat Jul 23 06:09:14 2011 +0200
+++ b/mercurial/windows.py	Mon Jul 25 15:03:02 2011 +0300
@@ -283,4 +283,11 @@
 
 from win32 import *
 
+class cachestat(object):
+    def __init__(self, path):
+        pass
+
+    def cacheable(self):
+        return False
+
 expandglobs = True
diff -r 545e00279670 -r 43e4a9f7c317 tests/hghave
--- a/tests/hghave	Sat Jul 23 06:09:14 2011 +0200
+++ b/tests/hghave	Mon Jul 25 15:03:02 2011 +0300
@@ -101,6 +101,16 @@
 def has_fifo():
     return hasattr(os, "mkfifo")
 
+def has_cacheable_fs():
+    from mercurial import util
+
+    fd, path = tempfile.mkstemp(prefix=tempprefix)
+    os.close(fd)
+    try:
+        return util.cachestat(path).cacheable()
+    finally:
+        os.remove(path)
+
 def has_lsprof():
     try:
         import _lsprof
@@ -200,6 +210,7 @@
     "baz": (has_baz, "GNU Arch baz client"),
     "bzr": (has_bzr, "Canonical's Bazaar client"),
     "bzr114": (has_bzr114, "Canonical's Bazaar client >= 1.14"),
+    "cacheable": (has_cacheable_fs, "cacheable filesystem"),
     "cvs": (has_cvs, "cvs client/server"),
     "darcs": (has_darcs, "darcs client"),
     "docutils": (has_docutils, "Docutils text processing library"),



More information about the Mercurial-devel mailing list