]> arthur.barton.de Git - bup.git/blob - lib/bup/xstat.py
0eee9b2af2d956d495d4775f207ecd0e613c414f
[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     # FIXME: Other types?
120     if pystat.S_ISREG(mode):
121         result += '-'
122     elif pystat.S_ISDIR(mode):
123         result += 'd'
124     elif pystat.S_ISCHR(mode):
125         result += 'c'
126     elif pystat.S_ISBLK(mode):
127         result += 'b'
128     elif pystat.S_ISFIFO(mode):
129         result += 'p'
130     elif pystat.S_ISLNK(mode):
131         result += 'l'
132     elif pystat.S_ISSOCK(mode):
133         result += 's'
134     else:
135         result += '?'
136
137     result += 'r' if (mode & pystat.S_IRUSR) else '-'
138     result += 'w' if (mode & pystat.S_IWUSR) else '-'
139     result += 'x' if (mode & pystat.S_IXUSR) else '-'
140     result += 'r' if (mode & pystat.S_IRGRP) else '-'
141     result += 'w' if (mode & pystat.S_IWGRP) else '-'
142     result += 'x' if (mode & pystat.S_IXGRP) else '-'
143     result += 'r' if (mode & pystat.S_IROTH) else '-'
144     result += 'w' if (mode & pystat.S_IWOTH) else '-'
145     result += 'x' if (mode & pystat.S_IXOTH) else '-'
146     return result
147
148
149 def classification_str(mode, include_exec):
150     if pystat.S_ISREG(mode):
151         if include_exec \
152            and (pystat.S_IMODE(mode) \
153                 & (pystat.S_IXUSR | pystat.S_IXGRP | pystat.S_IXOTH)):
154             return '*'
155         else:
156             return ''
157     elif pystat.S_ISDIR(mode):
158         return '/'
159     elif pystat.S_ISLNK(mode):
160         return '@'
161     elif pystat.S_ISFIFO(mode):
162         return '|'
163     elif pystat.S_ISSOCK(mode):
164         return '='
165     else:
166         return ''