Skip to content

Commit 8b44259

Browse files
committed
run-test262: handle scandir() results with DT_UNKNOWN d_type
In case the dirent items returned by scandir() have DT_UNKNOWN as d_type it is not possible to assume they are "non directories", and it is needed to stat() them to get the actual type. Hence, tweak the existing bits to account for that: - create an helper enum for file type to distinguish whether something is for sure a file, whether it is for sure a directory, or it is not known - map d_type to the newly created enum - adapt consider_test_file() to use the newly created enum, and case the file type is not known also to stat() the file to get the actual type
1 parent 3d3b58d commit 8b44259

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

run-test262.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <ctype.h>
3232
#include <errno.h>
3333
#include <time.h>
34+
#include <sys/stat.h>
3435

3536
#ifdef _WIN32
3637
#include <windows.h>
@@ -73,6 +74,12 @@ typedef struct namelist_t {
7374
int size;
7475
} namelist_t;
7576

77+
enum file_type_t {
78+
file_is_directory,
79+
file_is_not_directory,
80+
file_is_unknown,
81+
};
82+
7683
long nthreads; // invariant: 0 < nthreads < countof(threads)
7784
js_thread_t threads[32];
7885
js_thread_t progress_thread;
@@ -407,7 +414,7 @@ static bool ispathsep(int c)
407414
return c == '/' || c == '\\';
408415
}
409416

410-
static void consider_test_file(const char *path, const char *name, int is_dir)
417+
static void consider_test_file(const char *path, const char *name, enum file_type_t file_type)
411418
{
412419
size_t pathlen;
413420
char s[1024];
@@ -418,7 +425,11 @@ static void consider_test_file(const char *path, const char *name, int is_dir)
418425
while (pathlen > 0 && ispathsep(path[pathlen-1]))
419426
pathlen--;
420427
snprintf(s, sizeof(s), "%.*s/%s", (int)pathlen, path, name);
421-
if (is_dir)
428+
if (file_type == file_is_unknown) {
429+
struct stat st;
430+
file_type = stat(s, &st) == 0 && S_ISDIR(st.st_mode) ? file_is_directory : file_is_not_directory;
431+
}
432+
if (file_type == file_is_directory)
422433
find_test_files(s);
423434
else
424435
add_test_file(s);
@@ -437,7 +448,7 @@ static void find_test_files(const char *path)
437448
do {
438449
consider_test_file(path,
439450
d.cFileName,
440-
d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
451+
d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? file_is_directory : file_is_not_directory);
441452
} while (FindNextFileA(h, &d));
442453
FindClose(h);
443454
}
@@ -448,7 +459,19 @@ static void find_test_files(const char *path)
448459
n = scandir(path, &ds, NULL, alphasort);
449460
for (i = 0; i < n; i++) {
450461
d = ds[i];
451-
consider_test_file(path, d->d_name, d->d_type == DT_DIR);
462+
enum file_type_t file_type;
463+
switch (d->d_type) {
464+
case DT_DIR:
465+
file_type = file_is_directory;
466+
break;
467+
case DT_UNKNOWN:
468+
file_type = file_is_unknown;
469+
break;
470+
default:
471+
file_type = file_is_not_directory;
472+
break;
473+
}
474+
consider_test_file(path, d->d_name, file_type);
452475
free(d);
453476
}
454477
free(ds);

0 commit comments

Comments
 (0)