Class: JIO::File
- Inherits:
-
Object
- Object
- JIO::File
- Defined in:
- lib/jio/file.rb,
ext/jio/file.c
Instance Method Summary collapse
-
#autosync(5, 4000) ⇒ Boolean
Syncs to disk every X seconds, or every Y bytes written.
-
#clearerr ⇒ nil
Resets the error flag for this file, if any.
-
#close ⇒ Boolean
After a call to this method, the memory allocated for the open file will be freed.
-
#eof? ⇒ Boolean
Check for end-of-file.
-
#error? ⇒ Boolean
Determines if an error condition has occurred.
-
#fileno ⇒ Fixnum
Return the file descriptor number for the file.
-
#lseek(10, JIO: :SEEK_SET) ⇒ Fixnum
Reposition the file pointer to the given offset, according to the whence directive.
-
#move_journal("/path") ⇒ Boolean
Changes the location of the journal direction.
- #orig_transaction ⇒ Object
-
#pread(10, 10) ⇒ String
Reads from a libjio file handle at a given offset.
-
#pwrite("buffer", 10) ⇒ Fixnum
Writes to a libjio file handle at a given offset.
-
#read(10) ⇒ String
Reads from a libjio file handle.
-
#rewind ⇒ nil
Adjusts the file so that the next I/O operation will take place at the beginning of the file.
-
#stop_autosync ⇒ Boolean
Stops a previously started autosync thread.
-
#sync ⇒ Boolean
Sync a file to disk.
-
#tell ⇒ Fixnum
Determine the current file offset.
-
#transaction(JIO: :J_LINGER) ⇒ JIO::Transaction
Creates a new low level transaction from a libjio file reference.
-
#truncate(10) ⇒ Fixnum
Truncate the file to the given size.
-
#write("buffer") ⇒ Fixnum
Writes to a libjio file handle.
Instance Method Details
#autosync(5, 4000) ⇒ Boolean
Syncs to disk every X seconds, or every Y bytes written. Only one autosync thread per open file is allowed. Only makes sense with lingering transactions.
Examples
file.autosync(5, 4000) => boolean
122 123 124 125 126 127 128 129 130 |
# File 'ext/jio/file.c', line 122
static VALUE rb_jio_file_autosync(VALUE obj, VALUE max_seconds, VALUE max_bytes)
{
JioGetFile(obj);
Check_Type(max_seconds, T_FIXNUM);
Check_Type(max_bytes, T_FIXNUM);
TRAP_BEG;
return (jfs_autosync_start(file->fs, (time_t)FIX2LONG(max_seconds), (size_t)FIX2LONG(max_bytes)) == 0) ? Qtrue : Qfalse;
TRAP_END;
}
|
#clearerr ⇒ nil
Resets the error flag for this file, if any.
Examples
file.clearerr => nil
424 425 426 427 428 429 430 431 |
# File 'ext/jio/file.c', line 424
static VALUE rb_jio_file_clearerr(VALUE obj)
{
JioGetFile(obj);
TRAP_BEG;
jclearerr(file->fs);
TRAP_END;
return Qnil;
}
|
#close ⇒ Boolean
After a call to this method, the memory allocated for the open file will be freed. If there was an autosync thread started for this file, it will be stopped.
Examples
file.close => boolean
80 81 82 83 84 85 86 87 88 |
# File 'ext/jio/file.c', line 80
static VALUE rb_jio_file_close(VALUE obj)
{
JioGetFile(obj);
TRAP_BEG;
if (jclose(file->fs) != 0) return Qfalse;
TRAP_END;
file->flags |= JIO_FILE_CLOSED;
return Qtrue;
}
|
#eof? ⇒ Boolean
Check for end-of-file.
Examples
file.eof? => boolean
383 384 385 386 387 388 389 |
# File 'ext/jio/file.c', line 383
static VALUE rb_jio_file_eof_p(VALUE obj)
{
JioGetFile(obj);
TRAP_BEG;
return (jfeof(file->fs) != 0) ? Qtrue : Qfalse;
TRAP_END;
}
|
#error? ⇒ Boolean
Determines if an error condition has occurred.
Examples
file.error? => boolean
402 403 404 405 406 407 408 409 410 411 |
# File 'ext/jio/file.c', line 402
static VALUE rb_jio_file_error_p(VALUE obj)
{
int res;
JioGetFile(obj);
TRAP_BEG;
res = jferror(file->fs);
TRAP_END;
if (res == 0) return Qfalse;
return INT2NUM(res);
}
|
#fileno ⇒ Fixnum
Return the file descriptor number for the file.
Examples
file.fileno => Fixnum
319 320 321 322 323 324 325 326 327 328 |
# File 'ext/jio/file.c', line 319
static VALUE rb_jio_file_fileno(VALUE obj)
{
int fd;
JioGetFile(obj);
TRAP_BEG;
fd = jfileno(file->fs);
TRAP_END;
if (fd == -1) rb_sys_fail("jfileno");
return INT2NUM(fd);
}
|
#lseek(10, JIO: :SEEK_SET) ⇒ Fixnum
272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'ext/jio/file.c', line 272
static VALUE rb_jio_file_lseek(VALUE obj, VALUE offset, VALUE whence)
{
off_t off;
JioGetFile(obj);
AssertOffset(offset);
Check_Type(whence, T_FIXNUM);
TRAP_BEG;
off = jlseek(file->fs, (off_t)NUM2OFFT(offset), FIX2INT(whence));
TRAP_END;
if (off == -1) rb_sys_fail("jlseek");
return OFFT2NUM(off);
}
|
#move_journal("/path") ⇒ Boolean
Changes the location of the journal direction. The file cannot be in use at this time.
Examples
file.move_journal("/path") => boolean
101 102 103 104 105 106 107 108 |
# File 'ext/jio/file.c', line 101
static VALUE rb_jio_file_move_journal(VALUE obj, VALUE path)
{
JioGetFile(obj);
Check_Type(path, T_STRING);
TRAP_BEG;
return (jmove_journal(file->fs, RSTRING_PTR(path)) == 0) ? Qtrue : Qfalse;
TRAP_END;
}
|
#orig_transaction ⇒ Object
5 |
# File 'lib/jio/file.rb', line 5 alias orig_transaction transaction |
#pread(10, 10) ⇒ String
Reads from a libjio file handle at a given offset. Works just like UNIX pread(2)
Examples
file.pread(10, 10) => String
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'ext/jio/file.c', line 193
static VALUE rb_jio_file_pread(VALUE obj, VALUE length, VALUE offset)
{
ssize_t bytes;
char *buf = NULL;
ssize_t len;
JioGetFile(obj);
AssertLength(length);
AssertOffset(offset);
len = (ssize_t)FIX2LONG(length);
buf = xmalloc(len + 1);
if (buf == NULL) rb_memerror();
TRAP_BEG;
bytes = jpread(file->fs, buf, len, (off_t)NUM2OFFT(offset));
TRAP_END;
if (bytes == -1) {
xfree(buf);
rb_sys_fail("jpread");
}
return JioEncode(rb_str_new(buf, (long)len));
}
|
#pwrite("buffer", 10) ⇒ Fixnum
Writes to a libjio file handle at a given offset. Works just like UNIX pwrite(2)
Examples
file.pwrite("buffer", 10) => Fixnum
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'ext/jio/file.c', line 248
static VALUE rb_jio_file_pwrite(VALUE obj, VALUE buf, VALUE offset)
{
ssize_t bytes;
JioGetFile(obj);
Check_Type(buf, T_STRING);
AssertOffset(offset);
TRAP_BEG;
bytes = jpwrite(file->fs, RSTRING_PTR(buf), (size_t)RSTRING_LEN(buf), (off_t)NUM2OFFT(offset));
TRAP_END;
if (bytes == -1) rb_sys_fail("jpwrite");
return INT2NUM(bytes);
}
|
#read(10) ⇒ String
Reads from a libjio file handle. Works just like UNIX read(2)
Examples
file.read(10) => String
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'ext/jio/file.c', line 162
static VALUE rb_jio_file_read(VALUE obj, VALUE length)
{
ssize_t bytes;
char *buf = NULL;
ssize_t len;
JioGetFile(obj);
AssertLength(length);
len = (ssize_t)FIX2LONG(length);
buf = xmalloc(len + 1);
if (buf == NULL) rb_memerror();
TRAP_BEG;
bytes = jread(file->fs, buf, len);
TRAP_END;
if (bytes == -1) {
xfree(buf);
rb_sys_fail("jread");
}
return JioEncode(rb_str_new(buf, (long)len));
}
|
#rewind ⇒ nil
Adjusts the file so that the next I/O operation will take place at the beginning of the file.
Examples
file.rewind => nil
341 342 343 344 345 346 347 348 |
# File 'ext/jio/file.c', line 341
static VALUE rb_jio_file_rewind(VALUE obj)
{
JioGetFile(obj);
TRAP_BEG;
jrewind(file->fs);
TRAP_END;
return Qnil;
}
|
#stop_autosync ⇒ Boolean
Stops a previously started autosync thread.
Examples
file.stop_autosync => boolean
143 144 145 146 147 148 149 |
# File 'ext/jio/file.c', line 143
static VALUE rb_jio_file_stop_autosync(VALUE obj)
{
JioGetFile(obj);
TRAP_BEG;
return (jfs_autosync_stop(file->fs) == 0) ? Qtrue : Qfalse;
TRAP_END;
}
|
#sync ⇒ Boolean
Sync a file to disk. Makes sense only when using lingering transactions.
Examples
file.sync => boolean
60 61 62 63 64 65 66 |
# File 'ext/jio/file.c', line 60
static VALUE rb_jio_file_sync(VALUE obj)
{
JioGetFile(obj);
TRAP_BEG;
return (jsync(file->fs) == 0) ? Qtrue : Qfalse;
TRAP_END;
}
|
#tell ⇒ Fixnum
Determine the current file offset.
Examples
file.tell => Fixnum
361 362 363 364 365 366 367 368 369 370 |
# File 'ext/jio/file.c', line 361
static VALUE rb_jio_file_tell(VALUE obj)
{
long size;
JioGetFile(obj);
TRAP_BEG;
size = jftell(file->fs);
TRAP_END;
if (size == -1) rb_sys_fail("jftell");
return INT2NUM(size);
}
|
#transaction(JIO: :J_LINGER) ⇒ JIO::Transaction
Creates a new low level transaction from a libjio file reference.
Examples
file.transaction(JIO::J_LINGER) => JIO::Transaction
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'ext/jio/file.c', line 444 def transaction(flags) if block_given? begin trans = orig_transaction(flags) yield trans rescue trans.rollback error = true raise ensure trans.commit if !error && !trans.committed? trans.release end else orig_transaction(flags) end end |
#truncate(10) ⇒ Fixnum
Truncate the file to the given size.
Examples
file.truncate(10) => Fixnum
296 297 298 299 300 301 302 303 304 305 306 |
# File 'ext/jio/file.c', line 296
static VALUE rb_jio_file_truncate(VALUE obj, VALUE length)
{
off_t len;
JioGetFile(obj);
AssertLength(length);
TRAP_BEG;
len = jtruncate(file->fs, (off_t)NUM2OFFT(length));
TRAP_END;
if (len == -1) rb_sys_fail("jtruncate");
return OFFT2NUM(len);
}
|
#write("buffer") ⇒ Fixnum
Writes to a libjio file handle. Works just like UNIX write(2)
Examples
file.write("buffer") => Fixnum
225 226 227 228 229 230 231 232 233 234 235 |
# File 'ext/jio/file.c', line 225
static VALUE rb_jio_file_write(VALUE obj, VALUE buf)
{
ssize_t bytes;
JioGetFile(obj);
Check_Type(buf, T_STRING);
TRAP_BEG;
bytes = jwrite(file->fs, RSTRING_PTR(buf), (size_t)RSTRING_LEN(buf));
TRAP_END;
if (bytes == -1) rb_sys_fail("jwrite");
return INT2NUM(bytes);
}
|