From 8e03738f4c54a60d30fe0acc8e0163dff3bf4744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Mon, 29 Jul 2013 13:57:43 +0200 Subject: [PATCH] More robust mtime precision checks --- attic/helpers.py | 2 +- attic/testsuite/__init__.py | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/attic/helpers.py b/attic/helpers.py index b086c6742..23358f5ad 100644 --- a/attic/helpers.py +++ b/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): diff --git a/attic/testsuite/__init__.py b/attic/testsuite/__init__.py index b0a98b695..bced9dbcf 100644 --- a/attic/testsuite/__init__.py +++ b/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)