diff options
author | James M Snell <jasnell@gmail.com> | 2016-03-09 07:58:45 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2016-03-26 00:21:27 +0300 |
commit | 060e5f0c0064e578c2150f13e3f91ac15fdeed92 (patch) | |
tree | 50783508a123991a024a9a85a2994d5ef2a83c5d /src/fs_event_wrap.cc | |
parent | 4d4f3535a9fd7486d7a94d825ac8e92a65bf9121 (diff) |
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>
Diffstat (limited to 'src/fs_event_wrap.cc')
-rw-r--r-- | src/fs_event_wrap.cc | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/src/fs_event_wrap.cc b/src/fs_event_wrap.cc index 7768f94459c..58f2716a6c0 100644 --- a/src/fs_event_wrap.cc +++ b/src/fs_event_wrap.cc @@ -6,6 +6,7 @@ #include "util-inl.h" #include "node.h" #include "handle_wrap.h" +#include "string_bytes.h" #include <stdlib.h> @@ -41,6 +42,7 @@ class FSEventWrap: public HandleWrap { uv_fs_event_t handle_; bool initialized_; + enum encoding encoding_; }; @@ -86,16 +88,20 @@ void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) { FSEventWrap* wrap = Unwrap<FSEventWrap>(args.Holder()); - if (args.Length() < 1 || !args[0]->IsString()) { - return env->ThrowTypeError("filename must be a valid string"); - } + static const char kErrMsg[] = "filename must be a string or Buffer"; + if (args.Length() < 1) + return env->ThrowTypeError(kErrMsg); - node::Utf8Value path(env->isolate(), args[0]); + BufferValue path(env->isolate(), args[0]); + if (*path == nullptr) + return env->ThrowTypeError(kErrMsg); unsigned int flags = 0; if (args[2]->IsTrue()) flags |= UV_FS_EVENT_RECURSIVE; + wrap->encoding_ = ParseEncoding(env->isolate(), args[3], UTF8); + int err = uv_fs_event_init(wrap->env()->event_loop(), &wrap->handle_); if (err == 0) { wrap->initialized_ = true; @@ -156,7 +162,18 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename, }; if (filename != nullptr) { - argv[2] = OneByteString(env->isolate(), filename); + Local<Value> fn = StringBytes::Encode(env->isolate(), + filename, + wrap->encoding_); + if (fn.IsEmpty()) { + argv[0] = Integer::New(env->isolate(), UV_EINVAL); + argv[2] = StringBytes::Encode(env->isolate(), + filename, + strlen(filename), + BUFFER); + } else { + argv[2] = fn; + } } wrap->MakeCallback(env->onchange_string(), ARRAY_SIZE(argv), argv); |