1 """Enhanced stat operations for bup."""
4 from bup import _helpers
8 _have_bup_utime_ns = _helpers.bup_utime_ns
9 except AttributeError, e:
10 _have_bup_utime_ns = False
13 _have_bup_lutime_ns = _helpers.bup_lutime_ns
14 except AttributeError, e:
15 _have_bup_lutime_ns = False
18 def timespec_to_nsecs((ts_s, ts_ns)):
19 # c.f. _helpers.c: timespec_vals_to_py_ns()
20 if ts_ns < 0 or ts_ns > 999999999:
21 raise Exception('invalid timespec nsec value')
22 return ts_s * 10**9 + ts_ns
25 def nsecs_to_timespec(ns):
26 """Return (s, ns) where ns is always non-negative
27 and t = s + ns / 10e8""" # metadata record rep (and libc rep)
29 return (ns / 10**9, ns % 10**9)
32 def fstime_floor_secs(ns):
33 """Return largest integer not greater than ns / 10e8."""
34 return int(ns) / 10**9;
37 def fstime_to_timespec(ns):
38 return nsecs_to_timespec(ns)
41 if _have_bup_utime_ns:
42 def utime(path, times):
43 """Times must be provided as (atime_ns, mtime_ns)."""
44 atime = nsecs_to_timespec(times[0])
45 mtime = nsecs_to_timespec(times[1])
46 _helpers.bup_utime_ns(path, (atime, mtime))
48 def utime(path, times):
49 """Times must be provided as (atime_ns, mtime_ns)."""
50 atime = fstime_floor_secs(times[0])
51 mtime = fstime_floor_secs(times[1])
52 os.utime(path, (atime, mtime))
55 if _have_bup_lutime_ns:
56 def lutime(path, times):
57 """Times must be provided as (atime_ns, mtime_ns)."""
58 atime = nsecs_to_timespec(times[0])
59 mtime = nsecs_to_timespec(times[1])
60 _helpers.bup_lutime_ns(path, (atime, mtime))
67 def from_xstat_rep(st):
68 result = stat_result()
80 result.st_atime = timespec_to_nsecs(result.st_atime)
81 result.st_mtime = timespec_to_nsecs(result.st_mtime)
82 result.st_ctime = timespec_to_nsecs(result.st_ctime)
87 return stat_result.from_xstat_rep(_helpers.stat(path))
91 return stat_result.from_xstat_rep(_helpers.fstat(path))
95 return stat_result.from_xstat_rep(_helpers.lstat(path))
100 if pystat.S_ISREG(mode):
102 elif pystat.S_ISDIR(mode):
104 elif pystat.S_ISCHR(mode):
106 elif pystat.S_ISBLK(mode):
108 elif pystat.S_ISFIFO(mode):
110 elif pystat.S_ISLNK(mode):
115 result += 'r' if (mode & pystat.S_IRUSR) else '-'
116 result += 'w' if (mode & pystat.S_IWUSR) else '-'
117 result += 'x' if (mode & pystat.S_IXUSR) else '-'
118 result += 'r' if (mode & pystat.S_IRGRP) else '-'
119 result += 'w' if (mode & pystat.S_IWGRP) else '-'
120 result += 'x' if (mode & pystat.S_IXGRP) else '-'
121 result += 'r' if (mode & pystat.S_IROTH) else '-'
122 result += 'w' if (mode & pystat.S_IWOTH) else '-'
123 result += 'x' if (mode & pystat.S_IXOTH) else '-'