Skip to content

Commit c7b21c3

Browse files
authored
Fix dup'd streams so that point to the same offset/etc (#17184)
1 parent af3d98b commit c7b21c3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/library_fs.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,29 +403,39 @@ FS.staticInit();` +
403403
FS.FSStream = /** @constructor */ function() {
404404
this.shared = { };
405405
};
406-
FS.FSStream.prototype = {
406+
FS.FSStream.prototype = {};
407+
Object.defineProperties(FS.FSStream.prototype, {
407408
object: {
409+
/** @this {FS.FSStream} */
408410
get: function() { return this.node; },
411+
/** @this {FS.FSStream} */
409412
set: function(val) { this.node = val; }
410413
},
411414
isRead: {
415+
/** @this {FS.FSStream} */
412416
get: function() { return (this.flags & {{{ cDefine('O_ACCMODE') }}}) !== {{{ cDefine('O_WRONLY') }}}; }
413417
},
414418
isWrite: {
419+
/** @this {FS.FSStream} */
415420
get: function() { return (this.flags & {{{ cDefine('O_ACCMODE') }}}) !== {{{ cDefine('O_RDONLY') }}}; }
416421
},
417422
isAppend: {
423+
/** @this {FS.FSStream} */
418424
get: function() { return (this.flags & {{{ cDefine('O_APPEND') }}}); }
419425
},
420426
flags: {
427+
/** @this {FS.FSStream} */
421428
get: function() { return this.shared.flags; },
429+
/** @this {FS.FSStream} */
422430
set: function(val) { this.shared.flags = val; },
423431
},
424432
position : {
425-
get function() { return this.shared.position; },
433+
/** @this {FS.FSStream} */
434+
get: function() { return this.shared.position; },
435+
/** @this {FS.FSStream} */
426436
set: function(val) { this.shared.position = val; },
427437
},
428-
};
438+
});
429439
}
430440
// clone it, so we can return an instance of FSStream
431441
stream = Object.assign(new FS.FSStream(), stream);

tests/unistd/dup.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <unistd.h>
1111
#include <fcntl.h>
1212
#include <emscripten.h>
13+
#include <assert.h>
1314

1415

1516
int main() {
@@ -59,5 +60,14 @@ int main() {
5960
read(g, buf, 5);
6061
// should print "buf: abc\n"
6162
printf("buf: %s\n", buf);
63+
64+
65+
int fd1 = open("./blah.txt", O_RDWR | O_CREAT | O_EXCL, 0600);
66+
int fd2 = dup(fd1);
67+
int n = write(fd1, "abcabc\n", 7);
68+
assert(n == 7);
69+
assert(lseek(fd1, 0, SEEK_CUR) == 7);
70+
assert(lseek(fd2, 0, SEEK_CUR) == 7);
71+
6272
return 0;
6373
}

0 commit comments

Comments
 (0)