toolkit/modules/FileUtils.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial