qfs 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +15 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +201 -0
- data/README.md +56 -0
- data/Rakefile +19 -0
- data/ext/qfs/attr.c +54 -0
- data/ext/qfs/attr.h +10 -0
- data/ext/qfs/extconf.rb +33 -0
- data/ext/qfs/file.c +133 -0
- data/ext/qfs/file.h +19 -0
- data/ext/qfs/qfs.c +367 -0
- data/ext/qfs/qfs.h +20 -0
- data/ext/qfs/qfs_ext.version +8 -0
- data/ext/qfs/util.h +29 -0
- data/lib/qfs.rb +390 -0
- data/lib/qfs/version.rb +3 -0
- data/qfs.gemspec +29 -0
- data/test/qfs_test.rb +393 -0
- data/test/test_helper.rb +2 -0
- metadata +149 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright 2016 Quantcast, Inc
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# ruby-qfs
|
2
|
+
|
3
|
+
Ruby bindings for [QFS](https://github.com/quantcast/qfs).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'qfs'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install qfs
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
The entrypoint to QFS with these bindings is the `Qfs::Client` object. You can view the entire API in the documentation (TODO update this when open sourced) and see examples in "test/qfs_test.rb".
|
24
|
+
|
25
|
+
## Testing
|
26
|
+
|
27
|
+
You can run the tests on an existing instance of QFS. By default, an local instance running on port 10000 is assumed, but you can specify a different location using environment variables.
|
28
|
+
|
29
|
+
```shell
|
30
|
+
rake test
|
31
|
+
```
|
32
|
+
|
33
|
+
By default, a stock QFS instance will likely have restricted permissions. You may have to connect to QFS as root and manually chown/chmod the root to something that the user running the tests can access.
|
34
|
+
|
35
|
+
#### Environment Variables for Tests
|
36
|
+
* `QFS_TEST_PATH`: The directory in QFS to create and do all test-related operations in.
|
37
|
+
* `QFS_TEST_HOST`: The host running QFS.
|
38
|
+
* `QFS_TEST_PORT`: The port that QFS is running on.
|
39
|
+
|
40
|
+
You can also enable debugging output by setting the environment variable `RUBY_QFS_TRACE`.
|
41
|
+
|
42
|
+
```shell
|
43
|
+
export RUBY_QFS_TRACE=1
|
44
|
+
```
|
45
|
+
|
46
|
+
## Caveats
|
47
|
+
|
48
|
+
### `stat` can return stale data after modifications
|
49
|
+
|
50
|
+
The behavior of the C API results in a `stat` call using stale data if modifications were made to a file since the connection was opened. This can be avoided by opening and immediately closing the file that is being `stat`'ed. You can enable this behavior by setting the `refresh` option to true:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
client.stat("/path/to/file", refresh: true)
|
54
|
+
```
|
55
|
+
|
56
|
+
Be aware that this may come with a performance penalty, so it may be better to use this option only when necessary.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/extensiontask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
spec = Gem::Specification.load('qfs.gemspec')
|
6
|
+
|
7
|
+
Rake::ExtensionTask.new do |ext|
|
8
|
+
ext.name = 'qfs_ext'
|
9
|
+
ext.gem_spec = spec
|
10
|
+
ext.ext_dir = 'ext/qfs'
|
11
|
+
end
|
12
|
+
|
13
|
+
desc 'Run the test suite against a local instance of QFS'
|
14
|
+
Rake::TestTask.new test: :compile do |t|
|
15
|
+
t.pattern = 'test/*_test.rb'
|
16
|
+
t.libs << 'test'
|
17
|
+
end
|
18
|
+
|
19
|
+
task default: :test
|
data/ext/qfs/attr.c
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
#include "attr.h"
|
2
|
+
|
3
|
+
#include <ruby.h>
|
4
|
+
#include "qfs.h"
|
5
|
+
#include "util.h"
|
6
|
+
|
7
|
+
VALUE cQfsAttr;
|
8
|
+
|
9
|
+
#define QFS_ATTR_GET(f, t) static VALUE qfs_attr_##f(VALUE self) { \
|
10
|
+
struct qfs_attr *attr; \
|
11
|
+
Data_Get_Struct(self, struct qfs_attr, attr); \
|
12
|
+
return t(attr->f); \
|
13
|
+
}
|
14
|
+
|
15
|
+
QFS_ATTR_GET(filename, rb_str_new2)
|
16
|
+
QFS_ATTR_GET(id, INT2FIX)
|
17
|
+
QFS_ATTR_GET(mode, INT2FIX)
|
18
|
+
QFS_ATTR_GET(uid, INT2FIX)
|
19
|
+
QFS_ATTR_GET(gid, INT2FIX)
|
20
|
+
QFS_ATTR_GET(mtime, NTIME)
|
21
|
+
QFS_ATTR_GET(ctime, NTIME)
|
22
|
+
QFS_ATTR_GET(directory, INT2BOOL)
|
23
|
+
QFS_ATTR_GET(size, INT2FIX)
|
24
|
+
QFS_ATTR_GET(chunks, INT2FIX)
|
25
|
+
QFS_ATTR_GET(directories, INT2FIX)
|
26
|
+
QFS_ATTR_GET(replicas, INT2FIX)
|
27
|
+
QFS_ATTR_GET(stripes, INT2FIX)
|
28
|
+
QFS_ATTR_GET(recovery_stripes, INT2FIX)
|
29
|
+
QFS_ATTR_GET(striper_type, INT2FIX)
|
30
|
+
QFS_ATTR_GET(stripe_size, INT2FIX)
|
31
|
+
QFS_ATTR_GET(min_stier, INT2FIX)
|
32
|
+
QFS_ATTR_GET(max_stier, INT2FIX)
|
33
|
+
|
34
|
+
void init_qfs_ext_attr() {
|
35
|
+
cQfsAttr = rb_define_class_under(mQfs, "Attr", rb_cObject);
|
36
|
+
rb_define_method(cQfsAttr, "filename", qfs_attr_filename, 0);
|
37
|
+
rb_define_method(cQfsAttr, "id", qfs_attr_id, 0);
|
38
|
+
rb_define_method(cQfsAttr, "mode", qfs_attr_mode, 0);
|
39
|
+
rb_define_method(cQfsAttr, "uid", qfs_attr_uid, 0);
|
40
|
+
rb_define_method(cQfsAttr, "gid", qfs_attr_gid, 0);
|
41
|
+
rb_define_method(cQfsAttr, "mtime", qfs_attr_mtime, 0);
|
42
|
+
rb_define_method(cQfsAttr, "ctime", qfs_attr_ctime, 0);
|
43
|
+
rb_define_method(cQfsAttr, "directory?", qfs_attr_directory, 0);
|
44
|
+
rb_define_method(cQfsAttr, "size", qfs_attr_size, 0);
|
45
|
+
rb_define_method(cQfsAttr, "chunks", qfs_attr_chunks, 0);
|
46
|
+
rb_define_method(cQfsAttr, "directories", qfs_attr_directories, 0);
|
47
|
+
rb_define_method(cQfsAttr, "replicas", qfs_attr_replicas, 0);
|
48
|
+
rb_define_method(cQfsAttr, "stripes", qfs_attr_stripes, 0);
|
49
|
+
rb_define_method(cQfsAttr, "recovery_stripes", qfs_attr_recovery_stripes, 0);
|
50
|
+
rb_define_method(cQfsAttr, "striper_type", qfs_attr_striper_type, 0);
|
51
|
+
rb_define_method(cQfsAttr, "strip_size", qfs_attr_stripe_size, 0);
|
52
|
+
rb_define_method(cQfsAttr, "min_stier", qfs_attr_min_stier, 0);
|
53
|
+
rb_define_method(cQfsAttr, "max_stier", qfs_attr_max_stier, 0);
|
54
|
+
}
|
data/ext/qfs/attr.h
ADDED
data/ext/qfs/extconf.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
require 'rbconfig'
|
3
|
+
|
4
|
+
INCLUDE_DIRS = [
|
5
|
+
RbConfig::CONFIG['includedir'],
|
6
|
+
'/usr/include',
|
7
|
+
'/usr/local/include',
|
8
|
+
]
|
9
|
+
|
10
|
+
LIB_DIRS = [
|
11
|
+
RbConfig::CONFIG['libdir'],
|
12
|
+
'/usr/lib',
|
13
|
+
'/usr/local/lib',
|
14
|
+
]
|
15
|
+
|
16
|
+
dir_config 'qfs', INCLUDE_DIRS, LIB_DIRS
|
17
|
+
|
18
|
+
with_config('qfs-local-libs', '').split(':').each do |lib|
|
19
|
+
$LOCAL_LIBS << " #{lib} "
|
20
|
+
end
|
21
|
+
|
22
|
+
if with_config('version-script', '0') == '1'
|
23
|
+
$LDFLAGS << " -Wl,--version-script=#{File.expand_path('../qfs_ext.version', __FILE__)} "
|
24
|
+
end
|
25
|
+
|
26
|
+
abort '"kfs/c/qfs.h" is required' unless find_header 'kfs/c/qfs.h'
|
27
|
+
abort 'libqfsc is required' unless find_library 'qfsc', 'qfs_open'
|
28
|
+
|
29
|
+
$CFLAGS << ' -std=c99 -Wall -Wextra '
|
30
|
+
|
31
|
+
$warnflags.gsub!('-Wdeclaration-after-statement', '') if $warnflags
|
32
|
+
|
33
|
+
create_makefile 'qfs_ext'
|
data/ext/qfs/file.c
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
#include "file.h"
|
2
|
+
|
3
|
+
#include <ruby.h>
|
4
|
+
#include "attr.h"
|
5
|
+
#include "qfs.h"
|
6
|
+
#include "util.h"
|
7
|
+
|
8
|
+
VALUE cQfsFile;
|
9
|
+
|
10
|
+
/* This is a handle to a fd that can perform IO. */
|
11
|
+
static VALUE qfs_file_read(VALUE self, VALUE len) {
|
12
|
+
struct qfs_file *file;
|
13
|
+
struct qfs_client *client;
|
14
|
+
Check_Type(len, T_FIXNUM);
|
15
|
+
Data_Get_Struct(self, struct qfs_file, file);
|
16
|
+
Data_Get_Struct(file->client, struct qfs_client, client);
|
17
|
+
size_t n = (size_t)NUM2INT(len);
|
18
|
+
VALUE s = rb_str_buf_new((long)n);
|
19
|
+
ssize_t n_read = qfs_read(client->qfs, file->fd, RSTRING_PTR(s), n);
|
20
|
+
QFS_CHECK_ERR(n_read);
|
21
|
+
rb_str_set_len(s, n_read);
|
22
|
+
return s;
|
23
|
+
}
|
24
|
+
|
25
|
+
static VALUE qfs_file_tell(VALUE self) {
|
26
|
+
struct qfs_file *file;
|
27
|
+
struct qfs_client *client;
|
28
|
+
Data_Get_Struct(self, struct qfs_file, file);
|
29
|
+
Data_Get_Struct(file->client, struct qfs_client, client);
|
30
|
+
off_t offset = qfs_tell(client->qfs, file->fd);
|
31
|
+
QFS_CHECK_ERR(offset);
|
32
|
+
return INT2FIX(offset);
|
33
|
+
}
|
34
|
+
|
35
|
+
/* Returns a Qfs::Attr object for the file. */
|
36
|
+
static VALUE qfs_file_stat(VALUE self) {
|
37
|
+
struct qfs_file *file;
|
38
|
+
struct qfs_client *client;
|
39
|
+
Data_Get_Struct(self, struct qfs_file, file);
|
40
|
+
Data_Get_Struct(file->client, struct qfs_client, client);
|
41
|
+
struct qfs_attr *attr = ALLOC(struct qfs_attr);
|
42
|
+
int res = qfs_stat_fd(client->qfs, file->fd, attr);
|
43
|
+
QFS_CHECK_ERR(res);
|
44
|
+
return Data_Wrap_Struct(cQfsAttr, NULL, free, attr);
|
45
|
+
}
|
46
|
+
|
47
|
+
static VALUE qfs_file_write(VALUE self, VALUE str) {
|
48
|
+
struct qfs_file *file;
|
49
|
+
struct qfs_client *client;
|
50
|
+
Check_Type(str, T_STRING);
|
51
|
+
Data_Get_Struct(self, struct qfs_file, file);
|
52
|
+
Data_Get_Struct(file->client, struct qfs_client, client);
|
53
|
+
ssize_t n = qfs_write(client->qfs, file->fd, RSTRING_PTR(str),
|
54
|
+
(size_t)RSTRING_LEN(str));
|
55
|
+
QFS_CHECK_ERR(n);
|
56
|
+
if (n < RSTRING_LEN(str)) {
|
57
|
+
WARN("partial write");
|
58
|
+
}
|
59
|
+
return INT2FIX(n);
|
60
|
+
}
|
61
|
+
|
62
|
+
static VALUE qfs_file_close(VALUE self) {
|
63
|
+
TRACE;
|
64
|
+
struct qfs_file *file;
|
65
|
+
struct qfs_client *client;
|
66
|
+
Data_Get_Struct(self, struct qfs_file, file);
|
67
|
+
Data_Get_Struct(file->client, struct qfs_client, client);
|
68
|
+
int err = qfs_close(client->qfs, file->fd);
|
69
|
+
QFS_CHECK_ERR(err);
|
70
|
+
file->fd = QFS_NIL_FD;
|
71
|
+
file->client = Qnil;
|
72
|
+
TRACE_R;
|
73
|
+
return Qnil;
|
74
|
+
}
|
75
|
+
|
76
|
+
static VALUE qfs_file_chmod(VALUE self, VALUE mode) {
|
77
|
+
struct qfs_file *file;
|
78
|
+
struct qfs_client *client;
|
79
|
+
Check_Type(mode, T_FIXNUM);
|
80
|
+
mode_t imode = (mode_t)FIX2INT(mode);
|
81
|
+
Data_Get_Struct(self, struct qfs_file, file);
|
82
|
+
Data_Get_Struct(file->client, struct qfs_client, client);
|
83
|
+
int res = qfs_chmod_fd(client->qfs, file->fd, imode);
|
84
|
+
QFS_CHECK_ERR(res);
|
85
|
+
return RES2BOOL(res);
|
86
|
+
}
|
87
|
+
|
88
|
+
static VALUE qfs_file_seek(VALUE self, VALUE offset, VALUE whence) {
|
89
|
+
struct qfs_file *file;
|
90
|
+
struct qfs_client *client;
|
91
|
+
Check_Type(offset, T_FIXNUM);
|
92
|
+
Check_Type(whence, T_FIXNUM);
|
93
|
+
off_t toffset = (off_t)FIX2INT(offset);
|
94
|
+
int iwhence = FIX2INT(whence);
|
95
|
+
Data_Get_Struct(self, struct qfs_file, file);
|
96
|
+
Data_Get_Struct(file->client, struct qfs_client, client);
|
97
|
+
off_t res = qfs_seek(client->qfs, file->fd, toffset, iwhence);
|
98
|
+
QFS_CHECK_ERR(res);
|
99
|
+
return INT2FIX((int)res);
|
100
|
+
}
|
101
|
+
|
102
|
+
void qfs_file_deallocate(void *filevp) {
|
103
|
+
TRACE;
|
104
|
+
/* the client might be deallocated already, so don't try to close ourselves */
|
105
|
+
free(filevp);
|
106
|
+
TRACE_R;
|
107
|
+
}
|
108
|
+
|
109
|
+
void qfs_file_mark(void *filevp) {
|
110
|
+
struct qfs_file *file = filevp;
|
111
|
+
if (file->client) {
|
112
|
+
rb_gc_mark(file->client);
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
VALUE qfs_file_allocate(VALUE klass) {
|
117
|
+
struct qfs_file *file = malloc(sizeof(struct qfs_file));
|
118
|
+
file->client = Qnil;
|
119
|
+
file->fd = QFS_NIL_FD;
|
120
|
+
return Data_Wrap_Struct(klass, qfs_file_mark, qfs_file_deallocate, file);
|
121
|
+
}
|
122
|
+
|
123
|
+
void init_qfs_ext_file() {
|
124
|
+
cQfsFile = rb_define_class_under(mQfs, "File", rb_cObject);
|
125
|
+
rb_define_alloc_func(cQfsFile, qfs_file_allocate);
|
126
|
+
rb_define_method(cQfsFile, "read_len", qfs_file_read, 1);
|
127
|
+
rb_define_method(cQfsFile, "tell", qfs_file_tell, 0);
|
128
|
+
rb_define_method(cQfsFile, "stat", qfs_file_stat, 0);
|
129
|
+
rb_define_method(cQfsFile, "write", qfs_file_write, 1);
|
130
|
+
rb_define_method(cQfsFile, "close", qfs_file_close, 0);
|
131
|
+
rb_define_method(cQfsFile, "chmod", qfs_file_chmod, 1);
|
132
|
+
rb_define_private_method(cQfsFile, "seek_internal", qfs_file_seek, 2);
|
133
|
+
}
|