node.js - Nodejs FS |错误: ENOTDIR: not a directory

标签 node.js file directory filesystems

所以我正在使用第三方项目,并且有以下代码块:

// walk through /run and remove all filecontents, keeping 1 level directories.
var root = '/run';

if (fs.existsSync(root)) {
  fs.readdirSync(root).forEach(cleanFileOrDir);
}

哪里cleanFileOrDir是:

function cleanFileOrDir(f) {
  var fPath = path.join(root, f);
  if (fs.statSync(fPath).isFile()) {
    // if its a file delete it right away
    rimraf.sync(fPath);
  } else {
    // remove its contents
    rimrafKidsSync(fPath);
  }
}

我收到以下错误:

fs.js:945
  return binding.readdir(pathModule._makeLong(path), options.encoding);
                 ^

Error: ENOTDIR: not a directory, scandir '/run/acpid.socket'
    at Error (native)
    at Object.fs.readdirSync (fs.js:945:18)
    at rimrafKidsSync (/home/otis/Developer/project/dockworker/lib/controllers/dockCleaner.js:27:6)
    at cleanFileOrDir (/home/otis/Developer/project/dockworker/lib/controllers/dockCleaner.js:22:5)
    at Array.forEach (native)
    at Object.<anonymous> (/home/otis/Developer/project/dockworker/lib/controllers/dockCleaner.js:8:24)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)

还有我的/run/的内容目录是:

acpid.socket   crond.pid          docker       irqbalance.pid  mlocate.daily.lock  pppconfig        snapd.socket  udev     wpa_supplicant
agetty.reload  crond.reboot       docker.pid   lightdm         mount               resolvconf       sudo          udisks2  xtables.lock
alsa           cups               docker.sock  lightdm.pid     network             rsyslogd.pid     systemd       user
avahi-daemon   dbus               initctl      lock            NetworkManager      sendsigs.omit.d  thermald      utmp
containerd     dhclient-wlo1.pid  initramfs    log             plymouth            shm              tmpfiles.d    uuidd

我认为这可能是 Nodejs v6 引入的问题文件系统可能已更改?

更新我修改了cleanFileOrDir函数看起来像这样:

function cleanFileOrDir(f) {
  var fPath = path.join(root, f);
  console.log(fPath);
  if (fs.statSync(fPath).isFile()) {
    // if its a file delete it right away
    console.log('Is file');
    rimraf.sync(fPath);
  } else {
    // remove its contents
    console.log('Is directory');
    rimrafKidsSync(fPath);
  }
}

我现在得到以下输出:

/run/NetworkManager
Is directory
/run/acpid.socket
Is directory
fs.js:945

简而言之,它正在处理 /run/acpid.socket作为一个目录,你知道为什么会这样吗?

最佳答案

由于“aspid.socket”是一个套接字,因此它不是常规文件,也不是目录。可用列表tests :

stats.isFile()
stats.isDirectory()
stats.isBlockDevice()
stats.isCharacterDevice()
stats.isFIFO()
stats.isSocket()

所以你需要改变逻辑:

function cleanFileOrDir(f) {
  var fPath = path.join(root, f);
  var stat = fs.statSync(fPath);

  if (stat.isFile()) {
    // if its a file delete it right away
    rimraf.sync(fPath);
  } else 
  if (stat.isDirectory()){
    // remove its contents
    rimrafKidsSync(fPath);
  } else
  if (stat.isSocket()) {
    // We do something with the socket
  }

}

关于node.js - Nodejs FS |错误: ENOTDIR: not a directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37640865/

相关文章:

node.js - page.forEach() 不是函数

file - 从 sqlite 中提取

c - 如何在C中读取目录中的所有.txt文件

C++将文件复制到具有管理员权限的文件夹中

javascript - array.push 没有根据序列推送对象,而某些对象使用等待执行操作

javascript - 如何将 uglify-es 与 gulp 一起使用?

c - 如何用C语言编辑文件中的记录?

java - 在Java中,如何从后到前迭代文本文件中的行

java - 在 JNDI 中存储属性?环境特定位置

angularjs - Mean stack 网站在服务器上加载速度非常慢