@@ -89,18 +89,11 @@ static int After(eio_req *req) {
8989
9090 case EIO_READ :
9191 {
92- if (req->int3 ) {
93- // legacy interface
94- Local<Object> obj = Local<Object>::New (*callback);
95- Local<Value> enc_val = obj->GetHiddenValue (encoding_symbol);
96- argv[1 ] = Encode (req->ptr2 , req->result , ParseEncoding (enc_val));
97- argv[2 ] = Integer::New (req->result );
98- argc = 3 ;
99- } else {
100- // Buffer interface
101- argv[1 ] = Integer::New (req->result );
102- argc = 2 ;
103- }
92+ argc = 3 ;
93+ Local<Object> obj = Local<Object>::New (*callback);
94+ Local<Value> enc_val = obj->GetHiddenValue (encoding_symbol);
95+ argv[1 ] = Encode (req->ptr2 , req->result , ParseEncoding (enc_val));
96+ argv[2 ] = Integer::New (req->result );
10497 break ;
10598 }
10699
@@ -563,146 +556,55 @@ static Handle<Value> Write(const Arguments& args) {
563556 return Undefined ();
564557
565558 } else {
566- if (legacy) {
567- written = pos < 0 ? write (fd, buf, len) : pwrite (fd, buf, len, pos);
568- delete [] reinterpret_cast <char *>(buf);
569- if (written < 0 ) return ThrowException (ErrnoException (errno));
570- return scope.Close (Integer::New (written));
559+ if (pos < 0 ) {
560+ written = write (fd, buf, len);
571561 } else {
572- assert ( 0 && " fs.writeSync() with buffers is not yet supported " );
562+ written = pwrite (fd, buf, len, pos );
573563 }
564+ if (written < 0 ) return ThrowException (ErrnoException (errno));
565+ return scope.Close (Integer::New (written));
574566 }
575567}
576568
577- /*
569+ /* fs.read(fd, length, position, encoding)
578570 * Wrapper for read(2).
579571 *
580- * fs.read(fd, buffer, offset, length, position)
581- *
582- * 0 fd integer. file descriptor
583- * 1 buffer instance of Buffer
584- * 2 offset integer. offset to start reading into inside buffer
585- * 3 length integer. length to read
586- * 4 position file position - null for current position
587- *
588- * - OR -
589- *
590- * fs.read(fd, length, position, encoding)
591- *
592572 * 0 fd integer. file descriptor
593573 * 1 length integer. length to read
594574 * 2 position if integer, position to read from in the file.
595575 * if null, read from the current position
596576 * 3 encoding
597- *
598577 */
599578static Handle<Value> Read (const Arguments& args) {
600579 HandleScope scope;
601580
602- if (args.Length () < 2 || !args[0 ]->IsInt32 ()) {
581+ if (args.Length () < 2 || !args[0 ]->IsInt32 () || !args[ 1 ]-> IsNumber () ) {
603582 return THROW_BAD_ARGS ;
604583 }
605584
606585 int fd = args[0 ]->Int32Value ();
586+ size_t len = args[1 ]->IntegerValue ();
587+ off_t offset = args[2 ]->IsNumber () ? args[2 ]->IntegerValue () : -1 ;
588+ enum encoding encoding = ParseEncoding (args[3 ]);
607589
608- Local<Value> cb;
609- bool legacy;
610-
611- size_t len;
612- ssize_t pos;
613- enum encoding encoding;
614-
615- char * buf = NULL ;
616-
617- if (Buffer::HasInstance (args[1 ])) {
618- legacy = false ;
619- // 0 fd integer. file descriptor
620- // 1 buffer instance of Buffer
621- // 2 offset integer. offset to start reading into inside buffer
622- // 3 length integer. length to read
623- // 4 position file position - null for current position
624- Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[1 ]->ToObject ());
625-
626- size_t off = args[2 ]->Int32Value ();
627- if (off >= buffer->length ()) {
628- return ThrowException (Exception::Error (
629- String::New (" Offset is out of bounds" )));
630- }
631-
632- len = args[3 ]->Int32Value ();
633- if (off + len > buffer->length ()) {
634- return ThrowException (Exception::Error (
635- String::New (" Length is extends beyond buffer" )));
636- }
637-
638- pos = args[4 ]->IsNumber () ? args[4 ]->IntegerValue () : -1 ;
639-
640- buf = (char *)buffer->data () + off;
641-
642- cb = args[5 ];
643-
644- } else {
645- legacy = true ;
646- // 0 fd integer. file descriptor
647- // 1 length integer. length to read
648- // 2 position if integer, position to read from in the file.
649- // if null, read from the current position
650- // 3 encoding
651- len = args[1 ]->IntegerValue ();
652- pos = args[2 ]->IsNumber () ? args[2 ]->IntegerValue () : -1 ;
653- encoding = ParseEncoding (args[3 ]);
654-
655- buf = NULL ; // libeio will allocate and free it.
656-
657- cb = args[4 ];
658- }
659-
660-
661- if (cb->IsFunction ()) {
662- // WARNING: HACK AGAIN, PROCEED WITH CAUTION
663- // Normally here I would do
664- // ASYNC_CALL(read, args[4], fd, NULL, len, offset)
665- // but I'm trying to support a legacy interface where it's acceptable to
666- // return a string in the callback. As well as a new Buffer interface
667- // which reads data into a user supplied buffer.
668-
669- // Set the encoding on the callback
670- if (legacy) {
671- Local<Object> obj = cb->ToObject ();
672- obj->SetHiddenValue (encoding_symbol, args[3 ]);
673- }
674-
675- if (legacy) assert (buf == NULL );
676-
677-
678- eio_req *req = eio_read (fd, buf, len, pos,
679- EIO_PRI_DEFAULT ,
680- After,
681- cb_persist (cb));
682- assert (req);
683-
684- req->int3 = legacy ? 1 : 0 ;
685- ev_ref (EV_DEFAULT_UC );
686- return Undefined ();
687-
590+ if (args[4 ]->IsFunction ()) {
591+ Local<Object> obj = args[4 ]->ToObject ();
592+ obj->SetHiddenValue (encoding_symbol, args[3 ]);
593+ ASYNC_CALL (read, args[4 ], fd, NULL , len, offset)
688594 } else {
689- if (legacy) {
690595#define READ_BUF_LEN (16 *1024 )
691- char buf[READ_BUF_LEN ];
692- ssize_t ret;
693- if (pos < 0 ) {
694- ret = read (fd, buf, MIN (len, READ_BUF_LEN ));
695- } else {
696- ret = pread (fd, buf, MIN (len, READ_BUF_LEN ), pos);
697- }
698- if (ret < 0 ) return ThrowException (ErrnoException (errno));
699- Local<Array> a = Array::New (2 );
700- a->Set (Integer::New (0 ), Encode (buf, ret, encoding));
701- a->Set (Integer::New (1 ), Integer::New (ret));
702- return scope.Close (a);
596+ char *buf[READ_BUF_LEN ];
597+ ssize_t ret;
598+ if (offset < 0 ) {
599+ ret = read (fd, buf, MIN (len, READ_BUF_LEN ));
703600 } else {
704- assert ( 0 && " fs.readSync() with buffers is not support yet " );
601+ ret = pread (fd, buf, MIN (len, READ_BUF_LEN ), offset );
705602 }
603+ if (ret < 0 ) return ThrowException (ErrnoException (errno));
604+ Local<Array> a = Array::New (2 );
605+ a->Set (Integer::New (0 ), Encode (buf, ret, encoding));
606+ a->Set (Integer::New (1 ), Integer::New (ret));
607+ return scope.Close (a);
706608 }
707609}
708610
0 commit comments