Skip to content

Commit 11cf91b

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 On Windows the file type is always known, so there is no need to stat() files.
1 parent 3d3b58d commit 11cf91b

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

run-test262.c

Lines changed: 29 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,13 @@ 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+
#ifndef _WIN32
429+
if (file_type == file_is_unknown) {
430+
struct stat st;
431+
file_type = stat(s, &st) == 0 && S_ISDIR(st.st_mode) ? file_is_directory : file_is_not_directory;
432+
}
433+
#endif
434+
if (file_type == file_is_directory)
422435
find_test_files(s);
423436
else
424437
add_test_file(s);
@@ -437,7 +450,7 @@ static void find_test_files(const char *path)
437450
do {
438451
consider_test_file(path,
439452
d.cFileName,
440-
d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
453+
d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? file_is_directory : file_is_not_directory);
441454
} while (FindNextFileA(h, &d));
442455
FindClose(h);
443456
}
@@ -448,7 +461,19 @@ static void find_test_files(const char *path)
448461
n = scandir(path, &ds, NULL, alphasort);
449462
for (i = 0; i < n; i++) {
450463
d = ds[i];
451-
consider_test_file(path, d->d_name, d->d_type == DT_DIR);
464+
enum file_type_t file_type;
465+
switch (d->d_type) {
466+
case DT_DIR:
467+
file_type = file_is_directory;
468+
break;
469+
case DT_UNKNOWN:
470+
file_type = file_is_unknown;
471+
break;
472+
default:
473+
file_type = file_is_not_directory;
474+
break;
475+
}
476+
consider_test_file(path, d->d_name, file_type);
452477
free(d);
453478
}
454479
free(ds);

0 commit comments

Comments
 (0)