|
| 1 | +#include <node_api.h> |
| 2 | + |
| 3 | +#include <dirent.h> |
| 4 | +#include <fcntl.h> |
| 5 | +#include <sys/stat.h> |
| 6 | +#include <unistd.h> |
| 7 | + |
| 8 | +#include <cerrno> |
| 9 | +#include <climits> |
| 10 | +#include <cmath> |
| 11 | +#include <cstring> |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +struct Entry { |
| 16 | + std::string name; |
| 17 | + const char* kind; |
| 18 | +}; |
| 19 | + |
| 20 | +struct DirectoryRead { |
| 21 | + napi_async_work work = nullptr; |
| 22 | + napi_deferred deferred = nullptr; |
| 23 | + int descriptor = -1; |
| 24 | + size_t limit = 0; |
| 25 | + bool truncated = false; |
| 26 | + std::string error; |
| 27 | + std::vector<Entry> entries; |
| 28 | +}; |
| 29 | + |
| 30 | +static const char* EntryKind(DIR* directory, const dirent* entry) { |
| 31 | + switch (entry->d_type) { |
| 32 | + case DT_REG: return "file"; |
| 33 | + case DT_DIR: return "directory"; |
| 34 | + case DT_LNK: return "symlink"; |
| 35 | + case DT_UNKNOWN: { |
| 36 | + struct stat metadata; |
| 37 | + if (fstatat(dirfd(directory), entry->d_name, &metadata, AT_SYMLINK_NOFOLLOW) == 0) { |
| 38 | + if (S_ISREG(metadata.st_mode)) return "file"; |
| 39 | + if (S_ISDIR(metadata.st_mode)) return "directory"; |
| 40 | + if (S_ISLNK(metadata.st_mode)) return "symlink"; |
| 41 | + } |
| 42 | + return "other"; |
| 43 | + } |
| 44 | + default: return "other"; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +static void ReadEntries(napi_env, void* data) { |
| 49 | + auto* read = static_cast<DirectoryRead*>(data); |
| 50 | + DIR* directory = fdopendir(read->descriptor); |
| 51 | + if (!directory) { |
| 52 | + close(read->descriptor); |
| 53 | + read->descriptor = -1; |
| 54 | + read->error = "Could not enumerate the approved directory."; |
| 55 | + return; |
| 56 | + } |
| 57 | + read->descriptor = -1; |
| 58 | + while (true) { |
| 59 | + errno = 0; |
| 60 | + const dirent* entry = readdir(directory); |
| 61 | + if (!entry) { |
| 62 | + if (errno != 0) read->error = "Could not finish reading the approved directory."; |
| 63 | + break; |
| 64 | + } |
| 65 | + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; |
| 66 | + if (read->entries.size() == read->limit) { |
| 67 | + read->truncated = true; |
| 68 | + break; |
| 69 | + } |
| 70 | + read->entries.push_back({entry->d_name, EntryKind(directory, entry)}); |
| 71 | + } |
| 72 | + closedir(directory); |
| 73 | +} |
| 74 | + |
| 75 | +static napi_value String(napi_env env, const char* value) { |
| 76 | + napi_value result; |
| 77 | + napi_create_string_utf8(env, value, NAPI_AUTO_LENGTH, &result); |
| 78 | + return result; |
| 79 | +} |
| 80 | + |
| 81 | +static void Complete(napi_env env, napi_status status, void* data) { |
| 82 | + auto* read = static_cast<DirectoryRead*>(data); |
| 83 | + if (read->descriptor >= 0) close(read->descriptor); |
| 84 | + if (status != napi_ok || !read->error.empty()) { |
| 85 | + napi_value error; |
| 86 | + napi_create_error(env, nullptr, |
| 87 | + String(env, read->error.empty() ? "Directory read cancelled." : read->error.c_str()), |
| 88 | + &error); |
| 89 | + napi_reject_deferred(env, read->deferred, error); |
| 90 | + } else { |
| 91 | + napi_value result; |
| 92 | + napi_value entries; |
| 93 | + napi_value truncated; |
| 94 | + napi_create_object(env, &result); |
| 95 | + napi_create_array_with_length(env, read->entries.size(), &entries); |
| 96 | + for (size_t index = 0; index < read->entries.size(); index++) { |
| 97 | + napi_value entry; |
| 98 | + napi_create_object(env, &entry); |
| 99 | + napi_set_named_property(env, entry, "name", String(env, read->entries[index].name.c_str())); |
| 100 | + napi_set_named_property(env, entry, "kind", String(env, read->entries[index].kind)); |
| 101 | + napi_set_element(env, entries, index, entry); |
| 102 | + } |
| 103 | + napi_get_boolean(env, read->truncated, &truncated); |
| 104 | + napi_set_named_property(env, result, "entries", entries); |
| 105 | + napi_set_named_property(env, result, "truncated", truncated); |
| 106 | + napi_resolve_deferred(env, read->deferred, result); |
| 107 | + } |
| 108 | + napi_delete_async_work(env, read->work); |
| 109 | + delete read; |
| 110 | +} |
| 111 | + |
| 112 | +/** The descriptor is duplicated before scheduling; no pathname is reopened by the worker. */ |
| 113 | +static napi_value ReadDirectory(napi_env env, napi_callback_info info) { |
| 114 | + size_t count = 2; |
| 115 | + napi_value arguments[2]; |
| 116 | + double descriptor = -1; |
| 117 | + double limit = 0; |
| 118 | + if (napi_get_cb_info(env, info, &count, arguments, nullptr, nullptr) != napi_ok || count != 2 || |
| 119 | + napi_get_value_double(env, arguments[0], &descriptor) != napi_ok || |
| 120 | + napi_get_value_double(env, arguments[1], &limit) != napi_ok || |
| 121 | + !std::isfinite(descriptor) || descriptor < 0 || descriptor > INT_MAX || |
| 122 | + descriptor != std::floor(descriptor) || limit < 1 || limit > 1000 || limit != std::floor(limit)) { |
| 123 | + napi_throw_type_error(env, nullptr, "Expected a directory descriptor and an entry limit from 1 to 1000."); |
| 124 | + return nullptr; |
| 125 | + } |
| 126 | + auto* read = new DirectoryRead(); |
| 127 | + read->limit = static_cast<size_t>(limit); |
| 128 | + read->descriptor = fcntl(static_cast<int>(descriptor), F_DUPFD_CLOEXEC, 0); |
| 129 | + if (read->descriptor < 0) { |
| 130 | + delete read; |
| 131 | + napi_throw_error(env, nullptr, "Could not retain the approved directory descriptor."); |
| 132 | + return nullptr; |
| 133 | + } |
| 134 | + napi_value promise; |
| 135 | + if (napi_create_promise(env, &read->deferred, &promise) != napi_ok || |
| 136 | + napi_create_async_work(env, nullptr, String(env, "ReadApprovedDirectory"), ReadEntries, |
| 137 | + Complete, read, &read->work) != napi_ok || |
| 138 | + napi_queue_async_work(env, read->work) != napi_ok) { |
| 139 | + close(read->descriptor); |
| 140 | + if (read->work) napi_delete_async_work(env, read->work); |
| 141 | + delete read; |
| 142 | + napi_throw_error(env, nullptr, "Could not schedule the directory read."); |
| 143 | + return nullptr; |
| 144 | + } |
| 145 | + return promise; |
| 146 | +} |
| 147 | + |
| 148 | +NAPI_MODULE_INIT() { |
| 149 | + napi_property_descriptor property = { |
| 150 | + "readDirectory", nullptr, ReadDirectory, nullptr, nullptr, nullptr, napi_default, nullptr |
| 151 | + }; |
| 152 | + napi_define_properties(env, exports, 1, &property); |
| 153 | + return exports; |
| 154 | +} |
0 commit comments