1 """Enhanced stat operations for bup."""
3 from bup import _helpers
7 _have_bup_utime_ns = _helpers.bup_utime_ns
8 except AttributeError, e:
9 _have_bup_utime_ns = False
12 _have_bup_lutime_ns = _helpers.bup_lutime_ns
13 except AttributeError, e:
14 _have_bup_lutime_ns = False
17 def timespec_to_nsecs((ts_s, ts_ns)):
18 # c.f. _helpers.c: timespec_vals_to_py_ns()
19 if ts_ns < 0 or ts_ns > 999999999:
20 raise Exception('invalid timespec nsec value')
21 return ts_s * 10**9 + ts_ns
24 def nsecs_to_timespec(ns):
25 """Return (s, ns) where ns is always non-negative
26 and t = s + ns / 10e8""" # metadata record rep (and libc rep)
28 return (ns / 10**9, ns % 10**9)
31 def fstime_floor_secs(ns):
32 """Return largest integer not greater than ns / 10e8."""
33 return int(ns) / 10**9;
36 def fstime_to_timespec(ns):
37 return nsecs_to_timespec(ns)
40 if _have_bup_utime_ns:
41 def utime(path, times):
42 """Times must be provided as (atime_ns, mtime_ns)."""
43 atime = nsecs_to_timespec(times[0])
44 mtime = nsecs_to_timespec(times[1])
45 _helpers.bup_utime_ns(path, (atime, mtime))
47 def utime(path, times):
48 """Times must be provided as (atime_ns, mtime_ns)."""
49 atime = fstime_floor_secs(times[0])
50 mtime = fstime_floor_secs(times[1])
51 os.utime(path, (atime, mtime))
54 if _have_bup_lutime_ns:
55 def lutime(path, times):
56 """Times must be provided as (atime_ns, mtime_ns)."""
57 atime = nsecs_to_timespec(times[0])
58 mtime = nsecs_to_timespec(times[1])
59 _helpers.bup_lutime_ns(path, (atime, mtime))
66 def from_xstat_rep(st):
67 result = stat_result()
79 result.st_atime = timespec_to_nsecs(result.st_atime)
80 result.st_mtime = timespec_to_nsecs(result.st_mtime)
81 result.st_ctime = timespec_to_nsecs(result.st_ctime)
86 return stat_result.from_xstat_rep(_helpers.stat(path))
90 return stat_result.from_xstat_rep(_helpers.fstat(path))
94 return stat_result.from_xstat_rep(_helpers.lstat(path))