Module: EIO

Defined in:
lib/eio.rb,
ext/eio/eio_ext.c,
lib/eio/eventmachine.rb

Defined Under Namespace

Classes: Middleware, Request

Constant Summary collapse

VERSION =
'0.1'
PRI_MIN =
INT2NUM(EIO_PRI_MIN)
PRI_MAX =
INT2NUM(EIO_PRI_MAX)
PRI_DEFAULT =
INT2NUM(EIO_PRI_DEFAULT)
RDONLY =
INT2NUM(O_RDONLY)
WRONLY =
INT2NUM(O_WRONLY)
RDWR =
INT2NUM(O_RDWR)
APPEND =
INT2NUM(O_APPEND)
CREAT =
INT2NUM(O_CREAT)
EXCL =
INT2NUM(O_EXCL)

Class Method Summary collapse

Class Method Details

.chmod('/path/file') { ... } ⇒ EIO::Request

Asynchronously change permissions for a given file path.

Examples

EIO.chmod('/path/file', 0777){ p :chmodded }  =>  EIO::Request
cb = Proc.new{ p :chmodded }
EIO.chmod('/path/file', 0777, cb)             =>  EIO::Request

EIO.chmod('/path/file')                       =>  Fixnum
EIO.chmod('/path/file', 0777)                 =>  Fixnum

Yields:

Returns:



976
977
978
979
980
981
982
983
984
985
986
987
# File 'ext/eio/eio_ext.c', line 976

