toolkit/modules/FileUtils.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/FileUtils.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,173 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +this.EXPORTED_SYMBOLS = [ "FileUtils" ];
    1.10 +
    1.11 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.12 +
    1.13 +const Cc = Components.classes;
    1.14 +const Ci = Components.interfaces;
    1.15 +const Cr = Components.results;
    1.16 +
    1.17 +XPCOMUtils.defineLazyServiceGetter(this, "gDirService",
    1.18 +                                   "@mozilla.org/file/directory_service;1",
    1.19 +                                   "nsIProperties");
    1.20 +
    1.21 +this.FileUtils = {
    1.22 +  MODE_RDONLY   : 0x01,
    1.23 +  MODE_WRONLY   : 0x02,
    1.24 +  MODE_RDWR     : 0x04,
    1.25 +  MODE_CREATE   : 0x08,
    1.26 +  MODE_APPEND   : 0x10,
    1.27 +  MODE_TRUNCATE : 0x20,
    1.28 +
    1.29 +  PERMS_FILE      : 0o644,
    1.30 +  PERMS_DIRECTORY : 0o755,
    1.31 +
    1.32 +  /**
    1.33 +   * Gets a file at the specified hierarchy under a nsIDirectoryService key.
    1.34 +   * @param   key
    1.35 +   *          The Directory Service Key to start from
    1.36 +   * @param   pathArray
    1.37 +   *          An array of path components to locate beneath the directory
    1.38 +   *          specified by |key|. The last item in this array must be the
    1.39 +   *          leaf name of a file.
    1.40 +   * @return  nsIFile object for the file specified. The file is NOT created
    1.41 +   *          if it does not exist, however all required directories along
    1.42 +   *          the way are.
    1.43 +   */
    1.44 +  getFile: function FileUtils_getFile(key, pathArray, followLinks) {
    1.45 +    var file = this.getDir(key, pathArray.slice(0, -1), true, followLinks);
    1.46 +    file.append(pathArray[pathArray.length - 1]);
    1.47 +    return file;
    1.48 +  },
    1.49 +
    1.50 +  /**
    1.51 +   * Gets a directory at the specified hierarchy under a nsIDirectoryService
    1.52 +   * key.
    1.53 +   * @param   key
    1.54 +   *          The Directory Service Key to start from
    1.55 +   * @param   pathArray
    1.56 +   *          An array of path components to locate beneath the directory
    1.57 +   *          specified by |key|
    1.58 +   * @param   shouldCreate
    1.59 +   *          true if the directory hierarchy specified in |pathArray|
    1.60 +   *          should be created if it does not exist, false otherwise.
    1.61 +   * @param   followLinks (optional)
    1.62 +   *          true if links should be followed, false otherwise.
    1.63 +   * @return  nsIFile object for the location specified.
    1.64 +   */
    1.65 +  getDir: function FileUtils_getDir(key, pathArray, shouldCreate, followLinks) {
    1.66 +    var dir = gDirService.get(key, Ci.nsIFile);
    1.67 +    for (var i = 0; i < pathArray.length; ++i) {
    1.68 +      dir.append(pathArray[i]);
    1.69 +    }
    1.70 +
    1.71 +    if (shouldCreate) {
    1.72 +      try {
    1.73 +        dir.create(Ci.nsIFile.DIRECTORY_TYPE, this.PERMS_DIRECTORY);
    1.74 +      } catch (ex if ex.result == Cr.NS_ERROR_FILE_ALREADY_EXISTS) {
    1.75 +        // Ignore the exception due to a directory that already exists.
    1.76 +      }
    1.77 +    }
    1.78 +
    1.79 +    if (!followLinks)
    1.80 +      dir.followLinks = false;
    1.81 +    return dir;
    1.82 +  },
    1.83 +
    1.84 +  /**
    1.85 +   * Opens a file output stream for writing.
    1.86 +   * @param   file
    1.87 +   *          The file to write to.
    1.88 +   * @param   modeFlags
    1.89 +   *          (optional) File open flags. Can be undefined.
    1.90 +   * @returns nsIFileOutputStream to write to.
    1.91 +   * @note The stream is initialized with the DEFER_OPEN behavior flag.
    1.92 +   *       See nsIFileOutputStream.
    1.93 +   */
    1.94 +  openFileOutputStream: function FileUtils_openFileOutputStream(file, modeFlags) {
    1.95 +    var fos = Cc["@mozilla.org/network/file-output-stream;1"].
    1.96 +              createInstance(Ci.nsIFileOutputStream);
    1.97 +    return this._initFileOutputStream(fos, file, modeFlags);
    1.98 +  },
    1.99 +
   1.100 +  /**
   1.101 +   * Opens an atomic file output stream for writing.
   1.102 +   * @param   file
   1.103 +   *          The file to write to.
   1.104 +   * @param   modeFlags
   1.105 +   *          (optional) File open flags. Can be undefined.
   1.106 +   * @returns nsIFileOutputStream to write to.
   1.107 +   * @note The stream is initialized with the DEFER_OPEN behavior flag.
   1.108 +   *       See nsIFileOutputStream.
   1.109 +   *       OpeanAtomicFileOutputStream is generally better than openSafeFileOutputStream
   1.110 +   *       baecause flushing is not needed in most of the issues.
   1.111 +   */
   1.112 +  openAtomicFileOutputStream: function FileUtils_openAtomicFileOutputStream(file, modeFlags) {
   1.113 +    var fos = Cc["@mozilla.org/network/atomic-file-output-stream;1"].
   1.114 +              createInstance(Ci.nsIFileOutputStream);
   1.115 +    return this._initFileOutputStream(fos, file, modeFlags);
   1.116 +  },
   1.117 +
   1.118 +  /**
   1.119 +   * Opens a safe file output stream for writing.
   1.120 +   * @param   file
   1.121 +   *          The file to write to.
   1.122 +   * @param   modeFlags
   1.123 +   *          (optional) File open flags. Can be undefined.
   1.124 +   * @returns nsIFileOutputStream to write to.
   1.125 +   * @note The stream is initialized with the DEFER_OPEN behavior flag.
   1.126 +   *       See nsIFileOutputStream.
   1.127 +   */
   1.128 +  openSafeFileOutputStream: function FileUtils_openSafeFileOutputStream(file, modeFlags) {
   1.129 +    var fos = Cc["@mozilla.org/network/safe-file-output-stream;1"].
   1.130 +              createInstance(Ci.nsIFileOutputStream);
   1.131 +    return this._initFileOutputStream(fos, file, modeFlags);
   1.132 +  },
   1.133 +
   1.134 + _initFileOutputStream: function FileUtils__initFileOutputStream(fos, file, modeFlags) {
   1.135 +    if (modeFlags === undefined)
   1.136 +      modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
   1.137 +    fos.init(file, modeFlags, this.PERMS_FILE, fos.DEFER_OPEN);
   1.138 +    return fos;
   1.139 +  },
   1.140 +
   1.141 +  /**
   1.142 +   * Closes an atomic file output stream.
   1.143 +   * @param   stream
   1.144 +   *          The stream to close.
   1.145 +   */
   1.146 +  closeAtomicFileOutputStream: function FileUtils_closeAtomicFileOutputStream(stream) {
   1.147 +    if (stream instanceof Ci.nsISafeOutputStream) {
   1.148 +      try {
   1.149 +        stream.finish();
   1.150 +        return;
   1.151 +      }
   1.152 +      catch (e) {
   1.153 +      }
   1.154 +    }
   1.155 +    stream.close();
   1.156 +  },
   1.157 +
   1.158 +  /**
   1.159 +   * Closes a safe file output stream.
   1.160 +   * @param   stream
   1.161 +   *          The stream to close.
   1.162 +   */
   1.163 +  closeSafeFileOutputStream: function FileUtils_closeSafeFileOutputStream(stream) {
   1.164 +    if (stream instanceof Ci.nsISafeOutputStream) {
   1.165 +      try {
   1.166 +        stream.finish();
   1.167 +        return;
   1.168 +      }
   1.169 +      catch (e) {
   1.170 +      }
   1.171 +    }
   1.172 +    stream.close();
   1.173 +  },
   1.174 +
   1.175 +  File: Components.Constructor("@mozilla.org/file/local;1", Ci.nsILocalFile, "initWithPath")
   1.176 +};

mercurial