toolkit/modules/FileUtils.jsm

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:81fe47350d67
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 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/. */
5
6 this.EXPORTED_SYMBOLS = [ "FileUtils" ];
7
8 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
9
10 const Cc = Components.classes;
11 const Ci = Components.interfaces;
12 const Cr = Components.results;
13
14 XPCOMUtils.defineLazyServiceGetter(this, "gDirService",
15 "@mozilla.org/file/directory_service;1",
16 "nsIProperties");
17
18 this.FileUtils = {
19 MODE_RDONLY : 0x01,
20 MODE_WRONLY : 0x02,
21 MODE_RDWR : 0x04,
22 MODE_CREATE : 0x08,
23 MODE_APPEND : 0x10,
24 MODE_TRUNCATE : 0x20,
25
26 PERMS_FILE : 0o644,
27 PERMS_DIRECTORY : 0o755,
28
29 /**
30 * Gets a file at the specified hierarchy under a nsIDirectoryService key.
31 * @param key
32 * The Directory Service Key to start from
33 * @param pathArray
34 * An array of path components to locate beneath the directory
35 * specified by |key|. The last item in this array must be the
36 * leaf name of a file.
37 * @return nsIFile object for the file specified. The file is NOT created
38 * if it does not exist, however all required directories along
39 * the way are.
40 */
41 getFile: function FileUtils_getFile(key, pathArray, followLinks) {
42 var file = this.getDir(key, pathArray.slice(0, -1), true, followLinks);
43 file.append(pathArray[pathArray.length - 1]);
44 return file;
45 },
46
47 /**
48 * Gets a directory at the specified hierarchy under a nsIDirectoryService
49 * key.
50 * @param key
51 * The Directory Service Key to start from
52 * @param pathArray
53 * An array of path components to locate beneath the directory
54 * specified by |key|
55 * @param shouldCreate
56 * true if the directory hierarchy specified in |pathArray|
57 * should be created if it does not exist, false otherwise.
58 * @param followLinks (optional)
59 * true if links should be followed, false otherwise.
60 * @return nsIFile object for the location specified.
61 */
62 getDir: function FileUtils_getDir(key, pathArray, shouldCreate, followLinks) {
63 var dir = gDirService.get(key, Ci.nsIFile);
64 for (var i = 0; i < pathArray.length; ++i) {
65 dir.append(pathArray[i]);
66 }
67
68 if (shouldCreate) {
69 try {
70 dir.create(Ci.nsIFile.DIRECTORY_TYPE, this.PERMS_DIRECTORY);
71 } catch (ex if ex.result == Cr.NS_ERROR_FILE_ALREADY_EXISTS) {
72 // Ignore the exception due to a directory that already exists.
73 }
74 }
75
76 if (!followLinks)
77 dir.followLinks = false;
78 return dir;
79 },
80
81 /**
82 * Opens a file output stream for writing.
83 * @param file
84 * The file to write to.
85 * @param modeFlags
86 * (optional) File open flags. Can be undefined.
87 * @returns nsIFileOutputStream to write to.
88 * @note The stream is initialized with the DEFER_OPEN behavior flag.
89 * See nsIFileOutputStream.
90 */
91 openFileOutputStream: function FileUtils_openFileOutputStream(file, modeFlags) {
92 var fos = Cc["@mozilla.org/network/file-output-stream;1"].
93 createInstance(Ci.nsIFileOutputStream);
94 return this._initFileOutputStream(fos, file, modeFlags);
95 },
96
97 /**
98 * Opens an atomic file output stream for writing.
99 * @param file
100 * The file to write to.
101 * @param modeFlags
102 * (optional) File open flags. Can be undefined.
103 * @returns nsIFileOutputStream to write to.
104 * @note The stream is initialized with the DEFER_OPEN behavior flag.
105 * See nsIFileOutputStream.
106 * OpeanAtomicFileOutputStream is generally better than openSafeFileOutputStream
107 * baecause flushing is not needed in most of the issues.
108 */
109 openAtomicFileOutputStream: function FileUtils_openAtomicFileOutputStream(file, modeFlags) {
110 var fos = Cc["@mozilla.org/network/atomic-file-output-stream;1"].
111 createInstance(Ci.nsIFileOutputStream);
112 return this._initFileOutputStream(fos, file, modeFlags);
113 },
114
115 /**
116 * Opens a safe file output stream for writing.
117 * @param file
118 * The file to write to.
119 * @param modeFlags
120 * (optional) File open flags. Can be undefined.
121 * @returns nsIFileOutputStream to write to.
122 * @note The stream is initialized with the DEFER_OPEN behavior flag.
123 * See nsIFileOutputStream.
124 */
125 openSafeFileOutputStream: function FileUtils_openSafeFileOutputStream(file, modeFlags) {
126 var fos = Cc["@mozilla.org/network/safe-file-output-stream;1"].
127 createInstance(Ci.nsIFileOutputStream);
128 return this._initFileOutputStream(fos, file, modeFlags);
129 },
130
131 _initFileOutputStream: function FileUtils__initFileOutputStream(fos, file, modeFlags) {
132 if (modeFlags === undefined)
133 modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
134 fos.init(file, modeFlags, this.PERMS_FILE, fos.DEFER_OPEN);
135 return fos;
136 },
137
138 /**
139 * Closes an atomic file output stream.
140 * @param stream
141 * The stream to close.
142 */
143 closeAtomicFileOutputStream: function FileUtils_closeAtomicFileOutputStream(stream) {
144 if (stream instanceof Ci.nsISafeOutputStream) {
145 try {
146 stream.finish();
147 return;
148 }
149 catch (e) {
150 }
151 }
152 stream.close();
153 },
154
155 /**
156 * Closes a safe file output stream.
157 * @param stream
158 * The stream to close.
159 */
160 closeSafeFileOutputStream: function FileUtils_closeSafeFileOutputStream(stream) {
161 if (stream instanceof Ci.nsISafeOutputStream) {
162 try {
163 stream.finish();
164 return;
165 }
166 catch (e) {
167 }
168 }
169 stream.close();
170 },
171
172 File: Components.Constructor("@mozilla.org/file/local;1", Ci.nsILocalFile, "initWithPath")
173 };

mercurial