Class: JIO::File

Inherits:
Object
  • Object
show all
Defined in:
lib/jio/file.rb,
ext/jio/file.c

Instance Method Summary collapse

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

Returns:

  • (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;
}

#clearerrnil

Resets the error flag for this file, if any.

Examples

file.clearerr    =>  nil

Returns:

  • (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;
}

#closeBoolean

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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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);
}

#filenoFixnum

Return the file descriptor number for the file.

Examples

file.fileno    =>  Fixnum

Returns:

  • (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

Reposition the file pointer to the given offset, according to the whence directive.

Examples

file.lseek(10, JIO::SEEK_SET)    =>  Fixnum

Returns:

  • (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

Returns:

  • (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_transactionObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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));
}

#rewindnil

Adjusts the file so that the next I/O operation will take place at the beginning of the file.

Examples

file.rewind    =>  nil

Returns:

  • (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_autosyncBoolean

Stops a previously started autosync thread.

Examples

file.stop_autosync    =>  boolean

Returns:

  • (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;
}

#syncBoolean

Sync a file to disk. Makes sense only when using lingering transactions.

Examples

file.sync    =>  boolean

Returns:

  • (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;
}

#tellFixnum

Determine the current file offset.

Examples

file.tell    =>  Fixnum

Returns:

  • (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

Returns:



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

Returns:

  • (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

Returns:

  • (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);
}