fs.stat()可以在哪些文件上运行而不会出错?

时间:2019-05-19 05:09:56

标签: javascript node.js runtime-error fs macos-mojave

当我尝试使用以下脚本来递归目录时,我的用户主目录中的许多文件/文件夹都出现错误。

它们都具有类似的形式,我在这里将其放入对象文字中:

const enoent2 = { 
  error: 'no such file or directory',
  errno: -2,
  code: 'ENOENT',
  syscall: 'stat',
  path: '../../../Library/Containers/com.apple..NowPlayingWidgetContainer/Data/Documents/iChats' 
}

我已经查看了Node fs api here,但是找不到可以在其上调用fs.stat()的文件的限定符。

我需要一种在这些文件上运行fs.stat()的方法,脚本才能正确运行。

如何停止这些错误?

经过进一步分析,除路径更改外,以下形式的错误总数约为2000,但是它们都具有相同的根路径:

../../../Library/Containers/com.apple

enter image description here

代码:

// Libraries
const fs = require('fs');
const h = require('./helper');

// Start recursing here
const start = '../../../';

// API
getThings(start).then(() => {
  faults.paths.forEach((path)=>{
    console.log(path);  
  });
}).catch((err) => {
  console.log('Error Bubbled', err);
})

// Accesses the file system and returns an array of files / folders
async function getThings (folder) { 
  const things = await fs.promises.readdir(folder);
  for(let i = 0; i < things.length; i++) {
    await getStats(things[i], folder);
  }
}

// Gets statistics for each file/folder
async function getStats (thing, folder) {
  const path = folder + thing;
  let stats;
  try {
    stats = await fs.promises.stat(path);
  } catch(err) {
    logError(err);
  }
  await checkForFolder(stats, thing, path); 
}

// if the file/folder is a folder, recurse and do it again
async function checkForFolder(stats, thing, path){
  if (stats) {
    logThing(stats, thing, path);
    if (stats.isDirectory() ) {
      await getThings(path + '/');
    }
  }
}

出于此脚本的目的,我已经获得了对控制台的访问权限(临时运行此脚本)以进行完全磁盘访问。

还使用sudo这样运行脚本:

  

sudo节点_getInfo

enter image description here

0 个答案:

没有答案
相关问题