Skip to content

Instantly share code, notes, and snippets.

@saghul
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saghul/20236dbba12250e6f265 to your computer and use it in GitHub Desktop.
Save saghul/20236dbba12250e6f265 to your computer and use it in GitHub Desktop.
commit 833777e0c89bc9f3b05db09564134c8932e52ecf
Author: Saúl Ibarra Corretgé <saghul@gmail.com>
Date: Thu Aug 14 22:18:28 2014 +0200
fs: ifx uv_fs_readdir_next reported types
Some systems (hello SunOS!) don't have thr d_type field on struct
dirent, so mark them as UV_DIRENT_UNKNOWN, also mark anything not a
directory or a file as unknown.
diff --git a/include/uv-unix.h b/include/uv-unix.h
index 2a1ead5..1e66c6e 100644
--- a/include/uv-unix.h
+++ b/include/uv-unix.h
@@ -158,7 +158,15 @@ typedef uid_t uv_uid_t;
typedef struct dirent uv__dirent_t;
-#define UV__DT_DIR DT_DIR
+#define HAVE_DIRENT_TYPES
+#if defined(__sun)
+# undef HAVE_DIRENT_TYPES
+#endif
+
+#if defined(HAVE_DIRENT_TYPES)
+# define UV__DT_DIR DT_DIR
+# define UV__DT_FILE DT_REG
+#endif
/* Platform-specific definitions for uv_dlopen support. */
#define UV_DYNAMIC /* empty */
diff --git a/include/uv.h b/include/uv.h
index 221d5d0..78edae4 100644
--- a/include/uv.h
+++ b/include/uv.h
@@ -1788,6 +1788,7 @@ struct uv_interface_address_s {
};
typedef enum {
+ UV_DIRENT_UNKNOWN,
UV_DIRENT_FILE,
UV_DIRENT_DIR
} uv_dirent_type_t;
diff --git a/src/uv-common.c b/src/uv-common.c
index e2307aa..e92161d 100644
--- a/src/uv-common.c
+++ b/src/uv-common.c
@@ -482,10 +482,14 @@ int uv_fs_readdir_next(uv_fs_t* req, uv_dirent_t* ent) {
dent = dents[req->nbufs++];
ent->name = dent->d_name;
+#ifndef HAVE_DIRENT_TYPES
if (dent->d_type == UV__DT_DIR)
ent->type = UV_DIRENT_DIR;
- else
+ else if (dent->d_type == UV__DT_FILE)
ent->type = UV_DIRENT_FILE;
+ else
+#endif
+ ent->type = UV_DIRENT_UNKNOWN;
return 0;
}
diff --git a/test/test-fs.c b/test/test-fs.c
index 38bc3e0..a06ddad 100644
--- a/test/test-fs.c
+++ b/test/test-fs.c
@@ -425,7 +425,7 @@ static void readdir_cb(uv_fs_t* req) {
while (UV_EOF != uv_fs_readdir_next(req, &dent)) {
ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
- ASSERT(dent.type == UV_DIRENT_FILE);
+ ASSERT(dent.type == UV_DIRENT_FILE || dent.type == UV_DIRENT_UNKNOWN);
}
readdir_cb_count++;
ASSERT(req->path);
@@ -851,7 +851,7 @@ TEST_IMPL(fs_async_dir) {
ASSERT(readdir_req.ptr);
while (UV_EOF != uv_fs_readdir_next(&readdir_req, &dent)) {
ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
- ASSERT(dent.type == UV_DIRENT_FILE);
+ ASSERT(dent.type == UV_DIRENT_FILE || dent.type == UV_DIRENT_UNKNOWN);
}
uv_fs_req_cleanup(&readdir_req);
ASSERT(!readdir_req.ptr);
@@ -1607,7 +1607,7 @@ TEST_IMPL(fs_symlink_dir) {
ASSERT(readdir_req.ptr);
while (UV_EOF != uv_fs_readdir_next(&readdir_req, &dent)) {
ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
- ASSERT(dent.type == UV_DIRENT_FILE);
+ ASSERT(dent.type == UV_DIRENT_FILE || dent.type == UV_DIRENT_UNKNOWN);
}
uv_fs_req_cleanup(&readdir_req);
ASSERT(!readdir_req.ptr);
@@ -1627,7 +1627,7 @@ TEST_IMPL(fs_symlink_dir) {
ASSERT(readdir_req.ptr);
while (UV_EOF != uv_fs_readdir_next(&readdir_req, &dent)) {
ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
- ASSERT(dent.type == UV_DIRENT_FILE);
+ ASSERT(dent.type == UV_DIRENT_FILE || dent.type == UV_DIRENT_UNKNOWN);
}
uv_fs_req_cleanup(&readdir_req);
ASSERT(!readdir_req.ptr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment