1 """Enhanced stat operations for bup."""
4 from bup import _helpers
7 _bup_utimensat = _helpers.bup_utimensat
8 except AttributeError, e:
12 _bup_utimes = _helpers.bup_utimes
13 except AttributeError, e:
17 _bup_lutimes = _helpers.bup_lutimes
18 except AttributeError, e:
22 def timespec_to_nsecs((ts_s, ts_ns)):
23 # c.f. _helpers.c: timespec_vals_to_py_ns()
24 if ts_ns < 0 or ts_ns > 999999999:
25 raise Exception('invalid timespec nsec value')
26 return ts_s * 10**9 + ts_ns
29 def nsecs_to_timespec(ns):
30 """Return (s, ns) where ns is always non-negative
31 and t = s + ns / 10e8""" # metadata record rep (and libc rep)
33 return (ns / 10**9, ns % 10**9)
36 def nsecs_to_timeval(ns):
37 """Return (s, us) where ns is always non-negative
38 and t = s + us / 10e5"""
40 return (ns / 10**9, (ns % 10**9) / 1000)
43 def fstime_floor_secs(ns):
44 """Return largest integer not greater than ns / 10e8."""
45 return int(ns) / 10**9;
48 def fstime_to_timespec(ns):
49 return nsecs_to_timespec(ns)
52 def fstime_to_sec_str(fstime):
53 (s, ns) = fstime_to_timespec(fstime)
59 return '%d.%09d' % (s, ns)
63 def utime(path, times):
64 """Times must be provided as (atime_ns, mtime_ns)."""
65 atime = nsecs_to_timespec(times[0])
66 mtime = nsecs_to_timespec(times[1])
67 _bup_utimensat(_helpers.AT_FDCWD, path, (atime, mtime), 0)
68 def lutime(path, times):
69 """Times must be provided as (atime_ns, mtime_ns)."""
70 atime = nsecs_to_timespec(times[0])
71 mtime = nsecs_to_timespec(times[1])
72 _bup_utimensat(_helpers.AT_FDCWD, path, (atime, mtime),
73 _helpers.AT_SYMLINK_NOFOLLOW)
74 else: # Must have these if utimensat isn't available.
75 def utime(path, times):
76 """Times must be provided as (atime_ns, mtime_ns)."""
77 atime = nsecs_to_timeval(times[0])
78 mtime = nsecs_to_timeval(times[1])
79 _bup_utimes(path, (atime, mtime))
80 def lutime(path, times):
81 """Times must be provided as (atime_ns, mtime_ns)."""
82 atime = nsecs_to_timeval(times[0])
83 mtime = nsecs_to_timeval(times[1])
84 _bup_lutimes(path, (atime, mtime))
89 def from_xstat_rep(st):
90 result = stat_result()
101 result.st_ctime) = st
102 result.st_atime = timespec_to_nsecs(result.st_atime)
103 result.st_mtime = timespec_to_nsecs(result.st_mtime)
104 result.st_ctime = timespec_to_nsecs(result.st_ctime)
109 return stat_result.from_xstat_rep(_helpers.stat(path))
113 return stat_result.from_xstat_rep(_helpers.fstat(path))
117 return stat_result.from_xstat_rep(_helpers.lstat(path))
122 if pystat.S_ISREG(mode):
124 elif pystat.S_ISDIR(mode):
126 elif pystat.S_ISCHR(mode):
128 elif pystat.S_ISBLK(mode):
130 elif pystat.S_ISFIFO(mode):
132 elif pystat.S_ISLNK(mode):
134 elif pystat.S_ISSOCK(mode):
139 result += 'r' if (mode & pystat.S_IRUSR) else '-'
140 result += 'w' if (mode & pystat.S_IWUSR) else '-'
141 result += 'x' if (mode & pystat.S_IXUSR) else '-'
142 result += 'r' if (mode & pystat.S_IRGRP) else '-'
143 result += 'w' if (mode & pystat.S_IWGRP) else '-'
144 result += 'x' if (mode & pystat.S_IXGRP) else '-'
145 result += 'r' if (mode & pystat.S_IROTH) else '-'
146 result += 'w' if (mode & pystat.S_IWOTH) else '-'
147 result += 'x' if (mode & pystat.S_IXOTH) else '-'