Bug 662253 - Add FileUtils.openFileOutputStream to open non-safe file output streams. r=gavin

This commit is contained in:
Philipp von Weitershausen 2011-06-06 20:29:34 +02:00
Родитель 1afec6d0c5
Коммит c387a02dcc
2 изменённых файлов: 59 добавлений и 0 удалений

Просмотреть файл

@ -21,6 +21,7 @@
#
# 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
@ -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);

Просмотреть файл

@ -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);