File: vacuum.js

package info (click to toggle)
node-fs-vacuum 1.2.10-2~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 144 kB
  • sloc: makefile: 4; sh: 2
file content (117 lines) | stat: -rw-r--r-- 3,302 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var assert = require('assert')
var dirname = require('path').dirname
var resolve = require('path').resolve
var isInside = require('path-is-inside')

var rimraf = require('rimraf')
var lstat = require('graceful-fs').lstat
var readdir = require('graceful-fs').readdir
var rmdir = require('graceful-fs').rmdir
var unlink = require('graceful-fs').unlink

module.exports = vacuum

function vacuum (leaf, options, cb) {
  assert(typeof leaf === 'string', 'must pass in path to remove')
  assert(typeof cb === 'function', 'must pass in callback')

  if (!options) options = {}
  assert(typeof options === 'object', 'options must be an object')

  var log = options.log ? options.log : function () {}

  leaf = leaf && resolve(leaf)
  var base = options.base && resolve(options.base)
  if (base && !isInside(leaf, base)) {
    return cb(new Error(leaf + ' is not a child of ' + base))
  }

  lstat(leaf, function (error, stat) {
    if (error) {
      if (error.code === 'ENOENT') return cb(null)

      log(error.stack)
      return cb(error)
    }

    if (!(stat && (stat.isDirectory() || stat.isSymbolicLink() || stat.isFile()))) {
      log(leaf, 'is not a directory, file, or link')
      return cb(new Error(leaf + ' is not a directory, file, or link'))
    }

    if (options.purge) {
      log('purging', leaf)
      rimraf(leaf, function (error) {
        if (error) return cb(error)

        next(dirname(leaf))
      })
    } else if (!stat.isDirectory()) {
      log('removing', leaf)
      unlink(leaf, function (error) {
        if (error) return cb(error)

        next(dirname(leaf))
      })
    } else {
      next(leaf)
    }
  })

  function next (branch) {
    branch = branch && resolve(branch)
    // either we've reached the base or we've reached the root
    if ((base && branch === base) || branch === dirname(branch)) {
      log('finished vacuuming up to', branch)
      return cb(null)
    }

    readdir(branch, function (error, files) {
      if (error) {
        if (error.code === 'ENOENT') return cb(null)

        log('unable to check directory', branch, 'due to', error.message)
        return cb(error)
      }

      if (files.length > 0) {
        log('quitting because other entries in', branch)
        return cb(null)
      }

      if (branch === process.env.HOME) {
        log('quitting because cannot remove home directory', branch)
        return cb(null)
      }

      log('removing', branch)
      lstat(branch, function (error, stat) {
        if (error) {
          if (error.code === 'ENOENT') return cb(null)

          log('unable to lstat', branch, 'due to', error.message)
          return cb(error)
        }

        var remove = stat.isDirectory() ? rmdir : unlink
        remove(branch, function (error) {
          if (error) {
            if (error.code === 'ENOENT') {
              log('quitting because lost the race to remove', branch)
              return cb(null)
            }
            if (error.code === 'ENOTEMPTY' || error.code === 'EEXIST') {
              log('quitting because new (racy) entries in', branch)
              return cb(null)
            }

            log('unable to remove', branch, 'due to', error.message)
            return cb(error)
          }

          next(dirname(branch))
        })
      })
    })
  }
}