Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 44 additions & 34 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'use strict';

const {
Array,
ArrayFromAsync,
ArrayPrototypePush,
BigIntPrototypeToString,
Expand Down Expand Up @@ -531,12 +532,18 @@ function readFileSync(path, options) {
validateReadFileBufferOptions(options);
const hasUserBuffer = options.buffer !== undefined;

if ((options.encoding === 'utf8' || options.encoding === 'utf-8') &&
!hasUserBuffer) {
if (!hasUserBuffer) {
if (!isInt32(path)) {
path = getValidatedPath(path);
}
return binding.readFileUtf8(path, stringToFlags(options.flag));
const flags = stringToFlags(options.flag);
// C++ fast paths: single native call instead of open/stat/read/close.
if (options.encoding === 'utf8' || options.encoding === 'utf-8') {
return binding.readFileUtf8(path, flags);
}
if (options.encoding === undefined || options.encoding === null) {
return binding.readFileBuffer(path, flags);
}
}

const isUserFd = isFd(path); // File descriptor ownership
Expand Down Expand Up @@ -1756,45 +1763,35 @@ function handleFilePaths({ result, currentPath, context }) {
}

/**
* An iterative algorithm for reading the entire contents of the `basePath` directory.
* This function does not validate `basePath` as a directory. It is passed directly to
* `binding.readdir`.
* Reads the entire contents of the `basePath` directory recursively.
* This function does not validate `basePath` as a directory. It is passed
* directly to `binding.readdirRecursiveSync`.
* @param {string} basePath
* @param {{ encoding: string, withFileTypes: boolean }} options
* @returns {string[] | Dirent[]}
*/
function readdirSyncRecursive(basePath, options) {
const context = {
withFileTypes: Boolean(options.withFileTypes),
encoding: options.encoding,
const withFileTypes = Boolean(options.withFileTypes);
// C++ walk uses scandir dirent types and avoids per-entry stat() for the
// common case (known directory/file types).
const result = binding.readdirRecursiveSync(
basePath,
readdirResults: [],
pathsQueue: [basePath],
};

function read(path) {
const readdirResult = binding.readdir(
path,
context.encoding,
context.withFileTypes,
);

if (readdirResult === undefined) {
return;
}

processReaddirResult({
result: readdirResult,
currentPath: path,
context,
});
options.encoding,
withFileTypes,
);
if (result === undefined) {
return undefined;
}

for (let i = 0; i < context.pathsQueue.length; i++) {
read(context.pathsQueue[i]);
if (!withFileTypes) {
return result;
}

return context.readdirResults;
// result = [names, types, parentPaths]
const { 0: names, 1: types, 2: parentPaths } = result;
const out = new Array(names.length);
for (let i = 0; i < names.length; i++) {
out[i] = new Dirent(names[i], types[i], parentPaths[i]);
}
return out;
}

/**
Expand Down Expand Up @@ -2919,6 +2916,19 @@ function writeFileSync(path, data, options) {
);
}

// C++ fast path for Buffer / TypedArray payloads (no flush option).
if (isArrayBufferView(data) && !flush) {
if (!isInt32(path)) {
path = getValidatedPath(path);
}
return binding.writeFileBuffer(
path,
data,
stringToFlags(flag),
parseFileMode(options.mode, 'mode', 0o666),
);
}

if (!isArrayBufferView(data)) {
validateStringAfterArrayBufferView(data, 'data');
data = Buffer.from(data, options.encoding || 'utf8');
Expand Down
1 change: 1 addition & 0 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details);
V(ERR_FS_CP_NON_DIR_TO_DIR, Error) \
V(ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY, Error) \
V(ERR_FS_EISDIR, Error) \
V(ERR_FS_FILE_TOO_LARGE, RangeError) \
V(ERR_FS_CP_EEXIST, Error) \
V(ERR_FS_CP_SOCKET, Error) \
V(ERR_FS_CP_FIFO_PIPE, Error) \
Expand Down
Loading
Loading