"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/fs-vacuum/vacuum.js" (7 Feb 2017, 3159 Bytes) of archive /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 assert = require('assert')
    2 var dirname = require('path').dirname
    3 var resolve = require('path').resolve
    4 var isInside = require('path-is-inside')
    5 
    6 var rimraf = require('rimraf')
    7 var lstat = require('graceful-fs').lstat
    8 var readdir = require('graceful-fs').readdir
    9 var rmdir = require('graceful-fs').rmdir
   10 var unlink = require('graceful-fs').unlink
   11 
   12 module.exports = vacuum
   13 
   14 function vacuum (leaf, options, cb) {
   15   assert(typeof leaf === 'string', 'must pass in path to remove')
   16   assert(typeof cb === 'function', 'must pass in callback')
   17 
   18   if (!options) options = {}
   19   assert(typeof options === 'object', 'options must be an object')
   20 
   21   var log = options.log ? options.log : function () {}
   22 
   23   leaf = leaf && resolve(leaf)
   24   var base = options.base && resolve(options.base)
   25   if (base && !isInside(leaf, base)) {
   26     return cb(new Error(leaf + ' is not a child of ' + base))
   27   }
   28 
   29   lstat(leaf, function (error, stat) {
   30     if (error) {
   31       if (error.code === 'ENOENT') return cb(null)
   32 
   33       log(error.stack)
   34       return cb(error)
   35     }
   36 
   37     if (!(stat && (stat.isDirectory() || stat.isSymbolicLink() || stat.isFile()))) {
   38       log(leaf, 'is not a directory, file, or link')
   39       return cb(new Error(leaf + ' is not a directory, file, or link'))
   40     }
   41 
   42     if (options.purge) {
   43       log('purging', leaf)
   44       rimraf(leaf, function (error) {
   45         if (error) return cb(error)
   46 
   47         next(dirname(leaf))
   48       })
   49     } else if (!stat.isDirectory()) {
   50       log('removing', leaf)
   51       unlink(leaf, function (error) {
   52         if (error) return cb(error)
   53 
   54         next(dirname(leaf))
   55       })
   56     } else {
   57       next(leaf)
   58     }
   59   })
   60 
   61   function next (branch) {
   62     branch = branch && resolve(branch)
   63     // either we've reached the base or we've reached the root
   64     if ((base && branch === base) || branch === dirname(branch)) {
   65       log('finished vacuuming up to', branch)
   66       return cb(null)
   67     }
   68 
   69     readdir(branch, function (error, files) {
   70       if (error) {
   71         if (error.code === 'ENOENT') return cb(null)
   72 
   73         log('unable to check directory', branch, 'due to', error.message)
   74         return cb(error)
   75       }
   76 
   77       if (files.length > 0) {
   78         log('quitting because other entries in', branch)
   79         return cb(null)
   80       }
   81 
   82       log('removing', branch)
   83       lstat(branch, function (error, stat) {
   84         if (error) {
   85           if (error.code === 'ENOENT') return cb(null)
   86 
   87           log('unable to lstat', branch, 'due to', error.message)
   88           return cb(error)
   89         }
   90 
   91         var remove = stat.isDirectory() ? rmdir : unlink
   92         remove(branch, function (error) {
   93           if (error) {
   94             if (error.code === 'ENOENT') {
   95               log('quitting because lost the race to remove', branch)
   96               return cb(null)
   97             }
   98             if (error.code === 'ENOTEMPTY' || error.code === 'EEXIST') {
   99               log('quitting because new (racy) entries in', branch)
  100               return cb(null)
  101             }
  102 
  103             log('unable to remove', branch, 'due to', error.message)
  104             return cb(error)
  105           }
  106 
  107           next(dirname(branch))
  108         })
  109       })
  110     })
  111   }
  112 }