| 1 |
"""Annotation and rtyping support for the result of os.stat(), os.lstat() |
| 2 |
and os.fstat(). In RPython like in plain Python the stat result can be |
| 3 |
indexed like a tuple but also exposes the st_xxx attributes. |
| 4 |
""" |
| 5 |
|
| 6 |
import os, sys |
| 7 |
import collections |
| 8 |
|
| 9 |
from rpython.flowspace.model import Constant |
| 10 |
from rpython.flowspace.operation import op |
| 11 |
from rpython.annotator import model as annmodel |
| 12 |
from rpython.rtyper import extregistry |
| 13 |
from rpython.tool.pairtype import pairtype |
| 14 |
from rpython.rtyper.tool import rffi_platform as platform |
| 15 |
from rpython.rtyper.llannotation import lltype_to_annotation |
| 16 |
from rpython.rtyper.rmodel import Repr |
| 17 |
from rpython.rtyper.rint import IntegerRepr |
| 18 |
from rpython.rtyper.error import TyperError |
| 19 |
|
| 20 |
from rpython.rlib._os_support import utf8_traits |
| 21 |
from rpython.rlib.rutf8 import codepoints_in_utf8 |
| 22 |
from rpython.rlib.objectmodel import specialize, we_are_translated, not_rpython |
| 23 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 24 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 25 |
from rpython.rlib.rarithmetic import widen |
| 26 |
from rpython.rlib.rposix import ( |
| 27 |
replace_os_function, handle_posix_error, _as_bytes0) |
| 28 |
from rpython.rlib import rposix |
| 29 |
|
| 30 |
_WIN32 = sys.platform.startswith('win') |
| 31 |
_LINUX = sys.platform.startswith('linux') |
| 32 |
_MACOS = sys.platform.startswith('darwin') |
| 33 |
_BSD = sys.platform.startswith('openbsd') |
| 34 |
|
| 35 |
if _WIN32: |
| 36 |
from rpython.rlib import rwin32 |
| 37 |
from rpython.rlib.rwin32file import make_win32_traits |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
if _LINUX or _MACOS or _BSD: |
| 44 |
from rpython.rlib.rposix import TIMESPEC |
| 45 |
else: |
| 46 |
TIMESPEC = None |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
ALL_STAT_FIELDS = [ |
| 51 |
("st_mode", lltype.Signed), |
| 52 |
("st_ino", lltype.SignedLongLong), |
| 53 |
("st_dev", lltype.SignedLongLong), |
| 54 |
("st_nlink", lltype.Signed), |
| 55 |
("st_uid", lltype.Signed), |
| 56 |
("st_gid", lltype.Signed), |
| 57 |
("st_size", lltype.SignedLongLong), |
| 58 |
("st_atime", lltype.SignedLongLong), |
| 59 |
("st_mtime", lltype.SignedLongLong), |
| 60 |
("st_ctime", lltype.SignedLongLong), |
| 61 |
("st_blksize", lltype.Signed), |
| 62 |
("st_blocks", lltype.Signed), |
| 63 |
("st_rdev", lltype.Signed), |
| 64 |
("st_flags", lltype.Signed), |
| 65 |
|
| 66 |
|
| 67 |
("nsec_atime", lltype.Signed), |
| 68 |
("nsec_mtime", lltype.Signed), |
| 69 |
("nsec_ctime", lltype.Signed), |
| 70 |
] |
| 71 |
if sys.platform == 'win32': |
| 72 |
ALL_STAT_FIELDS.append(("st_file_attributes", lltype.Signed)) |
| 73 |
ALL_STAT_FIELDS.append(("st_reparse_tag", lltype.Signed)) |
| 74 |
|
| 75 |
N_INDEXABLE_FIELDS = 10 |
| 76 |
|
| 77 |
|
| 78 |
PORTABLE_STAT_FIELDS = ALL_STAT_FIELDS[:N_INDEXABLE_FIELDS] |
| 79 |
|
| 80 |
STATVFS_FIELDS = [ |
| 81 |
("f_bsize", lltype.Signed), |
| 82 |
("f_frsize", lltype.Signed), |
| 83 |
("f_blocks", lltype.Signed), |
| 84 |
("f_bfree", lltype.Signed), |
| 85 |
("f_bavail", lltype.Signed), |
| 86 |
("f_files", lltype.Signed), |
| 87 |
("f_ffree", lltype.Signed), |
| 88 |
("f_favail", lltype.Signed), |
| 89 |
("f_flag", lltype.Signed), |
| 90 |
("f_namemax", lltype.Signed), |
| 91 |
("f_fsid", lltype.Unsigned), |
| 92 |
] |
| 93 |
|
| 94 |
@specialize.arg(1) |
| 95 |
def get_stat_ns_as_bigint(st, name): |
| 96 |
"""'name' is one of the strings "atime", "mtime" or "ctime". |
| 97 |
Returns a bigint that represents the number of nanoseconds |
| 98 |
stored inside the RPython-level os.stat_result 'st'. |
| 99 |
|
| 100 |
Note that when running untranslated, the os.stat_result type |
| 101 |
is from Python 2.7, which doesn't store more precision than |
| 102 |
a float anyway. You will only get more after translation. |
| 103 |
""" |
| 104 |
from rpython.rlib.rbigint import rbigint |
| 105 |
|
| 106 |
if not we_are_translated(): |
| 107 |
as_float = getattr(st, "st_" + name) |
| 108 |
return rbigint.fromfloat(as_float * 1e9) |
| 109 |
|
| 110 |
if name == "atime": |
| 111 |
i, j = 7, -3 |
| 112 |
elif name == "mtime": |
| 113 |
i, j = 8, -2 |
| 114 |
elif name == "ctime": |
| 115 |
i, j = 9, -1 |
| 116 |
else: |
| 117 |
raise AssertionError(name) |
| 118 |
|
| 119 |
sec = st[i] |
| 120 |
nsec = st[j] |
| 121 |
result = rbigint.fromrarith_int(sec).int_mul(1000000000) |
| 122 |
result = result.int_add(nsec) |
| 123 |
return result |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
class SomeStatResult(annmodel.SomeObject): |
| 131 |
knowntype = os.stat_result |
| 132 |
|
| 133 |
def rtyper_makerepr(self, rtyper): |
| 134 |
return StatResultRepr(rtyper) |
| 135 |
|
| 136 |
def rtyper_makekey(self): |
| 137 |
return self.__class__, |
| 138 |
|
| 139 |
def getattr(self, s_attr): |
| 140 |
if not s_attr.is_constant(): |
| 141 |
raise annmodel.AnnotatorError("non-constant attr name in getattr()") |
| 142 |
attrname = s_attr.const |
| 143 |
if attrname in ('st_atime', 'st_mtime', 'st_ctime'): |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
TYPE = lltype.Float |
| 150 |
else: |
| 151 |
TYPE = STAT_FIELD_TYPES[attrname] |
| 152 |
return lltype_to_annotation(TYPE) |
| 153 |
|
| 154 |
if sys.platform == 'win32': |
| 155 |
def _get_rmarshall_support_(self): |
| 156 |
|
| 157 |
|
| 158 |
def stat_result_reduce(st): |
| 159 |
return (st[0], st[1], st[2], st[3], st[4], |
| 160 |
st[5], st[6], st.st_atime, st.st_mtime, st.st_ctime, |
| 161 |
st.st_file_attributes, st.st_reparse_tag) |
| 162 |
|
| 163 |
def stat_result_recreate(tup): |
| 164 |
atime, mtime, ctime = tup[7:10] |
| 165 |
result = tup[:7] |
| 166 |
result += (int(atime), int(mtime), int(ctime)) |
| 167 |
result += extra_zeroes |
| 168 |
result += (int((atime - result[7]) * 1e9), |
| 169 |
int((mtime - result[8]) * 1e9), |
| 170 |
int((ctime - result[9]) * 1e9)) |
| 171 |
result += tup[10:] |
| 172 |
return make_stat_result(result) |
| 173 |
s_reduced = annmodel.SomeTuple([lltype_to_annotation(TYPE) |
| 174 |
for name, TYPE in PORTABLE_STAT_FIELDS[:7]] |
| 175 |
+ 3 * [lltype_to_annotation(lltype.Float)] |
| 176 |
+ 2 * [lltype_to_annotation(lltype.Signed)]) |
| 177 |
extra_zeroes = (0,) * (len(STAT_FIELDS) - len(PORTABLE_STAT_FIELDS) - 5) |
| 178 |
return s_reduced, stat_result_reduce, stat_result_recreate |
| 179 |
else: |
| 180 |
def _get_rmarshall_support_(self): |
| 181 |
|
| 182 |
|
| 183 |
def stat_result_reduce(st): |
| 184 |
return (st[0], st[1], st[2], st[3], st[4], |
| 185 |
st[5], st[6], st.st_atime, st.st_mtime, st.st_ctime) |
| 186 |
|
| 187 |
def stat_result_recreate(tup): |
| 188 |
atime, mtime, ctime = tup[7:] |
| 189 |
result = tup[:7] |
| 190 |
result += (int(atime), int(mtime), int(ctime)) |
| 191 |
result += extra_zeroes |
| 192 |
result += (int((atime - result[7]) * 1e9), |
| 193 |
int((mtime - result[8]) * 1e9), |
| 194 |
int((ctime - result[9]) * 1e9)) |
| 195 |
return make_stat_result(result) |
| 196 |
s_reduced = annmodel.SomeTuple([lltype_to_annotation(TYPE) |
| 197 |
for name, TYPE in PORTABLE_STAT_FIELDS[:7]] |
| 198 |
+ 3 * [lltype_to_annotation(lltype.Float)]) |
| 199 |
extra_zeroes = (0,) * (len(STAT_FIELDS) - len(PORTABLE_STAT_FIELDS) - 3) |
| 200 |
return s_reduced, stat_result_reduce, stat_result_recreate |
| 201 |
|
| 202 |
|
| 203 |
class __extend__(pairtype(SomeStatResult, annmodel.SomeInteger)): |
| 204 |
def getitem((s_sta, s_int)): |
| 205 |
assert s_int.is_constant(), "os.stat()[index]: index must be constant" |
| 206 |
index = s_int.const |
| 207 |
assert -3 <= index < N_INDEXABLE_FIELDS, "os.stat()[index] out of range" |
| 208 |
name, TYPE = STAT_FIELDS[index] |
| 209 |
return lltype_to_annotation(TYPE) |
| 210 |
|
| 211 |
|
| 212 |
class StatResultRepr(Repr): |
| 213 |
|
| 214 |
def __init__(self, rtyper): |
| 215 |
self.rtyper = rtyper |
| 216 |
self.stat_field_indexes = {} |
| 217 |
for i, (name, TYPE) in enumerate(STAT_FIELDS): |
| 218 |
self.stat_field_indexes[name] = i |
| 219 |
|
| 220 |
self.s_tuple = annmodel.SomeTuple( |
| 221 |
[lltype_to_annotation(TYPE) for name, TYPE in STAT_FIELDS]) |
| 222 |
self.r_tuple = rtyper.getrepr(self.s_tuple) |
| 223 |
self.lowleveltype = self.r_tuple.lowleveltype |
| 224 |
|
| 225 |
def redispatch_getfield(self, hop, index): |
| 226 |
rtyper = self.rtyper |
| 227 |
s_index = rtyper.annotator.bookkeeper.immutablevalue(index) |
| 228 |
hop2 = hop.copy() |
| 229 |
spaceop = op.getitem(hop.args_v[0], Constant(index)) |
| 230 |
spaceop.result = hop.spaceop.result |
| 231 |
hop2.spaceop = spaceop |
| 232 |
hop2.args_v = spaceop.args |
| 233 |
hop2.args_s = [self.s_tuple, s_index] |
| 234 |
hop2.args_r = [self.r_tuple, rtyper.getrepr(s_index)] |
| 235 |
return hop2.dispatch() |
| 236 |
|
| 237 |
def rtype_getattr(self, hop): |
| 238 |
s_attr = hop.args_s[1] |
| 239 |
attr = s_attr.const |
| 240 |
if attr in ('st_atime', 'st_mtime', 'st_ctime'): |
| 241 |
ll_func = globals()['_ll_get_' + attr] |
| 242 |
v_tuple = hop.inputarg(self, arg=0) |
| 243 |
return hop.gendirectcall(ll_func, v_tuple) |
| 244 |
try: |
| 245 |
index = self.stat_field_indexes[attr] |
| 246 |
except KeyError: |
| 247 |
raise TyperError("os.stat().%s: field not available" % (attr,)) |
| 248 |
return self.redispatch_getfield(hop, index) |
| 249 |
|
| 250 |
@specialize.memo() |
| 251 |
def _stfld(name): |
| 252 |
index = STAT_FIELD_NAMES.index(name) |
| 253 |
return 'item%d' % index |
| 254 |
|
| 255 |
def _ll_get_st_atime(tup): |
| 256 |
return (float(getattr(tup, _stfld("st_atime"))) + |
| 257 |
1E-9 * getattr(tup, _stfld("nsec_atime"))) |
| 258 |
|
| 259 |
def _ll_get_st_mtime(tup): |
| 260 |
return (float(getattr(tup, _stfld("st_mtime"))) + |
| 261 |
1E-9 * getattr(tup, _stfld("nsec_mtime"))) |
| 262 |
|
| 263 |
def _ll_get_st_ctime(tup): |
| 264 |
return (float(getattr(tup, _stfld("st_ctime"))) + |
| 265 |
1E-9 * getattr(tup, _stfld("nsec_ctime"))) |
| 266 |
|
| 267 |
|
| 268 |
class __extend__(pairtype(StatResultRepr, IntegerRepr)): |
| 269 |
def rtype_getitem((r_sta, r_int), hop): |
| 270 |
s_int = hop.args_s[1] |
| 271 |
index = s_int.const |
| 272 |
if index < 0: |
| 273 |
index += len(STAT_FIELDS) |
| 274 |
return r_sta.redispatch_getfield(hop, index) |
| 275 |
|
| 276 |
s_StatResult = SomeStatResult() |
| 277 |
|
| 278 |
@not_rpython |
| 279 |
def make_stat_result(tup): |
| 280 |
"""Turn a tuple into an os.stat_result object.""" |
| 281 |
assert len(tup) == len(STAT_FIELDS) |
| 282 |
assert float not in [type(x) for x in tup] |
| 283 |
positional = [] |
| 284 |
for i in range(N_INDEXABLE_FIELDS): |
| 285 |
name, TYPE = STAT_FIELDS[i] |
| 286 |
value = lltype.cast_primitive(TYPE, tup[i]) |
| 287 |
positional.append(value) |
| 288 |
kwds = {} |
| 289 |
if sys.platform == 'win32': |
| 290 |
kwds['st_atime'] = tup[7] + 1e-9 * tup[-5] |
| 291 |
kwds['st_mtime'] = tup[8] + 1e-9 * tup[-4] |
| 292 |
kwds['st_ctime'] = tup[9] + 1e-9 * tup[-3] |
| 293 |
kwds['st_file_attributes'] = tup[-2] |
| 294 |
kwds['st_reparse_tag'] = tup[-1] |
| 295 |
else: |
| 296 |
kwds['st_atime'] = tup[7] + 1e-9 * tup[-3] |
| 297 |
kwds['st_mtime'] = tup[8] + 1e-9 * tup[-2] |
| 298 |
kwds['st_ctime'] = tup[9] + 1e-9 * tup[-1] |
| 299 |
for value, (name, TYPE) in zip(tup, STAT_FIELDS)[N_INDEXABLE_FIELDS:]: |
| 300 |
if name.startswith('nsec_'): |
| 301 |
continue |
| 302 |
kwds[name] = lltype.cast_primitive(TYPE, value) |
| 303 |
return os.stat_result(positional, kwds) |
| 304 |
|
| 305 |
|
| 306 |
class MakeStatResultEntry(extregistry.ExtRegistryEntry): |
| 307 |
_about_ = make_stat_result |
| 308 |
|
| 309 |
def compute_result_annotation(self, s_tup): |
| 310 |
return s_StatResult |
| 311 |
|
| 312 |
def specialize_call(self, hop): |
| 313 |
r_StatResult = hop.rtyper.getrepr(s_StatResult) |
| 314 |
[v_result] = hop.inputargs(r_StatResult.r_tuple) |
| 315 |
|
| 316 |
hop.exception_cannot_occur() |
| 317 |
return v_result |
| 318 |
|
| 319 |
|
| 320 |
class SomeStatvfsResult(annmodel.SomeObject): |
| 321 |
if hasattr(os, 'statvfs_result'): |
| 322 |
knowntype = os.statvfs_result |
| 323 |
else: |
| 324 |
knowntype = None |
| 325 |
|
| 326 |
def rtyper_makerepr(self, rtyper): |
| 327 |
return StatvfsResultRepr(rtyper) |
| 328 |
|
| 329 |
def rtyper_makekey(self): |
| 330 |
return self.__class__, |
| 331 |
|
| 332 |
def getattr(self, s_attr): |
| 333 |
assert s_attr.is_constant() |
| 334 |
TYPE = STATVFS_FIELD_TYPES[s_attr.const] |
| 335 |
return lltype_to_annotation(TYPE) |
| 336 |
|
| 337 |
|
| 338 |
class __extend__(pairtype(SomeStatvfsResult, annmodel.SomeInteger)): |
| 339 |
def getitem((s_stat, s_int)): |
| 340 |
assert s_int.is_constant() |
| 341 |
name, TYPE = STATVFS_FIELDS[s_int.const] |
| 342 |
return lltype_to_annotation(TYPE) |
| 343 |
|
| 344 |
|
| 345 |
s_StatvfsResult = SomeStatvfsResult() |
| 346 |
|
| 347 |
|
| 348 |
class StatvfsResultRepr(Repr): |
| 349 |
def __init__(self, rtyper): |
| 350 |
self.rtyper = rtyper |
| 351 |
self.statvfs_field_indexes = {} |
| 352 |
for i, (name, TYPE) in enumerate(STATVFS_FIELDS): |
| 353 |
self.statvfs_field_indexes[name] = i |
| 354 |
|
| 355 |
self.s_tuple = annmodel.SomeTuple( |
| 356 |
[lltype_to_annotation(TYPE) for name, TYPE in STATVFS_FIELDS]) |
| 357 |
self.r_tuple = rtyper.getrepr(self.s_tuple) |
| 358 |
self.lowleveltype = self.r_tuple.lowleveltype |
| 359 |
|
| 360 |
def redispatch_getfield(self, hop, index): |
| 361 |
rtyper = self.rtyper |
| 362 |
s_index = rtyper.annotator.bookkeeper.immutablevalue(index) |
| 363 |
hop2 = hop.copy() |
| 364 |
spaceop = op.getitem(hop.args_v[0], Constant(index)) |
| 365 |
spaceop.result = hop.spaceop.result |
| 366 |
hop2.spaceop = spaceop |
| 367 |
hop2.args_v = spaceop.args |
| 368 |
hop2.args_s = [self.s_tuple, s_index] |
| 369 |
hop2.args_r = [self.r_tuple, rtyper.getrepr(s_index)] |
| 370 |
return hop2.dispatch() |
| 371 |
|
| 372 |
def rtype_getattr(self, hop): |
| 373 |
s_attr = hop.args_s[1] |
| 374 |
attr = s_attr.const |
| 375 |
try: |
| 376 |
index = self.statvfs_field_indexes[attr] |
| 377 |
except KeyError: |
| 378 |
raise TyperError("os.statvfs().%s: field not available" % (attr,)) |
| 379 |
return self.redispatch_getfield(hop, index) |
| 380 |
|
| 381 |
|
| 382 |
class __extend__(pairtype(StatvfsResultRepr, IntegerRepr)): |
| 383 |
def rtype_getitem((r_sta, r_int), hop): |
| 384 |
s_int = hop.args_s[1] |
| 385 |
index = s_int.const |
| 386 |
return r_sta.redispatch_getfield(hop, index) |
| 387 |
|
| 388 |
def make_statvfs_result(tup): |
| 389 |
args = tuple( |
| 390 |
lltype.cast_primitive(TYPE, value) for value, (name, TYPE) in |
| 391 |
zip(tup, STATVFS_FIELDS)) |
| 392 |
|
| 393 |
return statvfs_result(*args) |
| 394 |
|
| 395 |
class MakeStatvfsResultEntry(extregistry.ExtRegistryEntry): |
| 396 |
_about_ = make_statvfs_result |
| 397 |
|
| 398 |
def compute_result_annotation(self, s_tup): |
| 399 |
return s_StatvfsResult |
| 400 |
|
| 401 |
def specialize_call(self, hop): |
| 402 |
r_StatvfsResult = hop.rtyper.getrepr(s_StatvfsResult) |
| 403 |
[v_result] = hop.inputargs(r_StatvfsResult.r_tuple) |
| 404 |
hop.exception_cannot_occur() |
| 405 |
return v_result |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
if sys.platform.startswith('win'): |
| 412 |
_name_struct_stat = '_stati64' |
| 413 |
INCLUDES = ['sys/types.h', 'sys/stat.h', 'sys/statvfs.h'] |
| 414 |
else: |
| 415 |
_name_struct_stat = 'stat' |
| 416 |
INCLUDES = ['sys/types.h', 'sys/stat.h', 'sys/statvfs.h', 'unistd.h'] |
| 417 |
|
| 418 |
compilation_info = ExternalCompilationInfo( |
| 419 |
includes=INCLUDES |
| 420 |
) |
| 421 |
|
| 422 |
|
| 423 |
def posix_declaration(try_to_add=None): |
| 424 |
global STAT_STRUCT, STATVFS_STRUCT |
| 425 |
|
| 426 |
LL_STAT_FIELDS = STAT_FIELDS[:] |
| 427 |
if try_to_add: |
| 428 |
LL_STAT_FIELDS.append(try_to_add) |
| 429 |
|
| 430 |
if TIMESPEC is not None: |
| 431 |
|
| 432 |
def _expand(lst, originalname, timespecname): |
| 433 |
if _MACOS: |
| 434 |
timespecname = originalname + "spec" |
| 435 |
else: |
| 436 |
timespecname = originalname[:-1] |
| 437 |
|
| 438 |
for i, (_name, _TYPE) in enumerate(lst): |
| 439 |
if _name == originalname: |
| 440 |
|
| 441 |
|
| 442 |
lst[i] = (timespecname, TIMESPEC) |
| 443 |
break |
| 444 |
|
| 445 |
_expand(LL_STAT_FIELDS, 'st_atime', 'st_atim') |
| 446 |
_expand(LL_STAT_FIELDS, 'st_mtime', 'st_mtim') |
| 447 |
_expand(LL_STAT_FIELDS, 'st_ctime', 'st_ctim') |
| 448 |
|
| 449 |
del _expand |
| 450 |
else: |
| 451 |
|
| 452 |
for name in ('st_atime', 'st_mtime', 'st_ctime', 'st_birthtime'): |
| 453 |
for i, (_name, _TYPE) in enumerate(LL_STAT_FIELDS): |
| 454 |
if _name == name: |
| 455 |
LL_STAT_FIELDS[i] = (_name, lltype.Signed) |
| 456 |
break |
| 457 |
|
| 458 |
class CConfig: |
| 459 |
_compilation_info_ = compilation_info |
| 460 |
STAT_STRUCT = platform.Struct('struct %s' % _name_struct_stat, LL_STAT_FIELDS) |
| 461 |
STATVFS_STRUCT = platform.Struct('struct statvfs', STATVFS_FIELDS) |
| 462 |
|
| 463 |
try: |
| 464 |
config = platform.configure(CConfig, ignore_errors=try_to_add is not None) |
| 465 |
except platform.CompilationError: |
| 466 |
if try_to_add: |
| 467 |
return |
| 468 |
raise |
| 469 |
|
| 470 |
STAT_STRUCT = lltype.Ptr(config['STAT_STRUCT']) |
| 471 |
STATVFS_STRUCT = lltype.Ptr(config['STATVFS_STRUCT']) |
| 472 |
if try_to_add: |
| 473 |
STAT_FIELDS.append(try_to_add) |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
STAT_FIELDS = PORTABLE_STAT_FIELDS[:] |
| 480 |
|
| 481 |
if sys.platform != 'win32': |
| 482 |
posix_declaration() |
| 483 |
for _i in range(len(PORTABLE_STAT_FIELDS), len(ALL_STAT_FIELDS)): |
| 484 |
posix_declaration(ALL_STAT_FIELDS[_i]) |
| 485 |
del _i |
| 486 |
|
| 487 |
if sys.platform == 'win32': |
| 488 |
STAT_FIELDS += ALL_STAT_FIELDS[-5:] |
| 489 |
else: |
| 490 |
STAT_FIELDS += ALL_STAT_FIELDS[-3:] |
| 491 |
|
| 492 |
|
| 493 |
STAT_FIELD_TYPES = dict(STAT_FIELDS) |
| 494 |
STAT_FIELD_NAMES = [_name for (_name, _TYPE) in STAT_FIELDS] |
| 495 |
del _name, _TYPE |
| 496 |
|
| 497 |
STATVFS_FIELD_TYPES = dict(STATVFS_FIELDS) |
| 498 |
STATVFS_FIELD_NAMES = [name for name, tp in STATVFS_FIELDS] |
| 499 |
statvfs_result = collections.namedtuple('statvfs_result', STATVFS_FIELD_NAMES) |
| 500 |
|
| 501 |
def build_stat_result(st): |
| 502 |
|
| 503 |
if TIMESPEC is not None: |
| 504 |
if _MACOS: |
| 505 |
atim = st.c_st_atimespec |
| 506 |
mtim = st.c_st_mtimespec |
| 507 |
ctim = st.c_st_ctimespec |
| 508 |
else: |
| 509 |
atim = st.c_st_atim |
| 510 |
mtim = st.c_st_mtim |
| 511 |
ctim = st.c_st_ctim |
| 512 |
atime, extra_atime = atim.c_tv_sec, int(atim.c_tv_nsec) |
| 513 |
mtime, extra_mtime = mtim.c_tv_sec, int(mtim.c_tv_nsec) |
| 514 |
ctime, extra_ctime = ctim.c_tv_sec, int(ctim.c_tv_nsec) |
| 515 |
else: |
| 516 |
atime, extra_atime = st.c_st_atime, 0 |
| 517 |
mtime, extra_mtime = st.c_st_mtime, 0 |
| 518 |
ctime, extra_ctime = st.c_st_ctime, 0 |
| 519 |
|
| 520 |
result = (st.c_st_mode, |
| 521 |
st.c_st_ino, |
| 522 |
st.c_st_dev, |
| 523 |
st.c_st_nlink, |
| 524 |
st.c_st_uid, |
| 525 |
st.c_st_gid, |
| 526 |
st.c_st_size, |
| 527 |
atime, |
| 528 |
mtime, |
| 529 |
ctime) |
| 530 |
|
| 531 |
if "st_blksize" in STAT_FIELD_TYPES: result += (st.c_st_blksize,) |
| 532 |
if "st_blocks" in STAT_FIELD_TYPES: result += (st.c_st_blocks,) |
| 533 |
if "st_rdev" in STAT_FIELD_TYPES: result += (st.c_st_rdev,) |
| 534 |
if "st_flags" in STAT_FIELD_TYPES: result += (st.c_st_flags,) |
| 535 |
|
| 536 |
result += (extra_atime, |
| 537 |
extra_mtime, |
| 538 |
extra_ctime) |
| 539 |
|
| 540 |
return make_stat_result(result) |
| 541 |
|
| 542 |
|
| 543 |
def build_statvfs_result(st): |
| 544 |
return make_statvfs_result(( |
| 545 |
st.c_f_bsize, |
| 546 |
st.c_f_frsize, |
| 547 |
st.c_f_blocks, |
| 548 |
st.c_f_bfree, |
| 549 |
st.c_f_bavail, |
| 550 |
st.c_f_files, |
| 551 |
st.c_f_ffree, |
| 552 |
st.c_f_favail, |
| 553 |
st.c_f_flag, |
| 554 |
st.c_f_namemax, |
| 555 |
st.c_f_fsid, |
| 556 |
)) |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
if not _WIN32: |
| 562 |
c_fstat = rffi.llexternal('fstat', |
| 563 |
[rffi.INT, STAT_STRUCT], rffi.INT, |
| 564 |
compilation_info=compilation_info, |
| 565 |
save_err=rffi.RFFI_SAVE_ERRNO, |
| 566 |
macro=True) |
| 567 |
c_stat = rffi.llexternal('stat', |
| 568 |
[rffi.CCHARP, STAT_STRUCT], rffi.INT, |
| 569 |
compilation_info=compilation_info, |
| 570 |
save_err=rffi.RFFI_SAVE_ERRNO, |
| 571 |
macro=True) |
| 572 |
c_lstat = rffi.llexternal('lstat', |
| 573 |
[rffi.CCHARP, STAT_STRUCT], rffi.INT, |
| 574 |
compilation_info=compilation_info, |
| 575 |
save_err=rffi.RFFI_SAVE_ERRNO, |
| 576 |
macro=True) |
| 577 |
|
| 578 |
c_fstatvfs = rffi.llexternal('fstatvfs', |
| 579 |
[rffi.INT, STATVFS_STRUCT], rffi.INT, |
| 580 |
compilation_info=compilation_info, |
| 581 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 582 |
c_statvfs = rffi.llexternal('statvfs', |
| 583 |
[rffi.CCHARP, STATVFS_STRUCT], rffi.INT, |
| 584 |
compilation_info=compilation_info, |
| 585 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 586 |
|
| 587 |
@replace_os_function('fstat') |
| 588 |
def fstat(fd): |
| 589 |
if not _WIN32: |
| 590 |
with lltype.scoped_alloc(STAT_STRUCT.TO) as stresult: |
| 591 |
handle_posix_error('fstat', c_fstat(fd, stresult)) |
| 592 |
return build_stat_result(stresult) |
| 593 |
else: |
| 594 |
handle = rwin32.get_osfhandle(fd) |
| 595 |
win32traits = make_win32_traits(utf8_traits) |
| 596 |
filetype = win32traits.GetFileType(handle) |
| 597 |
if filetype == win32traits.FILE_TYPE_CHAR: |
| 598 |
|
| 599 |
return make_stat_result((win32traits._S_IFCHR, |
| 600 |
0, 0, 0, 0, 0, |
| 601 |
0, 0, 0, 0, |
| 602 |
0, 0, 0, 0, 0)) |
| 603 |
elif filetype == win32traits.FILE_TYPE_PIPE: |
| 604 |
|
| 605 |
return make_stat_result((win32traits._S_IFIFO, |
| 606 |
0, 0, 0, 0, 0, |
| 607 |
0, 0, 0, 0, |
| 608 |
0, 0, 0, 0, 0)) |
| 609 |
elif filetype == win32traits.FILE_TYPE_UNKNOWN: |
| 610 |
error = rwin32.GetLastError_saved() |
| 611 |
if error != 0: |
| 612 |
raise WindowsError(error, "os_fstat failed") |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
with lltype.scoped_alloc(win32traits.BY_HANDLE_FILE_INFORMATION, |
| 617 |
zero=True) as fileInfo: |
| 618 |
res = win32traits.GetFileInformationByHandle(handle, fileInfo) |
| 619 |
if res == 0: |
| 620 |
raise WindowsError(rwin32.GetLastError_saved(), |
| 621 |
"os_fstat failed") |
| 622 |
return win32_by_handle_info_to_stat(win32traits, fileInfo, 0) |
| 623 |
|
| 624 |
@replace_os_function('stat') |
| 625 |
@specialize.argtype(0) |
| 626 |
def stat(path): |
| 627 |
if not _WIN32: |
| 628 |
with lltype.scoped_alloc(STAT_STRUCT.TO) as stresult: |
| 629 |
arg = _as_bytes0(path) |
| 630 |
handle_posix_error('stat', c_stat(arg, stresult)) |
| 631 |
return build_stat_result(stresult) |
| 632 |
else: |
| 633 |
traits = utf8_traits |
| 634 |
path = traits.as_utf80(path) |
| 635 |
return win32_xstat3(traits, path, traverse=True) |
| 636 |
|
| 637 |
@replace_os_function('lstat') |
| 638 |
@specialize.argtype(0) |
| 639 |
def lstat(path): |
| 640 |
if not _WIN32: |
| 641 |
with lltype.scoped_alloc(STAT_STRUCT.TO) as stresult: |
| 642 |
arg = _as_bytes0(path) |
| 643 |
handle_posix_error('lstat', c_lstat(arg, stresult)) |
| 644 |
return build_stat_result(stresult) |
| 645 |
else: |
| 646 |
traits = utf8_traits |
| 647 |
path = traits.as_utf80(path) |
| 648 |
return win32_xstat3(traits, path, traverse=False) |
| 649 |
|
| 650 |
@specialize.argtype(0) |
| 651 |
def stat3(path): |
| 652 |
if _WIN32: |
| 653 |
|
| 654 |
|
| 655 |
traits = utf8_traits |
| 656 |
path = traits.as_utf80(path) |
| 657 |
return win32_xstat3(traits, path, traverse=True) |
| 658 |
else: |
| 659 |
return stat(path) |
| 660 |
|
| 661 |
@specialize.argtype(0) |
| 662 |
def lstat3(path): |
| 663 |
if _WIN32: |
| 664 |
|
| 665 |
|
| 666 |
traits = utf8_traits |
| 667 |
path = traits.as_utf80(path) |
| 668 |
return win32_xstat3(traits, path, traverse=False) |
| 669 |
else: |
| 670 |
return lstat(path) |
| 671 |
|
| 672 |
if rposix.HAVE_FSTATAT: |
| 673 |
from rpython.rlib.rposix import AT_FDCWD, AT_SYMLINK_NOFOLLOW |
| 674 |
c_fstatat = rffi.llexternal('fstatat', |
| 675 |
[rffi.INT, rffi.CCHARP, STAT_STRUCT, rffi.INT], rffi.INT, |
| 676 |
compilation_info=compilation_info, |
| 677 |
save_err=rffi.RFFI_SAVE_ERRNO, macro=True) |
| 678 |
|
| 679 |
def fstatat(pathname, dir_fd=AT_FDCWD, follow_symlinks=True): |
| 680 |
if follow_symlinks: |
| 681 |
flags = 0 |
| 682 |
else: |
| 683 |
flags = AT_SYMLINK_NOFOLLOW |
| 684 |
with lltype.scoped_alloc(STAT_STRUCT.TO) as stresult: |
| 685 |
error = c_fstatat(dir_fd, pathname, stresult, flags) |
| 686 |
handle_posix_error('fstatat', error) |
| 687 |
return build_stat_result(stresult) |
| 688 |
|
| 689 |
@replace_os_function('fstatvfs') |
| 690 |
def fstatvfs(fd): |
| 691 |
with lltype.scoped_alloc(STATVFS_STRUCT.TO) as stresult: |
| 692 |
handle_posix_error('fstatvfs', c_fstatvfs(fd, stresult)) |
| 693 |
return build_statvfs_result(stresult) |
| 694 |
|
| 695 |
@replace_os_function('statvfs') |
| 696 |
@specialize.argtype(0) |
| 697 |
def statvfs(path): |
| 698 |
with lltype.scoped_alloc(STATVFS_STRUCT.TO) as stresult: |
| 699 |
arg = _as_bytes0(path) |
| 700 |
handle_posix_error('statvfs', c_statvfs(arg, stresult)) |
| 701 |
return build_statvfs_result(stresult) |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
if _WIN32: |
| 706 |
from rpython.rlib.rwin32file import FILE_TIME_to_time_t_nsec |
| 707 |
|
| 708 |
def make_longlong(high, low): |
| 709 |
return (rffi.r_longlong(high) << 32) + rffi.r_longlong(low) |
| 710 |
|
| 711 |
def IsReparseTagNameSurrogate(_tag): |
| 712 |
return widen(_tag) & 0x20000000 |
| 713 |
|
| 714 |
@specialize.arg(0) |
| 715 |
def win32_xstat3(traits, path0, traverse=False): |
| 716 |
|
| 717 |
win32traits = make_win32_traits(traits) |
| 718 |
path = traits.as_str0(path0) |
| 719 |
|
| 720 |
with lltype.scoped_alloc(win32traits.BY_HANDLE_FILE_INFORMATION, |
| 721 |
zero=True) as fileInfo: |
| 722 |
with lltype.scoped_alloc(win32traits.FILE_ATTRIBUTE_TAG_INFO, |
| 723 |
zero=True) as tagInfo: |
| 724 |
return win32_xstat_impl(win32traits, path, traverse, fileInfo, tagInfo) |
| 725 |
|
| 726 |
@specialize.arg(0) |
| 727 |
def win32_xstat_impl(traits, path, traverse, fileInfo, tagInfo): |
| 728 |
access = traits.FILE_READ_ATTRIBUTES |
| 729 |
flags = traits.FILE_FLAG_BACKUP_SEMANTICS |
| 730 |
isUnhandledTag = False |
| 731 |
if not traverse: |
| 732 |
flags |= traits.FILE_FLAG_OPEN_REPARSE_POINT |
| 733 |
with rffi.scoped_utf82wcharp(path, codepoints_in_utf8(path)) as buf: |
| 734 |
hFile = traits.CreateFile(buf, access, 0, |
| 735 |
lltype.nullptr(rwin32.LPSECURITY_ATTRIBUTES.TO), |
| 736 |
traits.OPEN_EXISTING, flags, rwin32.NULL_HANDLE) |
| 737 |
|
| 738 |
if hFile == rwin32.INVALID_HANDLE_VALUE: |
| 739 |
|
| 740 |
errcode = rwin32.GetLastError_saved() |
| 741 |
if (errcode == traits.ERROR_ACCESS_DENIED or |
| 742 |
errcode == traits.ERROR_SHARING_VIOLATION): |
| 743 |
|
| 744 |
if win32_attributes_from_dir(traits, path, fileInfo, tagInfo) == 0: |
| 745 |
raise WindowsError(rwin32.GetLastError_saved(), |
| 746 |
"win32_attributes_from_dir failed") |
| 747 |
if widen(fileInfo.c_dwFileAttributes) & traits.FILE_ATTRIBUTE_REPARSE_POINT: |
| 748 |
if traverse or not IsReparseTagNameSurrogate(tagInfo.c_ReparseTag): |
| 749 |
raise WindowsError(rwin32.GetLastError_saved(), |
| 750 |
"win32_xstat failed") |
| 751 |
elif errcode == traits.ERROR_INVALID_PARAMETER: |
| 752 |
|
| 753 |
with rffi.scoped_utf82wcharp(path, codepoints_in_utf8(path)) as buf: |
| 754 |
hFile = traits.CreateFile(buf, |
| 755 |
access | traits.GENERIC_READ, |
| 756 |
traits.FILE_SHARE_READ | traits.FILE_SHARE_WRITE, |
| 757 |
lltype.nullptr(rwin32.LPSECURITY_ATTRIBUTES.TO), |
| 758 |
traits.OPEN_EXISTING, flags, rwin32.NULL_HANDLE) |
| 759 |
if hFile == rwin32.INVALID_HANDLE_VALUE: |
| 760 |
raise WindowsError(rwin32.GetLastError_saved(), |
| 761 |
"win32_xstat failed") |
| 762 |
elif errcode == traits.ERROR_CANT_ACCESS_FILE: |
| 763 |
|
| 764 |
if traverse: |
| 765 |
traverse = False |
| 766 |
isUnhandledTag = True |
| 767 |
with rffi.scoped_utf82wcharp(path, codepoints_in_utf8(path)) as buf: |
| 768 |
hFile = traits.CreateFile(buf, access, 0, |
| 769 |
lltype.nullptr(rwin32.LPSECURITY_ATTRIBUTES.TO), |
| 770 |
traits.OPEN_EXISTING, |
| 771 |
flags | traits.FILE_FLAG_OPEN_REPARSE_POINT, |
| 772 |
rwin32.NULL_HANDLE) |
| 773 |
if hFile == rwin32.INVALID_HANDLE_VALUE: |
| 774 |
raise WindowsError(rwin32.GetLastError_saved(), |
| 775 |
"win32_xstat failed") |
| 776 |
else: |
| 777 |
raise WindowsError(errcode, "os_stat failed") |
| 778 |
|
| 779 |
if hFile != rwin32.INVALID_HANDLE_VALUE: |
| 780 |
|
| 781 |
fileType = traits.GetFileType(hFile) |
| 782 |
if fileType != traits.FILE_TYPE_DISK: |
| 783 |
errcode = rwin32.GetLastError_saved() |
| 784 |
if fileType == traits.FILE_TYPE_UNKNOWN and errcode != 0: |
| 785 |
rwin32.CloseHandle(hFile) |
| 786 |
raise WindowsError(errcode, "os_stat failed") |
| 787 |
with rffi.scoped_utf82wcharp(path, codepoints_in_utf8(path)) as buf: |
| 788 |
fileAttributes = widen(traits.GetFileAttributes(buf)) |
| 789 |
st_mode = 0 |
| 790 |
if (fileAttributes != traits.INVALID_FILE_ATTRIBUTES and |
| 791 |
fileAttributes & traits.FILE_ATTRIBUTE_DIRECTORY): |
| 792 |
|
| 793 |
st_mode = traits._S_IFDIR |
| 794 |
elif fileType == traits.FILE_TYPE_CHAR: |
| 795 |
|
| 796 |
st_mode = traits._S_IFCHR |
| 797 |
elif fileType == traits.FILE_TYPE_PIPE: |
| 798 |
|
| 799 |
st_mode = traits._S_IFIFO |
| 800 |
rwin32.CloseHandle(hFile) |
| 801 |
result = (st_mode, |
| 802 |
0, 0, 0, 0, 0, |
| 803 |
0, |
| 804 |
0, 0, 0, |
| 805 |
0, 0, 0, |
| 806 |
0, 0) |
| 807 |
|
| 808 |
return make_stat_result(result) |
| 809 |
|
| 810 |
if not traverse: |
| 811 |
if not traits.GetFileInformationByHandleEx(hFile, |
| 812 |
traits.FileAttributeTagInfo, tagInfo, |
| 813 |
traits.TagInfoSize): |
| 814 |
errcode = rwin32.GetLastError_saved() |
| 815 |
if errcode in (traits.ERROR_INVALID_PARAMETER, |
| 816 |
traits.ERROR_INVALID_FUNCTION, |
| 817 |
traits.ERROR_NOT_SUPPORTED): |
| 818 |
tagInfo.c_FileAttributes = rffi.cast( |
| 819 |
rwin32.DWORD, traits.FILE_ATTRIBUTE_NORMAL) |
| 820 |
tagInfo.c_ReparseTag = rffi.cast(rwin32.DWORD, 0) |
| 821 |
else: |
| 822 |
rwin32.CloseHandle(hFile) |
| 823 |
raise WindowsError(errcode, "os_stat failed") |
| 824 |
elif widen(tagInfo.c_FileAttributes) & traits.FILE_ATTRIBUTE_REPARSE_POINT: |
| 825 |
if IsReparseTagNameSurrogate(tagInfo.c_ReparseTag): |
| 826 |
if isUnhandledTag: |
| 827 |
|
| 828 |
|
| 829 |
rwin32.CloseHandle(hFile) |
| 830 |
raise WindowsError( |
| 831 |
traits.ERROR_CANT_ACCESS_FILE, |
| 832 |
"os_stat failed") |
| 833 |
|
| 834 |
|
| 835 |
elif not isUnhandledTag: |
| 836 |
rwin32.CloseHandle(hFile) |
| 837 |
return win32_xstat_impl(traits, path, True, fileInfo, tagInfo) |
| 838 |
|
| 839 |
res = traits.GetFileInformationByHandle(hFile, fileInfo) |
| 840 |
errcode = rwin32.GetLastError_saved() |
| 841 |
rwin32.CloseHandle(hFile) |
| 842 |
if res == 0: |
| 843 |
raise WindowsError(errcode, "GetFileInformationByHandle failed") |
| 844 |
result = win32_by_handle_info_to_stat(traits, fileInfo, tagInfo.c_ReparseTag) |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
return result |
| 850 |
|
| 851 |
@specialize.arg(0) |
| 852 |
def win32_attributes_to_mode(win32traits, attributes): |
| 853 |
m = 0 |
| 854 |
attributes = widen(attributes) |
| 855 |
if attributes & win32traits.FILE_ATTRIBUTE_DIRECTORY: |
| 856 |
m |= win32traits._S_IFDIR | 0111 |
| 857 |
else: |
| 858 |
m |= win32traits._S_IFREG |
| 859 |
if attributes & win32traits.FILE_ATTRIBUTE_READONLY: |
| 860 |
m |= 0444 |
| 861 |
else: |
| 862 |
m |= 0666 |
| 863 |
return m |
| 864 |
|
| 865 |
@specialize.arg(0) |
| 866 |
def win32_by_handle_info_to_stat(win32traits, info, reparse_tag): |
| 867 |
st_mode = win32_attributes_to_mode(win32traits, info.c_dwFileAttributes) |
| 868 |
st_size = make_longlong(info.c_nFileSizeHigh, info.c_nFileSizeLow) |
| 869 |
ctime, extra_ctime = FILE_TIME_to_time_t_nsec(info.c_ftCreationTime) |
| 870 |
mtime, extra_mtime = FILE_TIME_to_time_t_nsec(info.c_ftLastWriteTime) |
| 871 |
atime, extra_atime = FILE_TIME_to_time_t_nsec(info.c_ftLastAccessTime) |
| 872 |
|
| 873 |
|
| 874 |
st_ino = make_longlong(info.c_nFileIndexHigh, info.c_nFileIndexLow) |
| 875 |
st_dev = info.c_dwVolumeSerialNumber |
| 876 |
st_nlink = info.c_nNumberOfLinks |
| 877 |
st_file_attributes = widen(info.c_dwFileAttributes) |
| 878 |
st_reparse_tag = widen(reparse_tag) |
| 879 |
if (st_file_attributes & win32traits.FILE_ATTRIBUTE_REPARSE_POINT |
| 880 |
and st_reparse_tag == 0xa000000c): |
| 881 |
|
| 882 |
st_mode ^= (st_mode & 0170000) |
| 883 |
|
| 884 |
st_mode |= win32traits._S_IFLNK |
| 885 |
|
| 886 |
result = (st_mode, |
| 887 |
st_ino, st_dev, st_nlink, 0, 0, |
| 888 |
st_size, |
| 889 |
atime, mtime, ctime, |
| 890 |
extra_atime, extra_mtime, extra_ctime, |
| 891 |
st_file_attributes, st_reparse_tag) |
| 892 |
|
| 893 |
return make_stat_result(result) |
| 894 |
|
| 895 |
@specialize.arg(0) |
| 896 |
def win32_attributes_from_dir(traits, path, info, tagInfo): |
| 897 |
with lltype.scoped_alloc(traits.WIN32_FIND_DATA) as filedata: |
| 898 |
with rffi.scoped_utf82wcharp(path, codepoints_in_utf8(path)) as buf: |
| 899 |
hFindFile = traits.FindFirstFile(buf, filedata) |
| 900 |
if hFindFile == rwin32.INVALID_HANDLE_VALUE: |
| 901 |
return 0 |
| 902 |
traits.FindClose(hFindFile) |
| 903 |
tagInfo.c_ReparseTag = win32_find_data_to_file_info(traits, filedata, info) |
| 904 |
return 1 |
| 905 |
|
| 906 |
@specialize.arg(0) |
| 907 |
def win32_find_data_to_file_info(traits, filedata, info): |
| 908 |
info.c_dwFileAttributes = filedata.c_dwFileAttributes |
| 909 |
rffi.structcopy(info.c_ftCreationTime, filedata.c_ftCreationTime) |
| 910 |
rffi.structcopy(info.c_ftLastAccessTime, filedata.c_ftLastAccessTime) |
| 911 |
rffi.structcopy(info.c_ftLastWriteTime, filedata.c_ftLastWriteTime) |
| 912 |
info.c_nFileSizeHigh = filedata.c_nFileSizeHigh |
| 913 |
info.c_nFileSizeLow = filedata.c_nFileSizeLow |
| 914 |
attr = widen(filedata.c_dwFileAttributes) |
| 915 |
if attr & traits.FILE_ATTRIBUTE_REPARSE_POINT: |
| 916 |
return filedata.c_dwReserved0 |
| 917 |
return 0 |