@@ -1965,6 +1965,52 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
19651965}
19661966
19671967
1968+ // Wrapper for readv(2).
1969+ //
1970+ // bytesRead = fs.readv(fd, buffers[, position], callback)
1971+ // 0 fd integer. file descriptor
1972+ // 1 buffers array of buffers to read
1973+ // 2 position if integer, position to read at in the file.
1974+ // if null, read from the current position
1975+ static void ReadBuffers (const FunctionCallbackInfo<Value>& args) {
1976+ Environment* env = Environment::GetCurrent (args);
1977+
1978+ const int argc = args.Length ();
1979+ CHECK_GE (argc, 3 );
1980+
1981+ CHECK (args[0 ]->IsInt32 ());
1982+ const int fd = args[0 ].As <Int32>()->Value ();
1983+
1984+ CHECK (args[1 ]->IsArray ());
1985+ Local<Array> buffers = args[1 ].As <Array>();
1986+
1987+ int64_t pos = GetOffset (args[2 ]); // -1 if not a valid JS int
1988+
1989+ MaybeStackBuffer<uv_buf_t > iovs (buffers->Length ());
1990+
1991+ // Init uv buffers from ArrayBufferViews
1992+ for (uint32_t i = 0 ; i < iovs.length (); i++) {
1993+ Local<Value> buffer = buffers->Get (env->context (), i).ToLocalChecked ();
1994+ CHECK (Buffer::HasInstance (buffer));
1995+ iovs[i] = uv_buf_init (Buffer::Data (buffer), Buffer::Length (buffer));
1996+ }
1997+
1998+ FSReqBase* req_wrap_async = GetReqWrap (env, args[3 ]);
1999+ if (req_wrap_async != nullptr ) { // readBuffers(fd, buffers, pos, req)
2000+ AsyncCall (env, req_wrap_async, args, " read" , UTF8, AfterInteger,
2001+ uv_fs_read, fd, *iovs, iovs.length (), pos);
2002+ } else { // readBuffers(fd, buffers, undefined, ctx)
2003+ CHECK_EQ (argc, 5 );
2004+ FSReqWrapSync req_wrap_sync;
2005+ FS_SYNC_TRACE_BEGIN (read);
2006+ int bytesRead = SyncCall (env, /* ctx */ args[4 ], &req_wrap_sync, " read" ,
2007+ uv_fs_read, fd, *iovs, iovs.length (), pos);
2008+ FS_SYNC_TRACE_END (read, " bytesRead" , bytesRead);
2009+ args.GetReturnValue ().Set (bytesRead);
2010+ }
2011+ }
2012+
2013+
19682014/* fs.chmod(path, mode);
19692015 * Wrapper for chmod(1) / EIO_CHMOD
19702016 */
@@ -2228,6 +2274,7 @@ void Initialize(Local<Object> target,
22282274 env->SetMethod (target, " open" , Open);
22292275 env->SetMethod (target, " openFileHandle" , OpenFileHandle);
22302276 env->SetMethod (target, " read" , Read);
2277+ env->SetMethod (target, " readBuffers" , ReadBuffers);
22312278 env->SetMethod (target, " fdatasync" , Fdatasync);
22322279 env->SetMethod (target, " fsync" , Fsync);
22332280 env->SetMethod (target, " rename" , Rename);
0 commit comments