| 1 |
import os, sys, uuid |
| 2 |
import py |
| 3 |
from rpython.rlib import rposix_stat |
| 4 |
from rpython.tool.udir import udir |
| 5 |
from rpython.translator.c.test.test_genc import compile |
| 6 |
from rpython.rtyper.lltypesystem import lltype |
| 7 |
|
| 8 |
|
| 9 |
class TestPosixStatFunctions: |
| 10 |
@py.test.mark.skipif("sys.platform == 'win32'", |
| 11 |
reason="win32 only has the portable fields") |
| 12 |
def test_has_all_fields(self): |
| 13 |
if sys.platform == "darwin": |
| 14 |
assert rposix_stat.STAT_FIELDS == rposix_stat.ALL_STAT_FIELDS |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
else: |
| 19 |
assert rposix_stat.STAT_FIELDS == ( |
| 20 |
rposix_stat.ALL_STAT_FIELDS[:13] + |
| 21 |
rposix_stat.ALL_STAT_FIELDS[-3:]) |
| 22 |
|
| 23 |
def test_stat(self): |
| 24 |
def check(f): |
| 25 |
|
| 26 |
expected = int(os.stat(f).st_mtime * 1000) |
| 27 |
assert abs(int(rposix_stat.stat(f).st_mtime * 1000) - expected) < 2 |
| 28 |
assert abs(int(rposix_stat.stat(unicode(f)).st_mtime * 1000) - expected) < 2 |
| 29 |
|
| 30 |
if sys.platform == 'win32': |
| 31 |
check(os.environ['SYSTEMDRIVE']) |
| 32 |
check(os.environ['PROGRAMDATA']) |
| 33 |
else: |
| 34 |
check('/') |
| 35 |
check('/dev') |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
check(sys.executable) |
| 40 |
|
| 41 |
def test_fstat(self): |
| 42 |
stat = rposix_stat.fstat(0) |
| 43 |
assert stat.st_mode != 0 |
| 44 |
|
| 45 |
def test_stat_large_number(self): |
| 46 |
fname = udir.join('test_stat_large_number.txt') |
| 47 |
fname.ensure() |
| 48 |
t1 = 5000000000.0 |
| 49 |
try: |
| 50 |
os.utime(str(fname), (t1, t1)) |
| 51 |
except OverflowError: |
| 52 |
py.test.skip("This platform doesn't support setting stat times " |
| 53 |
"to large values") |
| 54 |
assert rposix_stat.stat(str(fname)).st_mtime == t1 |
| 55 |
|
| 56 |
@py.test.mark.skipif(not hasattr(os, 'statvfs'), |
| 57 |
reason='posix specific function') |
| 58 |
def test_statvfs(self): |
| 59 |
try: |
| 60 |
os.statvfs('.') |
| 61 |
except OSError as e: |
| 62 |
py.test.skip("the underlying os.statvfs() failed: %s" % e) |
| 63 |
rposix_stat.statvfs('.') |
| 64 |
|
| 65 |
@py.test.mark.skipif(not hasattr(os, 'fstatvfs'), |
| 66 |
reason='posix specific function') |
| 67 |
def test_fstatvfs(self): |
| 68 |
try: |
| 69 |
os.fstatvfs(0) |
| 70 |
except OSError as e: |
| 71 |
py.test.skip("the underlying os.fstatvfs() failed: %s" % e) |
| 72 |
rposix_stat.fstatvfs(0) |
| 73 |
|
| 74 |
@py.test.mark.skipif(sys.platform != 'win32', reason='win32 test') |
| 75 |
def test_stat3_ino_dev(self): |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
st = rposix_stat.stat3('C:\\') |
| 80 |
assert st.st_dev != 0 and st.st_ino != 0 |
| 81 |
assert st.st_file_attributes & 0x16 |
| 82 |
assert st.st_reparse_tag == 0 |
| 83 |
st2 = rposix_stat.lstat3('C:\\') |
| 84 |
assert (st2.st_dev, st2.st_ino) == (st.st_dev, st.st_ino) |
| 85 |
|
| 86 |
@py.test.mark.skipif("not hasattr(rposix_stat, 'fstatat')") |
| 87 |
def test_fstatat(tmpdir): |
| 88 |
tmpdir.join('file').write('text') |
| 89 |
dirfd = os.open(str(tmpdir), os.O_RDONLY) |
| 90 |
try: |
| 91 |
result = rposix_stat.fstatat('file', dir_fd=dirfd, follow_symlinks=False) |
| 92 |
finally: |
| 93 |
os.close(dirfd) |
| 94 |
assert result.st_atime == tmpdir.join('file').atime() |
| 95 |
|
| 96 |
@py.test.mark.skipif('sys.platform == "darwin"') |
| 97 |
def test_high_precision_stat_time(): |
| 98 |
|
| 99 |
|
| 100 |
dirname = os.path.abspath(os.path.join(".", "test" + uuid.uuid1().hex)) |
| 101 |
os.mkdir(dirname) |
| 102 |
|
| 103 |
def f(): |
| 104 |
st = os.stat(dirname) |
| 105 |
|
| 106 |
|
| 107 |
highprec = rposix_stat.get_stat_ns_as_bigint(st, "ctime") |
| 108 |
return '%s;%s' % (st.st_ctime, highprec.str()) |
| 109 |
|
| 110 |
try: |
| 111 |
fc = compile(f, []) |
| 112 |
as_string = fc() |
| 113 |
asfloat, highprec = as_string.split(';') |
| 114 |
asfloat = float(asfloat) |
| 115 |
highprec = int(highprec) |
| 116 |
st = os.stat(dirname) |
| 117 |
assert abs(asfloat - st.st_ctime) < 500e-9 |
| 118 |
assert abs(highprec - int(st.st_ctime * 1e9)) < 500 |
| 119 |
assert abs(rposix_stat.get_stat_ns_as_bigint(st, "ctime").tolong() |
| 120 |
- st.st_ctime * 1e9) < 3 |
| 121 |
if rposix_stat.TIMESPEC is not None: |
| 122 |
with lltype.scoped_alloc(rposix_stat.STAT_STRUCT.TO) as stresult: |
| 123 |
rposix_stat.c_stat(".", stresult) |
| 124 |
if sys.platform == "darwin": |
| 125 |
assert 0 <= stresult.c_st_ctimespec.c_tv_nsec <= 999999999 |
| 126 |
assert highprec == (int(stresult.c_st_ctimespec.c_tv_sec) * 1000000000 |
| 127 |
+ int(stresult.c_st_ctimespec.c_tv_nsec)) |
| 128 |
else: |
| 129 |
assert 0 <= stresult.c_st_ctim.c_tv_nsec <= 999999999 |
| 130 |
assert highprec == (int(stresult.c_st_ctim.c_tv_sec) * 1000000000 |
| 131 |
+ int(stresult.c_st_ctim.c_tv_nsec)) |
| 132 |
finally: |
| 133 |
os.rmdir(dirname) |