如何在Windows下从node.js写入NUL设备? [英] How can I write to the NUL device under Windows from node.js?
问题描述
这困扰了我好几天了.我知道将标准流重定向到NUL设备的情况,但事实并非如此. node.js在其fs本机/libuv绑定下使用CreateFileW.
This is bugging me for several days now. I know about the standard stream redirection to the NUL device, but this isn't the case. node.js uses CreateFileW under its fs native/libuv bindings.
不幸的是使用了类似的东西:
Unfortunately using something like:
require('fs').writeFileSync('NUL', 'foo')
在具有3个字节的cwd中创建一个NUL文件.
creates a NUL file into the cwd that has 3 bytes.
我尝试写入\ Device \ Null,但是由于我几乎是* nix头,其中的所有内容都是文件,因此我未能真正找到\ Device \ Null的工作路径.例如\\.\ Device \ Null会抛出ENOENT.
I tried writing to the \Device\Null, but since I'm pretty much a *nix head where everything is a file, I failed to actually find a working path for \Device\Null. Such as \\.\Device\Null that throws ENOENT.
关于如何在Windows下执行此操作的任何想法?
Any ideas about how to make this work under Windows?
这似乎是相关的,但我无法跟踪全部从lib/fs.js到uv/src/win/fs.c进行检查,以确保path参数不受某种相对于绝对路径分辨率的影响.
This seems to be related, but I can not track the whole flow from lib/fs.js to uv/src/win/fs.c to check that the path argument doesn't suffer from some kind of relative to absolute path resolution.
推荐答案
NUL
设备的有效路径是"\\\\.\\NUL"
,而不是NUL
,因此用法是:fs.writeFileSync("\\\\.\\NUL", "foo")
.在GitHub上针对Node.js引发了此问题: https://github. com/nodejs/node-v0.x-archive/issues/9271
Valid path to NUL
device is "\\\\.\\NUL"
, not NUL
, so the usage is: fs.writeFileSync("\\\\.\\NUL", "foo")
. This issue was raised against Node.js on GitHub: https://github.com/nodejs/node-v0.x-archive/issues/9271
由于NUL
是设备而不是文件,因此必须通过设备名称空间进行访问-这需要将\\.\
放在开头(其他斜杠用于转义)-请参见
Since NUL
is a device, not a file, it has to be accessed via device namespace - this requires putting \\.\
in the beginning (the other slashes are for escaping) - see https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247.aspx#Win32_Device_Namespaces.
NPM上还有一个简单的dev-null
库,可用于流: https://www.npmjs.com/package/dev-null (虽然不是.writeFile
).
There is also a simple dev-null
library on NPM that can be used with streams: https://www.npmjs.com/package/dev-null (not with .writeFile
though).
这篇关于如何在Windows下从node.js写入NUL设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!