diff --git a/toolkit/mozapps/shared/FileUtils.jsm b/toolkit/mozapps/shared/FileUtils.jsm index 2f7f5bcf59f8..27f3bd6e4170 100644 --- a/toolkit/mozapps/shared/FileUtils.jsm +++ b/toolkit/mozapps/shared/FileUtils.jsm @@ -21,6 +21,7 @@ # # Contributor(s): # Robert Strong +# Philipp von Weitershausen # # 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 @@ -103,6 +104,22 @@ var FileUtils = { 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 @@ -116,6 +133,10 @@ var FileUtils = { 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); diff --git a/toolkit/mozapps/shared/test/unit/test_FileUtils.js b/toolkit/mozapps/shared/test/unit/test_FileUtils.js index 3e7bf8b67598..832ed84e305d 100644 --- a/toolkit/mozapps/shared/test/unit/test_FileUtils.js +++ b/toolkit/mozapps/shared/test/unit/test_FileUtils.js @@ -91,6 +91,44 @@ add_test(function test_getDir_shouldCreate() { 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);