瀏覽代碼

More robust mtime precision checks

Jonas Borgström 12 年之前
父節點
當前提交
8e03738f4c
共有 2 個文件被更改,包括 20 次插入8 次删除
  1. 1 1
      attic/helpers.py
  2. 19 7
      attic/testsuite/__init__.py

+ 1 - 1
attic/helpers.py

@@ -385,7 +385,7 @@ def daemonize():
 if sys.version < '3.3':
     # st_mtime_ns attribute only available in 3.3+
     def st_mtime_ns(st):
-        return int(st.st_mtime * 10**9)
+        return int(st.st_mtime * 1e9)
 
     # unhexlify in < 3.3 incorrectly only accepts bytes input
     def unhexlify(data):

+ 19 - 7
attic/testsuite/__init__.py

@@ -1,10 +1,21 @@
 import filecmp
 import os
+import posix
 import sys
+import sysconfig
 import time
 import unittest
+from attic.helpers import st_mtime_ns
 from attic.xattr import get_all
 
+# The mtime get/set precison varies on different OS and Python versions
+if 'HAVE_FUTIMENS' in posix._have_functions:
+    st_mtime_ns_round = 0
+elif 'HAVE_UTIMES' in sysconfig.get_config_vars():
+    st_mtime_ns_round = -3
+else:
+    st_mtime_ns_round = -9
+
 
 has_mtime_ns = sys.version >= '3.3'
 utime_supports_fd = os.utime in getattr(os, 'supports_fd', {})
@@ -41,15 +52,16 @@ class AtticTestCase(unittest.TestCase):
             if not fuse or not os.path.isdir(path1):
                 # dir nlink is always 1 on our fuse fileystem
                 attrs.append('st_nlink')
-            if not os.path.islink(path1) or utime_supports_fd:
-                # Fuse api is does not support ns precision
-                attrs.append('st_mtime_ns' if has_mtime_ns and not fuse else 'st_mtime')
             d1 = [filename] + [getattr(s1, a) for a in attrs]
             d2 = [filename] + [getattr(s2, a) for a in attrs]
-            # 'st_mtime precision is limited'
-            if attrs[-1] == 'st_mtime':
-                d1[-1] = round(d1[-1], 2)
-                d2[-1] = round(d2[-1], 2)
+            if not os.path.islink(path1) or utime_supports_fd:
+                # llfuse does not provide ns precision for now
+                if fuse:
+                    d1.append(round(st_mtime_ns(s1), -4))
+                    d2.append(round(st_mtime_ns(s2), -4))
+                else:
+                    d1.append(round(st_mtime_ns(s1), st_mtime_ns_round))
+                    d2.append(round(st_mtime_ns(s2), st_mtime_ns_round))
             d1.append(self._get_xattrs(path1))
             d2.append(self._get_xattrs(path2))
             self.assert_equal(d1, d2)