Skip to content

Commit 6710c0c

Browse files
Hoodhoodmane
authored andcommitted
Fix dup
This fixes two problems with the `dup` system calls: 1. `dup` expects that every file descriptor has a corresponding file (so pipes and (#14640) files that have been unlinked (#15012) cannot be duplicated ). 2. The dup'd file descriptor does not share flags and position with the original file desciptor. This is a simplification of an upstream pull request that would fix this problem. https://github.com/emscripten-core/emscripten/pull/9396/files This patch is simpler than the upstream one but leaves NODERAWFS broken.
1 parent bbb6bf9 commit 6710c0c

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/library_fs.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ FS.staticInit();` +
400400
// SOCKFS is completed.
401401
createStream: (stream, fd_start, fd_end) => {
402402
if (!FS.FSStream) {
403-
FS.FSStream = /** @constructor */ function(){};
403+
FS.FSStream = /** @constructor */ function(){ this.shared = {}; };
404404
FS.FSStream.prototype = {
405405
object: {
406406
get: function() { return this.node; },
@@ -414,7 +414,11 @@ FS.staticInit();` +
414414
},
415415
isAppend: {
416416
get: function() { return (this.flags & {{{ cDefine('O_APPEND') }}}); }
417-
}
417+
},
418+
get flags() { return this.shared.flags; },
419+
set flags(value) { this.shared.flags = value; },
420+
get position() { return this.shared.position; },
421+
set position(value) { this.shared.position = value; },
418422
};
419423
}
420424
// clone it, so we can return an instance of FSStream

src/library_syscall.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ var SyscallsLibrary = {
131131
}
132132
return 0;
133133
},
134-
doDup: function(path, flags, suggestFD) {
134+
doDup: function(stream, flags, suggestFD) {
135135
var suggest = FS.getStream(suggestFD);
136136
if (suggest) FS.close(suggest);
137-
return FS.open(path, flags, 0, suggestFD, suggestFD).fd;
137+
return FS.createStream(stream, suggestFD, suggestFD).fd;
138138
},
139139
doReadv: function(stream, iov, iovcnt, offset) {
140140
var ret = 0;
@@ -292,7 +292,7 @@ var SyscallsLibrary = {
292292
},
293293
__syscall_dup: function(fd) {
294294
var old = SYSCALLS.getStreamFromFD(fd);
295-
return FS.open(old.path, old.flags, 0).fd;
295+
return FS.createStream(old, 0).fd;
296296
},
297297
__syscall_pipe__deps: ['$PIPEFS'],
298298
__syscall_pipe: function(fdPtr) {
@@ -821,7 +821,7 @@ var SyscallsLibrary = {
821821
return -{{{ cDefine('EINVAL') }}};
822822
}
823823
var newStream;
824-
newStream = FS.open(stream.path, stream.flags, 0, arg);
824+
newStream = FS.createStream(stream, arg);
825825
return newStream.fd;
826826
}
827827
case {{{ cDefine('F_GETFD') }}}:
@@ -1036,7 +1036,7 @@ var SyscallsLibrary = {
10361036
assert(!flags);
10371037
#endif
10381038
if (old.fd === suggestFD) return -{{{ cDefine('EINVAL') }}};
1039-
return SYSCALLS.doDup(old.path, old.flags, suggestFD);
1039+
return SYSCALLS.doDup(old, suggestFD);
10401040
},
10411041
};
10421042

0 commit comments

Comments
 (0)