lib/remove/rimraf.js
function rimraf_ (p, options, cb) {
  assert(p)
  assert(options)
  assert(typeof cb === 'function')

  // sunos lets the root user unlink directories, which is... weird.
  // so we have to lstat here and make sure it's not a dir.
  options.lstat(p, (er, st) => {
    if (er && er.code === 'ENOENT') {
      return cb(null)
    }

    // Windows can EPERM on stat.  Life is suffering.
    if (er && er.code === 'EPERM' && isWindows) {
      return fixWinEPERM(p, options, er, cb)
    }

    if (st && st.isDirectory()) {
      return rmdir(p, options, er, cb)
    }

    options.unlink(p, er => {
      if (er) {
        if (er.code === 'ENOENT') {
          return cb(null)
        }
        if (er.code === 'EPERM') {
          return (isWindows)
            ? fixWinEPERM(p, options, er, cb)
            : rmdir(p, options, er, cb)
        }
        if (er.code === 'EISDIR') {
          return rmdir(p, options, er, cb)
        }
      }
      return cb(er)
    })
  })
}
function rimraf_ (p, options, cb) {
  assert(p)
  assert(options)
  assert(typeof cb === 'function')

  // sunos lets the root user unlink directories, which is... weird.
  // so we have to lstat here and make sure it's not a dir.
  options.lstat(p, (er, st) => {
    if (er && er.code === 'ENOENT') {
      return cb(null)
    }

    // Windows can EPERM on stat.  Life is suffering.
    if (er && er.code === 'EPERM' && isWindows) {
      return fixWinEPERM(p, options, er, cb)
    }

    if (st && st.isDirectory()) {
      return rmdir(p, options, er, cb)
    }

    options.unlink(p, er => {
      if (er) {
        if (er.code === 'ENOENT') {
          return cb(null)
        }
        if (er.code === 'EPERM') {
          return (isWindows)
            ? fixWinEPERM(p, options, er, cb)
            : rmdir(p, options, er, cb)
        }
        if (er.code === 'EISDIR') {
          return rmdir(p, options, er, cb)
        }
      }
      return cb(er)
    })
  })
}
function rimraf_ ( p , options , cb ) { assert ( p ) assert ( options ) assert ( typeof cb === 'function' ) // sunos lets the root user unlink directories, which is... weird. // so we have to lstat here and make sure it's not a dir. options . lstat ( p , ( er , st ) => { if ( er && er . code === 'ENOENT' ) { return cb ( null ) } // Windows can EPERM on stat.  Life is suffering. if ( er && er . code === 'EPERM' && isWindows ) { return fixWinEPERM ( p , options , er , cb ) } if ( st && st . isDirectory ( ) ) { return rmdir ( p , options , er , cb ) } options . unlink ( p , er => { if ( er ) { if ( er . code === 'ENOENT' ) { return cb ( null ) } if ( er . code === 'EPERM' ) { return ( isWindows ) ? fixWinEPERM ( p , options , er , cb ) : rmdir ( p , options , er , cb ) } if ( er . code === 'EISDIR' ) { return rmdir ( p , options , er , cb ) } } return cb ( er ) } ) } ) }
Two possible strategies. 1. Assume it's a file.  unlink it, then do the dir stuff on EPERM or EISDIR 2. Assume it's a directory.  readdir, then do the file stuff on ENOTDIR  Both result in an extra syscall when you guess wrong.  However, there are likely far more normal files in the world than directories.  This is based on the assumption that a the average number of files per directory is >= 1.  If anyone ever complains about this, then I guess the strategy could be made configurable somehow.  But until then, YAGNI.
Two possible strategies . 1 . Assume it s a file . unlink it then do the dir stuff on EPERM or EISDIR 2 . Assume it s a directory . readdir then do the file stuff on ENOTDIR Both result in an extra syscall when you guess wrong . However there are likely far more normal files in the world than directories . This is based on the assumption that a the average number of files per directory is > = 1 . If anyone ever complains about this then I guess the strategy could be made configurable somehow . But until then YAGNI .