1 """Enhanced stat operations for bup."""
3 import bup._helpers as _helpers
7 _have_utimensat = _helpers.utimensat
8 except AttributeError, e:
9 _have_utimensat = False
13 # Class to represent filesystem timestamps. Use integer
14 # nanoseconds on platforms where we have the higher resolution
15 # lstat. Use the native python stat representation (floating
16 # point seconds) otherwise.
19 return self._value.__cmp__(x._value)
21 def to_timespec(self):
22 """Return (s, ns) where ns is always non-negative
23 and t = s + ns / 10e8""" # metadata record rep (and libc rep)
24 s_ns = self.secs_nsecs()
25 if s_ns[0] > 0 or s_ns[1] >= 0:
27 return (s_ns[0] - 1, 10**9 + s_ns[1]) # ns is negative
32 ts._value = int(secs * 10**9)
36 def from_timespec(timespec):
38 ts._value = timespec[0] * 10**9 + timespec[1]
41 def approx_secs(self):
42 return self._value / 10e8;
45 "Return a (s, ns) pair: -1.5s -> (-1, -10**9 / 2)."
47 return (self._value / 10**9, self._value % 10**9)
48 abs_val = -self._value
49 return (- (abs_val / 10**9), - (abs_val % 10**9))
51 if _helpers._have_ns_fs_timestamps: # Use integer nanoseconds.
54 def from_stat_time(stat_time):
55 return FSTime.from_timespec(stat_time)
57 else: # Use python default floating-point seconds.
60 def from_stat_time(stat_time):
62 x = math.modf(stat_time)
63 ts._value = int(x[1]) + int(x[0] * 10**9)
69 def lutime(path, times):
70 atime = times[0].to_timespec()
71 mtime = times[1].to_timespec()
72 return _helpers.utimensat(_helpers.AT_FDCWD, path, (atime, mtime),
73 _helpers.AT_SYMLINK_NOFOLLOW)
74 def utime(path, times):
75 atime = times[0].to_timespec()
76 mtime = times[1].to_timespec()
77 return _helpers.utimensat(_helpers.AT_FDCWD, path, (atime, mtime), 0)
81 def lutime(path, times):
84 def utime(path, times):
85 atime = times[0].approx_secs()
86 mtime = times[1].approx_secs()
87 os.utime(path, (atime, mtime))
93 def from_stat_rep(st):
94 result = stat_result()
95 if _helpers._have_ns_fs_timestamps:
108 result.st_mode = st.st_mode
109 result.st_ino = st.st_ino
110 result.st_dev = st.st_dev
111 result.st_nlink = st.st_nlink
112 result.st_uid = st.st_uid
113 result.st_gid = st.st_gid
114 result.st_rdev = st.st_rdev
115 result.st_size = st.st_size
116 atime = FSTime.from_stat_time(st.st_atime)
117 mtime = FSTime.from_stat_time(st.st_mtime)
118 ctime = FSTime.from_stat_time(st.st_ctime)
119 result.st_atime = FSTime.from_stat_time(atime)
120 result.st_mtime = FSTime.from_stat_time(mtime)
121 result.st_ctime = FSTime.from_stat_time(ctime)
126 _stat = _helpers.stat
127 except AttributeError, e:
131 return stat_result.from_stat_rep(_stat(path))
135 _fstat = _helpers.fstat
136 except AttributeError, e:
140 return stat_result.from_stat_rep(_fstat(path))
144 _lstat = _helpers.lstat
145 except AttributeError, e:
149 return stat_result.from_stat_rep(_lstat(path))