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