From 7631e2c12730dec621ca803c9cca3e9418a03915 Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Sun, 27 Oct 2013 19:03:25 -0400 Subject: [PATCH] Bug 912457 - Expose async |flush()| in OS.File. r=yoric --- .../osfile/modules/osfile_async_front.jsm | 16 ++++++++++ .../osfile/modules/osfile_async_worker.js | 6 ++++ .../osfile/modules/osfile_unix_front.jsm | 5 ++++ .../osfile/modules/osfile_win_front.jsm | 5 ++++ .../tests/xpcshell/test_osfile_async_flush.js | 30 +++++++++++++++++++ .../osfile/tests/xpcshell/xpcshell.ini | 1 + 6 files changed, 63 insertions(+) create mode 100644 toolkit/components/osfile/tests/xpcshell/test_osfile_async_flush.js diff --git a/toolkit/components/osfile/modules/osfile_async_front.jsm b/toolkit/components/osfile/modules/osfile_async_front.jsm index f154ea0f515..a340adb8626 100644 --- a/toolkit/components/osfile/modules/osfile_async_front.jsm +++ b/toolkit/components/osfile/modules/osfile_async_front.jsm @@ -485,6 +485,22 @@ File.prototype = { setPosition: function setPosition(pos, whence) { return Scheduler.post("File_prototype_setPosition", [this._fdmsg, pos, whence]); + }, + + /** + * Flushes the file's buffers and causes all buffered data + * to be written. + * Disk flushes are very expensive and therefore should be used carefully, + * sparingly and only in scenarios where it is vital that data survives + * system crashes. Even though the function will be executed off the + * main-thread, it might still affect the overall performance of any running + * application. + * + * @return {promise} + */ + flush: function flush() { + return Scheduler.post("File_prototype_flush", + [this._fdmsg]); } }; diff --git a/toolkit/components/osfile/modules/osfile_async_worker.js b/toolkit/components/osfile/modules/osfile_async_worker.js index 2b1a96af625..a306da91b12 100644 --- a/toolkit/components/osfile/modules/osfile_async_worker.js +++ b/toolkit/components/osfile/modules/osfile_async_worker.js @@ -408,6 +408,12 @@ if (this.Components) { return this.getPosition(); }); }, + File_prototype_flush: function flush(fd) { + return withFile(fd, + function do_flush() { + return this.flush(); + }); + }, // Methods of OS.File.DirectoryIterator DirectoryIterator_prototype_next: function next(dir) { return withDir(dir, diff --git a/toolkit/components/osfile/modules/osfile_unix_front.jsm b/toolkit/components/osfile/modules/osfile_unix_front.jsm index bd572d8264f..324b83da585 100644 --- a/toolkit/components/osfile/modules/osfile_unix_front.jsm +++ b/toolkit/components/osfile/modules/osfile_unix_front.jsm @@ -171,6 +171,11 @@ /** * Flushes the file's buffers and causes all buffered data * to be written. + * Disk flushes are very expensive and therefore should be used carefully, + * sparingly and only in scenarios where it is vital that data survives + * system crashes. Even though the function will be executed off the + * main-thread, it might still affect the overall performance of any + * running application. * * @throws {OS.File.Error} In case of I/O error. */ diff --git a/toolkit/components/osfile/modules/osfile_win_front.jsm b/toolkit/components/osfile/modules/osfile_win_front.jsm index a175d8a231e..085e50db027 100644 --- a/toolkit/components/osfile/modules/osfile_win_front.jsm +++ b/toolkit/components/osfile/modules/osfile_win_front.jsm @@ -191,6 +191,11 @@ /** * Flushes the file's buffers and causes all buffered data * to be written. + * Disk flushes are very expensive and therefore should be used carefully, + * sparingly and only in scenarios where it is vital that data survives + * system crashes. Even though the function will be executed off the + * main-thread, it might still affect the overall performance of any + * running application. * * @throws {OS.File.Error} In case of I/O error. */ diff --git a/toolkit/components/osfile/tests/xpcshell/test_osfile_async_flush.js b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_flush.js new file mode 100644 index 00000000000..9ed087f4e9d --- /dev/null +++ b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_flush.js @@ -0,0 +1,30 @@ +"use strict"; + +Components.utils.import("resource://gre/modules/osfile.jsm"); +Components.utils.import("resource://gre/modules/Task.jsm"); + +function run_test() { + do_test_pending(); + run_next_test(); +} + +/** + * Test to ensure that |File.prototype.flush| is available in the async API. + */ + +add_task(function test_flush() { + let path = OS.Path.join(OS.Constants.Path.tmpDir, + "test_osfile_async_flush.tmp"); + let file = yield OS.File.open(path, {trunc: true, write: true}); + try { + try { + yield file.flush(); + } finally { + yield file.close(); + } + } finally { + yield OS.File.remove(path); + } +}); + +add_task(do_test_finished); diff --git a/toolkit/components/osfile/tests/xpcshell/xpcshell.ini b/toolkit/components/osfile/tests/xpcshell/xpcshell.ini index e2f916e0fc6..04180820ed5 100644 --- a/toolkit/components/osfile/tests/xpcshell/xpcshell.ini +++ b/toolkit/components/osfile/tests/xpcshell/xpcshell.ini @@ -8,6 +8,7 @@ tail = [test_osfile_async_append.js] [test_osfile_async_bytes.js] [test_osfile_async_copy.js] +[test_osfile_async_flush.js] [test_profiledir.js] [test_logging.js] [test_creationDate.js]