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 var EXPORTED_SYMBOLS = ["FileUtils"];
8 const { XPCOMUtils } = ChromeUtils.import(
9 "resource://gre/modules/XPCOMUtils.jsm"
12 XPCOMUtils.defineLazyServiceGetter(
15 "@mozilla.org/file/directory_service;1",
28 PERMS_DIRECTORY: 0o755,
31 * Gets a file at the specified hierarchy under a nsIDirectoryService key.
33 * The Directory Service Key to start from
35 * An array of path components to locate beneath the directory
36 * specified by |key|. The last item in this array must be the
37 * leaf name of a file.
38 * @return nsIFile object for the file specified. The file is NOT created
39 * if it does not exist, however all required directories along
40 * the way are if pathArray has more than one item.
42 getFile: function FileUtils_getFile(key, pathArray, followLinks) {
43 var file = this.getDir(
45 pathArray.slice(0, -1),
49 file.append(pathArray[pathArray.length - 1]);
54 * Gets a directory at the specified hierarchy under a nsIDirectoryService
57 * The Directory Service Key to start from
59 * An array of path components to locate beneath the directory
62 * true if the directory hierarchy specified in |pathArray|
63 * should be created if it does not exist, false otherwise.
64 * @param followLinks (optional)
65 * true if links should be followed, false otherwise.
66 * @return nsIFile object for the location specified.
68 getDir: function FileUtils_getDir(key, pathArray, shouldCreate, followLinks) {
69 var dir = gDirService.get(key, Ci.nsIFile);
70 for (var i = 0; i < pathArray.length; ++i) {
71 dir.append(pathArray[i]);
76 dir.create(Ci.nsIFile.DIRECTORY_TYPE, this.PERMS_DIRECTORY);
78 if (ex.result != Cr.NS_ERROR_FILE_ALREADY_EXISTS) {
81 // Ignore the exception due to a directory that already exists.
86 dir.followLinks = false;
92 * Opens a file output stream for writing.
94 * The file to write to.
96 * (optional) File open flags. Can be undefined.
97 * @returns nsIFileOutputStream to write to.
98 * @note The stream is initialized with the DEFER_OPEN behavior flag.
99 * See nsIFileOutputStream.
101 openFileOutputStream: function FileUtils_openFileOutputStream(
105 var fos = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
106 Ci.nsIFileOutputStream
108 return this._initFileOutputStream(fos, file, modeFlags);
112 * Opens an atomic file output stream for writing.
114 * The file to write to.
116 * (optional) File open flags. Can be undefined.
117 * @returns nsIFileOutputStream to write to.
118 * @note The stream is initialized with the DEFER_OPEN behavior flag.
119 * See nsIFileOutputStream.
120 * OpeanAtomicFileOutputStream is generally better than openSafeFileOutputStream
121 * baecause flushing is not needed in most of the issues.
123 openAtomicFileOutputStream: function FileUtils_openAtomicFileOutputStream(
128 "@mozilla.org/network/atomic-file-output-stream;1"
129 ].createInstance(Ci.nsIFileOutputStream);
130 return this._initFileOutputStream(fos, file, modeFlags);
134 * Opens a safe file output stream for writing.
136 * The file to write to.
138 * (optional) File open flags. Can be undefined.
139 * @returns nsIFileOutputStream to write to.
140 * @note The stream is initialized with the DEFER_OPEN behavior flag.
141 * See nsIFileOutputStream.
143 openSafeFileOutputStream: function FileUtils_openSafeFileOutputStream(
148 "@mozilla.org/network/safe-file-output-stream;1"
149 ].createInstance(Ci.nsIFileOutputStream);
150 return this._initFileOutputStream(fos, file, modeFlags);
153 _initFileOutputStream: function FileUtils__initFileOutputStream(
158 if (modeFlags === undefined) {
159 modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
161 fos.init(file, modeFlags, this.PERMS_FILE, fos.DEFER_OPEN);
166 * Closes an atomic file output stream.
168 * The stream to close.
170 closeAtomicFileOutputStream: function FileUtils_closeAtomicFileOutputStream(
173 if (stream instanceof Ci.nsISafeOutputStream) {
183 * Closes a safe file output stream.
185 * The stream to close.
187 closeSafeFileOutputStream: function FileUtils_closeSafeFileOutputStream(
190 if (stream instanceof Ci.nsISafeOutputStream) {
199 File: Components.Constructor(
200 "@mozilla.org/file/local;1",