1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 export var FileUtils = {
15 PERMS_DIRECTORY: 0o755,
18 * Gets a directory at the specified hierarchy under a nsIDirectoryService
21 * The Directory Service Key to start from
23 * An array of path components to locate beneath the directory
25 * @return nsIFile object for the location specified.
27 getDir: function FileUtils_getDir(key, pathArray) {
28 var dir = Services.dirsvc.get(key, Ci.nsIFile);
29 for (var i = 0; i < pathArray.length; ++i) {
30 dir.append(pathArray[i]);
36 * Opens a file output stream for writing.
38 * The file to write to.
40 * (optional) File open flags. Can be undefined.
41 * @returns nsIFileOutputStream to write to.
42 * @note The stream is initialized with the DEFER_OPEN behavior flag.
43 * See nsIFileOutputStream.
45 openFileOutputStream: function FileUtils_openFileOutputStream(
49 var fos = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
50 Ci.nsIFileOutputStream
52 return this._initFileOutputStream(fos, file, modeFlags);
56 * Opens an atomic file output stream for writing.
58 * The file to write to.
60 * (optional) File open flags. Can be undefined.
61 * @returns nsIFileOutputStream to write to.
62 * @note The stream is initialized with the DEFER_OPEN behavior flag.
63 * See nsIFileOutputStream.
64 * OpeanAtomicFileOutputStream is generally better than openSafeFileOutputStream
65 * baecause flushing is not needed in most of the issues.
67 openAtomicFileOutputStream: function FileUtils_openAtomicFileOutputStream(
72 "@mozilla.org/network/atomic-file-output-stream;1"
73 ].createInstance(Ci.nsIFileOutputStream);
74 return this._initFileOutputStream(fos, file, modeFlags);
78 * Opens a safe file output stream for writing.
80 * The file to write to.
82 * (optional) File open flags. Can be undefined.
83 * @returns nsIFileOutputStream to write to.
84 * @note The stream is initialized with the DEFER_OPEN behavior flag.
85 * See nsIFileOutputStream.
87 openSafeFileOutputStream: function FileUtils_openSafeFileOutputStream(
92 "@mozilla.org/network/safe-file-output-stream;1"
93 ].createInstance(Ci.nsIFileOutputStream);
94 return this._initFileOutputStream(fos, file, modeFlags);
97 _initFileOutputStream: function FileUtils__initFileOutputStream(
102 if (modeFlags === undefined) {
103 modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
105 fos.init(file, modeFlags, this.PERMS_FILE, fos.DEFER_OPEN);
110 * Closes an atomic file output stream.
112 * The stream to close.
114 closeAtomicFileOutputStream: function FileUtils_closeAtomicFileOutputStream(
117 if (stream instanceof Ci.nsISafeOutputStream) {
127 * Closes a safe file output stream.
129 * The stream to close.
131 closeSafeFileOutputStream: function FileUtils_closeSafeFileOutputStream(
134 if (stream instanceof Ci.nsISafeOutputStream) {
143 File: Components.Constructor(
144 "@mozilla.org/file/local;1",