Bug 912457 - Expose async |flush()| in OS.File. r=yoric
authorNils Maier <maierman@web.de>
Sun, 27 Oct 2013 19:03:25 -0400
changeset 152435 57f39d5c71fac208029215152d203716d87595b1
parent 152402 da02805334ece606e3a614cbf4cc1ad4ce493f7e
child 152436 3685c303b72343849658ef2d22c3535648223a6c
push id25540
push userphilringnalda@gmail.com
push dateMon, 28 Oct 2013 04:45:12 +0000
treeherdermozilla-central@59ff3a2a708a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersyoric
bugs912457
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 912457 - Expose async |flush()| in OS.File. r=yoric
toolkit/components/osfile/modules/osfile_async_front.jsm
toolkit/components/osfile/modules/osfile_async_worker.js
toolkit/components/osfile/modules/osfile_unix_front.jsm
toolkit/components/osfile/modules/osfile_win_front.jsm
toolkit/components/osfile/tests/xpcshell/test_osfile_async_flush.js
toolkit/components/osfile/tests/xpcshell/xpcshell.ini
--- a/toolkit/components/osfile/modules/osfile_async_front.jsm
+++ b/toolkit/components/osfile/modules/osfile_async_front.jsm
@@ -480,16 +480,32 @@ File.prototype = {
    * POS_END (from the end of the file) or POS_CUR (from the
    * current position in the file).
    *
    * @return {promise}
    */
   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]);
   }
 };
 
 /**
  * Open a file asynchronously.
  *
  * @return {promise}
  * @resolves {OS.File}
--- a/toolkit/components/osfile/modules/osfile_async_worker.js
+++ b/toolkit/components/osfile/modules/osfile_async_worker.js
@@ -403,16 +403,22 @@ if (this.Components) {
        });
    },
    File_prototype_getPosition: function getPosition(fd) {
      return withFile(fd,
        function do_getPosition() {
          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,
        function do_next() {
          try {
            return File.DirectoryIterator.Entry.toMsg(this.next());
          } catch (x) {
            if (x == StopIteration) {
--- a/toolkit/components/osfile/modules/osfile_unix_front.jsm
+++ b/toolkit/components/osfile/modules/osfile_unix_front.jsm
@@ -166,16 +166,21 @@
      File.prototype.stat = function stat() {
        throw_on_negative("stat", UnixFile.fstat(this.fd, gStatDataPtr));
          return new File.Info(gStatData);
      };
 
      /**
       * 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.
       */
      File.prototype.flush = function flush() {
        throw_on_negative("flush", UnixFile.fsync(this.fd));
      };
 
      // The default unix mode for opening (0600)
--- a/toolkit/components/osfile/modules/osfile_win_front.jsm
+++ b/toolkit/components/osfile/modules/osfile_win_front.jsm
@@ -186,16 +186,21 @@
        throw_on_zero("stat",
          WinFile.GetFileInformationByHandle(this.fd, gFileInfoPtr));
        return new File.Info(gFileInfo);
      };
 
      /**
       * 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.
       */
      File.prototype.flush = function flush() {
        throw_on_zero("flush", WinFile.FlushFileBuffers(this.fd));
      };
 
      // The default sharing mode for opening files: files are not
new file mode 100644
--- /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);
--- a/toolkit/components/osfile/tests/xpcshell/xpcshell.ini
+++ b/toolkit/components/osfile/tests/xpcshell/xpcshell.ini
@@ -3,16 +3,17 @@ head =
 tail =
 
 [test_osfile_closed.js]
 [test_path.js]
 [test_osfile_async.js]
 [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]
 [test_exception.js]
 [test_path_constants.js]
 [test_removeDir.js]
 [test_reset.js]
 [test_shutdown.js]