| 1 |
from rpython.rlib import rposix, rwin32 |
| 2 |
from rpython.rlib.objectmodel import specialize |
| 3 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 4 |
from rpython.rlib.rarithmetic import intmask |
| 5 |
|
| 6 |
|
| 7 |
if not rwin32.WIN32: |
| 8 |
@specialize.argtype(0) |
| 9 |
def opendir(path, lgt): |
| 10 |
|
| 11 |
path = rposix._as_bytes0(path) |
| 12 |
return opendir_bytes(path) |
| 13 |
|
| 14 |
def opendir_bytes(path): |
| 15 |
dirp = rposix.c_opendir(path) |
| 16 |
if not dirp: |
| 17 |
raise OSError(rposix.get_saved_errno(), "opendir failed") |
| 18 |
return dirp |
| 19 |
|
| 20 |
def closedir(dirp): |
| 21 |
rposix.c_closedir(dirp) |
| 22 |
|
| 23 |
NULL_DIRP = lltype.nullptr(rposix.DIRP.TO) |
| 24 |
|
| 25 |
def nextentry(dirp): |
| 26 |
"""Read the next entry and returns an opaque object. |
| 27 |
Use the methods has_xxx() and get_xxx() to read from that |
| 28 |
opaque object. The opaque object is valid until the next |
| 29 |
time nextentry() or closedir() is called. This may raise |
| 30 |
OSError, or return a NULL pointer when exhausted. Note |
| 31 |
that this doesn't filter out the "." and ".." entries. |
| 32 |
""" |
| 33 |
direntp = rposix.c_readdir(dirp) |
| 34 |
if direntp: |
| 35 |
error = rposix.get_saved_errno() |
| 36 |
if error: |
| 37 |
raise OSError(error, "readdir failed") |
| 38 |
return direntp |
| 39 |
|
| 40 |
def get_name_bytes(direntp): |
| 41 |
namep = rffi.cast(rffi.CCHARP, direntp.c_d_name) |
| 42 |
return rffi.charp2str(namep) |
| 43 |
|
| 44 |
DT_UNKNOWN = rposix.dirent_config.get('DT_UNKNOWN', 0) |
| 45 |
DT_REG = rposix.dirent_config.get('DT_REG', 255) |
| 46 |
DT_DIR = rposix.dirent_config.get('DT_DIR', 255) |
| 47 |
DT_LNK = rposix.dirent_config.get('DT_LNK', 255) |
| 48 |
|
| 49 |
def get_known_type(direntp): |
| 50 |
if rposix.HAVE_D_TYPE: |
| 51 |
return rffi.getintfield(direntp, 'c_d_type') |
| 52 |
return DT_UNKNOWN |
| 53 |
|
| 54 |
def get_inode(direntp): |
| 55 |
return rffi.getintfield(direntp, 'c_d_ino') |
| 56 |
|
| 57 |
else: |
| 58 |
|
| 59 |
import stat |
| 60 |
from rpython.rlib._os_support import utf8_traits |
| 61 |
from rpython.rlib.rwin32file import make_win32_traits |
| 62 |
from rpython.rlib import rposix_stat |
| 63 |
|
| 64 |
win32traits = make_win32_traits(utf8_traits) |
| 65 |
|
| 66 |
|
| 67 |
SCANDIRP = lltype.Ptr(lltype.Struct('SCANDIRP', |
| 68 |
('filedata', win32traits.WIN32_FIND_DATA), |
| 69 |
('hFindFile', rwin32.HANDLE), |
| 70 |
('first_time', lltype.Bool), |
| 71 |
)) |
| 72 |
NULL_DIRP = lltype.nullptr(SCANDIRP.TO) |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
def opendir(path, lgt): |
| 77 |
if lgt == 0: |
| 78 |
path = '.' |
| 79 |
if path[-1] not in ('\\', '/', ':'): |
| 80 |
mask = path + '\\*.*' |
| 81 |
lgt += 4 |
| 82 |
else: |
| 83 |
mask = path + '*.*' |
| 84 |
lgt += 3 |
| 85 |
dirp = lltype.malloc(SCANDIRP.TO, flavor='raw') |
| 86 |
with rffi.scoped_utf82wcharp(mask, lgt) as src_buf: |
| 87 |
hFindFile = win32traits.FindFirstFile(src_buf, dirp.filedata) |
| 88 |
if hFindFile == rwin32.INVALID_HANDLE_VALUE: |
| 89 |
error = rwin32.GetLastError_saved() |
| 90 |
lltype.free(dirp, flavor='raw') |
| 91 |
raise WindowsError(error, "FindFirstFileW failed") |
| 92 |
dirp.hFindFile = hFindFile |
| 93 |
dirp.first_time = True |
| 94 |
return dirp |
| 95 |
|
| 96 |
def closedir(dirp): |
| 97 |
if dirp.hFindFile != rwin32.INVALID_HANDLE_VALUE: |
| 98 |
win32traits.FindClose(dirp.hFindFile) |
| 99 |
lltype.free(dirp, flavor='raw') |
| 100 |
|
| 101 |
def nextentry(dirp): |
| 102 |
"""Read the next entry and returns an opaque object. |
| 103 |
Use the methods has_xxx() and get_xxx() to read from that |
| 104 |
opaque object. The opaque object is valid until the next |
| 105 |
time nextentry() or closedir() is called. This may raise |
| 106 |
WindowsError, or return NULL when exhausted. Note |
| 107 |
that this doesn't filter out the "." and ".." entries. |
| 108 |
""" |
| 109 |
if dirp.first_time: |
| 110 |
dirp.first_time = False |
| 111 |
else: |
| 112 |
if not win32traits.FindNextFile(dirp.hFindFile, dirp.filedata): |
| 113 |
|
| 114 |
error = rwin32.GetLastError_saved() |
| 115 |
if error == win32traits.ERROR_NO_MORE_FILES: |
| 116 |
return lltype.nullptr(win32traits.WIN32_FIND_DATA) |
| 117 |
raise WindowsError(error, "FindNextFileW failed") |
| 118 |
return dirp.filedata |
| 119 |
|
| 120 |
def get_name_unicode(filedata): |
| 121 |
return utf8_traits.charp2str(rffi.cast(utf8_traits.CCHARP, |
| 122 |
filedata.c_cFileName)) |
| 123 |
|
| 124 |
def get_name_bytes(filedata): |
| 125 |
wcharp = rffi.cast(utf8_traits.CCHARP, filedata.c_cFileName) |
| 126 |
utf8, i = rffi.wcharp2utf8(wcharp) |
| 127 |
return utf8 |
| 128 |
|
| 129 |
def get_known_type(filedata): |
| 130 |
attr = filedata.c_dwFileAttributes |
| 131 |
st_mode = rposix_stat.win32_attributes_to_mode(win32traits, attr) |
| 132 |
return stat.S_IFMT(st_mode) |
| 133 |
|
| 134 |
def get_inode(filedata): |
| 135 |
return None |