]> arthur.barton.de Git - bup.git/blob - lib/bup/xstat.py
8daa2c2509e34246f917cadf08e94046aa4d02f1
[bup.git] / lib / bup / xstat.py
1 """Enhanced stat operations for bup."""
2 import os
3 import stat as pystat
4 from bup import _helpers
5
6 try:
7     _bup_utimensat = _helpers.bup_utimensat
8 except AttributeError, e:
9     _bup_utimensat = False
10
11 try:
12     _bup_utimes = _helpers.bup_utimes
13 except AttributeError, e:
14     _bup_utimes = False
15
16 try:
17     _bup_lutimes = _helpers.bup_lutimes
18 except AttributeError, e:
19     _bup_lutimes = False
20
21
22 def timespec_to_nsecs((ts_s, ts_ns)):
23     return ts_s * 10**9 + ts_ns
24
25
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
29     ns = int(ns)
30     return (ns / 10**9, ns % 10**9)
31
32
33 def nsecs_to_timeval(ns):
34     """Return (s, us) where ns is always non-negative
35     and t = s + us / 10e5"""
36     ns = int(ns)
37     return (ns / 10**9, (ns % 10**9) / 1000)
38
39
40 def fstime_floor_secs(ns):
41     """Return largest integer not greater than ns / 10e8."""
42     return int(ns) / 10**9;
43
44
45 def fstime_to_timespec(ns):
46     return nsecs_to_timespec(ns)
47
48
49 def fstime_to_sec_str(fstime):
50     (s, ns) = fstime_to_timespec(fstime)
51     if(s < 0):
52         s += 1
53     if ns == 0:
54         return '%d' % s
55     else:
56         return '%d.%09d' % (s, ns)
57
58
59 if _bup_utimensat:
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))
82
83
84 class stat_result:
85     @staticmethod
86     def from_xstat_rep(st):
87         result = stat_result()
88         (result.st_mode,
89          result.st_ino,
90          result.st_dev,
91          result.st_nlink,
92          result.st_uid,
93          result.st_gid,
94          result.st_rdev,
95          result.st_size,
96          result.st_atime,
97          result.st_mtime,
98          result.st_ctime) = st
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)
102         return result
103
104
105 def stat(path):
106     return stat_result.from_xstat_rep(_helpers.stat(path))
107
108
109 def fstat(path):
110     return stat_result.from_xstat_rep(_helpers.fstat(path))
111
112
113 def lstat(path):
114     return stat_result.from_xstat_rep(_helpers.lstat(path))
115
116
117 def mode_str(mode):
118     result = ''
119     if pystat.S_ISREG(mode):
120         result += '-'
121     elif pystat.S_ISDIR(mode):
122         result += 'd'
123     elif pystat.S_ISCHR(mode):
124         result += 'c'
125     elif pystat.S_ISBLK(mode):
126         result += 'b'
127     elif pystat.S_ISFIFO(mode):
128         result += 'p'
129     elif pystat.S_ISLNK(mode):
130         result += 'l'
131     elif pystat.S_ISSOCK(mode):
132         result += 's'
133     else:
134         result += '?'
135
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 '-'
145     return result