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 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))
86 def from_xstat_rep(st):
87 result = stat_result()
99 result.st_atime = timespec_to_nsecs(result.st_atime)
100 result.st_mtime = timespec_to_nsecs(result.st_mtime)
101 result.st_ctime = timespec_to_nsecs(result.st_ctime)
106 return stat_result.from_xstat_rep(_helpers.stat(path))
110 return stat_result.from_xstat_rep(_helpers.fstat(path))
114 return stat_result.from_xstat_rep(_helpers.lstat(path))
119 if pystat.S_ISREG(mode):
121 elif pystat.S_ISDIR(mode):
123 elif pystat.S_ISCHR(mode):
125 elif pystat.S_ISBLK(mode):
127 elif pystat.S_ISFIFO(mode):
129 elif pystat.S_ISLNK(mode):
131 elif pystat.S_ISSOCK(mode):
136 result += 'r' if (mode & pystat.S_IRUSR) else '-'
137 result += 'w' if (mode & pystat.S_IWUSR) else '-'
138 result += 'x' if (mode & pystat.S_IXUSR) else '-'
139 result += 'r' if (mode & pystat.S_IRGRP) else '-'
140 result += 'w' if (mode & pystat.S_IWGRP) else '-'
141 result += 'x' if (mode & pystat.S_IXGRP) else '-'
142 result += 'r' if (mode & pystat.S_IROTH) else '-'
143 result += 'w' if (mode & pystat.S_IWOTH) else '-'
144 result += 'x' if (mode & pystat.S_IXOTH) else '-'