1 """Enhanced stat operations for bup."""
4 from bup import _helpers
7 _bup_utimensat = _helpers.bup_utimensat
8 except AttributeError as e:
12 _bup_utimes = _helpers.bup_utimes
13 except AttributeError as e:
17 _bup_lutimes = _helpers.bup_lutimes
18 except AttributeError as e:
22 def timespec_to_nsecs((ts_s, ts_ns)):
23 return ts_s * 10**9 + ts_ns
26 def nsecs_to_timespec(ns):
27 """Return (s, ns) where ns is always non-negative
28 and t = s + ns / 10e8""" # metadata record rep
30 return (ns / 10**9, ns % 10**9)
33 def nsecs_to_timeval(ns):
34 """Return (s, us) where ns is always non-negative
35 and t = s + us / 10e5"""
37 return (ns / 10**9, (ns % 10**9) / 1000)
40 def fstime_floor_secs(ns):
41 """Return largest integer not greater than ns / 10e8."""
42 return int(ns) / 10**9;
45 def fstime_to_timespec(ns):
46 return nsecs_to_timespec(ns)
49 def fstime_to_sec_str(fstime):
50 (s, ns) = fstime_to_timespec(fstime)
56 return '%d.%09d' % (s, ns)
60 def utime(path, times):
61 """Times must be provided as (atime_ns, mtime_ns)."""
62 atime = nsecs_to_timespec(times[0])
63 mtime = nsecs_to_timespec(times[1])
64 _bup_utimensat(_helpers.AT_FDCWD, path, (atime, mtime), 0)
65 def lutime(path, times):
66 """Times must be provided as (atime_ns, mtime_ns)."""
67 atime = nsecs_to_timespec(times[0])
68 mtime = nsecs_to_timespec(times[1])
69 _bup_utimensat(_helpers.AT_FDCWD, path, (atime, mtime),
70 _helpers.AT_SYMLINK_NOFOLLOW)
71 else: # Must have these if utimensat isn't available.
72 def utime(path, times):
73 """Times must be provided as (atime_ns, mtime_ns)."""
74 atime = nsecs_to_timeval(times[0])
75 mtime = nsecs_to_timeval(times[1])
76 _bup_utimes(path, (atime, mtime))
77 def lutime(path, times):
78 """Times must be provided as (atime_ns, mtime_ns)."""
79 atime = nsecs_to_timeval(times[0])
80 mtime = nsecs_to_timeval(times[1])
81 _bup_lutimes(path, (atime, mtime))
83 _cygwin_sys = sys.platform.startswith('cygwin')
85 def _fix_cygwin_id(id):
94 def from_xstat_rep(st):
96 result = stat_result()
107 result.st_ctime) = st
108 # Inlined timespec_to_nsecs after profiling
109 result.st_atime = result.st_atime[0] * 10**9 + result.st_atime[1]
110 result.st_mtime = result.st_mtime[0] * 10**9 + result.st_mtime[1]
111 result.st_ctime = result.st_ctime[0] * 10**9 + result.st_ctime[1]
113 result.st_uid = _fix_cygwin_id(result.st_uid)
114 result.st_gid = _fix_cygwin_id(result.st_gid)
119 return stat_result.from_xstat_rep(_helpers.stat(path))
123 return stat_result.from_xstat_rep(_helpers.fstat(path))
127 return stat_result.from_xstat_rep(_helpers.lstat(path))
132 # FIXME: Other types?
133 if pystat.S_ISREG(mode):
135 elif pystat.S_ISDIR(mode):
137 elif pystat.S_ISCHR(mode):
139 elif pystat.S_ISBLK(mode):
141 elif pystat.S_ISFIFO(mode):
143 elif pystat.S_ISLNK(mode):
145 elif pystat.S_ISSOCK(mode):
150 result += 'r' if (mode & pystat.S_IRUSR) else '-'
151 result += 'w' if (mode & pystat.S_IWUSR) else '-'
152 result += 'x' if (mode & pystat.S_IXUSR) else '-'
153 result += 'r' if (mode & pystat.S_IRGRP) else '-'
154 result += 'w' if (mode & pystat.S_IWGRP) else '-'
155 result += 'x' if (mode & pystat.S_IXGRP) else '-'
156 result += 'r' if (mode & pystat.S_IROTH) else '-'
157 result += 'w' if (mode & pystat.S_IWOTH) else '-'
158 result += 'x' if (mode & pystat.S_IXOTH) else '-'
162 def classification_str(mode, include_exec):
163 if pystat.S_ISREG(mode):
165 and (pystat.S_IMODE(mode) \
166 & (pystat.S_IXUSR | pystat.S_IXGRP | pystat.S_IXOTH)):
170 elif pystat.S_ISDIR(mode):
172 elif pystat.S_ISLNK(mode):
174 elif pystat.S_ISFIFO(mode):
176 elif pystat.S_ISSOCK(mode):