Skip to content

Commit 353fffe

Browse files
authored
Enable fsync() support in src/library_tty.js (#11675)
The fd_sync syscall looks for `fsync` in the fd opts and as far as I cant tell there are no uses of flush (`fflush` is a libc function whereas `fsync` is the syscall equivalent).
1 parent 85793a2 commit 353fffe

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

src/library_tty.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ mergeInto(LibraryManager.library, {
5050
},
5151
close: function(stream) {
5252
// flush any pending line data
53-
stream.tty.ops.flush(stream.tty);
53+
stream.tty.ops.fsync(stream.tty);
5454
},
55-
flush: function(stream) {
56-
stream.tty.ops.flush(stream.tty);
55+
fsync: function(stream) {
56+
stream.tty.ops.fsync(stream.tty);
5757
},
5858
read: function(stream, buffer, offset, length, pos /* ignored */) {
5959
if (!stream.tty || !stream.tty.ops.get_char) {
@@ -156,7 +156,7 @@ mergeInto(LibraryManager.library, {
156156
if (val != 0) tty.output.push(val); // val == 0 would cut text output off in the middle.
157157
}
158158
},
159-
flush: function(tty) {
159+
fsync: function(tty) {
160160
if (tty.output && tty.output.length > 0) {
161161
out(UTF8ArrayToString(tty.output, 0));
162162
tty.output = [];
@@ -172,7 +172,7 @@ mergeInto(LibraryManager.library, {
172172
if (val != 0) tty.output.push(val);
173173
}
174174
},
175-
flush: function(tty) {
175+
fsync: function(tty) {
176176
if (tty.output && tty.output.length > 0) {
177177
err(UTF8ArrayToString(tty.output, 0));
178178
tty.output = [];

src/library_wasi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ var WasiLibrary = {
417417
});
418418
#else
419419
if (stream.stream_ops && stream.stream_ops.fsync) {
420-
return -stream.stream_ops.fsync(stream);
420+
return stream.stream_ops.fsync(stream);
421421
}
422422
return 0; // we can't do anything synchronously; the in-memory FS is already synced to
423423
#endif // ASYNCIFY

test/test_core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5962,6 +5962,9 @@ def test_unistd_curdir(self):
59625962
def test_unistd_close(self):
59635963
self.do_run_in_out_file_test('unistd/close.c')
59645964

5965+
def test_unistd_fsync_stdout(self):
5966+
self.do_run_in_out_file_test(test_file('unistd/fsync_stdout.c'))
5967+
59655968
@also_with_noderawfs
59665969
def test_unistd_pipe(self):
59675970
self.do_runf(test_file('unistd/pipe.c'), 'success')

test/unistd/fsync_stdout.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2011 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <stdio.h>
9+
#include <unistd.h>
10+
11+
int main() {
12+
write(STDOUT_FILENO, "a", 1);
13+
write(STDOUT_FILENO, "b", 1);
14+
write(STDOUT_FILENO, "c", 1);
15+
write(STDOUT_FILENO, "end\n", 4);
16+
17+
write(STDOUT_FILENO, "d", 1);
18+
fsync(STDOUT_FILENO);
19+
write(STDOUT_FILENO, "e", 1);
20+
fsync(STDOUT_FILENO);
21+
write(STDOUT_FILENO, "f", 1);
22+
fsync(STDOUT_FILENO);
23+
return 0;
24+
}

test/unistd/fsync_stdout.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
abcend
2+
d
3+
e
4+
f

0 commit comments

Comments
 (0)