1 """Enhanced stat operations for bup."""
3 from __future__ import absolute_import
6 from bup import _helpers
9 _bup_utimensat = _helpers.bup_utimensat
10 except AttributeError as e:
11 _bup_utimensat = False
14 _bup_utimes = _helpers.bup_utimes
15 except AttributeError as e:
19 _bup_lutimes = _helpers.bup_lutimes
20 except AttributeError as e:
24 def timespec_to_nsecs(ts):
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
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_bytes(fstime):
53 (s, ns) = fstime_to_timespec(fstime)
59 return b'%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))
86 _cygwin_sys = sys.platform.startswith('cygwin')
88 def _fix_cygwin_id(id):
97 def from_xstat_rep(st):
99 result = stat_result()
110 result.st_ctime) = st
111 # Inlined timespec_to_nsecs after profiling
112 result.st_atime = result.st_atime[0] * 10**9 + result.st_atime[1]
113 result.st_mtime = result.st_mtime[0] * 10**9 + result.st_mtime[1]
114 result.st_ctime = result.st_ctime[0] * 10**9 + result.st_ctime[1]
116 result.st_uid = _fix_cygwin_id(result.st_uid)
117 result.st_gid = _fix_cygwin_id(result.st_gid)
122 return stat_result.from_xstat_rep(_helpers.stat(path))
126 return stat_result.from_xstat_rep(_helpers.fstat(path))
130 return stat_result.from_xstat_rep(_helpers.lstat(path))
135 # FIXME: Other types?
136 if pystat.S_ISREG(mode):
138 elif pystat.S_ISDIR(mode):
140 elif pystat.S_ISCHR(mode):
142 elif pystat.S_ISBLK(mode):
144 elif pystat.S_ISFIFO(mode):
146 elif pystat.S_ISLNK(mode):
148 elif pystat.S_ISSOCK(mode):
153 result += 'r' if (mode & pystat.S_IRUSR) else '-'
154 result += 'w' if (mode & pystat.S_IWUSR) else '-'
155 result += 'x' if (mode & pystat.S_IXUSR) else '-'
156 result += 'r' if (mode & pystat.S_IRGRP) else '-'
157 result += 'w' if (mode & pystat.S_IWGRP) else '-'
158 result += 'x' if (mode & pystat.S_IXGRP) else '-'
159 result += 'r' if (mode & pystat.S_IROTH) else '-'
160 result += 'w' if (mode & pystat.S_IWOTH) else '-'
161 result += 'x' if (mode & pystat.S_IXOTH) else '-'
165 def classification_str(mode, include_exec):
166 if pystat.S_ISREG(mode):
168 and (pystat.S_IMODE(mode) \
169 & (pystat.S_IXUSR | pystat.S_IXGRP | pystat.S_IXOTH)):
173 elif pystat.S_ISDIR(mode):
175 elif pystat.S_ISLNK(mode):
177 elif pystat.S_ISFIFO(mode):
179 elif pystat.S_ISSOCK(mode):