Bug 662253 - Add FileUtils.openFileOutputStream to open non-safe file output streams. r=gavin
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Mon, 06 Jun 2011 20:29:34 +0200
changeset 70566 b0e0455d510273b88dfa6bc1eec2f52cd8e118af
parent 70565 615040e391dee59f58e35847c0731513e2e0d1a2
child 70567 874ac197d20658685ec9d8e81222636d407ed537
push id107
push userpweitershausen@mozilla.com
push dateMon, 06 Jun 2011 18:31:27 +0000
treeherderservices-central@c69124dfd7b7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin
bugs662253
milestone7.0a1
Bug 662253 - Add FileUtils.openFileOutputStream to open non-safe file output streams. r=gavin
toolkit/mozapps/shared/FileUtils.jsm
toolkit/mozapps/shared/test/unit/test_FileUtils.js
--- a/toolkit/mozapps/shared/FileUtils.jsm
+++ b/toolkit/mozapps/shared/FileUtils.jsm
@@ -16,16 +16,17 @@
 # The Original Code is mozilla.org Code.
 #
 # The Initial Developer of the Original Code is Ben Goodger.
 # Portions created by the Initial Developer are Copyright (C) 2004
 # the Initial Developer. All Rights Reserved.
 #
 # Contributor(s):
 #  Robert Strong <robert.bugzilla@gmail.com>
+#  Philipp von Weitershausen <philipp@weitershausen.de>
 #
 # Alternatively, the contents of this file may be used under the terms of
 # either the GNU General Public License Version 2 or later (the "GPL"), or
 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 # in which case the provisions of the GPL or the LGPL are applicable instead
 # of those above. If you wish to allow use of your version of this file only
 # under the terms of either the GPL or the LGPL, and not to allow others to
 # use your version of this file under the terms of the MPL, indicate your
@@ -99,28 +100,48 @@ var FileUtils = {
         dir.create(Ci.nsILocalFile.DIRECTORY_TYPE, this.PERMS_DIRECTORY);
     }
     if (!followLinks)
       dir.followLinks = false;
     return dir;
   },
 
   /**
+   * Opens a file output stream for writing.
+   * @param   file
+   *          The file to write to.
+   * @param   modeFlags
+   *          (optional) File open flags. Can be undefined.
+   * @returns nsIFileOutputStream to write to.
+   * @note The stream is initialized with the DEFER_OPEN behavior flag.
+   *       See nsIFileOutputStream.
+   */
+  openFileOutputStream: function FileUtils_openFileOutputStream(file, modeFlags) {
+    var fos = Cc["@mozilla.org/network/file-output-stream;1"].
+              createInstance(Ci.nsIFileOutputStream);
+    return this._initFileOutputStream(fos, file, modeFlags);
+  },
+
+  /**
    * Opens a safe file output stream for writing.
    * @param   file
    *          The file to write to.
    * @param   modeFlags
    *          (optional) File open flags. Can be undefined.
    * @returns nsIFileOutputStream to write to.
    * @note The stream is initialized with the DEFER_OPEN behavior flag.
    *       See nsIFileOutputStream.
    */
   openSafeFileOutputStream: function FileUtils_openSafeFileOutputStream(file, modeFlags) {
     var fos = Cc["@mozilla.org/network/safe-file-output-stream;1"].
               createInstance(Ci.nsIFileOutputStream);
+    return this._initFileOutputStream(fos, file, modeFlags);
+  },
+
+ _initFileOutputStream: function FileUtils__initFileOutputStream(fos, file, modeFlags) {
     if (modeFlags === undefined)
       modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
     fos.init(file, modeFlags, this.PERMS_FILE, fos.DEFER_OPEN);
     return fos;
   },
 
   /**
    * Closes a safe file output stream.
--- a/toolkit/mozapps/shared/test/unit/test_FileUtils.js
+++ b/toolkit/mozapps/shared/test/unit/test_FileUtils.js
@@ -86,16 +86,54 @@ add_test(function test_getDir_shouldCrea
   other.append("d");
   do_check_true(other.isDirectory());
   other.append("foodir");
   do_check_true(dir.equals(other));
 
   run_next_test();
 });
 
+add_test(function test_openFileOutputStream_defaultFlags() {
+  let file = FileUtils.getFile("ProfD", ["george"]);
+  let fos = FileUtils.openFileOutputStream(file);
+  do_check_true(fos instanceof Components.interfaces.nsIFileOutputStream);
+
+  // FileUtils.openFileOutputStream() opens the stream with DEFER_OPEN
+  // which means the file will not be open until we write to it.
+  do_check_false(file.exists());
+
+  let data = "imagine";
+  fos.write(data, data.length);
+  do_check_true(file.exists());
+
+  // No nsIXULRuntime in xpcshell, so use this trick to determine whether we're
+  // on Windows.
+  if ("@mozilla.org/windows-registry-key;1" in Components.classes) {
+    do_check_eq(file.permissions, 0666);
+  } else {
+    do_check_eq(file.permissions, FileUtils.PERMS_FILE);
+  }
+
+  run_next_test();
+});
+
+// openFileOutputStream will uses MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE
+// as the default mode flags, but we can pass in our own if we want to.
+add_test(function test_openFileOutputStream_modeFlags() {
+  let file = FileUtils.getFile("ProfD", ["ringo"]);
+  let fos = FileUtils.openFileOutputStream(file, FileUtils.MODE_WRONLY);
+  let data = "yesterday";
+  do_check_throws(function () {
+    fos.write(data, data.length);
+  }, Components.results.NS_ERROR_FILE_NOT_FOUND);
+  do_check_false(file.exists());
+
+  run_next_test();
+});
+
 add_test(function test_openSafeFileOutputStream_defaultFlags() {
   let file = FileUtils.getFile("ProfD", ["john"]);
   let fos = FileUtils.openSafeFileOutputStream(file);
   do_check_true(fos instanceof Components.interfaces.nsIFileOutputStream);
   do_check_true(fos instanceof Components.interfaces.nsISafeOutputStream);
 
   // FileUtils.openSafeFileOutputStream() opens the stream with DEFER_OPEN
   // which means the file will not be open until we write to it.