2009-10-23 12:42:48 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-29 08:52:43 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = [ "FileUtils" ];
|
2009-10-23 12:42:48 -07:00
|
|
|
|
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gDirService",
|
|
|
|
"@mozilla.org/file/directory_service;1",
|
|
|
|
"nsIProperties");
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.FileUtils = {
|
2009-10-23 12:42:48 -07:00
|
|
|
MODE_RDONLY : 0x01,
|
|
|
|
MODE_WRONLY : 0x02,
|
2012-07-05 03:12:09 -07:00
|
|
|
MODE_RDWR : 0x04,
|
2009-10-23 12:42:48 -07:00
|
|
|
MODE_CREATE : 0x08,
|
|
|
|
MODE_APPEND : 0x10,
|
|
|
|
MODE_TRUNCATE : 0x20,
|
|
|
|
|
|
|
|
PERMS_FILE : 0644,
|
|
|
|
PERMS_DIRECTORY : 0755,
|
|
|
|
|
|
|
|
/**
|
2011-04-18 15:41:09 -07:00
|
|
|
* Gets a file at the specified hierarchy under a nsIDirectoryService key.
|
2009-10-23 12:42:48 -07:00
|
|
|
* @param key
|
|
|
|
* The Directory Service Key to start from
|
|
|
|
* @param pathArray
|
|
|
|
* An array of path components to locate beneath the directory
|
|
|
|
* specified by |key|. The last item in this array must be the
|
|
|
|
* leaf name of a file.
|
|
|
|
* @return nsIFile object for the file specified. The file is NOT created
|
|
|
|
* if it does not exist, however all required directories along
|
|
|
|
* the way are.
|
|
|
|
*/
|
|
|
|
getFile: function FileUtils_getFile(key, pathArray, followLinks) {
|
|
|
|
var file = this.getDir(key, pathArray.slice(0, -1), true, followLinks);
|
|
|
|
file.append(pathArray[pathArray.length - 1]);
|
|
|
|
return file;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-04-18 15:41:09 -07:00
|
|
|
* Gets a directory at the specified hierarchy under a nsIDirectoryService
|
|
|
|
* key.
|
2009-10-23 12:42:48 -07:00
|
|
|
* @param key
|
|
|
|
* The Directory Service Key to start from
|
|
|
|
* @param pathArray
|
|
|
|
* An array of path components to locate beneath the directory
|
|
|
|
* specified by |key|
|
|
|
|
* @param shouldCreate
|
|
|
|
* true if the directory hierarchy specified in |pathArray|
|
|
|
|
* should be created if it does not exist, false otherwise.
|
|
|
|
* @param followLinks (optional)
|
|
|
|
* true if links should be followed, false otherwise.
|
2011-04-18 15:41:09 -07:00
|
|
|
* @return nsIFile object for the location specified.
|
2009-10-23 12:42:48 -07:00
|
|
|
*/
|
|
|
|
getDir: function FileUtils_getDir(key, pathArray, shouldCreate, followLinks) {
|
|
|
|
var dir = gDirService.get(key, Ci.nsILocalFile);
|
|
|
|
for (var i = 0; i < pathArray.length; ++i) {
|
|
|
|
dir.append(pathArray[i]);
|
|
|
|
if (shouldCreate && !dir.exists())
|
|
|
|
dir.create(Ci.nsILocalFile.DIRECTORY_TYPE, this.PERMS_DIRECTORY);
|
|
|
|
}
|
|
|
|
if (!followLinks)
|
|
|
|
dir.followLinks = false;
|
|
|
|
return dir;
|
|
|
|
},
|
|
|
|
|
2011-06-06 11:29:34 -07:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
},
|
|
|
|
|
2009-10-23 12:42:48 -07:00
|
|
|
/**
|
|
|
|
* Opens a safe 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.
|
2011-04-18 15:41:09 -07:00
|
|
|
* @note The stream is initialized with the DEFER_OPEN behavior flag.
|
|
|
|
* See nsIFileOutputStream.
|
2009-10-23 12:42:48 -07:00
|
|
|
*/
|
|
|
|
openSafeFileOutputStream: function FileUtils_openSafeFileOutputStream(file, modeFlags) {
|
|
|
|
var fos = Cc["@mozilla.org/network/safe-file-output-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileOutputStream);
|
2011-06-06 11:29:34 -07:00
|
|
|
return this._initFileOutputStream(fos, file, modeFlags);
|
|
|
|
},
|
|
|
|
|
|
|
|
_initFileOutputStream: function FileUtils__initFileOutputStream(fos, file, modeFlags) {
|
2009-10-23 12:42:48 -07:00
|
|
|
if (modeFlags === undefined)
|
|
|
|
modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
|
2011-04-18 15:41:09 -07:00
|
|
|
fos.init(file, modeFlags, this.PERMS_FILE, fos.DEFER_OPEN);
|
2009-10-23 12:42:48 -07:00
|
|
|
return fos;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes a safe file output stream.
|
|
|
|
* @param stream
|
|
|
|
* The stream to close.
|
|
|
|
*/
|
|
|
|
closeSafeFileOutputStream: function FileUtils_closeSafeFileOutputStream(stream) {
|
|
|
|
if (stream instanceof Ci.nsISafeOutputStream) {
|
|
|
|
try {
|
|
|
|
stream.finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stream.close();
|
2011-09-20 09:20:51 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
File: Components.Constructor("@mozilla.org/file/local;1", Ci.nsILocalFile, "initWithPath")
|
2009-10-23 12:42:48 -07:00
|
|
|
};
|