Advertisement
Guest User

FileUtils.jsm

a guest
Mar 31st, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 a safe 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.    */
  107.   openSafeFileOutputStream: function FileUtils_openSafeFileOutputStream(file, modeFlags) {
  108.     var fos = Cc["@mozilla.org/network/safe-file-output-stream;1"].
  109.               createInstance(Ci.nsIFileOutputStream);
  110.     return this._initFileOutputStream(fos, file, modeFlags);
  111.   },
  112.  
  113.  _initFileOutputStream: function FileUtils__initFileOutputStream(fos, file, modeFlags) {
  114.     if (modeFlags === undefined)
  115.       modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
  116.     fos.init(file, modeFlags, this.PERMS_FILE, fos.DEFER_OPEN);
  117.     return fos;
  118.   },
  119.  
  120.   /**
  121.    * Closes a safe file output stream.
  122.    * @param   stream
  123.    *          The stream to close.
  124.    */
  125.   closeSafeFileOutputStream: function FileUtils_closeSafeFileOutputStream(stream) {
  126.     if (stream instanceof Ci.nsISafeOutputStream) {
  127.       try {
  128.         stream.finish();
  129.         return;
  130.       }
  131.       catch (e) {
  132.       }
  133.     }
  134.     stream.close();
  135.   },
  136.  
  137.   File: Components.Constructor("@mozilla.org/file/local;1", Ci.nsILocalFile, "initWithPath")
  138. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement