_scandir.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  1. /* C speedups for scandir module
  2. This is divided into four sections (each prefixed with a "SECTION:"
  3. comment):
  4. 1) Python 2/3 compatibility
  5. 2) Helper utilities from posixmodule.c, fileutils.h, etc
  6. 3) SECTION: Main DirEntry and scandir implementation, taken from
  7. Python 3.5's posixmodule.c
  8. 4) Module and method definitions and initialization code
  9. */
  10. #include <Python.h>
  11. #include <structseq.h>
  12. #include <structmember.h>
  13. #include "osdefs.h"
  14. #ifdef MS_WINDOWS
  15. #include <windows.h>
  16. #include <winioctl.h>
  17. #include "winreparse.h"
  18. #else
  19. #include <dirent.h>
  20. #ifndef HAVE_DIRENT_H
  21. #define HAVE_DIRENT_H 1
  22. #endif
  23. #endif
  24. #define MODNAME "scandir"
  25. /* SECTION: Python 2/3 compatibility */
  26. #if PY_MAJOR_VERSION >= 3
  27. #define INIT_ERROR return NULL
  28. #else
  29. #define INIT_ERROR return
  30. // Because on PyPy, Py_FileSystemDefaultEncoding is (was) defined to be NULL
  31. // (see PyPy Bitbucket issue #2669)
  32. #define FS_ENCODING (Py_FileSystemDefaultEncoding ? Py_FileSystemDefaultEncoding : "UTF-8")
  33. #endif
  34. #if PY_MAJOR_VERSION < 3 || PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 2
  35. #define _Py_IDENTIFIER(name) static char * PyId_##name = #name;
  36. #define _PyObject_GetAttrId(obj, pyid_name) PyObject_GetAttrString((obj), *(pyid_name))
  37. #define PyExc_FileNotFoundError PyExc_OSError
  38. #define PyUnicode_AsUnicodeAndSize(unicode, addr_length) \
  39. PyUnicode_AsUnicode(unicode); *(addr_length) = PyUnicode_GetSize(unicode)
  40. #endif
  41. // Because on PyPy not working without
  42. #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION > 2 && defined(PYPY_VERSION_NUM)
  43. #define _Py_IDENTIFIER(name) static char * PyId_##name = #name;
  44. #define _PyObject_GetAttrId(obj, pyid_name) PyObject_GetAttrString((obj), *(pyid_name))
  45. #endif
  46. /* SECTION: Helper utilities from posixmodule.c, fileutils.h, etc */
  47. #if !defined(MS_WINDOWS) && defined(DT_UNKNOWN)
  48. #define HAVE_DIRENT_D_TYPE 1
  49. #endif
  50. #ifdef HAVE_DIRENT_H
  51. #include <dirent.h>
  52. #define NAMLEN(dirent) strlen((dirent)->d_name)
  53. #else
  54. #if defined(__WATCOMC__) && !defined(__QNX__)
  55. #include <direct.h>
  56. #define NAMLEN(dirent) strlen((dirent)->d_name)
  57. #else
  58. #define dirent direct
  59. #define NAMLEN(dirent) (dirent)->d_namlen
  60. #endif
  61. #ifdef HAVE_SYS_NDIR_H
  62. #include <sys/ndir.h>
  63. #endif
  64. #ifdef HAVE_SYS_DIR_H
  65. #include <sys/dir.h>
  66. #endif
  67. #ifdef HAVE_NDIR_H
  68. #include <ndir.h>
  69. #endif
  70. #endif
  71. #ifndef Py_CLEANUP_SUPPORTED
  72. #define Py_CLEANUP_SUPPORTED 0x20000
  73. #endif
  74. #ifndef S_IFLNK
  75. /* Windows doesn't define S_IFLNK but posixmodule.c maps
  76. * IO_REPARSE_TAG_SYMLINK to S_IFLNK */
  77. # define S_IFLNK 0120000
  78. #endif
  79. // _Py_stat_struct is already defined in fileutils.h on Python 3.5+
  80. // But not in PyPy
  81. #if PY_MAJOR_VERSION < 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 5) || defined(PYPY_VERSION_NUM)
  82. #ifdef MS_WINDOWS
  83. struct _Py_stat_struct {
  84. unsigned long st_dev;
  85. unsigned __int64 st_ino;
  86. unsigned short st_mode;
  87. int st_nlink;
  88. int st_uid;
  89. int st_gid;
  90. unsigned long st_rdev;
  91. __int64 st_size;
  92. time_t st_atime;
  93. int st_atime_nsec;
  94. time_t st_mtime;
  95. int st_mtime_nsec;
  96. time_t st_ctime;
  97. int st_ctime_nsec;
  98. unsigned long st_file_attributes;
  99. };
  100. #else
  101. # define _Py_stat_struct stat
  102. #endif
  103. #endif
  104. /* choose the appropriate stat and fstat functions and return structs */
  105. #undef STAT
  106. #undef FSTAT
  107. #undef STRUCT_STAT
  108. #ifdef MS_WINDOWS
  109. # define STAT win32_stat
  110. # define LSTAT win32_lstat
  111. # define FSTAT _Py_fstat_noraise
  112. # define STRUCT_STAT struct _Py_stat_struct
  113. #else
  114. # define STAT stat
  115. # define LSTAT lstat
  116. # define FSTAT fstat
  117. # define STRUCT_STAT struct stat
  118. #endif
  119. #ifdef MS_WINDOWS
  120. static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
  121. static void
  122. FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
  123. {
  124. /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
  125. /* Cannot simply cast and dereference in_ptr,
  126. since it might not be aligned properly */
  127. __int64 in;
  128. memcpy(&in, in_ptr, sizeof(in));
  129. *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
  130. *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
  131. }
  132. /* Below, we *know* that ugo+r is 0444 */
  133. #if _S_IREAD != 0400
  134. #error Unsupported C library
  135. #endif
  136. static int
  137. attributes_to_mode(DWORD attr)
  138. {
  139. int m = 0;
  140. if (attr & FILE_ATTRIBUTE_DIRECTORY)
  141. m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
  142. else
  143. m |= _S_IFREG;
  144. if (attr & FILE_ATTRIBUTE_READONLY)
  145. m |= 0444;
  146. else
  147. m |= 0666;
  148. return m;
  149. }
  150. void
  151. _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
  152. struct _Py_stat_struct *result)
  153. {
  154. memset(result, 0, sizeof(*result));
  155. result->st_mode = attributes_to_mode(info->dwFileAttributes);
  156. result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
  157. result->st_dev = info->dwVolumeSerialNumber;
  158. result->st_rdev = result->st_dev;
  159. FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
  160. FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
  161. FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
  162. result->st_nlink = info->nNumberOfLinks;
  163. result->st_ino = (((unsigned __int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
  164. if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
  165. /* first clear the S_IFMT bits */
  166. result->st_mode ^= (result->st_mode & S_IFMT);
  167. /* now set the bits that make this a symlink */
  168. result->st_mode |= S_IFLNK;
  169. }
  170. result->st_file_attributes = info->dwFileAttributes;
  171. }
  172. static BOOL
  173. get_target_path(HANDLE hdl, wchar_t **target_path)
  174. {
  175. int buf_size, result_length;
  176. wchar_t *buf;
  177. /* We have a good handle to the target, use it to determine
  178. the target path name (then we'll call lstat on it). */
  179. buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
  180. VOLUME_NAME_DOS);
  181. if(!buf_size)
  182. return FALSE;
  183. buf = PyMem_New(wchar_t, buf_size+1);
  184. if (!buf) {
  185. SetLastError(ERROR_OUTOFMEMORY);
  186. return FALSE;
  187. }
  188. result_length = GetFinalPathNameByHandleW(hdl,
  189. buf, buf_size, VOLUME_NAME_DOS);
  190. if(!result_length) {
  191. PyMem_Free(buf);
  192. return FALSE;
  193. }
  194. if(!CloseHandle(hdl)) {
  195. PyMem_Free(buf);
  196. return FALSE;
  197. }
  198. buf[result_length] = 0;
  199. *target_path = buf;
  200. return TRUE;
  201. }
  202. static int
  203. win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
  204. {
  205. char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
  206. REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
  207. DWORD n_bytes_returned;
  208. if (0 == DeviceIoControl(
  209. reparse_point_handle,
  210. FSCTL_GET_REPARSE_POINT,
  211. NULL, 0, /* in buffer */
  212. target_buffer, sizeof(target_buffer),
  213. &n_bytes_returned,
  214. NULL)) /* we're not using OVERLAPPED_IO */
  215. return FALSE;
  216. if (reparse_tag)
  217. *reparse_tag = rdb->ReparseTag;
  218. return TRUE;
  219. }
  220. static void
  221. find_data_to_file_info_w(WIN32_FIND_DATAW *pFileData,
  222. BY_HANDLE_FILE_INFORMATION *info,
  223. ULONG *reparse_tag)
  224. {
  225. memset(info, 0, sizeof(*info));
  226. info->dwFileAttributes = pFileData->dwFileAttributes;
  227. info->ftCreationTime = pFileData->ftCreationTime;
  228. info->ftLastAccessTime = pFileData->ftLastAccessTime;
  229. info->ftLastWriteTime = pFileData->ftLastWriteTime;
  230. info->nFileSizeHigh = pFileData->nFileSizeHigh;
  231. info->nFileSizeLow = pFileData->nFileSizeLow;
  232. /* info->nNumberOfLinks = 1; */
  233. if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
  234. *reparse_tag = pFileData->dwReserved0;
  235. else
  236. *reparse_tag = 0;
  237. }
  238. static BOOL
  239. attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
  240. {
  241. HANDLE hFindFile;
  242. WIN32_FIND_DATAW FileData;
  243. hFindFile = FindFirstFileW(pszFile, &FileData);
  244. if (hFindFile == INVALID_HANDLE_VALUE)
  245. return FALSE;
  246. FindClose(hFindFile);
  247. find_data_to_file_info_w(&FileData, info, reparse_tag);
  248. return TRUE;
  249. }
  250. static int
  251. win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result,
  252. BOOL traverse)
  253. {
  254. int code;
  255. HANDLE hFile, hFile2;
  256. BY_HANDLE_FILE_INFORMATION info;
  257. ULONG reparse_tag = 0;
  258. wchar_t *target_path;
  259. const wchar_t *dot;
  260. hFile = CreateFileW(
  261. path,
  262. FILE_READ_ATTRIBUTES, /* desired access */
  263. 0, /* share mode */
  264. NULL, /* security attributes */
  265. OPEN_EXISTING,
  266. /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
  267. /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
  268. Because of this, calls like GetFinalPathNameByHandle will return
  269. the symlink path again and not the actual final path. */
  270. FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
  271. FILE_FLAG_OPEN_REPARSE_POINT,
  272. NULL);
  273. if (hFile == INVALID_HANDLE_VALUE) {
  274. /* Either the target doesn't exist, or we don't have access to
  275. get a handle to it. If the former, we need to return an error.
  276. If the latter, we can use attributes_from_dir. */
  277. if (GetLastError() != ERROR_SHARING_VIOLATION)
  278. return -1;
  279. /* Could not get attributes on open file. Fall back to
  280. reading the directory. */
  281. if (!attributes_from_dir_w(path, &info, &reparse_tag))
  282. /* Very strange. This should not fail now */
  283. return -1;
  284. if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
  285. if (traverse) {
  286. /* Should traverse, but could not open reparse point handle */
  287. SetLastError(ERROR_SHARING_VIOLATION);
  288. return -1;
  289. }
  290. }
  291. } else {
  292. if (!GetFileInformationByHandle(hFile, &info)) {
  293. CloseHandle(hFile);
  294. return -1;
  295. }
  296. if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
  297. if (!win32_get_reparse_tag(hFile, &reparse_tag))
  298. return -1;
  299. /* Close the outer open file handle now that we're about to
  300. reopen it with different flags. */
  301. if (!CloseHandle(hFile))
  302. return -1;
  303. if (traverse) {
  304. /* In order to call GetFinalPathNameByHandle we need to open
  305. the file without the reparse handling flag set. */
  306. hFile2 = CreateFileW(
  307. path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
  308. NULL, OPEN_EXISTING,
  309. FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
  310. NULL);
  311. if (hFile2 == INVALID_HANDLE_VALUE)
  312. return -1;
  313. if (!get_target_path(hFile2, &target_path))
  314. return -1;
  315. code = win32_xstat_impl_w(target_path, result, FALSE);
  316. PyMem_Free(target_path);
  317. return code;
  318. }
  319. } else
  320. CloseHandle(hFile);
  321. }
  322. _Py_attribute_data_to_stat(&info, reparse_tag, result);
  323. /* Set S_IEXEC if it is an .exe, .bat, ... */
  324. dot = wcsrchr(path, '.');
  325. if (dot) {
  326. if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
  327. _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
  328. result->st_mode |= 0111;
  329. }
  330. return 0;
  331. }
  332. static int
  333. win32_xstat_w(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
  334. {
  335. /* Protocol violation: we explicitly clear errno, instead of
  336. setting it to a POSIX error. Callers should use GetLastError. */
  337. int code = win32_xstat_impl_w(path, result, traverse);
  338. errno = 0;
  339. return code;
  340. }
  341. static int
  342. win32_lstat_w(const wchar_t* path, struct _Py_stat_struct *result)
  343. {
  344. return win32_xstat_w(path, result, FALSE);
  345. }
  346. static int
  347. win32_stat_w(const wchar_t* path, struct _Py_stat_struct *result)
  348. {
  349. return win32_xstat_w(path, result, TRUE);
  350. }
  351. #endif /* MS_WINDOWS */
  352. static PyTypeObject StatResultType;
  353. static PyObject *billion = NULL;
  354. static newfunc structseq_new;
  355. static PyObject *
  356. statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  357. {
  358. PyStructSequence *result;
  359. int i;
  360. result = (PyStructSequence*)structseq_new(type, args, kwds);
  361. if (!result)
  362. return NULL;
  363. /* If we have been initialized from a tuple,
  364. st_?time might be set to None. Initialize it
  365. from the int slots. */
  366. for (i = 7; i <= 9; i++) {
  367. if (result->ob_item[i+3] == Py_None) {
  368. Py_DECREF(Py_None);
  369. Py_INCREF(result->ob_item[i]);
  370. result->ob_item[i+3] = result->ob_item[i];
  371. }
  372. }
  373. return (PyObject*)result;
  374. }
  375. /* If true, st_?time is float. */
  376. static int _stat_float_times = 1;
  377. static void
  378. fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
  379. {
  380. #if SIZEOF_TIME_T > SIZEOF_LONG
  381. PyObject *s = PyLong_FromLongLong((PY_LONG_LONG)sec);
  382. #else
  383. #if PY_MAJOR_VERSION >= 3
  384. PyObject *s = PyLong_FromLong((long)sec);
  385. #else
  386. PyObject *s = PyInt_FromLong((long)sec);
  387. #endif
  388. #endif
  389. PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
  390. PyObject *s_in_ns = NULL;
  391. PyObject *ns_total = NULL;
  392. PyObject *float_s = NULL;
  393. if (!(s && ns_fractional))
  394. goto exit;
  395. s_in_ns = PyNumber_Multiply(s, billion);
  396. if (!s_in_ns)
  397. goto exit;
  398. ns_total = PyNumber_Add(s_in_ns, ns_fractional);
  399. if (!ns_total)
  400. goto exit;
  401. if (_stat_float_times) {
  402. float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
  403. if (!float_s)
  404. goto exit;
  405. }
  406. else {
  407. float_s = s;
  408. Py_INCREF(float_s);
  409. }
  410. PyStructSequence_SET_ITEM(v, index, s);
  411. PyStructSequence_SET_ITEM(v, index+3, float_s);
  412. PyStructSequence_SET_ITEM(v, index+6, ns_total);
  413. s = NULL;
  414. float_s = NULL;
  415. ns_total = NULL;
  416. exit:
  417. Py_XDECREF(s);
  418. Py_XDECREF(ns_fractional);
  419. Py_XDECREF(s_in_ns);
  420. Py_XDECREF(ns_total);
  421. Py_XDECREF(float_s);
  422. }
  423. #ifdef MS_WINDOWS
  424. #define HAVE_STAT_NSEC 1
  425. #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
  426. #endif
  427. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  428. #define ST_BLKSIZE_IDX 16
  429. #else
  430. #define ST_BLKSIZE_IDX 15
  431. #endif
  432. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  433. #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
  434. #else
  435. #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
  436. #endif
  437. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  438. #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
  439. #else
  440. #define ST_RDEV_IDX ST_BLOCKS_IDX
  441. #endif
  442. #ifdef HAVE_STRUCT_STAT_ST_FLAGS
  443. #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
  444. #else
  445. #define ST_FLAGS_IDX ST_RDEV_IDX
  446. #endif
  447. #ifdef HAVE_STRUCT_STAT_ST_GEN
  448. #define ST_GEN_IDX (ST_FLAGS_IDX+1)
  449. #else
  450. #define ST_GEN_IDX ST_FLAGS_IDX
  451. #endif
  452. #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
  453. #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
  454. #else
  455. #define ST_BIRTHTIME_IDX ST_GEN_IDX
  456. #endif
  457. #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
  458. #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
  459. #else
  460. #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
  461. #endif
  462. #ifdef HAVE_LONG_LONG
  463. # define _PyLong_FromDev PyLong_FromLongLong
  464. #else
  465. # define _PyLong_FromDev PyLong_FromLong
  466. #endif
  467. #ifndef MS_WINDOWS
  468. PyObject *
  469. _PyLong_FromUid(uid_t uid)
  470. {
  471. if (uid == (uid_t)-1)
  472. return PyLong_FromLong(-1);
  473. return PyLong_FromUnsignedLong(uid);
  474. }
  475. PyObject *
  476. _PyLong_FromGid(gid_t gid)
  477. {
  478. if (gid == (gid_t)-1)
  479. return PyLong_FromLong(-1);
  480. return PyLong_FromUnsignedLong(gid);
  481. }
  482. #endif
  483. /* pack a system stat C structure into the Python stat tuple
  484. (used by posix_stat() and posix_fstat()) */
  485. static PyObject*
  486. _pystat_fromstructstat(STRUCT_STAT *st)
  487. {
  488. unsigned long ansec, mnsec, cnsec;
  489. PyObject *v = PyStructSequence_New(&StatResultType);
  490. if (v == NULL)
  491. return NULL;
  492. PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
  493. #ifdef HAVE_LARGEFILE_SUPPORT
  494. PyStructSequence_SET_ITEM(v, 1,
  495. PyLong_FromUnsignedLongLong(st->st_ino));
  496. #else
  497. PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLong((unsigned long)st->st_ino));
  498. #endif
  499. #ifdef MS_WINDOWS
  500. PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
  501. #else
  502. PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
  503. #endif
  504. PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
  505. #if defined(MS_WINDOWS)
  506. PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
  507. PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
  508. #else
  509. PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
  510. PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
  511. #endif
  512. #ifdef HAVE_LARGEFILE_SUPPORT
  513. PyStructSequence_SET_ITEM(v, 6,
  514. PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
  515. #else
  516. PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
  517. #endif
  518. #if defined(HAVE_STAT_TV_NSEC)
  519. ansec = st->st_atim.tv_nsec;
  520. mnsec = st->st_mtim.tv_nsec;
  521. cnsec = st->st_ctim.tv_nsec;
  522. #elif defined(HAVE_STAT_TV_NSEC2)
  523. ansec = st->st_atimespec.tv_nsec;
  524. mnsec = st->st_mtimespec.tv_nsec;
  525. cnsec = st->st_ctimespec.tv_nsec;
  526. #elif defined(HAVE_STAT_NSEC)
  527. ansec = st->st_atime_nsec;
  528. mnsec = st->st_mtime_nsec;
  529. cnsec = st->st_ctime_nsec;
  530. #else
  531. ansec = mnsec = cnsec = 0;
  532. #endif
  533. fill_time(v, 7, st->st_atime, ansec);
  534. fill_time(v, 8, st->st_mtime, mnsec);
  535. fill_time(v, 9, st->st_ctime, cnsec);
  536. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  537. PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
  538. PyLong_FromLong((long)st->st_blksize));
  539. #endif
  540. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  541. PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
  542. PyLong_FromLong((long)st->st_blocks));
  543. #endif
  544. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  545. PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
  546. PyLong_FromLong((long)st->st_rdev));
  547. #endif
  548. #ifdef HAVE_STRUCT_STAT_ST_GEN
  549. PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
  550. PyLong_FromLong((long)st->st_gen));
  551. #endif
  552. #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
  553. {
  554. PyObject *val;
  555. unsigned long bsec,bnsec;
  556. bsec = (long)st->st_birthtime;
  557. #ifdef HAVE_STAT_TV_NSEC2
  558. bnsec = st->st_birthtimespec.tv_nsec;
  559. #else
  560. bnsec = 0;
  561. #endif
  562. if (_stat_float_times) {
  563. val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
  564. } else {
  565. val = PyLong_FromLong((long)bsec);
  566. }
  567. PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
  568. val);
  569. }
  570. #endif
  571. #ifdef HAVE_STRUCT_STAT_ST_FLAGS
  572. PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
  573. PyLong_FromLong((long)st->st_flags));
  574. #endif
  575. #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
  576. PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
  577. PyLong_FromUnsignedLong(st->st_file_attributes));
  578. #endif
  579. if (PyErr_Occurred()) {
  580. Py_DECREF(v);
  581. return NULL;
  582. }
  583. return v;
  584. }
  585. //char *PyStructSequence_UnnamedField = "unnamed field";
  586. PyDoc_STRVAR(stat_result__doc__,
  587. "stat_result: Result from stat, fstat, or lstat.\n\n\
  588. This object may be accessed either as a tuple of\n\
  589. (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
  590. or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
  591. \n\
  592. Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
  593. or st_flags, they are available as attributes only.\n\
  594. \n\
  595. See os.stat for more information.");
  596. static PyStructSequence_Field stat_result_fields[] = {
  597. {"st_mode", "protection bits"},
  598. {"st_ino", "inode"},
  599. {"st_dev", "device"},
  600. {"st_nlink", "number of hard links"},
  601. {"st_uid", "user ID of owner"},
  602. {"st_gid", "group ID of owner"},
  603. {"st_size", "total size, in bytes"},
  604. /* The NULL is replaced with PyStructSequence_UnnamedField later. */
  605. {NULL, "integer time of last access"},
  606. {NULL, "integer time of last modification"},
  607. {NULL, "integer time of last change"},
  608. {"st_atime", "time of last access"},
  609. {"st_mtime", "time of last modification"},
  610. {"st_ctime", "time of last change"},
  611. {"st_atime_ns", "time of last access in nanoseconds"},
  612. {"st_mtime_ns", "time of last modification in nanoseconds"},
  613. {"st_ctime_ns", "time of last change in nanoseconds"},
  614. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  615. {"st_blksize", "blocksize for filesystem I/O"},
  616. #endif
  617. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  618. {"st_blocks", "number of blocks allocated"},
  619. #endif
  620. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  621. {"st_rdev", "device type (if inode device)"},
  622. #endif
  623. #ifdef HAVE_STRUCT_STAT_ST_FLAGS
  624. {"st_flags", "user defined flags for file"},
  625. #endif
  626. #ifdef HAVE_STRUCT_STAT_ST_GEN
  627. {"st_gen", "generation number"},
  628. #endif
  629. #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
  630. {"st_birthtime", "time of creation"},
  631. #endif
  632. #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
  633. {"st_file_attributes", "Windows file attribute bits"},
  634. #endif
  635. {0}
  636. };
  637. static PyStructSequence_Desc stat_result_desc = {
  638. "scandir.stat_result", /* name */
  639. stat_result__doc__, /* doc */
  640. stat_result_fields,
  641. 10
  642. };
  643. #ifdef MS_WINDOWS
  644. static int
  645. win32_warn_bytes_api()
  646. {
  647. return PyErr_WarnEx(PyExc_DeprecationWarning,
  648. "The Windows bytes API has been deprecated, "
  649. "use Unicode filenames instead",
  650. 1);
  651. }
  652. #endif
  653. typedef struct {
  654. const char *function_name;
  655. const char *argument_name;
  656. int nullable;
  657. wchar_t *wide;
  658. char *narrow;
  659. int fd;
  660. Py_ssize_t length;
  661. PyObject *object;
  662. PyObject *cleanup;
  663. } path_t;
  664. static void
  665. path_cleanup(path_t *path) {
  666. if (path->cleanup) {
  667. Py_CLEAR(path->cleanup);
  668. }
  669. }
  670. static int
  671. path_converter(PyObject *o, void *p) {
  672. path_t *path = (path_t *)p;
  673. PyObject *unicode, *bytes;
  674. Py_ssize_t length;
  675. char *narrow;
  676. #define FORMAT_EXCEPTION(exc, fmt) \
  677. PyErr_Format(exc, "%s%s" fmt, \
  678. path->function_name ? path->function_name : "", \
  679. path->function_name ? ": " : "", \
  680. path->argument_name ? path->argument_name : "path")
  681. /* Py_CLEANUP_SUPPORTED support */
  682. if (o == NULL) {
  683. path_cleanup(path);
  684. return 1;
  685. }
  686. /* ensure it's always safe to call path_cleanup() */
  687. path->cleanup = NULL;
  688. if (o == Py_None) {
  689. if (!path->nullable) {
  690. FORMAT_EXCEPTION(PyExc_TypeError,
  691. "can't specify None for %s argument");
  692. return 0;
  693. }
  694. path->wide = NULL;
  695. path->narrow = NULL;
  696. path->length = 0;
  697. path->object = o;
  698. path->fd = -1;
  699. return 1;
  700. }
  701. unicode = PyUnicode_FromObject(o);
  702. if (unicode) {
  703. #ifdef MS_WINDOWS
  704. wchar_t *wide;
  705. wide = PyUnicode_AsUnicodeAndSize(unicode, &length);
  706. if (!wide) {
  707. Py_DECREF(unicode);
  708. return 0;
  709. }
  710. if (length > 32767) {
  711. FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
  712. Py_DECREF(unicode);
  713. return 0;
  714. }
  715. if (wcslen(wide) != length) {
  716. FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character");
  717. Py_DECREF(unicode);
  718. return 0;
  719. }
  720. path->wide = wide;
  721. path->narrow = NULL;
  722. path->length = length;
  723. path->object = o;
  724. path->fd = -1;
  725. path->cleanup = unicode;
  726. return Py_CLEANUP_SUPPORTED;
  727. #else
  728. #if PY_MAJOR_VERSION >= 3
  729. if (!PyUnicode_FSConverter(unicode, &bytes))
  730. bytes = NULL;
  731. #else
  732. bytes = PyUnicode_AsEncodedString(unicode, FS_ENCODING, "strict");
  733. #endif
  734. Py_DECREF(unicode);
  735. #endif
  736. }
  737. else {
  738. PyErr_Clear();
  739. #if PY_MAJOR_VERSION >= 3
  740. if (PyObject_CheckBuffer(o)) {
  741. bytes = PyBytes_FromObject(o);
  742. }
  743. #else
  744. if (PyString_Check(o)) {
  745. bytes = o;
  746. Py_INCREF(bytes);
  747. }
  748. #endif
  749. else
  750. bytes = NULL;
  751. if (!bytes) {
  752. PyErr_Clear();
  753. }
  754. }
  755. if (!bytes) {
  756. if (!PyErr_Occurred())
  757. FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter");
  758. return 0;
  759. }
  760. #ifdef MS_WINDOWS
  761. if (win32_warn_bytes_api()) {
  762. Py_DECREF(bytes);
  763. return 0;
  764. }
  765. #endif
  766. length = PyBytes_GET_SIZE(bytes);
  767. #ifdef MS_WINDOWS
  768. if (length > MAX_PATH-1) {
  769. FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
  770. Py_DECREF(bytes);
  771. return 0;
  772. }
  773. #endif
  774. narrow = PyBytes_AS_STRING(bytes);
  775. if ((size_t)length != strlen(narrow)) {
  776. FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
  777. Py_DECREF(bytes);
  778. return 0;
  779. }
  780. path->wide = NULL;
  781. path->narrow = narrow;
  782. path->length = length;
  783. path->object = o;
  784. path->fd = -1;
  785. path->cleanup = bytes;
  786. return Py_CLEANUP_SUPPORTED;
  787. }
  788. static PyObject *
  789. path_error(path_t *path)
  790. {
  791. #ifdef MS_WINDOWS
  792. return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
  793. 0, path->object);
  794. #else
  795. return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
  796. #endif
  797. }
  798. /* SECTION: Main DirEntry and scandir implementation, taken from
  799. Python 3.5's posixmodule.c */
  800. PyDoc_STRVAR(posix_scandir__doc__,
  801. "scandir(path='.') -> iterator of DirEntry objects for given path");
  802. static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL};
  803. #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
  804. static char *follow_symlinks_format = "|$p:DirEntry.stat";
  805. #else
  806. static char *follow_symlinks_format = "|i:DirEntry.stat";
  807. #endif
  808. typedef struct {
  809. PyObject_HEAD
  810. PyObject *name;
  811. PyObject *path;
  812. PyObject *stat;
  813. PyObject *lstat;
  814. #ifdef MS_WINDOWS
  815. struct _Py_stat_struct win32_lstat;
  816. unsigned __int64 win32_file_index;
  817. int got_file_index;
  818. #if PY_MAJOR_VERSION < 3
  819. int name_path_bytes;
  820. #endif
  821. #else /* POSIX */
  822. #ifdef HAVE_DIRENT_D_TYPE
  823. unsigned char d_type;
  824. #endif
  825. ino_t d_ino;
  826. #endif
  827. } DirEntry;
  828. static void
  829. DirEntry_dealloc(DirEntry *entry)
  830. {
  831. Py_XDECREF(entry->name);
  832. Py_XDECREF(entry->path);
  833. Py_XDECREF(entry->stat);
  834. Py_XDECREF(entry->lstat);
  835. Py_TYPE(entry)->tp_free((PyObject *)entry);
  836. }
  837. /* Forward reference */
  838. static int
  839. DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
  840. /* Set exception and return -1 on error, 0 for False, 1 for True */
  841. static int
  842. DirEntry_is_symlink(DirEntry *self)
  843. {
  844. #ifdef MS_WINDOWS
  845. return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
  846. #elif defined(HAVE_DIRENT_D_TYPE)
  847. /* POSIX */
  848. if (self->d_type != DT_UNKNOWN)
  849. return self->d_type == DT_LNK;
  850. else
  851. return DirEntry_test_mode(self, 0, S_IFLNK);
  852. #else
  853. /* POSIX without d_type */
  854. return DirEntry_test_mode(self, 0, S_IFLNK);
  855. #endif
  856. }
  857. static PyObject *
  858. DirEntry_py_is_symlink(DirEntry *self)
  859. {
  860. int result;
  861. result = DirEntry_is_symlink(self);
  862. if (result == -1)
  863. return NULL;
  864. return PyBool_FromLong(result);
  865. }
  866. static PyObject *
  867. DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
  868. {
  869. int result;
  870. struct _Py_stat_struct st;
  871. #ifdef MS_WINDOWS
  872. wchar_t *path;
  873. path = PyUnicode_AsUnicode(self->path);
  874. if (!path)
  875. return NULL;
  876. if (follow_symlinks)
  877. result = win32_stat_w(path, &st);
  878. else
  879. result = win32_lstat_w(path, &st);
  880. if (result != 0) {
  881. return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
  882. 0, self->path);
  883. }
  884. #else /* POSIX */
  885. PyObject *bytes;
  886. char *path;
  887. #if PY_MAJOR_VERSION >= 3
  888. if (!PyUnicode_FSConverter(self->path, &bytes))
  889. return NULL;
  890. #else
  891. if (PyString_Check(self->path)) {
  892. bytes = self->path;
  893. Py_INCREF(bytes);
  894. } else {
  895. bytes = PyUnicode_AsEncodedString(self->path, FS_ENCODING, "strict");
  896. if (!bytes)
  897. return NULL;
  898. }
  899. #endif
  900. path = PyBytes_AS_STRING(bytes);
  901. if (follow_symlinks)
  902. result = STAT(path, &st);
  903. else
  904. result = LSTAT(path, &st);
  905. Py_DECREF(bytes);
  906. if (result != 0)
  907. return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path);
  908. #endif
  909. return _pystat_fromstructstat(&st);
  910. }
  911. static PyObject *
  912. DirEntry_get_lstat(DirEntry *self)
  913. {
  914. if (!self->lstat) {
  915. #ifdef MS_WINDOWS
  916. self->lstat = _pystat_fromstructstat(&self->win32_lstat);
  917. #else /* POSIX */
  918. self->lstat = DirEntry_fetch_stat(self, 0);
  919. #endif
  920. }
  921. Py_XINCREF(self->lstat);
  922. return self->lstat;
  923. }
  924. static PyObject *
  925. DirEntry_get_stat(DirEntry *self, int follow_symlinks)
  926. {
  927. if (!follow_symlinks)
  928. return DirEntry_get_lstat(self);
  929. if (!self->stat) {
  930. int result = DirEntry_is_symlink(self);
  931. if (result == -1)
  932. return NULL;
  933. else if (result)
  934. self->stat = DirEntry_fetch_stat(self, 1);
  935. else
  936. self->stat = DirEntry_get_lstat(self);
  937. }
  938. Py_XINCREF(self->stat);
  939. return self->stat;
  940. }
  941. static PyObject *
  942. DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs)
  943. {
  944. int follow_symlinks = 1;
  945. if (!PyArg_ParseTupleAndKeywords(args, kwargs, follow_symlinks_format,
  946. follow_symlinks_keywords, &follow_symlinks))
  947. return NULL;
  948. return DirEntry_get_stat(self, follow_symlinks);
  949. }
  950. /* Set exception and return -1 on error, 0 for False, 1 for True */
  951. static int
  952. DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
  953. {
  954. PyObject *stat = NULL;
  955. PyObject *st_mode = NULL;
  956. long mode;
  957. int result;
  958. #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
  959. int is_symlink;
  960. int need_stat;
  961. #endif
  962. #ifdef MS_WINDOWS
  963. unsigned long dir_bits;
  964. #endif
  965. _Py_IDENTIFIER(st_mode);
  966. #ifdef MS_WINDOWS
  967. is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
  968. need_stat = follow_symlinks && is_symlink;
  969. #elif defined(HAVE_DIRENT_D_TYPE)
  970. is_symlink = self->d_type == DT_LNK;
  971. need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
  972. #endif
  973. #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
  974. if (need_stat) {
  975. #endif
  976. stat = DirEntry_get_stat(self, follow_symlinks);
  977. if (!stat) {
  978. if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
  979. /* If file doesn't exist (anymore), then return False
  980. (i.e., say it's not a file/directory) */
  981. PyErr_Clear();
  982. return 0;
  983. }
  984. goto error;
  985. }
  986. st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
  987. if (!st_mode)
  988. goto error;
  989. mode = PyLong_AsLong(st_mode);
  990. if (mode == -1 && PyErr_Occurred())
  991. goto error;
  992. Py_CLEAR(st_mode);
  993. Py_CLEAR(stat);
  994. result = (mode & S_IFMT) == mode_bits;
  995. #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
  996. }
  997. else if (is_symlink) {
  998. assert(mode_bits != S_IFLNK);
  999. result = 0;
  1000. }
  1001. else {
  1002. assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
  1003. #ifdef MS_WINDOWS
  1004. dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
  1005. if (mode_bits == S_IFDIR)
  1006. result = dir_bits != 0;
  1007. else
  1008. result = dir_bits == 0;
  1009. #else /* POSIX */
  1010. if (mode_bits == S_IFDIR)
  1011. result = self->d_type == DT_DIR;
  1012. else
  1013. result = self->d_type == DT_REG;
  1014. #endif
  1015. }
  1016. #endif
  1017. return result;
  1018. error:
  1019. Py_XDECREF(st_mode);
  1020. Py_XDECREF(stat);
  1021. return -1;
  1022. }
  1023. static PyObject *
  1024. DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
  1025. {
  1026. int result;
  1027. result = DirEntry_test_mode(self, follow_symlinks, mode_bits);
  1028. if (result == -1)
  1029. return NULL;
  1030. return PyBool_FromLong(result);
  1031. }
  1032. static PyObject *
  1033. DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs)
  1034. {
  1035. int follow_symlinks = 1;
  1036. if (!PyArg_ParseTupleAndKeywords(args, kwargs, follow_symlinks_format,
  1037. follow_symlinks_keywords, &follow_symlinks))
  1038. return NULL;
  1039. return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR);
  1040. }
  1041. static PyObject *
  1042. DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs)
  1043. {
  1044. int follow_symlinks = 1;
  1045. if (!PyArg_ParseTupleAndKeywords(args, kwargs, follow_symlinks_format,
  1046. follow_symlinks_keywords, &follow_symlinks))
  1047. return NULL;
  1048. return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG);
  1049. }
  1050. static PyObject *
  1051. DirEntry_inode(DirEntry *self)
  1052. {
  1053. #ifdef MS_WINDOWS
  1054. if (!self->got_file_index) {
  1055. wchar_t *path;
  1056. struct _Py_stat_struct stat;
  1057. path = PyUnicode_AsUnicode(self->path);
  1058. if (!path)
  1059. return NULL;
  1060. if (win32_lstat_w(path, &stat) != 0) {
  1061. return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
  1062. 0, self->path);
  1063. }
  1064. self->win32_file_index = stat.st_ino;
  1065. self->got_file_index = 1;
  1066. }
  1067. return PyLong_FromUnsignedLongLong(self->win32_file_index);
  1068. #else /* POSIX */
  1069. #ifdef HAVE_LARGEFILE_SUPPORT
  1070. return PyLong_FromUnsignedLongLong(self->d_ino);
  1071. #else
  1072. return PyLong_FromUnsignedLong((unsigned long)self->d_ino);
  1073. #endif
  1074. #endif
  1075. }
  1076. #if PY_MAJOR_VERSION < 3 && defined(MS_WINDOWS)
  1077. PyObject *DirEntry_name_getter(DirEntry *self, void *closure) {
  1078. if (self->name_path_bytes) {
  1079. return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(self->name),
  1080. PyUnicode_GetSize(self->name), "strict");
  1081. } else {
  1082. Py_INCREF(self->name);
  1083. return self->name;
  1084. }
  1085. }
  1086. PyObject *DirEntry_path_getter(DirEntry *self, void *closure) {
  1087. if (self->name_path_bytes) {
  1088. return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(self->path),
  1089. PyUnicode_GetSize(self->path), "strict");
  1090. } else {
  1091. Py_INCREF(self->path);
  1092. return self->path;
  1093. }
  1094. }
  1095. static PyGetSetDef DirEntry_getset[] = {
  1096. {"name", (getter)DirEntry_name_getter, NULL,
  1097. "the entry's base filename, relative to scandir() \"path\" argument", NULL},
  1098. {"path", (getter)DirEntry_path_getter, NULL,
  1099. "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)", NULL},
  1100. {NULL}
  1101. };
  1102. #else
  1103. static PyMemberDef DirEntry_members[] = {
  1104. {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
  1105. "the entry's base filename, relative to scandir() \"path\" argument"},
  1106. {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
  1107. "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
  1108. {NULL}
  1109. };
  1110. #endif
  1111. static PyObject *
  1112. DirEntry_repr(DirEntry *self)
  1113. {
  1114. #if PY_MAJOR_VERSION >= 3
  1115. return PyUnicode_FromFormat("<DirEntry %R>", self->name);
  1116. #elif defined(MS_WINDOWS)
  1117. PyObject *name;
  1118. PyObject *name_repr;
  1119. PyObject *entry_repr;
  1120. name = DirEntry_name_getter(self, NULL);
  1121. if (!name)
  1122. return NULL;
  1123. name_repr = PyObject_Repr(name);
  1124. Py_DECREF(name);
  1125. if (!name_repr)
  1126. return NULL;
  1127. entry_repr = PyString_FromFormat("<DirEntry %s>", PyString_AsString(name_repr));
  1128. Py_DECREF(name_repr);
  1129. return entry_repr;
  1130. #else
  1131. PyObject *name_repr;
  1132. PyObject *entry_repr;
  1133. name_repr = PyObject_Repr(self->name);
  1134. if (!name_repr)
  1135. return NULL;
  1136. entry_repr = PyString_FromFormat("<DirEntry %s>", PyString_AsString(name_repr));
  1137. Py_DECREF(name_repr);
  1138. return entry_repr;
  1139. #endif
  1140. }
  1141. static PyMethodDef DirEntry_methods[] = {
  1142. {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS,
  1143. "return True if the entry is a directory; cached per entry"
  1144. },
  1145. {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS,
  1146. "return True if the entry is a file; cached per entry"
  1147. },
  1148. {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS,
  1149. "return True if the entry is a symbolic link; cached per entry"
  1150. },
  1151. {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS,
  1152. "return stat_result object for the entry; cached per entry"
  1153. },
  1154. {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS,
  1155. "return inode of the entry; cached per entry",
  1156. },
  1157. {NULL}
  1158. };
  1159. static PyTypeObject DirEntryType = {
  1160. PyVarObject_HEAD_INIT(NULL, 0)
  1161. MODNAME ".DirEntry", /* tp_name */
  1162. sizeof(DirEntry), /* tp_basicsize */
  1163. 0, /* tp_itemsize */
  1164. /* methods */
  1165. (destructor)DirEntry_dealloc, /* tp_dealloc */
  1166. 0, /* tp_print */
  1167. 0, /* tp_getattr */
  1168. 0, /* tp_setattr */
  1169. 0, /* tp_compare */
  1170. (reprfunc)DirEntry_repr, /* tp_repr */
  1171. 0, /* tp_as_number */
  1172. 0, /* tp_as_sequence */
  1173. 0, /* tp_as_mapping */
  1174. 0, /* tp_hash */
  1175. 0, /* tp_call */
  1176. 0, /* tp_str */
  1177. 0, /* tp_getattro */
  1178. 0, /* tp_setattro */
  1179. 0, /* tp_as_buffer */
  1180. Py_TPFLAGS_DEFAULT, /* tp_flags */
  1181. 0, /* tp_doc */
  1182. 0, /* tp_traverse */
  1183. 0, /* tp_clear */
  1184. 0, /* tp_richcompare */
  1185. 0, /* tp_weaklistoffset */
  1186. 0, /* tp_iter */
  1187. 0, /* tp_iternext */
  1188. DirEntry_methods, /* tp_methods */
  1189. #if PY_MAJOR_VERSION < 3 && defined(MS_WINDOWS)
  1190. NULL, /* tp_members */
  1191. DirEntry_getset, /* tp_getset */
  1192. #else
  1193. DirEntry_members, /* tp_members */
  1194. NULL, /* tp_getset */
  1195. #endif
  1196. };
  1197. #ifdef MS_WINDOWS
  1198. static wchar_t *
  1199. join_path_filenameW(wchar_t *path_wide, wchar_t* filename)
  1200. {
  1201. Py_ssize_t path_len;
  1202. Py_ssize_t size;
  1203. wchar_t *result;
  1204. wchar_t ch;
  1205. if (!path_wide) { /* Default arg: "." */
  1206. path_wide = L".";
  1207. path_len = 1;
  1208. }
  1209. else {
  1210. path_len = wcslen(path_wide);
  1211. }
  1212. /* The +1's are for the path separator and the NUL */
  1213. size = path_len + 1 + wcslen(filename) + 1;
  1214. result = PyMem_New(wchar_t, size);
  1215. if (!result) {
  1216. PyErr_NoMemory();
  1217. return NULL;
  1218. }
  1219. wcscpy(result, path_wide);
  1220. if (path_len > 0) {
  1221. ch = result[path_len - 1];
  1222. if (ch != SEP && ch != ALTSEP && ch != L':')
  1223. result[path_len++] = SEP;
  1224. wcscpy(result + path_len, filename);
  1225. }
  1226. return result;
  1227. }
  1228. static PyObject *
  1229. DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
  1230. {
  1231. DirEntry *entry;
  1232. BY_HANDLE_FILE_INFORMATION file_info;
  1233. ULONG reparse_tag;
  1234. wchar_t *joined_path;
  1235. entry = PyObject_New(DirEntry, &DirEntryType);
  1236. if (!entry)
  1237. return NULL;
  1238. entry->name = NULL;
  1239. entry->path = NULL;
  1240. entry->stat = NULL;
  1241. entry->lstat = NULL;
  1242. entry->got_file_index = 0;
  1243. #if PY_MAJOR_VERSION < 3
  1244. entry->name_path_bytes = path->object && PyBytes_Check(path->object);
  1245. #endif
  1246. entry->name = PyUnicode_FromWideChar(dataW->cFileName, wcslen(dataW->cFileName));
  1247. if (!entry->name)
  1248. goto error;
  1249. joined_path = join_path_filenameW(path->wide, dataW->cFileName);
  1250. if (!joined_path)
  1251. goto error;
  1252. entry->path = PyUnicode_FromWideChar(joined_path, wcslen(joined_path));
  1253. PyMem_Free(joined_path);
  1254. if (!entry->path)
  1255. goto error;
  1256. find_data_to_file_info_w(dataW, &file_info, &reparse_tag);
  1257. _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
  1258. return (PyObject *)entry;
  1259. error:
  1260. Py_DECREF(entry);
  1261. return NULL;
  1262. }
  1263. #else /* POSIX */
  1264. static char *
  1265. join_path_filename(char *path_narrow, char* filename, Py_ssize_t filename_len)
  1266. {
  1267. Py_ssize_t path_len;
  1268. Py_ssize_t size;
  1269. char *result;
  1270. if (!path_narrow) { /* Default arg: "." */
  1271. path_narrow = ".";
  1272. path_len = 1;
  1273. }
  1274. else {
  1275. path_len = strlen(path_narrow);
  1276. }
  1277. if (filename_len == -1)
  1278. filename_len = strlen(filename);
  1279. /* The +1's are for the path separator and the NUL */
  1280. size = path_len + 1 + filename_len + 1;
  1281. result = PyMem_New(char, size);
  1282. if (!result) {
  1283. PyErr_NoMemory();
  1284. return NULL;
  1285. }
  1286. strcpy(result, path_narrow);
  1287. if (path_len > 0 && result[path_len - 1] != '/')
  1288. result[path_len++] = '/';
  1289. strcpy(result + path_len, filename);
  1290. return result;
  1291. }
  1292. static PyObject *
  1293. DirEntry_from_posix_info(path_t *path, char *name, Py_ssize_t name_len,
  1294. ino_t d_ino
  1295. #ifdef HAVE_DIRENT_D_TYPE
  1296. , unsigned char d_type
  1297. #endif
  1298. )
  1299. {
  1300. DirEntry *entry;
  1301. char *joined_path;
  1302. entry = PyObject_New(DirEntry, &DirEntryType);
  1303. if (!entry)
  1304. return NULL;
  1305. entry->name = NULL;
  1306. entry->path = NULL;
  1307. entry->stat = NULL;
  1308. entry->lstat = NULL;
  1309. joined_path = join_path_filename(path->narrow, name, name_len);
  1310. if (!joined_path)
  1311. goto error;
  1312. if (!path->narrow || !PyBytes_Check(path->object)) {
  1313. #if PY_MAJOR_VERSION >= 3
  1314. entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
  1315. entry->path = PyUnicode_DecodeFSDefault(joined_path);
  1316. #else
  1317. entry->name = PyUnicode_Decode(name, name_len,
  1318. FS_ENCODING, "strict");
  1319. entry->path = PyUnicode_Decode(joined_path, strlen(joined_path),
  1320. FS_ENCODING, "strict");
  1321. #endif
  1322. }
  1323. else {
  1324. entry->name = PyBytes_FromStringAndSize(name, name_len);
  1325. entry->path = PyBytes_FromString(joined_path);
  1326. }
  1327. PyMem_Free(joined_path);
  1328. if (!entry->name || !entry->path)
  1329. goto error;
  1330. #ifdef HAVE_DIRENT_D_TYPE
  1331. entry->d_type = d_type;
  1332. #endif
  1333. entry->d_ino = d_ino;
  1334. return (PyObject *)entry;
  1335. error:
  1336. Py_XDECREF(entry);
  1337. return NULL;
  1338. }
  1339. #endif
  1340. typedef struct {
  1341. PyObject_HEAD
  1342. path_t path;
  1343. #ifdef MS_WINDOWS
  1344. HANDLE handle;
  1345. WIN32_FIND_DATAW file_data;
  1346. int first_time;
  1347. #else /* POSIX */
  1348. DIR *dirp;
  1349. #endif
  1350. } ScandirIterator;
  1351. #ifdef MS_WINDOWS
  1352. static void
  1353. ScandirIterator_close(ScandirIterator *iterator)
  1354. {
  1355. if (iterator->handle == INVALID_HANDLE_VALUE)
  1356. return;
  1357. Py_BEGIN_ALLOW_THREADS
  1358. FindClose(iterator->handle);
  1359. Py_END_ALLOW_THREADS
  1360. iterator->handle = INVALID_HANDLE_VALUE;
  1361. }
  1362. static PyObject *
  1363. ScandirIterator_iternext(ScandirIterator *iterator)
  1364. {
  1365. WIN32_FIND_DATAW *file_data = &iterator->file_data;
  1366. BOOL success;
  1367. /* Happens if the iterator is iterated twice */
  1368. if (iterator->handle == INVALID_HANDLE_VALUE) {
  1369. PyErr_SetNone(PyExc_StopIteration);
  1370. return NULL;
  1371. }
  1372. while (1) {
  1373. if (!iterator->first_time) {
  1374. Py_BEGIN_ALLOW_THREADS
  1375. success = FindNextFileW(iterator->handle, file_data);
  1376. Py_END_ALLOW_THREADS
  1377. if (!success) {
  1378. if (GetLastError() != ERROR_NO_MORE_FILES)
  1379. return path_error(&iterator->path);
  1380. /* No more files found in directory, stop iterating */
  1381. break;
  1382. }
  1383. }
  1384. iterator->first_time = 0;
  1385. /* Skip over . and .. */
  1386. if (wcscmp(file_data->cFileName, L".") != 0 &&
  1387. wcscmp(file_data->cFileName, L"..") != 0)
  1388. return DirEntry_from_find_data(&iterator->path, file_data);
  1389. /* Loop till we get a non-dot directory or finish iterating */
  1390. }
  1391. ScandirIterator_close(iterator);
  1392. PyErr_SetNone(PyExc_StopIteration);
  1393. return NULL;
  1394. }
  1395. #else /* POSIX */
  1396. static void
  1397. ScandirIterator_close(ScandirIterator *iterator)
  1398. {
  1399. if (!iterator->dirp)
  1400. return;
  1401. Py_BEGIN_ALLOW_THREADS
  1402. closedir(iterator->dirp);
  1403. Py_END_ALLOW_THREADS
  1404. iterator->dirp = NULL;
  1405. return;
  1406. }
  1407. static PyObject *
  1408. ScandirIterator_iternext(ScandirIterator *iterator)
  1409. {
  1410. struct dirent *direntp;
  1411. Py_ssize_t name_len;
  1412. int is_dot;
  1413. /* Happens if the iterator is iterated twice */
  1414. if (!iterator->dirp) {
  1415. PyErr_SetNone(PyExc_StopIteration);
  1416. return NULL;
  1417. }
  1418. while (1) {
  1419. errno = 0;
  1420. Py_BEGIN_ALLOW_THREADS
  1421. direntp = readdir(iterator->dirp);
  1422. Py_END_ALLOW_THREADS
  1423. if (!direntp) {
  1424. if (errno != 0)
  1425. return path_error(&iterator->path);
  1426. /* No more files found in directory, stop iterating */
  1427. break;
  1428. }
  1429. /* Skip over . and .. */
  1430. name_len = NAMLEN(direntp);
  1431. is_dot = direntp->d_name[0] == '.' &&
  1432. (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
  1433. if (!is_dot) {
  1434. return DirEntry_from_posix_info(&iterator->path, direntp->d_name,
  1435. name_len, direntp->d_ino
  1436. #ifdef HAVE_DIRENT_D_TYPE
  1437. , direntp->d_type
  1438. #endif
  1439. );
  1440. }
  1441. /* Loop till we get a non-dot directory or finish iterating */
  1442. }
  1443. ScandirIterator_close(iterator);
  1444. PyErr_SetNone(PyExc_StopIteration);
  1445. return NULL;
  1446. }
  1447. #endif
  1448. static void
  1449. ScandirIterator_dealloc(ScandirIterator *iterator)
  1450. {
  1451. ScandirIterator_close(iterator);
  1452. Py_XDECREF(iterator->path.object);
  1453. path_cleanup(&iterator->path);
  1454. Py_TYPE(iterator)->tp_free((PyObject *)iterator);
  1455. }
  1456. static PyTypeObject ScandirIteratorType = {
  1457. PyVarObject_HEAD_INIT(NULL, 0)
  1458. MODNAME ".ScandirIterator", /* tp_name */
  1459. sizeof(ScandirIterator), /* tp_basicsize */
  1460. 0, /* tp_itemsize */
  1461. /* methods */
  1462. (destructor)ScandirIterator_dealloc, /* tp_dealloc */
  1463. 0, /* tp_print */
  1464. 0, /* tp_getattr */
  1465. 0, /* tp_setattr */
  1466. 0, /* tp_compare */
  1467. 0, /* tp_repr */
  1468. 0, /* tp_as_number */
  1469. 0, /* tp_as_sequence */
  1470. 0, /* tp_as_mapping */
  1471. 0, /* tp_hash */
  1472. 0, /* tp_call */
  1473. 0, /* tp_str */
  1474. 0, /* tp_getattro */
  1475. 0, /* tp_setattro */
  1476. 0, /* tp_as_buffer */
  1477. Py_TPFLAGS_DEFAULT, /* tp_flags */
  1478. 0, /* tp_doc */
  1479. 0, /* tp_traverse */
  1480. 0, /* tp_clear */
  1481. 0, /* tp_richcompare */
  1482. 0, /* tp_weaklistoffset */
  1483. PyObject_SelfIter, /* tp_iter */
  1484. (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
  1485. };
  1486. static PyObject *
  1487. posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs)
  1488. {
  1489. ScandirIterator *iterator;
  1490. static char *keywords[] = {"path", NULL};
  1491. #ifdef MS_WINDOWS
  1492. wchar_t *path_strW;
  1493. #else
  1494. char *path;
  1495. #endif
  1496. iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
  1497. if (!iterator)
  1498. return NULL;
  1499. memset(&iterator->path, 0, sizeof(path_t));
  1500. iterator->path.function_name = "scandir";
  1501. iterator->path.nullable = 1;
  1502. #ifdef MS_WINDOWS
  1503. iterator->handle = INVALID_HANDLE_VALUE;
  1504. #else
  1505. iterator->dirp = NULL;
  1506. #endif
  1507. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords,
  1508. path_converter, &iterator->path))
  1509. goto error;
  1510. /* path_converter doesn't keep path.object around, so do it
  1511. manually for the lifetime of the iterator here (the refcount
  1512. is decremented in ScandirIterator_dealloc)
  1513. */
  1514. Py_XINCREF(iterator->path.object);
  1515. #ifdef MS_WINDOWS
  1516. if (iterator->path.narrow) {
  1517. PyErr_SetString(PyExc_TypeError,
  1518. "os.scandir() doesn't support bytes path on Windows, use Unicode instead");
  1519. goto error;
  1520. }
  1521. iterator->first_time = 1;
  1522. path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
  1523. if (!path_strW)
  1524. goto error;
  1525. Py_BEGIN_ALLOW_THREADS
  1526. iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
  1527. Py_END_ALLOW_THREADS
  1528. PyMem_Free(path_strW);
  1529. if (iterator->handle == INVALID_HANDLE_VALUE) {
  1530. path_error(&iterator->path);
  1531. goto error;
  1532. }
  1533. #else /* POSIX */
  1534. if (iterator->path.narrow)
  1535. path = iterator->path.narrow;
  1536. else
  1537. path = ".";
  1538. errno = 0;
  1539. Py_BEGIN_ALLOW_THREADS
  1540. iterator->dirp = opendir(path);
  1541. Py_END_ALLOW_THREADS
  1542. if (!iterator->dirp) {
  1543. path_error(&iterator->path);
  1544. goto error;
  1545. }
  1546. #endif
  1547. return (PyObject *)iterator;
  1548. error:
  1549. Py_DECREF(iterator);
  1550. return NULL;
  1551. }
  1552. /* SECTION: Module and method definitions and initialization code */
  1553. static PyMethodDef scandir_methods[] = {
  1554. {"scandir", (PyCFunction)posix_scandir,
  1555. METH_VARARGS | METH_KEYWORDS,
  1556. posix_scandir__doc__},
  1557. {NULL, NULL},
  1558. };
  1559. #if PY_MAJOR_VERSION >= 3
  1560. static struct PyModuleDef moduledef = {
  1561. PyModuleDef_HEAD_INIT,
  1562. "_scandir",
  1563. NULL,
  1564. 0,
  1565. scandir_methods,
  1566. NULL,
  1567. NULL,
  1568. NULL,
  1569. NULL,
  1570. };
  1571. #endif
  1572. #if PY_MAJOR_VERSION >= 3
  1573. PyObject *
  1574. PyInit__scandir(void)
  1575. {
  1576. PyObject *module = PyModule_Create(&moduledef);
  1577. #else
  1578. void
  1579. init_scandir(void)
  1580. {
  1581. PyObject *module = Py_InitModule("_scandir", scandir_methods);
  1582. #endif
  1583. if (module == NULL) {
  1584. INIT_ERROR;
  1585. }
  1586. billion = PyLong_FromLong(1000000000);
  1587. if (!billion)
  1588. INIT_ERROR;
  1589. stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
  1590. stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
  1591. stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
  1592. PyStructSequence_InitType(&StatResultType, &stat_result_desc);
  1593. structseq_new = StatResultType.tp_new;
  1594. StatResultType.tp_new = statresult_new;
  1595. if (PyType_Ready(&ScandirIteratorType) < 0)
  1596. INIT_ERROR;
  1597. if (PyType_Ready(&DirEntryType) < 0)
  1598. INIT_ERROR;
  1599. PyModule_AddObject(module, "DirEntry", (PyObject *)&DirEntryType);
  1600. #if PY_MAJOR_VERSION >= 3
  1601. return module;
  1602. #endif
  1603. }