Skip to content

Commit c307f03

Browse files
XadillaXMylesBorins
authored andcommitted
doc,test: fs - reserved characters under win32
Explain the behavior of `fs.open()` under win32 that file path contains some characters and add some test cases for them. < (less than) > (greater than) : (colon) " (double quote) / (forward slash) \ (backslash) | (vertical bar or pipe) ? (question mark) * (asterisk) PR-URL: #13875 Refs: #13868 Refs: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx Refs: https://msdn.microsoft.com/en-us/library/windows/desktop/bb540537.aspx Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Bartosz Sosnowski <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 2af2165 commit c307f03

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

doc/api/fs.md

+10
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,14 @@ fs.open('<directory>', 'a+', (err, fd) => {
12681268
});
12691269
```
12701270

1271+
Some characters (`< > : " / \ | ? *`) are reserved under Windows as documented
1272+
by [Naming Files, Paths, and Namespaces][]. Under NTFS, if the filename contains
1273+
a colon, Node.js will open a file system stream, as described by
1274+
[this MSDN page][MSDN-Using-Streams].
1275+
1276+
Functions based on `fs.open()` exhibit this behavior as well. eg.
1277+
`fs.writeFile()`, `fs.readFile()`, etc.
1278+
12711279
## fs.openSync(path, flags[, mode])
12721280
<!-- YAML
12731281
added: v0.1.21
@@ -2264,3 +2272,5 @@ The following constants are meant for use with the [`fs.Stats`][] object's
22642272
[`ReadDirectoryChangesW`]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v=vs.85%29.aspx
22652273
[`AHAFS`]: https://www.ibm.com/developerworks/aix/library/au-aix_event_infrastructure/
22662274
[Common System Errors]: errors.html#errors_common_system_errors
2275+
[Naming Files, Paths, and Namespaces]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
2276+
[MSDN-Using-Streams]: https://msdn.microsoft.com/en-us/library/windows/desktop/bb540537.aspx
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const fs = require('fs');
6+
const path = require('path');
7+
8+
if (!common.isWindows)
9+
common.skip('This test is for Windows only.');
10+
11+
common.refreshTmpDir();
12+
13+
const DATA_VALUE = 'hello';
14+
15+
// Refs: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
16+
// Ignore '/', '\\' and ':'
17+
const RESERVED_CHARACTERS = '<>"|?*';
18+
19+
[...RESERVED_CHARACTERS].forEach((ch) => {
20+
const pathname = path.join(common.tmpDir, `somefile_${ch}`);
21+
assert.throws(
22+
() => {
23+
fs.writeFileSync(pathname, DATA_VALUE);
24+
},
25+
/^Error: ENOENT: no such file or directory, open '.*'$/,
26+
`failed with '${ch}'`);
27+
});
28+
29+
// Test for ':' (NTFS data streams).
30+
// Refs: https://msdn.microsoft.com/en-us/library/windows/desktop/bb540537.aspx
31+
const pathname = path.join(common.tmpDir, 'foo:bar');
32+
fs.writeFileSync(pathname, DATA_VALUE);
33+
34+
let content = '';
35+
const fileDataStream = fs.createReadStream(pathname, {
36+
encoding: 'utf8'
37+
});
38+
39+
fileDataStream.on('data', (data) => {
40+
content += data;
41+
});
42+
43+
fileDataStream.on('end', common.mustCall(() => {
44+
assert.strictEqual(content, DATA_VALUE);
45+
}));

0 commit comments

Comments
 (0)