no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / toolkit / modules / FileUtils.sys.mjs
blobbea38e3161c4669638809ecd181eff93f5008abf
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 = {
7   MODE_RDONLY: 0x01,
8   MODE_WRONLY: 0x02,
9   MODE_RDWR: 0x04,
10   MODE_CREATE: 0x08,
11   MODE_APPEND: 0x10,
12   MODE_TRUNCATE: 0x20,
14   PERMS_FILE: 0o644,
15   PERMS_DIRECTORY: 0o755,
17   /**
18    * Gets a directory at the specified hierarchy under a nsIDirectoryService
19    * key.
20    * @param   key
21    *          The Directory Service Key to start from
22    * @param   pathArray
23    *          An array of path components to locate beneath the directory
24    *          specified by |key|
25    * @return  nsIFile object for the location specified.
26    */
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]);
31     }
32     return dir;
33   },
35   /**
36    * Opens a file output stream for writing.
37    * @param   file
38    *          The file to write to.
39    * @param   modeFlags
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.
44    */
45   openFileOutputStream: function FileUtils_openFileOutputStream(
46     file,
47     modeFlags
48   ) {
49     var fos = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
50       Ci.nsIFileOutputStream
51     );
52     return this._initFileOutputStream(fos, file, modeFlags);
53   },
55   /**
56    * Opens an atomic file output stream for writing.
57    * @param   file
58    *          The file to write to.
59    * @param   modeFlags
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.
66    */
67   openAtomicFileOutputStream: function FileUtils_openAtomicFileOutputStream(
68     file,
69     modeFlags
70   ) {
71     var fos = Cc[
72       "@mozilla.org/network/atomic-file-output-stream;1"
73     ].createInstance(Ci.nsIFileOutputStream);
74     return this._initFileOutputStream(fos, file, modeFlags);
75   },
77   /**
78    * Opens a safe file output stream for writing.
79    * @param   file
80    *          The file to write to.
81    * @param   modeFlags
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.
86    */
87   openSafeFileOutputStream: function FileUtils_openSafeFileOutputStream(
88     file,
89     modeFlags
90   ) {
91     var fos = Cc[
92       "@mozilla.org/network/safe-file-output-stream;1"
93     ].createInstance(Ci.nsIFileOutputStream);
94     return this._initFileOutputStream(fos, file, modeFlags);
95   },
97   _initFileOutputStream: function FileUtils__initFileOutputStream(
98     fos,
99     file,
100     modeFlags
101   ) {
102     if (modeFlags === undefined) {
103       modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
104     }
105     fos.init(file, modeFlags, this.PERMS_FILE, fos.DEFER_OPEN);
106     return fos;
107   },
109   /**
110    * Closes an atomic file output stream.
111    * @param   stream
112    *          The stream to close.
113    */
114   closeAtomicFileOutputStream: function FileUtils_closeAtomicFileOutputStream(
115     stream
116   ) {
117     if (stream instanceof Ci.nsISafeOutputStream) {
118       try {
119         stream.finish();
120         return;
121       } catch (e) {}
122     }
123     stream.close();
124   },
126   /**
127    * Closes a safe file output stream.
128    * @param   stream
129    *          The stream to close.
130    */
131   closeSafeFileOutputStream: function FileUtils_closeSafeFileOutputStream(
132     stream
133   ) {
134     if (stream instanceof Ci.nsISafeOutputStream) {
135       try {
136         stream.finish();
137         return;
138       } catch (e) {}
139     }
140     stream.close();
141   },
143   File: Components.Constructor(
144     "@mozilla.org/file/local;1",
145     Ci.nsIFile,
146     "initWithPath"
147   ),