Node.js:使用 fs.writeFileSync 写入系统文件

标签 node.js file-io fs file-writing

我正在尝试写入 /sys/kernel/config/usb_gadget 下的系统文件与 fs.writeFileSync但是在写 "" 时作为内容,文件保持不变(原始内容完好无损)并导致

Error: EBUSY: resource busy or locked, write
    at Object.writeSync (fs.js:581:3)
    at Object.writeFileSync (fs.js:1275:26)
    at Socket.<anonymous> (/opt/sterling/ip-kvm-interface/app.js:249:6)
    at Socket.emit (events.js:210:5)
    at /opt/sterling/ip-kvm-interface/node_modules/socket.io/lib/socket.js:528:12
    at processTicksAndRejections (internal/process/task_queues.js:75:11) {
  errno: -16,
  syscall: 'write',
  code: 'EBUSY'
}

在写一些其他内容时。目标写入文件的权限为 777。

fs.writeFileSync无法写入 sys 下的文件还是我错过了其他东西?

使用 fsuser /sys/kernel/config/usb_gadget/kvm-gadget/UDC不返回任何内容(即使 Node 进程正在运行)和 lsof | grep /sys/kernel/config/usb_gadget/kvm-gadget/UDC也不返回任何内容。

我必须要spawn echo使其工作的过程(不是首选但我想到了 - 因为不确定如何将其转换为同步任务)?

最佳答案

https://github.com/nodejs/help/issues/2459

Are there undocumented limitations to fs.writeFileSync that I am unaware of?

Nothing specific to fs.writeFileSync(), you can get the same error with a plain C program.

/sys/kernel/config/usb_gadget is not a real file, it's a communication channel with the kernel's usb gadget driver. It's that driver that is returning the error.

(I could point you to the line of code if you're really interested. It's drivers/usb/gadget/configfs.c in the kernel source tree in any case.)

关于Node.js:使用 fs.writeFileSync 写入系统文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59996022/

相关文章:

node.js - 在 Node 中使用 mongoosejs 更新多条记录

javascript - 在 Node.js 中导出变量的推荐方法

c++ - 如何使用系统调用 write() 输出到文件(写入文件)?

ruby - 如何从文件中获取特定行

Java 文件 I/O 吞吐量下降

javascript - 从 'fs' 读取和写入文件不异步工作

javascript - client.startTyping 不是一个函数(Discord.JS)

node.js - react 虚拟化表 x 滚动

javascript - fs.writeFileSync 写入空白文件

node.js - 在nodejs中将一个大文件附加到另一大文件