如何从node中的文件描述符获取fullpath ?
var fs = require('fs')
var fd = fs.openSync('package.json', 'r')
console.log(fd) // 10
console.log(get_file_path_from_fd(fd)) // HELP
编辑:我已经找到了
> fs.openSync('.', 'r')
10
> fs.readlinkSync('/proc/self/fd/10')
'/home/alfred/repos/test
但是我没有在Mac中找到proc文件夹
const {execSync} = require('child_process')
const fs = require('fs')
var fd = fs.openSync('package.json', 'r')
var fullpath = execSync(`lsof -a -p ${process.pid} -d ${fd}`).toString().split('\n')[1].split(/\s+/).pop()
console.log(fullpath) // result: /fullpath/package.json
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句