"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/temp/lib/temp.js" (11 Apr 2017, 4674 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var fs   = require('fs'),
    2     os   = require('os'),
    3     path = require('path'),
    4     cnst = require('constants');
    5 
    6 /* HELPERS */
    7 
    8 var RDWR_EXCL = cnst.O_CREAT | cnst.O_TRUNC | cnst.O_RDWR | cnst.O_EXCL;
    9 
   10 var generateName = function(rawAffixes, defaultPrefix) {
   11   var affixes = parseAffixes(rawAffixes, defaultPrefix);
   12   var now = new Date();
   13   var name = [affixes.prefix,
   14               now.getYear(), now.getMonth(), now.getDate(),
   15               '-',
   16               process.pid,
   17               '-',
   18               (Math.random() * 0x100000000 + 1).toString(36),
   19               affixes.suffix].join('');
   20   return path.join(affixes.dir || exports.dir, name);
   21 }
   22 
   23 var parseAffixes = function(rawAffixes, defaultPrefix) {
   24   var affixes = {prefix: null, suffix: null};
   25   if(rawAffixes) {
   26     switch (typeof(rawAffixes)) {
   27     case 'string':
   28       affixes.prefix = rawAffixes;
   29       break;
   30     case 'object':
   31       affixes = rawAffixes;
   32       break
   33     default:
   34       throw("Unknown affix declaration: " + affixes);
   35     }
   36   } else {
   37     affixes.prefix = defaultPrefix;
   38   }
   39   return affixes;
   40 }
   41 
   42 /* -------------------------------------------------------------------------
   43  * Don't forget to call track() if you want file tracking and exit handlers!
   44  * -------------------------------------------------------------------------
   45  * When any temp file or directory is created, it is added to filesToDelete
   46  * or dirsToDelete. The first time any temp file is created, a listener is
   47  * added to remove all temp files and directories at exit.
   48  */
   49 var tracking = false;
   50 var track = function(value) {
   51   tracking = (value != false);
   52   return module.exports; // chainable
   53 };
   54 var exitListenerAttached = false;
   55 var filesToDelete = [];
   56 var dirsToDelete = [];
   57 
   58 var deleteFileOnExit = function(filePath) {
   59   if (!tracking) return false;
   60   attachExitListener();
   61   filesToDelete.push(filePath);
   62 };
   63 
   64 var deleteDirOnExit = function(dirPath) {
   65   if (!tracking) return false;
   66   attachExitListener();
   67   dirsToDelete.push(dirPath);
   68 };
   69 
   70 var attachExitListener = function() {
   71   if (!tracking) return false;
   72   if (!exitListenerAttached) {
   73     process.addListener('exit', cleanup);
   74     exitListenerAttached = true;
   75   }
   76 };
   77 
   78 var cleanupFiles = function() {
   79   if (!tracking) return false;
   80   var counts = {removed: 0, missing: 0}
   81   var toDelete;
   82   while (toDelete = filesToDelete.shift()) {
   83     try {
   84       fs.unlinkSync(toDelete);
   85       counts.removed++;
   86     } catch (rmErr) {
   87       /* removed normally */
   88       counts.missing++;
   89     }
   90   }
   91   return counts;
   92 };
   93 
   94 var cleanupDirs = function() {
   95   if (!tracking) return false;
   96   var rimrafSync = require('rimraf').sync;
   97   var counts = {removed: 0, missing: 0}
   98   var toDelete;
   99   while (toDelete = dirsToDelete.shift()) {
  100     try {
  101       rimrafSync(toDelete, function (er) {
  102         if (er) {
  103           throw er;
  104         } else {
  105           counts.removed++;
  106         }
  107       });
  108     } catch (rmErr) {
  109       /* removed normally */
  110       counts.missing++;
  111     }
  112   }
  113   return counts;
  114 };
  115 
  116 var cleanup = function() {
  117   if (!tracking) return false;
  118   var fileCount = cleanupFiles();
  119   var dirCount  = cleanupDirs();
  120   return {files: fileCount, dirs: dirCount};
  121 }
  122 
  123 /* DIRECTORIES */
  124 
  125 var mkdir = function(affixes, callback) {
  126   var dirPath = generateName(affixes, 'd-');
  127   fs.mkdir(dirPath, 0700, function(err) {
  128     if (!err) {
  129       deleteDirOnExit(dirPath);
  130     }
  131     if (callback)
  132       callback(err, dirPath);
  133   });
  134 }
  135 var mkdirSync = function(affixes) {
  136   var dirPath = generateName(affixes, 'd-');
  137   fs.mkdirSync(dirPath, 0700);
  138   deleteDirOnExit(dirPath);
  139   return dirPath;
  140 }
  141 
  142 /* FILES */
  143 
  144 var open = function(affixes, callback) {
  145   var filePath = generateName(affixes, 'f-')
  146   fs.open(filePath, RDWR_EXCL, 0600, function(err, fd) {
  147     if (!err)
  148       deleteFileOnExit(filePath);
  149     if (callback)
  150       callback(err, {path: filePath, fd: fd});
  151   });
  152 }
  153 
  154 var openSync = function(affixes) {
  155   var filePath = generateName(affixes, 'f-')
  156   var fd = fs.openSync(filePath, RDWR_EXCL, 0600);
  157   deleteFileOnExit(filePath);
  158   return {path: filePath, fd: fd};
  159 }
  160 
  161 var createWriteStream = function(affixes) {
  162   var filePath = generateName(affixes, 's-')
  163   var stream = fs.createWriteStream(filePath, {flags: RDWR_EXCL, mode: 0600});
  164   deleteFileOnExit(filePath);
  165   return stream;
  166 }
  167 
  168 /* EXPORTS */
  169 // Settings
  170 exports.dir               = path.resolve(os.tmpDir());
  171 exports.track             = track;
  172 // Functions
  173 exports.mkdir             = mkdir;
  174 exports.mkdirSync         = mkdirSync;
  175 exports.open              = open;
  176 exports.openSync          = openSync;
  177 exports.path              = generateName;
  178 exports.cleanup           = cleanup;
  179 exports.createWriteStream = createWriteStream;
  180 
  181