Class: Qfs::File

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

Overview

A File on QFS.

Instance Method Summary collapse

Instance Method Details

#chmod(mode) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
# File 'ext/qfs/file.c', line 76

static VALUE qfs_file_chmod(VALUE self, VALUE mode) {
	struct qfs_file *file;
	struct qfs_client *client;
	Check_Type(mode, T_FIXNUM);
	mode_t imode = (mode_t)FIX2INT(mode);
	Data_Get_Struct(self, struct qfs_file, file);
	Data_Get_Struct(file->client, struct qfs_client, client);
	int res = qfs_chmod_fd(client->qfs, file->fd, imode);
	QFS_CHECK_ERR(res);
	return RES2BOOL(res);
}

#closeObject


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/qfs/file.c', line 62

static VALUE qfs_file_close(VALUE self) {
	TRACE;
	struct qfs_file *file;
	struct qfs_client *client;
	Data_Get_Struct(self, struct qfs_file, file);
	Data_Get_Struct(file->client, struct qfs_client, client);
	int err = qfs_close(client->qfs, file->fd);
	QFS_CHECK_ERR(err);
	file->fd = QFS_NIL_FD;
	file->client = Qnil;
	TRACE_R;
	return Qnil;
}

#read(len = nil) ⇒ String

Read from a file. Don’t specify a length to read the entire file.

read the entire file.

Parameters:

  • len (Int) (defaults to: nil)

    the number of bytes to read. Omit or set to nil to

Returns:

  • (String)

    the data read from the file.


322
323
324
325
# File 'lib/qfs.rb', line 322

def read(len = nil)
  len ||= stat.size
  read_len(len)
end

#read_len(len) ⇒ Object

This is a handle to a fd that can perform IO.


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'ext/qfs/file.c', line 11

static VALUE qfs_file_read(VALUE self, VALUE len) {
	struct qfs_file *file;
	struct qfs_client *client;
	Check_Type(len, T_FIXNUM);
	Data_Get_Struct(self, struct qfs_file, file);
	Data_Get_Struct(file->client, struct qfs_client, client);
	size_t n = (size_t)NUM2INT(len);
	VALUE s = rb_str_buf_new((long)n);
	ssize_t n_read = qfs_read(client->qfs, file->fd, RSTRING_PTR(s), n);
	QFS_CHECK_ERR(n_read);
	rb_str_set_len(s, n_read);
	return s;
}

#seek(offset, whence = IO::SEEK_CUR) ⇒ Int

Seek to the specified position in the file. The default ‘whence’ value is SEEK_CUR, so the offset will be applied to the current file position.

IO::SEEK_END, IO::SEEK_SET. Defaults to IO::SEEK_CUR

Parameters:

  • offset (Int)

    The offset to seek to

  • whence (Int) (defaults to: IO::SEEK_CUR)

    One of the following constants: IO::SEEK_CUR,

Returns:

  • (Int)

    The current file position


335
336
337
# File 'lib/qfs.rb', line 335

def seek(offset, whence=IO::SEEK_CUR)
  seek_internal(offset, whence)
end

#statObject

Returns a Qfs::Attr object for the file.


36
37
38
39
40
41
42
43
44
45
# File 'ext/qfs/file.c', line 36

static VALUE qfs_file_stat(VALUE self) {
	struct qfs_file *file;
	struct qfs_client *client;
	Data_Get_Struct(self, struct qfs_file, file);
	Data_Get_Struct(file->client, struct qfs_client, client);
	struct qfs_attr *attr = ALLOC(struct qfs_attr);
	int res = qfs_stat_fd(client->qfs, file->fd, attr);
	QFS_CHECK_ERR(res);
	return Data_Wrap_Struct(cQfsAttr, NULL, free, attr);
}

#tellObject


25
26
27
28
29
30
31
32
33
# File 'ext/qfs/file.c', line 25

static VALUE qfs_file_tell(VALUE self) {
	struct qfs_file *file;
	struct qfs_client *client;
	Data_Get_Struct(self, struct qfs_file, file);
	Data_Get_Struct(file->client, struct qfs_client, client);
	off_t offset = qfs_tell(client->qfs, file->fd);
	QFS_CHECK_ERR(offset);
	return INT2FIX(offset);
}

#write(str) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'ext/qfs/file.c', line 47

static VALUE qfs_file_write(VALUE self, VALUE str) {
	struct qfs_file *file;
	struct qfs_client *client;
	Check_Type(str, T_STRING);
	Data_Get_Struct(self, struct qfs_file, file);
	Data_Get_Struct(file->client, struct qfs_client, client);
	ssize_t n = qfs_write(client->qfs, file->fd, RSTRING_PTR(str),
			(size_t)RSTRING_LEN(str));
	QFS_CHECK_ERR(n);
	if (n < RSTRING_LEN(str)) {
		WARN("partial write");
	}
	return INT2FIX(n);
}