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
64 changes: 63 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const {

const {
FSReqCallback,
ReadFileJob,
} = binding;
const { toPathIfFileURL } = require('internal/url');
const {
Expand All @@ -98,6 +99,7 @@ const {
const {
constants: {
kIoMaxLength,
kReadFileBufferLength,
kMaxUserId,
},
copyObject,
Expand Down Expand Up @@ -428,10 +430,70 @@ function readFile(path, options, callback) {
return;

const flagsNumber = stringToFlags(options.flag, 'options.flag');
path = getValidatedPath(path);
if (options.buffer === undefined) {
// Open + fstat + read + close in one thread pool round trip for files of
// up to one chunk; larger files come back as an open fd + size and take
// the chunked reader below (readFileAfterOneShot). `true`: a handed-back
// fd will be closed through fs.close(), so track it as unmanaged.
const job = new ReadFileJob(path, flagsNumber, kReadFileBufferLength, true);
job.context = context;
job.ondone = readFileAfterOneShot;
const accessError = job.run(path);
if (accessError !== undefined) {
// Not scheduled: report it the way the request-based open() did.
callback(accessError);
}
return;
}
const req = new FSReqCallback();
req.context = context;
req.oncomplete = readFileAfterOpen;
binding.open(getValidatedPath(path), flagsNumber, 0o666, req);
binding.open(path, flagsNumber, 0o666, req);
}

function readFileAfterOneShot(err, buffer, fd, size, closeErr) {
const context = this.context;
if (err) {
context.callback(err);
return;
}
if (fd !== -1) {
// (context.read() below performs the abort check for this case.)
// Larger than one chunk: continue exactly like after open + fstat.
context.fd = fd;
context.size = size;
if (size > kIoMaxLength) {
return context.close(new ERR_FS_FILE_TOO_LARGE(size));
}
try {
context.prepare();
} catch (err) {
return context.close(err);
}
context.read();
return;
}
if (closeErr) {
context.callback(closeErr);
return;
}
if (context.signal?.aborted) {
// An abort that arrived while the read was in flight wins, as it did when
// it was noticed between the open/fstat/read steps.
context.callback(new AbortError(undefined, { cause: context.signal.reason }));
return;
}
let result = buffer;
if (context.encoding) {
try {
result = buffer.toString(context.encoding);
} catch (err) {
context.callback(err);
return;
}
}
context.callback(null, result);
}

function tryStatSync(fd, isUserFd) {
Expand Down
67 changes: 56 additions & 11 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -1211,26 +1211,32 @@ async function readFileHandleWithUserBuffer(filehandle, options, size) {
return encoding ? buffer.toString(encoding) : buffer.subarray(0, totalRead);
}

async function readFileHandle(filehandle, options) {
async function readFileHandle(filehandle, options, knownRegularFileSize) {
const signal = options?.signal;
const encoding = options?.encoding;
const decoder = encoding && new StringDecoder(encoding);

checkAborted(signal);

const statFields = await PromisePrototypeThen(
binding.fstat(filehandle.fd, false, kUsePromises),
undefined,
handleErrorFromBinding,
);

checkAborted(signal);

let size = 0;
let length = 0;
if ((statFields[1/* mode */] & S_IFMT) === S_IFREG) {
size = statFields[8/* size */];
if (knownRegularFileSize !== undefined) {
// Handed over by readFile() together with an already open fd.
size = knownRegularFileSize;
length = encoding ? MathMin(size, kReadFileBufferLength) : size;
} else {
const statFields = await PromisePrototypeThen(
binding.fstat(filehandle.fd, false, kUsePromises),
undefined,
handleErrorFromBinding,
);

checkAborted(signal);

if ((statFields[1/* mode */] & S_IFMT) === S_IFREG) {
size = statFields[8/* size */];
length = encoding ? MathMin(size, kReadFileBufferLength) : size;
}
}
if (length === 0) {
length = kReadFileUnknownBufferLength;
Expand Down Expand Up @@ -2146,10 +2152,49 @@ async function readFile(path, options) {

checkAborted(options.signal);

if (options.buffer === undefined && vfsState.handlers === null) {
// Open + fstat + read + close in one thread pool round trip for files of
// up to one chunk; larger files come back as an open fd + size and are
// read by readFileHandle() as before.
path = getValidatedPath(path);
const { 0: buffer, 1: fd, 2: size } = await readFileInOneRoundTrip(path, stringToFlags(flag));
if (fd === -1) {
checkAborted(options.signal); // An abort during the read still wins.
return options.encoding ? buffer.toString(options.encoding) : buffer;
}
const filehandle = new FileHandle(new binding.FileHandle(fd));
return handleFdClose(readFileHandle(filehandle, options, size), filehandle.close);
}

const fd = await open(path, flag, 0o666);
return handleFdClose(readFileHandle(fd, options), fd.close);
}

/**
* @param {string|Buffer} path Validated path
* @param {number} flagsNumber
* @returns {Promise<[Buffer|undefined, number, number|undefined]>} [buffer, -1] or [undefined, fd, size]
*/
function readFileInOneRoundTrip(path, flagsNumber) {
return new Promise((resolve, reject) => {
const job = new binding.ReadFileJob(path, flagsNumber, kReadFileBufferLength);
job.ondone = (err, buffer, fd, size, closeErr) => {
const error = err ?? closeErr;
if (error != null) {
ErrorCaptureStackTrace(error, readFileInOneRoundTrip);
reject(error);
} else {
resolve([buffer, fd, size]);
}
};
const accessError = job.run(path);
if (accessError !== undefined) {
ErrorCaptureStackTrace(accessError, readFileInOneRoundTrip);
reject(accessError);
}
});
}

async function* _watch(filename, options = kEmptyObject) {
const h = vfsState.handlers;
if (h !== null) {
Expand Down
Loading
Loading