Skip to content

Commit c77be74

Browse files
authored
Honor buffer.byteOffset in read/write for nodefs (#25977)
I ran into this when writing a bunch of files from a tar archive to the file system, where each file was a subarray of the main buffer.
1 parent 8694832 commit c77be74

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

src/lib/libnodefs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ addToLibrary({
272272
},
273273
read(stream, buffer, offset, length, position) {
274274
return NODEFS.tryFSOperation(() =>
275-
fs.readSync(stream.nfd, new Int8Array(buffer.buffer, offset, length), 0, length, position)
275+
fs.readSync(stream.nfd, buffer, offset, length, position)
276276
);
277277
},
278278
write(stream, buffer, offset, length, position) {
279279
return NODEFS.tryFSOperation(() =>
280-
fs.writeSync(stream.nfd, new Int8Array(buffer.buffer, offset, length), 0, length, position)
280+
fs.writeSync(stream.nfd, buffer, offset, length, position)
281281
);
282282
},
283283
llseek(stream, offset, whence) {

src/lib/libnoderawfs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ addToLibrary({
207207
}
208208
var seeking = typeof position != 'undefined';
209209
if (!seeking && stream.seekable) position = stream.position;
210-
var bytesRead = fs.readSync(stream.nfd, new Int8Array(buffer.buffer, offset, length), 0, length, position);
210+
var bytesRead = fs.readSync(stream.nfd, buffer, offset, length, position);
211211
// update position marker when non-seeking
212212
if (!seeking) stream.position += bytesRead;
213213
return bytesRead;
@@ -223,7 +223,7 @@ addToLibrary({
223223
}
224224
var seeking = typeof position != 'undefined';
225225
if (!seeking && stream.seekable) position = stream.position;
226-
var bytesWritten = fs.writeSync(stream.nfd, new Int8Array(buffer.buffer, offset, length), 0, length, position);
226+
var bytesWritten = fs.writeSync(stream.nfd, buffer, offset, length, position);
227227
// update position marker when non-seeking
228228
if (!seeking) stream.position += bytesWritten;
229229
return bytesWritten;

test/fs/test_writeFile.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88

99
int main() {
1010
EM_ASM(
11+
const buf = Uint8Array.from('c=3\nd=4\ne=5', x => x.charCodeAt(0));
1112
FS.writeFile("testfile", "a=1\nb=2\n");
12-
FS.writeFile("testfile", new Uint8Array([99, 61, 51]) /* c=3 */, { flags: "a" });
13+
FS.writeFile("testfile", buf.subarray(4, 7) /* d=4 */, { flags: "a" });
1314
);
1415

1516
std::ifstream file("testfile");
1617

1718
while (!file.eof() && !file.fail()) {
1819
std::string line;
1920
getline(file, line);
20-
std::string name;
21+
std::string key;
22+
std::string val;
2123

2224
std::cout << "read " << line << std::endl;
2325

@@ -35,7 +37,10 @@ int main() {
3537
continue;
3638
}
3739

38-
name = line.substr(0, equalsPos);
40+
key = line.substr(0, equalsPos);
41+
val = line.substr(equalsPos + 1);
42+
43+
std::cout << "parsed " << key << "=" << val << std::endl;
3944
}
4045

4146
return 0;

test/fs/test_writeFile.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
read a=1
2+
parsed a=1
23
read b=2
3-
read c=3
4+
parsed b=2
5+
read d=4
6+
parsed d=4

0 commit comments

Comments
 (0)