1 """Enhanced stat operations for bup."""
4 import bup._helpers as _helpers
8 _have_utimensat = _helpers.utimensat
9 except AttributeError, e:
10 _have_utimensat = False
14 # Class to represent filesystem timestamps. Use integer
15 # nanoseconds on platforms where we have the higher resolution
16 # lstat. Use the native python stat representation (floating
17 # point seconds) otherwise.
20 return self._value.__cmp__(x._value)
22 def to_timespec(self):
23 """Return (s, ns) where ns is always non-negative
24 and t = s + ns / 10e8""" # metadata record rep (and libc rep)
25 s_ns = self.secs_nsecs()
26 if s_ns[0] > 0 or s_ns[1] >= 0:
28 return (s_ns[0] - 1, 10**9 + s_ns[1]) # ns is negative
33 ts._value = int(secs * 10**9)
37 def from_timespec(timespec):
39 ts._value = timespec[0] * 10**9 + timespec[1]
42 def approx_secs(self):
43 return self._value / 10e8;
46 "Return a (s, ns) pair: -1.5s -> (-1, -10**9 / 2)."
48 return (self._value / 10**9, self._value % 10**9)
49 abs_val = -self._value
50 return (- (abs_val / 10**9), - (abs_val % 10**9))
52 if _helpers._have_ns_fs_timestamps: # Use integer nanoseconds.
55 def from_stat_time(stat_time):
56 return FSTime.from_timespec(stat_time)
58 else: # Use python default floating-point seconds.
61 def from_stat_time(stat_time):
63 x = math.modf(stat_time)
64 ts._value = int(x[1]) + int(x[0] * 10**9)
70 def lutime(path, times):
71 atime = times[0].to_timespec()
72 mtime = times[1].to_timespec()
73 return _helpers.utimensat(_helpers.AT_FDCWD, path, (atime, mtime),
74 _helpers.AT_SYMLINK_NOFOLLOW)
75 def utime(path, times):
76 atime = times[0].to_timespec()
77 mtime = times[1].to_timespec()
78 return _helpers.utimensat(_helpers.AT_FDCWD, path, (atime, mtime), 0)
82 def lutime(path, times):
85 def utime(path, times):
86 atime = times[0].approx_secs()
87 mtime = times[1].approx_secs()
88 os.utime(path, (atime, mtime))
94 def from_stat_rep(st):
95 result = stat_result()
96 if _helpers._have_ns_fs_timestamps:
109 result.st_mode = st.st_mode
110 result.st_ino = st.st_ino
111 result.st_dev = st.st_dev
112 result.st_nlink = st.st_nlink
113 result.st_uid = st.st_uid
114 result.st_gid = st.st_gid
115 result.st_rdev = st.st_rdev
116 result.st_size = st.st_size
117 atime = FSTime.from_stat_time(st.st_atime)
118 mtime = FSTime.from_stat_time(st.st_mtime)
119 ctime = FSTime.from_stat_time(st.st_ctime)
120 result.st_atime = FSTime.from_stat_time(atime)
121 result.st_mtime = FSTime.from_stat_time(mtime)
122 result.st_ctime = FSTime.from_stat_time(ctime)
127 _stat = _helpers.stat
128 except AttributeError, e:
132 return stat_result.from_stat_rep(_stat(path))
136 _fstat = _helpers.fstat
137 except AttributeError, e:
141 return stat_result.from_stat_rep(_fstat(path))
145 _lstat = _helpers.lstat
146 except AttributeError, e:
150 return stat_result.from_stat_rep(_lstat(path))