static VALUE
rb_eio_s_chmod(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, mode, proc, cb;
    rb_scan_args(argc, argv, "12&", &path, &mode, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(path, T_STRING);
    if (NIL_P(mode)) mode = eio_default_mode;
    Check_Type(mode, T_FIXNUM);
    SubmitRequest(chmod, rb_eio_generic_cb, StringValueCStr(path), FIX2INT(mode));
}

.chown('/path/file') { ... } ⇒ EIO::Request

Asynchronously changes ownership for a given file path.

Examples

EIO.chown('/path/file', 500){ p :chowned }       =>  EIO::Request
EIO.chown('/path/file', 500, 500){ p :chowned }  =>  EIO::Request
cb = Proc.new{ p :chowned }
EIO.chown('/path/file', 500, 500, cb)            =>  EIO::Request

EIO.chown('/path/file', 500)                     =>  Fixnum
EIO.chown('/path/file', 500, 500)                =>  Fixnum

Yields:

Returns:



1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# File 'ext/eio/eio_ext.c', line 1089

static VALUE
rb_eio_s_chown(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, uid, gid, proc, cb;
    rb_scan_args(argc, argv, "13&", &path, &uid, &gid, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(path, T_STRING);
    if (NIL_P(uid)) uid = INT2NUM(getuid());
    Check_Type(uid, T_FIXNUM);
    if (NIL_P(gid)) gid = INT2NUM(getgid());
    Check_Type(gid, T_FIXNUM);
    SubmitRequest(chown, rb_eio_generic_cb, StringValueCStr(path), FIX2INT(uid), FIX2INT(gid));
}

.close(fd) { ... } ⇒ EIO::Request

Asynchronously close a file and call the callback with the result code.

Examples

cb = Proc.new{ p :closed }
EIO.close(fd, cb)           =>  EIO::Request

EIO.close(fd)               =>  Fixnum

Yields:

Returns:



533
534
535
536
537
538
539
540
541
542
# File 'ext/eio/eio_ext.c', line 533

static VALUE
rb_eio_s_close(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE fd, proc, cb;
    rb_scan_args(argc, argv, "11&", &fd, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(fd, T_FIXNUM);
    SubmitRequest(close, rb_eio_generic_cb, FIX2INT(fd));
}

.eventmachine_handlerObject

Registers the read end of a pipe with Eventmachine which wakes up the event loop whenever there’s results to process.



21
22
23
# File 'lib/eio/eventmachine.rb', line 21

def self.eventmachine_handler
  EM.watch(EIO.fd, EM::EioHandler){ |c| c.notify_readable = true }
end

.fchmod(fd) { ... } ⇒ EIO::Request

Asynchronously change ownership for a given file descriptor.

Examples

EIO.fchmod(fd, 0777){ p :chmodded }  =>  EIO::Request
cb = Proc.new{ p :chmodded }
EIO.fchmod(fd, 0777, cb)             =>  EIO::Request

EIO.fchmod(fd)                       =>  Fixnum
EIO.fchmod(fd, 0777)                 =>  Fixnum

Yields:

Returns:



1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
# File 'ext/eio/eio_ext.c', line 1004

static VALUE
rb_eio_s_fchmod(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE fd, mode, proc, cb;
    rb_scan_args(argc, argv, "12&", &fd, &mode, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(fd, T_FIXNUM);
    if (NIL_P(mode)) mode = eio_default_mode;
    Check_Type(mode, T_FIXNUM);
    SubmitRequest(fchmod, rb_eio_generic_cb, FIX2INT(fd), FIX2INT(mode));
}

.fchown(fd) { ... } ⇒ EIO::Request

Asynchronously changes ownership for a given file descriptor.

Examples

EIO.fchown(fd, 500){ p :chowned }       =>  EIO::Request
EIO.fchown(fd, 500, 500){ p :chowned }  =>  EIO::Request
cb = Proc.new{ p :chowned }
EIO.fchown(fd, 500, 500, cb)            =>  EIO::Request

EIO.fchown(fd, 500)                     =>  Fixnum
EIO.fchown(fd, 500, 500)                =>  Fixnum

Yields:

Returns:



1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
# File 'ext/eio/eio_ext.c', line 1120

static VALUE
rb_eio_s_fchown(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE fd, uid, gid, proc, cb;
    rb_scan_args(argc, argv, "13&", &fd, &uid, &gid, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(fd, T_FIXNUM);
    if (NIL_P(uid)) uid = INT2NUM(getuid());
    Check_Type(uid, T_FIXNUM);
    if (NIL_P(gid)) gid = INT2NUM(getgid());
    Check_Type(gid, T_FIXNUM);
    SubmitRequest(fchown, rb_eio_generic_cb, FIX2INT(fd), FIX2INT(uid), FIX2INT(gid));
}

.fdFixnum

Read end of the pipe an event loop can monitor for readability

Returns:

  • (Fixnum)


397
398
399
400
401
# File 'ext/eio/eio_ext.c', line 397

static VALUE
rb_eio_s_fd(VALUE eio)
{
    return INT2NUM(eio_pipe_r_fd);
}

.fdatasync(fd) { ... } ⇒ EIO::Request

Asynchronously call fdatasync on the given filehandle and call the callback with the result code.

Examples

cb = Proc.new{ p :synced }
EIO.fdatasync(fd, cb)           =>  EIO::Request

EIO.fdatasync(fd)               =>  Fixnum

Yields:

Returns:



583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'ext/eio/eio_ext.c', line 583

static VALUE
rb_eio_s_fdatasync(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE fd, proc, cb;
    rb_scan_args(argc, argv, "11&", &fd, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(fd, T_FIXNUM);
    SyncRequest({
#if HAVE_FDATASYNC
       ret = fdatasync(FIX2INT(fd));
#else
       ret = fsync(FIX2INT(fd));
#endif
       if (ret == -1) rb_sys_fail("fdatasync");
       return INT2NUM(ret);
    });
    AsyncRequest(fdatasync, rb_eio_generic_cb, FIX2INT(fd));
}

.fsync(fd) { ... } ⇒ EIO::Request

Asynchronously call fsync on the given filehandle and call the callback with the result code.

Examples

cb = Proc.new{ p :synced }
EIO.fsync(fd, cb)           =>  EIO::Request

EIO.fsync(fd)               =>  Fixnum

Yields:

Returns:



558
559
560
561
562
563
564
565
566
567
# File 'ext/eio/eio_ext.c', line 558

static VALUE
rb_eio_s_fsync(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE fd, proc, cb;
    rb_scan_args(argc, argv, "11&", &fd, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(fd, T_FIXNUM);
    SubmitRequest(fsync, rb_eio_generic_cb, FIX2INT(fd));
}

.ftruncate(fd) { ... } ⇒ EIO::Request

Asynchronously truncates a given file descriptor.

Examples

EIO.ftruncate(fd, 100){ p :truncated }  =>  EIO::Request
cb = Proc.new{ p :truncated }
EIO.ftruncate(fd, 100, cb)              =>  EIO::Request

EIO.ftruncate(fd)                       =>  Fixnum
EIO.ftruncate(fd, 100)                  =>  Fixnum

Yields:

Returns:



1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
# File 'ext/eio/eio_ext.c', line 1060

static VALUE
rb_eio_s_ftruncate(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE fd, offset, proc, cb;
    rb_scan_args(argc, argv, "12&", &fd, &offset, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(fd, T_FIXNUM);
    if (NIL_P(offset)) offset = eio_zero;
    Check_Type(offset, T_FIXNUM);
    SubmitRequest(ftruncate, rb_eio_generic_cb, FIX2INT(fd), FIX2INT(offset));
}

.idle_timeout=(x) ⇒ Fixnum

Set the minimum idle timeout before a thread is allowed to exit

Returns:

  • (Fixnum)


480
481
482
483
484
485
# File 'ext/eio/eio_ext.c', line 480

static VALUE
rb_eio_s_set_idle_timeout(VALUE eio, VALUE seconds)
{
    eio_set_idle_timeout(FIX2INT(seconds));
    return seconds;
}

Asynchronously create a new link to the existing object at source path at the destination path and call the callback with the result code.

Examples

cb = Proc.new{ p :linked }
EIO.link('/path/a', '/path/b', cb)           =>  EIO::Request

EIO.link('/path/a', '/path/b')               =>  Fixnum

Yields:

Returns:



1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'ext/eio/eio_ext.c', line 1149

static VALUE
rb_eio_s_link(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, new_path, proc, cb;
    rb_scan_args(argc, argv, "21&", &path, &new_path, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(path, T_STRING);
    Check_Type(new_path, T_STRING);
    SubmitRequest(link, rb_eio_generic_cb, StringValueCStr(path), StringValueCStr(new_path));
}

.max_idle=(x) ⇒ Fixnum

Limit the number of threads allowed to be idle

Returns:

  • (Fixnum)


466
467
468
469
470
471
# File 'ext/eio/eio_ext.c', line 466

static VALUE
rb_eio_s_set_max_idle(VALUE eio, VALUE threads)
{
    eio_set_max_idle(FIX2INT(threads));
    return threads;
}

.max_parallel=(x) ⇒ Fixnum

Set the maximum number of AIO threads to run in parallel. default: 8

Returns:

  • (Fixnum)


452
453
454
455
456
457
# File 'ext/eio/eio_ext.c', line 452

static VALUE
rb_eio_s_set_max_parallel(VALUE eio, VALUE threads)
{
    eio_set_max_parallel(FIX2INT(threads));
    return threads;
}

.max_poll_reqs=(x) ⇒ Fixnum

Set the maximum number of requests by each eio_poll() invocation

Returns:

  • (Fixnum)


424
425
426
427
428
429
# File 'ext/eio/eio_ext.c', line 424

static VALUE
rb_eio_s_set_max_poll_reqs(VALUE eio, VALUE requests)
{
    eio_set_max_poll_reqs(FIX2INT(requests));
    return requests;
}

.max_poll_time=(x) ⇒ Fixnum

Set the maximum amount of time spent in each eio_poll() invocation

Returns:

  • (Fixnum)


410
411
412
413
414
415
# File 'ext/eio/eio_ext.c', line 410

static VALUE
rb_eio_s_set_max_poll_time(VALUE eio, VALUE seconds)
{
    eio_set_max_poll_time(FIX2LONG(seconds));
    return seconds;
}

.min_parallel=(x) ⇒ Fixnum

Set the minimum number of libeio threads to run in parallel. default: 8

Returns:

  • (Fixnum)


438
439
440
441
442
443
# File 'ext/eio/eio_ext.c', line 438

static VALUE
rb_eio_s_set_min_parallel(VALUE eio, VALUE threads)
{
    eio_set_min_parallel(FIX2INT(threads));
    return threads;
}

.mkdir('/path') { ... } ⇒ EIO::Request

Asynchronously mkdir (create) a directory and call the callback with the result code.

Examples

EIO.mkdir('/path', 0777){ p :created }   =>  EIO::Request
cb = Proc.new{ p :created }
EIO.mkdir('/path', 0777, cb)             =>  EIO::Request

EIO.mkdir('/path')                       =>  Fixnum
EIO.mkdir('/path', 0777)                 =>  Fixnum

Yields:

Returns:



821
822
823
824
825
826
827
828
829
830
831
832
# File 'ext/eio/eio_ext.c', line 821

static VALUE
rb_eio_s_mkdir(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, mode, proc, cb;
    rb_scan_args(argc, argv, "12&", &path, &mode, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(path, T_STRING);
    if (NIL_P(mode)) mode = eio_default_mode;
    Check_Type(mode, T_FIXNUM);
    SubmitRequest(mkdir, rb_eio_generic_cb, StringValueCStr(path), FIX2INT(mode));
}

.open('/path/file') {|fd| ... } ⇒ EIO::Request

Asynchronously open or create a file and call the callback with a newly created file handle for the file.

Examples

EIO.open('/path/file', EIO::RDONLY){|fd| p fd }       =>  EIO::Request
EIO.open('/path/file', EIO::RDWR, 0777){|fd| p fd }   =>  EIO::Request
cb = Proc.new{|fd| p fd }
EIO.open('/path/file', EIO::RDWR, 0777, cb)           =>  EIO::Request

EIO.open('/path/file')                                =>  Fixnum
EIO.open('/path/file', EIO::RDWR)                     =>  Fixnum
EIO.open('/path/file', EIO::RDWR, 0777)               =>  Fixnum

Yields:

Returns:



505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'ext/eio/eio_ext.c', line 505

static VALUE
rb_eio_s_open(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, flags, mode, proc, cb;
    rb_scan_args(argc, argv, "13&", &path, &flags, &mode, &proc, &cb);
    AssertCallback(cb, 1);
    Check_Type(path, T_STRING);
    if (NIL_P(flags)) flags = INT2NUM(O_RDONLY);
    Check_Type(flags, T_FIXNUM);
    if (NIL_P(mode)) mode = eio_default_mode;
    Check_Type(mode, T_FIXNUM);
    SubmitRequest(open, rb_eio_open_cb, StringValueCStr(path), FIX2INT(flags), FIX2INT(mode));
}

.pendingFixnum

Number of requests currently in the pending state

Returns:

  • (Fixnum)


371
372
373
374
375
# File 'ext/eio/eio_ext.c', line 371

static VALUE
rb_eio_s_pending(VALUE eio)
{
    return INT2NUM(eio_npending());
}

.pollFixnum

Called when pending requests need finishing

Returns:

  • (Fixnum)


329
330
331
332
333
334
335
336
# File 'ext/eio/eio_ext.c', line 329

static VALUE
rb_eio_s_poll(VALUE eio)
{
    int res;
    res = eio_poll();
    if (res > 0) rb_sys_fail("eio_poll");
    return INT2NUM(res);
}

.read(fd) {|d| ... } ⇒ EIO::Request

Asynchronously reads length bytes from a specified offset into a buffer.

Examples

EIO.read(fd, 100){|d| p d }      =>  EIO::Request
EIO.read(fd, 100, 50){|d| p d }  =>  EIO::Request
cb = Proc.new{|d| p d }
EIO.read(fd, 100, 50, cb)        =>  EIO::Request

EIO.read(fd)                     =>  String
EIO.read(fd, 100)                =>  String
EIO.read(fd, 100, 50)            =>  String

Yields:

  • (d)

Returns:



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'ext/eio/eio_ext.c', line 620

static VALUE
rb_eio_s_read(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE fd, len, offset, proc, cb;
    VALUE buf;
    rb_scan_args(argc, argv, "13&", &fd, &len, &offset, &proc, &cb);
    AssertCallback(cb, 1);
    Check_Type(fd, T_FIXNUM);
    if (NIL_P(len)) len = eio_default_bufsize;
    Check_Type(len, T_FIXNUM);
    if (len == eio_zero) len = eio_default_bufsize;
    if (NIL_P(offset)) offset = eio_zero;
    Check_Type(offset, T_FIXNUM);
    SyncRequest({
        buf = rb_str_new(0, FIX2INT(len));
        if (offset == eio_zero){
            ret = read(FIX2INT(fd), RSTRING_PTR(buf), FIX2INT(len));
        } else {
            ret = pread(FIX2INT(fd), RSTRING_PTR(buf), FIX2INT(len), FIX2INT(offset));
        }
        if (ret == -1) rb_sys_fail("read");
        return buf;
    });
    AsyncRequest(read, rb_eio_read_cb, FIX2INT(fd), 0, FIX2INT(len), FIX2INT(offset));
}

.readahead(fd) {|d| ... } ⇒ EIO::Request

Populates the page cache with data from a file so that subsequent reads from that file will not block on disk I/O.

Examples

EIO.readahead(fd, 100){|d| p :read }   =>  EIO::Request
EIO.readahead(fd, 100, 50){ p :read }  =>  EIO::Request
cb = Proc.new{ p :read }
EIO.readahead(fd, 100, 50, cb)         =>  EIO::Request

EIO.readahead(fd)                      =>  Fixnum
EIO.readahead(fd, 100)                 =>  Fixnum
EIO.readahead(fd, 100, 50)             =>  Fixnum

Yields:

  • (d)

Returns:



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# File 'ext/eio/eio_ext.c', line 665

static VALUE
rb_eio_s_readahead(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE fd, len, offset, proc, cb;
    rb_scan_args(argc, argv, "13&", &fd, &len, &offset, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(fd, T_FIXNUM);
    if (NIL_P(len)) len = eio_default_bufsize;
    Check_Type(len, T_FIXNUM);
    if (len == eio_zero) len = eio_default_bufsize;
    if (NIL_P(offset)) offset = eio_zero;
    Check_Type(offset, T_FIXNUM);
    SubmitRequest(readahead, rb_eio_generic_cb, FIX2INT(fd), FIX2INT(offset), FIX2INT(len));
}

.readdir('/path') {|fs| ... } ⇒ EIO::Request

Unlike the POSIX call of the same name, aio_readdir reads an entire directory (i.e. opendir + readdir + closedir). The entries will not be sorted, and will NOT include the . and .. entries.

Examples

cb = Proc.new{|b| p b }
EIO.readdir('/path', cb)           =>  EIO::Request

EIO.readdir('/path')               =>  Array

Yields:

  • (fs)

Returns:



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# File 'ext/eio/eio_ext.c', line 776

static VALUE
rb_eio_s_readdir(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, proc, cb;
    VALUE files;
    char *name;
    struct dirent *ent;
    rb_scan_args(argc, argv, "11&", &path, &proc, &cb);
    AssertCallback(cb, 1);
    Check_Type(path, T_STRING);
    SyncRequest({
        DIR *dir = opendir(StringValueCStr(path));
        if (!dir) rb_sys_fail(StringValueCStr(path));

        files = rb_ary_new();

        while ((ent = readdir(dir))) {
          name = ent->d_name;
          if (name[0] != '.' || (name[1] && (name[1] != '.' || name[2]))) {
              rb_ary_push(files, rb_str_new2(name));
          }
        }
        ret = closedir(dir);
        if (ret == -1) rb_sys_fail("closedir");
        return(files);
    });
    AsyncRequest(readdir, rb_eio_readdir_cb, StringValueCStr(path), EIO_READDIR_STAT_ORDER);
}

Asynchronously read the symlink specified by path and pass it to the callback.

Examples

cb = Proc.new{|l| p l }
EIO.readlink('/path/link', cb)         =>  EIO::Request

EIO.readlink('/path/link')             =>  Fixnum

Yields:

  • (l)

Returns:



895
896
897
898
899
900
901
902
903
904
905
906
907
# File 'ext/eio/eio_ext.c', line 895

static VALUE
rb_eio_s_readlink(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, proc, cb;
    rb_scan_args(argc, argv, "11&", &path, &proc, &cb);
    AssertCallback(cb, 1);
    Check_Type(path, T_STRING);
    SyncRequest({
        return rb_funcall(rb_cFile, sym_readlink, 1, path);
    });
    AsyncRequest(readlink, rb_eio_read_cb, StringValueCStr(path));
}

.readyFixnum

Number of requests currently in the ready state (not yet executed)

Returns:

  • (Fixnum)


358
359
360
361
362
# File 'ext/eio/eio_ext.c', line 358

static VALUE
rb_eio_s_ready(VALUE eio)
{
    return INT2NUM(eio_nready());
}

.rename('/path/a', '/path/b') { ... } ⇒ EIO::Request

Asynchronously rename the object at source path to destination path

Examples

cb = Proc.new{ p :renamed }
EIO.rename('/path/a', '/path/b', cb)            =>  EIO::Request

EIO.rename('/path/a', '/path/b')                =>  Fixnum

Yields:

Returns:



949
950
951
952
953
954
955
956
957
958
959
# File 'ext/eio/eio_ext.c', line 949

static VALUE
rb_eio_s_rename(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, new_path, proc, cb;
    rb_scan_args(argc, argv, "21&", &path, &new_path, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(path, T_STRING);
    Check_Type(new_path, T_STRING);
    SubmitRequest(rename, rb_eio_generic_cb, StringValueCStr(path), StringValueCStr(new_path));
}

.requestsFixnum

Number of requests currently in the ready, execute or pending states

Returns:

  • (Fixnum)


345
346
347
348
349
# File 'ext/eio/eio_ext.c', line 345

static VALUE
rb_eio_s_requests(VALUE eio)
{
    return INT2NUM(eio_nreqs());
}

.rmdir('/path') { ... } ⇒ EIO::Request

Asynchronously rmdir (delete) a directory and call the callback with the result code.

Examples

cb = Proc.new{ p :removed }
EIO.rmdir('/path', cb)             =>  EIO::Request

EIO.rmdir('/path')                 =>  Fixnum

Yields:

Returns:



847
848
849
850
851
852
853
854
855
856
# File 'ext/eio/eio_ext.c', line 847

static VALUE
rb_eio_s_rmdir(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, proc, cb;
    rb_scan_args(argc, argv, "11&", &path, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(path, T_STRING);
    SubmitRequest(rmdir, rb_eio_generic_cb, StringValueCStr(path));
}

.sendfile(in_fd, out_fd) {|b| ... } ⇒ EIO::Request

Tries to copy length bytes from in fd to out fd, starting at a given offset.

Examples

EIO.sendfile(in_fd, out_fd, 100){|b| p b }      =>  EIO::Request
EIO.sendfile(in_fd, out_fd, 100, 50){|b| p b }  =>  EIO::Request
cb = Proc.new{|b| p b }
EIO.sendfile(in_fd, out_fd, 100, 50, cb)        =>  EIO::Request

EIO.sendfile(in_fd, out_fd)                     =>  Fixnum
EIO.sendfile(in_fd, out_fd, 100)                =>  Fixnum
EIO.sendfile(in_fd, out_fd, 100, 50)            =>  Fixnum

Yields:

  • (b)

Returns:



744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# File 'ext/eio/eio_ext.c', line 744

static VALUE
rb_eio_s_sendfile(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE out_fd, in_fd, offset, len, proc, cb;
    rb_scan_args(argc, argv, "23&", &out_fd, &in_fd, &offset, &len, &proc, &cb);
    AssertCallback(cb, 1);
    Check_Type(in_fd, T_FIXNUM);
    Check_Type(out_fd, T_FIXNUM);
    if (NIL_P(len)) len = eio_default_bufsize;
    Check_Type(len, T_FIXNUM);
    if (len == eio_zero) len = eio_default_bufsize;
    if (NIL_P(offset)) offset = eio_zero;
    Check_Type(offset, T_FIXNUM);
    SubmitRequest(sendfile, rb_eio_write_cb, FIX2INT(out_fd), FIX2INT(in_fd), FIX2INT(offset), FIX2INT(len));
}

.stat('/path/file') {|s| ... } ⇒ EIO::Request

Works like Ruby’s stat. The callback will be called after the stat.

Examples

cb = Proc.new{|s| p s }
EIO.stat('/path/file', cb)         =>  EIO::Request

EIO.stat('/path/file')             =>  File::Stat

Yields:

  • (s)

Returns:



922
923
924
925
926
927
928
929
930
931
932
933
934
# File 'ext/eio/eio_ext.c', line 922

static VALUE
rb_eio_s_stat(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, proc, cb;
    rb_scan_args(argc, argv, "11&", &path, &proc, &cb);
    AssertCallback(cb, 1);
    Check_Type(path, T_STRING);
    SyncRequest({
        return rb_funcall(rb_cFile, sym_stat, 1, path);
    });
    AsyncRequest(stat, rb_eio_stat_cb, StringValueCStr(path));
}

Asynchronously create a new symbolic link to the existing object at sourc path at the destination path and call the callback with the result code.

Examples

cb = Proc.new{ p :linked }
EIO.symlink('/path/a', '/path/b', cb)           =>  EIO::Request

EIO.symlink('/path/a', '/path/b')               =>  Fixnum

Yields:

Returns:



1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
# File 'ext/eio/eio_ext.c', line 1175

static VALUE
rb_eio_s_symlink(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, new_path, proc, cb;
    rb_scan_args(argc, argv, "21&", &path, &new_path, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(path, T_STRING);
    Check_Type(new_path, T_STRING);
    SubmitRequest(symlink, rb_eio_generic_cb, StringValueCStr(path), StringValueCStr(new_path));
}

.threadsFixnum

Number of worker threads spawned

Returns:

  • (Fixnum)


384
385
386
387
388
# File 'ext/eio/eio_ext.c', line 384

static VALUE
rb_eio_s_threads(VALUE eio)
{
    return INT2NUM(eio_nthreads());
}

.truncate('/path/file') { ... } ⇒ EIO::Request

Asynchronously truncates a given file path.

Examples

EIO.truncate('/path/file', 100){ p :truncated }  =>  EIO::Request
cb = Proc.new{ p :truncated }
EIO.truncate('/path/file', 100, cb)              =>  EIO::Request

EIO.truncate('/path/file')                       =>  Fixnum
EIO.truncate('/path/file', 100)                  =>  Fixnum

Yields:

Returns:



1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# File 'ext/eio/eio_ext.c', line 1032

static VALUE
rb_eio_s_truncate(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, offset, proc, cb;
    rb_scan_args(argc, argv, "12&", &path, &offset, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(path, T_STRING);
    if (NIL_P(offset)) offset = eio_zero;
    Check_Type(offset, T_FIXNUM);
    SubmitRequest(truncate, rb_eio_generic_cb, StringValueCStr(path), FIX2INT(offset));
}

Asynchronously unlink (delete) a file and call the callback with the result code.

Examples

cb = Proc.new{ p :removed }
EIO.unlink('/path/file', cb)             =>  EIO::Request

EIO.unlink('/path/file')                 =>  Fixnum

Yields:

Returns:



871
872
873
874
875
876
877
878
879
880
# File 'ext/eio/eio_ext.c', line 871

static VALUE
rb_eio_s_unlink(int argc, VALUE *argv, VALUE eio)
{
    int ret;
    VALUE path, proc, cb;
    rb_scan_args(argc, argv, "11&", &path, &proc, &cb);
    AssertCallback(cb, NO_CB_ARGS);
    Check_Type(path, T_STRING);
    SubmitRequest(unlink, rb_eio_generic_cb, StringValueCStr(path));
}

.waitnil

Drain / flush all pending requests - BLOCKS

Returns:

  • (nil)


306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'ext/eio/eio_ext.c', line 306

static VALUE
rb_eio_s_wait(VALUE eio)
{
    int res;
    eio_req *req;
    req = eio_sync(EIO_PRI_DEFAULT, NULL, NULL);
    assert(req);
    while (eio_nreqs())
    {
        rb_eio_s_wait0();
        res = eio_poll();
        if (res > 0) rb_sys_fail("eio_poll");
    }
    return Qnil;
}

.write(fd, buf) {|b| ... } ⇒ EIO::Request

Asynchronously writes length bytes from a specified offset into a buffer.

Examples

EIO.write(fd, buf, 100){|b| p b }      =>  EIO::Request
EIO.write(fd, buf, 100, 50){|b| p b }  =>  EIO::Request
cb = Proc.new{|b| p b }
EIO.write(fd, buf, 100, 50, cb)        =>  EIO::Request

EIO.write(fd, buf)                     =>  Fixnum
EIO.write(fd, buf, 100)                =>  Fixnum
EIO.write(fd, buf, 100, 50)            =>  Fixnum

Yields:

  • (b)

Returns:



698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'ext/eio/eio_ext.c', line 698

static VALUE
rb_eio_s_write(int argc, VALUE *argv, VALUE eio)
{
    int ret, i_len, i_offset;
    VALUE fd, buf, len, offset, proc, cb, buf_len;
    rb_scan_args(argc, argv, "23&", &fd, &buf, &len, &offset, &proc, &cb);
    AssertCallback(cb, 1);
    Check_Type(fd, T_FIXNUM);
    Check_Type(buf, T_STRING);
    if (NIL_P(len)) len = INT2NUM(RSTRING_LEN(buf));
    Check_Type(len, T_FIXNUM);
    if (NIL_P(offset)) offset = eio_zero;
    Check_Type(offset, T_FIXNUM);
    i_offset = FIX2INT(offset);
    i_len = FIX2INT(len);
    if (i_offset >= RSTRING_LEN(buf)) rb_raise(rb_eArgError, "out of bounds offset");
    if ((i_offset + i_len) > RSTRING_LEN(buf)) rb_raise(rb_eArgError, "length extends beyond buffer");
    SyncRequest({
        if (offset == eio_zero){
            ret = write(FIX2INT(fd), StringValueCStr(buf), i_len);
        } else {
            ret = pwrite(FIX2INT(fd), StringValueCStr(buf), i_len, i_offset);
        }
        if (ret == -1) rb_sys_fail("write");
        return INT2NUM(ret);
    });
    AsyncRequest(write, rb_eio_write_cb, FIX2INT(fd), StringValueCStr(buf), i_len, i_offset);
}