Class: IO
- Inherits:
-
Object
- Object
- IO
- Defined in:
- lib/io-extra.rb
Constant Summary collapse
- EXTRA_VERSION =
'1.4.0'.freeze
- DIRECTIO_OFF =
Applications get the default system behavior when accessing file data.
0
- DIRECTIO_ON =
File data is not cached in the system’s memory pages.
1
- IOV_MAX =
LONG2NUM(IOV_MAX)
Class Method Summary collapse
-
.closefrom(lowfd) ⇒ Object
Close all open file descriptors (associated with the current process) that are greater than or equal to
lowfd
. -
.fdwalk(lowfd) {|fh| ... } ⇒ Object
Iterates over each open file descriptor and yields back a File object.
-
.pread(fd, maxlen, offset) ⇒ Object
Singleton version of the IO#pread method.
-
.pwrite(fd, string, offset) ⇒ Object
Singleton version of the IO#pwrite method.
-
.writev(fd, ary) ⇒ Object
IO.writev(fd, %w(hello world)).
Instance Method Summary collapse
-
#directio=(advice) ⇒ Object
Sets the advice for the current file descriptor using directio().
-
#directio? ⇒ Boolean
Returns true or false, based on whether directio has been set for the current handle.
-
#ttyname ⇒ Object
io.ttyname.
Class Method Details
.closefrom(lowfd) ⇒ Object
Close all open file descriptors (associated with the current process) that are greater than or equal to lowfd
.
This method uses your system’s builtin closefrom() function, if supported. Otherwise, it uses a manual, and (probably) less efficient approach.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'ext/io/extra.c', line 88
static VALUE io_closefrom(VALUE klass, VALUE v_low_fd){
int i, lowfd;
int maxfd = open_max();
lowfd = NUM2INT(v_low_fd);
for(i = lowfd; i < maxfd; i++) {
if(!RB_RESERVED_FD_P(i))
close(i);
}
return klass;
}
|
.fdwalk(lowfd) {|fh| ... } ⇒ Object
Iterates over each open file descriptor and yields back a File object.
Not supported on all platforms.
180 181 182 183 184 185 186 187 188 189 190 |
# File 'ext/io/extra.c', line 180
static VALUE io_fdwalk(int argc, VALUE* argv, VALUE klass){
VALUE v_low_fd, v_block;
int lowfd;
rb_scan_args(argc, argv, "1&", &v_low_fd, &v_block);
lowfd = NUM2INT(v_low_fd);
fdwalk(close_func, &lowfd);
return klass;
}
|
.pread(fd, maxlen, offset) ⇒ Object
Singleton version of the IO#pread method.
14 15 16 |
# File 'lib/io-extra.rb', line 14 def self.pread(fd, maxlen, offset) fd.pread(maxlen, offset) end |
.pwrite(fd, string, offset) ⇒ Object
Singleton version of the IO#pwrite method.
8 9 10 |
# File 'lib/io-extra.rb', line 8 def self.pwrite(fd, string, offset) fd.pwrite(string, offset) end |
.writev(fd, ary) ⇒ Object
IO.writev(fd, %w(hello world))
This method writes the contents of an array of strings to the given fd
. It can be useful to avoid generating a temporary string via Array#join when writing out large arrays of strings.
The given array should have fewer elements than the IO::IOV_MAX constant.
Returns the number of bytes written.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'ext/io/extra.c', line 335
static VALUE s_io_writev(VALUE klass, VALUE fd, VALUE ary) {
ssize_t result = 0;
ssize_t left;
struct writev_args args;
// Allow a fileno or filehandle
if(rb_respond_to(fd, rb_intern("fileno")))
fd = rb_funcall(fd, rb_intern("fileno"), 0, 0);
args.fd = NUM2INT(fd);
ARY2IOVEC(args.iov, args.iovcnt, left, ary);
for(;;) {
ssize_t w = (ssize_t)rb_thread_call_without_gvl(
(void*)nogvl_writev, &args, RUBY_UBF_IO, 0
);
if(w == -1){
if(rb_io_wait_writable(args.fd)){
continue;
}
else{
if(result > 0){
/* unlikely to hit this case, return the already written bytes,
* we'll let the next write (or close) fail instead */
break;
}
rb_sys_fail("writev");
}
}
result += w;
if(w == left){
break;
}
else{
// Partial write, this can get tricky
int i;
struct iovec *new_iov = args.iov;
left -= w;
// Skip over iovecs we've already written completely
for(i = 0; i < args.iovcnt; i++, new_iov++){
if (w == 0)
break;
// Partially written iov, modify and retry with current iovec in front
if(new_iov->iov_len > (size_t)w){
VALUE base = (VALUE)new_iov->iov_base;
new_iov->iov_len -= w;
new_iov->iov_base = (void *)(base + w);
break;
}
w -= new_iov->iov_len;
}
// Retry without the already-written iovecs
args.iovcnt -= i;
args.iov = new_iov;
}
}
return LONG2NUM(result);
}
|
Instance Method Details
#directio=(advice) ⇒ Object
Sets the advice for the current file descriptor using directio(). Valid values are IO::DIRECTIO_ON and IO::DIRECTIO_OFF. See the directio(3c) man page for more information.
All file descriptors start at DIRECTIO_OFF, unless your filesystem has been mounted using ‘forcedirectio’ (and supports that option).
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'ext/io/extra.c', line 231
static VALUE io_set_directio(VALUE self, VALUE v_advice){
int fd;
int advice = NUM2INT(v_advice);
/* Only two possible valid values */
if( (advice != DIRECTIO_OFF) && (advice != DIRECTIO_ON) )
rb_raise(rb_eStandardError, "Invalid value passed to IO#directio=");
/* Retrieve the current file descriptor in order to pass it to directio() */
fd = NUM2INT(rb_funcall(self, rb_intern("fileno"), 0, 0));
#if defined(HAVE_DIRECTIO)
if(directio(fd, advice) < 0)
rb_raise(rb_eStandardError, "The directio() call failed");
if(advice == DIRECTIO_ON)
rb_iv_set(self, "@directio", Qtrue);
else
rb_iv_set(self, "@directio", Qfalse);
#else
{
#if defined(O_DIRECT)
int flags = fcntl(fd, F_GETFL);
if(flags < 0)
rb_sys_fail("fcntl");
if(advice == DIRECTIO_OFF){
if(flags & O_DIRECT){
if(fcntl(fd, F_SETFL, flags & ~O_DIRECT) < 0)
rb_sys_fail("fcntl");
}
} else { /* DIRECTIO_ON */
if(!(flags & O_DIRECT)){
if(fcntl(fd, F_SETFL, flags | O_DIRECT) < 0)
rb_sys_fail("fcntl");
}
}
#elif defined(F_NOCACHE)
if(advice == DIRECTIO_OFF){
if(fcntl(fd, F_NOCACHE, 0) < 0)
rb_sys_fail("fcntl");
rb_iv_set(self, "@directio", Qfalse);
} else { /* DIRECTIO_ON*/
if(fcntl(fd, F_NOCACHE, 1) < 0)
rb_sys_fail("fcntl");
rb_iv_set(self, "@directio", Qtrue);
}
#endif
}
#endif
return self;
}
|
#directio? ⇒ Boolean
Returns true or false, based on whether directio has been set for the current handle. The default is false.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'ext/io/extra.c', line 201
static VALUE io_get_directio(VALUE self){
#if defined(HAVE_DIRECTIO) || defined(F_NOCACHE)
VALUE v_advice = rb_iv_get(self, "@directio");
if(NIL_P(v_advice))
v_advice = Qfalse;
return v_advice;
#elif defined(O_DIRECT)
int fd = NUM2INT(rb_funcall(self, rb_intern("fileno"), 0, 0));
int flags = fcntl(fd, F_GETFL);
if(flags < 0)
rb_sys_fail("fcntl");
return (flags & O_DIRECT) ? Qtrue : Qfalse;
#endif /* O_DIRECT */
}
|
#ttyname ⇒ Object
io.ttyname
Returns the ttyname associated with the IO object, or nil if the IO object isn’t associated with a tty.
Example:
STDOUT.ttyname # => '/dev/ttyp1'
416 417 418 419 420 421 422 423 424 425 |
# File 'ext/io/extra.c', line 416
static VALUE io_get_ttyname(VALUE self){
VALUE v_return = Qnil;
int fd = NUM2INT(rb_funcall(self, rb_intern("fileno"), 0, 0));
if(isatty(fd))
v_return = rb_str_new2(ttyname(fd));
return v_return;
}
|