fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-08 20:58:45 -08:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-21 17:16:19 -07:00
|
|
|
// This test is a bit more complicated than it ideally needs to be to work
|
|
|
|
// around issues on OS X and SmartOS.
|
|
|
|
//
|
|
|
|
// On OS X, watch events are subject to peculiar timing oddities such that an
|
|
|
|
// event might fire out of order. The synchronous refreshing of the tmp
|
|
|
|
// directory might trigger an event on the watchers that are instantiated after
|
|
|
|
// it!
|
|
|
|
//
|
|
|
|
// On SmartOS, the watch events fire but the filename is null.
|
|
|
|
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-08 20:58:45 -08:00
|
|
|
const common = require('../common');
|
|
|
|
|
2016-12-02 00:55:02 -05:00
|
|
|
// fs-watch on folders have limited capability in AIX.
|
|
|
|
// The testcase makes use of folder watching, and causes
|
|
|
|
// hang. This behavior is documented. Skip this for AIX.
|
|
|
|
|
2017-07-16 15:07:33 +08:00
|
|
|
if (common.isAIX)
|
2016-12-02 00:55:02 -05:00
|
|
|
common.skip('folder watch capability is limited in AIX.');
|
2017-07-01 02:29:09 +03:00
|
|
|
|
2022-10-31 19:15:45 -04:00
|
|
|
if (common.isIBMi)
|
|
|
|
common.skip('IBMi does not support `fs.watch()`');
|
|
|
|
|
2017-07-01 02:29:09 +03:00
|
|
|
const fs = require('fs');
|
2016-12-02 00:55:02 -05:00
|
|
|
|
2017-12-24 22:38:11 -08:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
2016-06-16 09:48:21 -07:00
|
|
|
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-08 20:58:45 -08:00
|
|
|
const fn = '新建文夹件.txt';
|
2023-08-15 22:45:24 +09:00
|
|
|
const a = tmpdir.resolve(fn);
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-08 20:58:45 -08:00
|
|
|
|
2016-06-21 17:16:19 -07:00
|
|
|
const watchers = new Set();
|
|
|
|
|
|
|
|
function registerWatcher(watcher) {
|
|
|
|
watchers.add(watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
function unregisterWatcher(watcher) {
|
|
|
|
watcher.close();
|
|
|
|
watchers.delete(watcher);
|
|
|
|
if (watchers.size === 0) {
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 05:24:43 -05:00
|
|
|
{
|
|
|
|
// Test that using the `encoding` option has the expected result.
|
|
|
|
const watcher = fs.watch(
|
|
|
|
tmpdir.path,
|
|
|
|
{ encoding: 'hex' },
|
|
|
|
(event, filename) => {
|
|
|
|
if (['e696b0e5bbbae69687e5a4b9e4bbb62e747874', null].includes(filename))
|
|
|
|
done(watcher);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
registerWatcher(watcher);
|
|
|
|
}
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-08 20:58:45 -08:00
|
|
|
|
2019-01-16 05:24:43 -05:00
|
|
|
{
|
|
|
|
// Test that in absence of `encoding` option has the expected result.
|
|
|
|
const watcher = fs.watch(
|
|
|
|
tmpdir.path,
|
|
|
|
(event, filename) => {
|
|
|
|
if ([fn, null].includes(filename))
|
|
|
|
done(watcher);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
registerWatcher(watcher);
|
|
|
|
}
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-08 20:58:45 -08:00
|
|
|
|
2019-01-16 05:24:43 -05:00
|
|
|
{
|
|
|
|
// Test that using the `encoding` option has the expected result.
|
|
|
|
const watcher = fs.watch(
|
|
|
|
tmpdir.path,
|
|
|
|
{ encoding: 'buffer' },
|
|
|
|
(event, filename) => {
|
|
|
|
if (filename instanceof Buffer && filename.toString('utf8') === fn)
|
|
|
|
done(watcher);
|
|
|
|
else if (filename === null)
|
|
|
|
done(watcher);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
registerWatcher(watcher);
|
|
|
|
}
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-08 20:58:45 -08:00
|
|
|
|
2016-06-21 17:16:19 -07:00
|
|
|
const done = common.mustCall(unregisterWatcher, watchers.size);
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-08 20:58:45 -08:00
|
|
|
|
2016-06-21 17:16:19 -07:00
|
|
|
// OS X and perhaps other systems can have surprising race conditions with
|
|
|
|
// file events. So repeat the operation in case it is missed the first time.
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
const fd = fs.openSync(a, 'w+');
|
|
|
|
fs.closeSync(fd);
|
|
|
|
fs.unlinkSync(a);
|
|
|
|
}, common.platformTimeout(100));
|