gecko/toolkit/modules/FileUtils.jsm
Ekanan Ketunuti b0c1ba9c9e Bug 828116 - Move modules in toolkit/content and toolkit/mozapps/shared to toolkit/modules. r=Mossop
--HG--
rename : toolkit/mozapps/shared/CertUtils.jsm => toolkit/modules/CertUtils.jsm
rename : toolkit/content/DeferredTask.jsm => toolkit/modules/DeferredTask.jsm
rename : toolkit/content/Deprecated.jsm => toolkit/modules/Deprecated.jsm
rename : toolkit/content/Dict.jsm => toolkit/modules/Dict.jsm
rename : toolkit/mozapps/shared/FileUtils.jsm => toolkit/modules/FileUtils.jsm
rename : toolkit/content/Geometry.jsm => toolkit/modules/Geometry.jsm
rename : toolkit/content/InlineSpellChecker.jsm => toolkit/modules/InlineSpellChecker.jsm
rename : toolkit/content/LightweightThemeConsumer.jsm => toolkit/modules/LightweightThemeConsumer.jsm
rename : toolkit/content/PageMenu.jsm => toolkit/modules/PageMenu.jsm
rename : toolkit/content/PopupNotifications.jsm => toolkit/modules/PopupNotifications.jsm
rename : toolkit/content/PrivateBrowsingUtils.jsm => toolkit/modules/PrivateBrowsingUtils.jsm
rename : toolkit/content/PropertyListUtils.jsm => toolkit/modules/PropertyListUtils.jsm
rename : toolkit/content/Services.jsm => toolkit/modules/Services.jsm
rename : toolkit/content/Task.jsm => toolkit/modules/Task.jsm
rename : toolkit/content/Troubleshoot.jsm => toolkit/modules/Troubleshoot.jsm
rename : toolkit/content/UpdateChannel.jsm => toolkit/modules/UpdateChannel.jsm
rename : toolkit/content/WindowDraggingUtils.jsm => toolkit/modules/WindowDraggingUtils.jsm
rename : toolkit/content/debug.js => toolkit/modules/debug.js
rename : toolkit/content/tests/browser/browser_DeferredTask.js => toolkit/modules/tests/browser/browser_DeferredTask.js
rename : toolkit/content/tests/browser/browser_Deprecated.js => toolkit/modules/tests/browser/browser_Deprecated.js
rename : toolkit/content/tests/browser/browser_Geometry.js => toolkit/modules/tests/browser/browser_Geometry.js
rename : toolkit/content/tests/browser/browser_InlineSpellChecker.js => toolkit/modules/tests/browser/browser_InlineSpellChecker.js
rename : toolkit/content/tests/browser/browser_Services.js => toolkit/modules/tests/browser/browser_Services.js
rename : toolkit/content/tests/browser/browser_Troubleshoot.js => toolkit/modules/tests/browser/browser_Troubleshoot.js
rename : toolkit/mozapps/shared/test/chrome/Makefile.in => toolkit/modules/tests/chrome/Makefile.in
rename : toolkit/mozapps/shared/test/chrome/moz.build => toolkit/modules/tests/chrome/moz.build
rename : toolkit/mozapps/shared/test/chrome/test_bug544442_checkCert.xul => toolkit/modules/tests/chrome/test_bug544442_checkCert.xul
rename : toolkit/content/tests/unit/propertyLists/bug710259_propertyListBinary.plist => toolkit/modules/tests/xpcshell/propertyLists/bug710259_propertyListBinary.plist
rename : toolkit/content/tests/unit/propertyLists/bug710259_propertyListXML.plist => toolkit/modules/tests/xpcshell/propertyLists/bug710259_propertyListXML.plist
rename : toolkit/mozapps/shared/test/unit/test_FileUtils.js => toolkit/modules/tests/xpcshell/test_FileUtils.js
rename : toolkit/content/tests/unit/test_dict.js => toolkit/modules/tests/xpcshell/test_dict.js
rename : toolkit/content/tests/unit/test_propertyListsUtils.js => toolkit/modules/tests/xpcshell/test_propertyListsUtils.js
rename : toolkit/mozapps/shared/test/unit/test_readCertPrefs.js => toolkit/modules/tests/xpcshell/test_readCertPrefs.js
rename : toolkit/content/tests/unit/test_task.js => toolkit/modules/tests/xpcshell/test_task.js
2013-05-14 14:37:18 -07:00

132 lines
4.6 KiB
JavaScript

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
this.EXPORTED_SYMBOLS = [ "FileUtils" ];
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");
this.FileUtils = {
MODE_RDONLY : 0x01,
MODE_WRONLY : 0x02,
MODE_RDWR : 0x04,
MODE_CREATE : 0x08,
MODE_APPEND : 0x10,
MODE_TRUNCATE : 0x20,
PERMS_FILE : 0644,
PERMS_DIRECTORY : 0755,
/**
* Gets a file at the specified hierarchy under a nsIDirectoryService key.
* @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;
},
/**
* Gets a directory at the specified hierarchy under a nsIDirectoryService
* key.
* @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.
* @return nsIFile object for the location specified.
*/
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;
},
/**
* 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);
},
/**
* 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.
* @note The stream is initialized with the DEFER_OPEN behavior flag.
* See nsIFileOutputStream.
*/
openSafeFileOutputStream: function FileUtils_openSafeFileOutputStream(file, modeFlags) {
var fos = Cc["@mozilla.org/network/safe-file-output-stream;1"].
createInstance(Ci.nsIFileOutputStream);
return this._initFileOutputStream(fos, file, modeFlags);
},
_initFileOutputStream: function FileUtils__initFileOutputStream(fos, file, modeFlags) {
if (modeFlags === undefined)
modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
fos.init(file, modeFlags, this.PERMS_FILE, fos.DEFER_OPEN);
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();
},
File: Components.Constructor("@mozilla.org/file/local;1", Ci.nsILocalFile, "initWithPath")
};