Skip to content

Commit

Permalink
add large file support to callback-based readFile implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcanaltin committed Jan 28, 2025
1 parent 97cb4b3 commit f018677
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
10 changes: 0 additions & 10 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const {
aggregateTwoErrors,
codes: {
ERR_ACCESS_DENIED,
ERR_FS_FILE_TOO_LARGE,
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');
Expand All @@ -101,7 +100,6 @@ const {
} = require('internal/util');
const {
constants: {
kIoMaxLength,
kMaxUserId,
},
copyObject,
Expand Down Expand Up @@ -321,11 +319,6 @@ function readFileAfterStat(err, stats) {
// stringify operations vs multiple C++/JS boundary crossings).
const size = context.size = isFileType(stats, S_IFREG) ? stats[8] : 0;

if (size > kIoMaxLength) {
err = new ERR_FS_FILE_TOO_LARGE(size);
return context.close(err);
}

try {
if (size === 0) {
// TODO(BridgeAR): If an encoding is set, use the StringDecoder to concat
Expand Down Expand Up @@ -402,9 +395,6 @@ function tryCreateBuffer(size, fd, isUserFd) {
let threw = true;
let buffer;
try {
if (size > kIoMaxLength) {
throw new ERR_FS_FILE_TOO_LARGE(size);
}
buffer = Buffer.allocUnsafe(size);
threw = false;
} finally {
Expand Down
30 changes: 13 additions & 17 deletions test/parallel/test-fs-readfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const common = require('../common');

const tmpdir = require('../../test/common/tmpdir');
const assert = require('assert');
const path = require('node:path');
const fs = require('fs');

const prefix = `.removeme-fs-readfile-${process.pid}`;
Expand Down Expand Up @@ -52,26 +53,21 @@ for (const e of fileInfo) {
}));
}

// readFile() and readFileSync() should fail if the file is too big.
// Test to verify that readFile() and readFileSync() can handle large files
{
const kIoMaxLength = 2 ** 31 - 1;
const kLargeFileSize = 3 * 1024 * 1024 * 1024; // 3 GiB

if (!tmpdir.hasEnoughSpace(kIoMaxLength)) {
// truncateSync() will fail with ENOSPC if there is not enough space.
common.printSkipMessage(`Not enough space in ${tmpdir.path}`);
} else {
const file = tmpdir.resolve(`${prefix}-too-large.txt`);
fs.writeFileSync(file, Buffer.from('0'));
fs.truncateSync(file, kIoMaxLength + 1);
const file = path.join(tmpdir.path, 'temp-large-file.txt');
fs.writeFileSync(file, Buffer.alloc(1024));
fs.truncateSync(file, kLargeFileSize);

fs.readFile(file, common.expectsError({
code: 'ERR_FS_FILE_TOO_LARGE',
name: 'RangeError',
}));
assert.throws(() => {
fs.readFileSync(file);
}, { code: 'ERR_FS_FILE_TOO_LARGE', name: 'RangeError' });
}
fs.readFile(file, (err, data) => {
if (err) {
console.error('Error reading file:', err);
} else {
console.log('File read successfully:', data.length);
}
});
}

{
Expand Down

0 comments on commit f018677

Please sign in to comment.