Skip to content

Commit 4fa7150

Browse files
TimothyGutargos
authored andcommitted
fs: support pseudofiles in promises.readFile
PR-URL: #21497 Fixes: #21331 Refs: http://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html Refs: https://groups.google.com/forum/#!topic/nodejs-dev/rxZ_RoH1Gn0 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 6f8ebc0 commit 4fa7150

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

lib/internal/fs/promises.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ async function writeFileHandle(filehandle, data, options) {
125125
} while (remaining > 0);
126126
}
127127

128+
// Note: This is different from kReadFileBufferLength used for non-promisified
129+
// fs.readFile.
130+
const kReadFileMaxChunkSize = 16384;
131+
128132
async function readFileHandle(filehandle, options) {
129133
const statFields = await binding.fstat(filehandle.fd, false, kUsePromises);
130134

@@ -135,22 +139,19 @@ async function readFileHandle(filehandle, options) {
135139
size = 0;
136140
}
137141

138-
if (size === 0)
139-
return options.encoding ? '' : Buffer.alloc(0);
140-
141142
if (size > kMaxLength)
142143
throw new ERR_FS_FILE_TOO_LARGE(size);
143144

144145
const chunks = [];
145-
const chunkSize = Math.min(size, 16384);
146-
let totalRead = 0;
146+
const chunkSize = size === 0 ?
147+
kReadFileMaxChunkSize :
148+
Math.min(size, kReadFileMaxChunkSize);
147149
let endOfFile = false;
148150
do {
149151
const buf = Buffer.alloc(chunkSize);
150152
const { bytesRead, buffer } =
151-
await read(filehandle, buf, 0, chunkSize, totalRead);
152-
totalRead += bytesRead;
153-
endOfFile = bytesRead !== chunkSize;
153+
await read(filehandle, buf, 0, chunkSize, -1);
154+
endOfFile = bytesRead === 0;
154155
if (bytesRead > 0)
155156
chunks.push(buffer.slice(0, bytesRead));
156157
} while (!endOfFile);

test/parallel/test-fs-promises-file-handle-readFile.js

+17
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,22 @@ async function validateReadFile() {
2828
assert.deepStrictEqual(buffer, readFileData);
2929
}
3030

31+
async function validateReadFileProc() {
32+
// Test to make sure reading a file under the /proc directory works. Adapted
33+
// from test-fs-read-file-sync-hostname.js.
34+
// Refs:
35+
// - https://groups.google.com/forum/#!topic/nodejs-dev/rxZ_RoH1Gn0
36+
// - https://github.com/nodejs/node/issues/21331
37+
38+
// Test is Linux-specific.
39+
if (!common.isLinux)
40+
return;
41+
42+
const fileHandle = await open('/proc/sys/kernel/hostname', 'r');
43+
const hostname = await fileHandle.readFile();
44+
assert.ok(hostname.length > 0);
45+
}
46+
3147
validateReadFile()
48+
.then(() => validateReadFileProc())
3249
.then(common.mustCall());

test/parallel/test-fs-promises-readfile.js

+31-13
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,35 @@ const fn = path.join(tmpdir.path, 'large-file');
1212

1313
common.crashOnUnhandledRejection();
1414

15-
// Creating large buffer with random content
16-
const buffer = Buffer.from(
17-
Array.apply(null, { length: 16834 * 2 })
18-
.map(Math.random)
19-
.map((number) => (number * (1 << 8)))
20-
);
21-
22-
// Writing buffer to a file then try to read it
23-
writeFile(fn, buffer)
24-
.then(() => readFile(fn))
25-
.then((readBuffer) => {
26-
assert.strictEqual(readBuffer.equals(buffer), true);
27-
})
15+
async function validateReadFile() {
16+
// Creating large buffer with random content
17+
const buffer = Buffer.from(
18+
Array.apply(null, { length: 16834 * 2 })
19+
.map(Math.random)
20+
.map((number) => (number * (1 << 8)))
21+
);
22+
23+
// Writing buffer to a file then try to read it
24+
await writeFile(fn, buffer);
25+
const readBuffer = await readFile(fn);
26+
assert.strictEqual(readBuffer.equals(buffer), true);
27+
}
28+
29+
async function validateReadFileProc() {
30+
// Test to make sure reading a file under the /proc directory works. Adapted
31+
// from test-fs-read-file-sync-hostname.js.
32+
// Refs:
33+
// - https://groups.google.com/forum/#!topic/nodejs-dev/rxZ_RoH1Gn0
34+
// - https://github.com/nodejs/node/issues/21331
35+
36+
// Test is Linux-specific.
37+
if (!common.isLinux)
38+
return;
39+
40+
const hostname = await readFile('/proc/sys/kernel/hostname');
41+
assert.ok(hostname.length > 0);
42+
}
43+
44+
validateReadFile()
45+
.then(() => validateReadFileProc())
2846
.then(common.mustCall());

0 commit comments

Comments
 (0)