aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-06-11 17:34:30 -0700
committerAlon Zakai <alonzakai@gmail.com>2014-06-11 17:34:30 -0700
commitf0c0f89da70c1924237b4befffef0a6745579e39 (patch)
tree8bca55c131a0c28e475c16e498a0996e0ffb0f26
parent6ef510e386345042ae720269c6c120a7c15252db (diff)
parent00a321f5955538dd9eef324901d7e3126600ddaa (diff)
Merge pull request #2424 from gsathya/fix_append_bug
Fix append bug
-rw-r--r--AUTHORS2
-rw-r--r--src/library_fs.js8
-rw-r--r--tests/fs/test_append.c24
-rw-r--r--tests/test_core.py5
4 files changed, 34 insertions, 5 deletions
diff --git a/AUTHORS b/AUTHORS
index d356a05b..5930c012 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -144,4 +144,4 @@ a license to everyone to use it as detailed in LICENSE.)
* Jason Green <jason@transgaming.com> (copyright owned by TransGaming, Inc.)
* Ningxin Hu <ningxin.hu@intel.com> (copyright owned by Intel)
* Nicolas Guillemot <nlguillemot@gmail.com>
-
+* Sathyanarayanan Gunasekaran <gsathya.ceg@gmail.com> (copyright owned by Mozilla Foundation)
diff --git a/src/library_fs.js b/src/library_fs.js
index 1fff6348..d825892c 100644
--- a/src/library_fs.js
+++ b/src/library_fs.js
@@ -1077,6 +1077,10 @@ mergeInto(LibraryManager.library, {
if (!stream.stream_ops.write) {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
}
+ if (stream.flags & {{{ cDefine('O_APPEND') }}}) {
+ // seek to the end before writing in append mode
+ FS.llseek(stream, 0, {{{ cDefine('SEEK_END') }}});
+ }
var seeking = true;
if (typeof position === 'undefined') {
position = stream.position;
@@ -1084,10 +1088,6 @@ mergeInto(LibraryManager.library, {
} else if (!stream.seekable) {
throw new FS.ErrnoError(ERRNO_CODES.ESPIPE);
}
- if (stream.flags & {{{ cDefine('O_APPEND') }}}) {
- // seek to the end before writing in append mode
- FS.llseek(stream, 0, {{{ cDefine('SEEK_END') }}});
- }
var bytesWritten = stream.stream_ops.write(stream, buffer, offset, length, position, canOwn);
if (!seeking) stream.position += bytesWritten;
try {
diff --git a/tests/fs/test_append.c b/tests/fs/test_append.c
new file mode 100644
index 00000000..27909ba3
--- /dev/null
+++ b/tests/fs/test_append.c
@@ -0,0 +1,24 @@
+#include<assert.h>
+#include<stdio.h>
+
+int main (int argc, char *argv[])
+{
+ FILE *fp;
+ int res;
+ long len;
+
+ fp = fopen("testappend", "wb+");
+ res = fwrite("1234567890", 10, 1, fp);
+ fclose(fp);
+
+ fp = fopen("testappend", "ab+");
+ res = fwrite("1234567890", 10, 1, fp);
+
+ fseek(fp, -7, SEEK_END);
+ len = ftell(fp);
+ assert(len == 13);
+ fclose(fp);
+
+ puts("success");
+ return 0;
+}
diff --git a/tests/test_core.py b/tests/test_core.py
index 13b6c0f3..08e3594e 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -4348,6 +4348,11 @@ def process(filename):
out = path_from_root('tests', 'fs', 'test_writeFile.out')
self.do_run_from_file(src, out)
+ def test_fs_append(self):
+ if self.emcc_args is None: return self.skip('requires emcc')
+ src = open(path_from_root('tests', 'fs', 'test_append.c'), 'r').read()
+ self.do_run(src, 'success', force_c=True)
+
def test_unistd_access(self):
self.clear()
if not self.is_emscripten_abi(): return self.skip('asmjs-unknown-emscripten needed for inline js')