Skip to content

Commit 4d16408

Browse files
committed
src: combine internal file read utils
1 parent af19f41 commit 4d16408

File tree

5 files changed

+71
-100
lines changed

5 files changed

+71
-100
lines changed

src/module_wrap.cc

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -510,29 +510,6 @@ inline bool ShouldBeTreatedAsRelativeOrAbsolutePath(
510510
return false;
511511
}
512512

513-
std::string ReadFile(uv_file file) {
514-
std::string contents;
515-
uv_fs_t req;
516-
char buffer_memory[4096];
517-
uv_buf_t buf = uv_buf_init(buffer_memory, sizeof(buffer_memory));
518-
519-
do {
520-
const int r = uv_fs_read(nullptr,
521-
&req,
522-
file,
523-
&buf,
524-
1,
525-
contents.length(), // offset
526-
nullptr);
527-
uv_fs_req_cleanup(&req);
528-
529-
if (r <= 0)
530-
break;
531-
contents.append(buf.base, r);
532-
} while (true);
533-
return contents;
534-
}
535-
536513
enum DescriptorType {
537514
FILE,
538515
DIRECTORY,
@@ -583,16 +560,6 @@ DescriptorType CheckDescriptorAtPath(const std::string& path) {
583560
return type;
584561
}
585562

586-
Maybe<std::string> ReadIfFile(const std::string& path) {
587-
Maybe<uv_file> fd = OpenDescriptor(path);
588-
if (fd.IsNothing()) return Nothing<std::string>();
589-
DescriptorType type = CheckDescriptorAtFile(fd.FromJust());
590-
if (type != FILE) return Nothing<std::string>();
591-
std::string source = ReadFile(fd.FromJust());
592-
CloseDescriptor(fd.FromJust());
593-
return Just(source);
594-
}
595-
596563
using Exists = PackageConfig::Exists;
597564
using IsValid = PackageConfig::IsValid;
598565
using HasMain = PackageConfig::HasMain;
@@ -614,7 +581,7 @@ Maybe<const PackageConfig*> GetPackageConfig(Environment* env,
614581
return Just(pcfg);
615582
}
616583

617-
Maybe<std::string> source = ReadIfFile(path);
584+
Maybe<std::string> source = ReadFileSync(path);
618585

619586
if (source.IsNothing()) {
620587
auto entry = env->package_json_cache.emplace(path,

src/node_file.cc

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -812,56 +812,22 @@ void Close(const FunctionCallbackInfo<Value>& args) {
812812
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
813813
Environment* env = Environment::GetCurrent(args);
814814
Isolate* isolate = env->isolate();
815-
uv_loop_t* loop = env->event_loop();
816815

817816
CHECK(args[0]->IsString());
818817
node::Utf8Value path(isolate, args[0]);
819818

820-
if (strlen(*path) != path.length())
821-
return; // Contains a nul byte.
822-
823-
uv_fs_t open_req;
824-
const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr);
825-
uv_fs_req_cleanup(&open_req);
826-
827-
if (fd < 0) {
819+
std::string chars;
820+
const ssize_t nread = ReadFileSyncRaw(*path, path.length(), &chars);
821+
if (nread < 0) {
828822
return;
829823
}
830824

831-
std::shared_ptr<void> defer_close(nullptr, [fd, loop] (...) {
832-
uv_fs_t close_req;
833-
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
834-
uv_fs_req_cleanup(&close_req);
835-
});
836-
837-
const size_t kBlockSize = 32 << 10;
838-
std::vector<char> chars;
839-
int64_t offset = 0;
840-
ssize_t numchars;
841-
do {
842-
const size_t start = chars.size();
843-
chars.resize(start + kBlockSize);
844-
845-
uv_buf_t buf;
846-
buf.base = &chars[start];
847-
buf.len = kBlockSize;
848-
849-
uv_fs_t read_req;
850-
numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr);
851-
uv_fs_req_cleanup(&read_req);
852-
853-
if (numchars < 0)
854-
return;
855-
856-
offset += numchars;
857-
} while (static_cast<size_t>(numchars) == kBlockSize);
858-
859825
size_t start = 0;
860-
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
826+
if (nread >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
861827
start = 3; // Skip UTF-8 BOM.
862828
}
863829

864-
const size_t size = offset - start;
830+
const size_t size = nread - start;
865831
char* p = &chars[start];
866832
char* pe = &chars[size];
867833
char* pos[2];

src/node_native_module.cc

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "node_native_module.h"
22
#include "util-inl.h"
3+
#include "node_internals.h"
34

45
namespace node {
56
namespace native_module {
@@ -195,33 +196,7 @@ MaybeLocal<String> NativeModuleLoader::LoadBuiltinModuleSource(Isolate* isolate,
195196
const char* id) {
196197
#ifdef NODE_BUILTIN_MODULES_PATH
197198
std::string filename = OnDiskFileName(id);
198-
199-
uv_fs_t req;
200-
uv_file file =
201-
uv_fs_open(nullptr, &req, filename.c_str(), O_RDONLY, 0, nullptr);
202-
CHECK_GE(req.result, 0);
203-
uv_fs_req_cleanup(&req);
204-
205-
std::shared_ptr<void> defer_close(nullptr, [file](...) {
206-
uv_fs_t close_req;
207-
CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr));
208-
uv_fs_req_cleanup(&close_req);
209-
});
210-
211-
std::string contents;
212-
char buffer[4096];
213-
uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer));
214-
215-
while (true) {
216-
const int r =
217-
uv_fs_read(nullptr, &req, file, &buf, 1, contents.length(), nullptr);
218-
CHECK_GE(req.result, 0);
219-
uv_fs_req_cleanup(&req);
220-
if (r <= 0) {
221-
break;
222-
}
223-
contents.append(buf.base, r);
224-
}
199+
std::string contents = ReadFileSync(filename).FromJust();
225200

226201
return String::NewFromUtf8(
227202
isolate, contents.c_str(), v8::NewStringType::kNormal, contents.length());

src/util.cc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ namespace node {
5656

5757
using v8::ArrayBufferView;
5858
using v8::Isolate;
59+
using v8::Just;
5960
using v8::Local;
61+
using v8::Maybe;
62+
using v8::Nothing;
6063
using v8::String;
6164
using v8::Value;
6265

@@ -218,6 +221,62 @@ int WriteFileSync(v8::Isolate* isolate,
218221
return WriteFileSync(path, buf);
219222
}
220223

224+
ssize_t ReadFileSyncRaw(
225+
const char* path, size_t path_len, std::string* contents) {
226+
if (strlen(path) != path_len) {
227+
return UV_EINVAL; // Contains a nul byte.
228+
}
229+
230+
uv_fs_t req;
231+
232+
const int fd = uv_fs_open(nullptr, &req, path, O_RDONLY, 0, nullptr);
233+
uv_fs_req_cleanup(&req);
234+
if (fd < 0) {
235+
return static_cast<ssize_t>(fd);
236+
}
237+
238+
std::shared_ptr<void> defer_close(nullptr, [fd] (...) {
239+
uv_fs_t close_req;
240+
CHECK_EQ(0, uv_fs_close(nullptr, &close_req, fd, nullptr));
241+
uv_fs_req_cleanup(&close_req);
242+
});
243+
244+
const size_t kBlockSize = 32 << 10;
245+
size_t offset = 0;
246+
while (true) {
247+
contents->resize(offset + kBlockSize);
248+
249+
uv_buf_t buf;
250+
buf.base = &(*contents)[offset];
251+
buf.len = kBlockSize;
252+
253+
const ssize_t r = uv_fs_read(nullptr, &req, fd, &buf, 1, offset, nullptr);
254+
uv_fs_req_cleanup(&req);
255+
256+
if (r < 0) {
257+
return r;
258+
}
259+
260+
offset += r;
261+
262+
if (static_cast<size_t>(r) != kBlockSize) {
263+
break;
264+
}
265+
}
266+
267+
return static_cast<ssize_t>(offset);
268+
}
269+
270+
Maybe<std::string> ReadFileSync(const std::string& path) {
271+
std::string contents;
272+
const ssize_t nread = ReadFileSyncRaw(path.c_str(), path.length(), &contents);
273+
if (nread < 0) {
274+
return Nothing<std::string>();
275+
}
276+
contents.resize(static_cast<size_t>(nread));
277+
return Just(contents);
278+
}
279+
221280
void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) {
222281
#ifdef _WIN32
223282
GetLocalTime(tm_struct);

src/util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,10 @@ class PersistentToLocal {
764764
}
765765
};
766766

767+
ssize_t ReadFileSyncRaw(
768+
const char* path, size_t path_len, std::string* contents);
769+
v8::Maybe<std::string> ReadFileSync(const std::string& path);
770+
767771
} // namespace node
768772

769773
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)