1 """Enhanced stat operations for bup."""
3 from bup import _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)
22 return 'FSTime(%d)' % self._value
24 def to_timespec(self):
25 """Return (s, ns) where ns is always non-negative
26 and t = s + ns / 10e8""" # metadata record rep (and libc rep)
27 s_ns = self.secs_nsecs()
28 if s_ns[0] > 0 or s_ns[1] >= 0:
30 return (s_ns[0] - 1, 10**9 + s_ns[1]) # ns is negative
35 ts._value = int(round(secs * 10**9))
39 def from_timespec(timespec):
41 ts._value = timespec[0] * 10**9 + timespec[1]
44 def approx_secs(self):
45 return self._value / 10e8;
48 "Return a (s, ns) pair: -1.5s -> (-1, -10**9 / 2)."
50 return (self._value / 10**9, self._value % 10**9)
51 abs_val = -self._value
52 return (- (abs_val / 10**9), - (abs_val % 10**9))
54 if _helpers._have_ns_fs_timestamps: # Use integer nanoseconds.
56 def from_stat_time(stat_time):
57 return FSTime.from_timespec(stat_time)
58 else: # Use python default floating-point seconds.
60 def from_stat_time(stat_time):
61 return FSTime.from_secs(stat_time)
65 def lutime(path, times):
66 atime = times[0].to_timespec()
67 mtime = times[1].to_timespec()
68 return _helpers.utimensat(_helpers.AT_FDCWD, path, (atime, mtime),
69 _helpers.AT_SYMLINK_NOFOLLOW)
70 def utime(path, times):
71 atime = times[0].to_timespec()
72 mtime = times[1].to_timespec()
73 return _helpers.utimensat(_helpers.AT_FDCWD, path, (atime, mtime), 0)
75 def lutime(path, times):
78 def utime(path, times):
79 atime = times[0].approx_secs()
80 mtime = times[1].approx_secs()
81 os.utime(path, (atime, mtime))
86 def from_stat_rep(st):
87 result = stat_result()
88 if _helpers._have_ns_fs_timestamps:
101 result.st_mode = st.st_mode
102 result.st_ino = st.st_ino
103 result.st_dev = st.st_dev
104 result.st_nlink = st.st_nlink
105 result.st_uid = st.st_uid
106 result.st_gid = st.st_gid
107 result.st_rdev = st.st_rdev
108 result.st_size = st.st_size
112 result.st_atime = FSTime.from_stat_time(atime)
113 result.st_mtime = FSTime.from_stat_time(mtime)
114 result.st_ctime = FSTime.from_stat_time(ctime)
119 _stat = _helpers.stat
120 except AttributeError, e:
124 return stat_result.from_stat_rep(_stat(path))
128 _fstat = _helpers.fstat
129 except AttributeError, e:
133 return stat_result.from_stat_rep(_fstat(path))
137 _lstat = _helpers.lstat
138 except AttributeError, e:
142 return stat_result.from_stat_rep(_lstat(path))