io-extra 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +6 -0
- data/MANIFEST +1 -1
- data/README +7 -7
- data/doc/io_extra.txt +22 -5
- data/ext/extconf.rb +3 -3
- data/ext/io/extra.c +67 -3
- data/test/{tc_ioextra.rb → test_io_extra.rb} +16 -3
- metadata +44 -37
data/CHANGES
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 1.1.0 - 28-Aug-2008
|
2
|
+
* Added IO.pread and IO.pwrite methods for those platforms that
|
3
|
+
support them.
|
4
|
+
* Fixed a warning that could occur in the IO#directio method.
|
5
|
+
* Some updates the README, CHANGES, and test files.
|
6
|
+
|
1
7
|
== 1.0.2 - 16-Jul-2007
|
2
8
|
* Added a Rakefile with tasks for testing and installation.
|
3
9
|
* Some doc updates and internal reorganization, but no significant code
|
data/MANIFEST
CHANGED
data/README
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= Description
|
2
|
-
The io-extra
|
3
|
-
handy.
|
2
|
+
The io-extra library provides a few extra IO methods that you may find
|
3
|
+
handy. They are IO.closefrom, IO.fdwalk, IO#directio? and IO#directio=.
|
4
4
|
|
5
5
|
Not supported on MS Windows or OS X.
|
6
6
|
|
@@ -18,14 +18,14 @@ You might be wondering what the difference is between my implementation of
|
|
18
18
|
IO.closefrom and a pure Ruby version that looks something like this:
|
19
19
|
|
20
20
|
def IO.closefrom(n)
|
21
|
-
0.upto
|
21
|
+
0.upto(n) do |fd|
|
22
22
|
IO.for_fd(fd).close
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
The primary difference is that this walks all file descriptors, rather
|
27
|
-
than only open file descriptors.
|
28
|
-
applies if your platform supports the closefrom() function.
|
27
|
+
than only open file descriptors. However, I should note that this only
|
28
|
+
applies if your platform supports the closefrom() function. In that case,
|
29
29
|
the only advantage is speed.
|
30
30
|
|
31
31
|
You might also be wondering what the difference is between my implementation
|
@@ -38,7 +38,7 @@ of IO.fdwalk and a pure Ruby version that looks something like this:
|
|
38
38
|
end
|
39
39
|
|
40
40
|
The primary difference is that this only closes Ruby file objects, not
|
41
|
-
necessarily every
|
41
|
+
necessarily every file handle opened by the Ruby process. For example, handles
|
42
42
|
opened via system() calls.
|
43
43
|
|
44
44
|
= Note to OS X Users
|
@@ -61,7 +61,7 @@ I may add the File::O_DIRECT open constant on platforms that support it.
|
|
61
61
|
Ruby's
|
62
62
|
|
63
63
|
= Copyright
|
64
|
-
(C) 2003-
|
64
|
+
(C) 2003-2008 Daniel J. Berger
|
65
65
|
All Rights Reserved
|
66
66
|
|
67
67
|
= Warranty
|
data/doc/io_extra.txt
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
= Description
|
2
|
-
The io-extra
|
3
|
-
handy.
|
2
|
+
The io-extra library provides a few extra IO methods that you may find
|
3
|
+
handy.
|
4
4
|
|
5
|
+
= Supported Platforms
|
6
|
+
Works on most Unix platforms.
|
7
|
+
|
5
8
|
Not supported on MS Windows or OS X.
|
6
9
|
|
7
10
|
= Synopsis
|
@@ -29,7 +32,21 @@ IO.fdwalk(low_fd){ |fh| ... }
|
|
29
32
|
Note that it is up to you to close file handles, if desired, when this
|
30
33
|
method is used.
|
31
34
|
|
32
|
-
Not supported
|
35
|
+
Not supported on all platforms.
|
36
|
+
|
37
|
+
IO.pread(fd, length, offset)
|
38
|
+
Reads +length+ bytes of data from the given +fd+, starting at +offset.
|
39
|
+
The primary advantage of this method over Ruby's IO#read method is that
|
40
|
+
it performs the read without changing the file pointer.
|
41
|
+
|
42
|
+
Not supported on all platforms.
|
43
|
+
|
44
|
+
IO.pwrite(fd, buf, offset)
|
45
|
+
Writes +buf+ to the given +fd+, starting at +offset. The primary advantage
|
46
|
+
of this method over a standard seek & write approach is that it performs
|
47
|
+
the write without changing the file pointer.
|
48
|
+
|
49
|
+
Not supported on all platforms.
|
33
50
|
|
34
51
|
= Instance methods
|
35
52
|
IO#directio?
|
@@ -62,7 +79,7 @@ IO::DIRECTIO_OFF
|
|
62
79
|
function.
|
63
80
|
|
64
81
|
IO::EXTRA_VERSION
|
65
|
-
Returns the current version number of this
|
82
|
+
Returns the current version number of this library as a String.
|
66
83
|
|
67
84
|
= Other documentation
|
68
|
-
See the README for more documentation.
|
85
|
+
See the README for more documentation.
|
data/ext/extconf.rb
CHANGED
@@ -3,10 +3,10 @@ require 'fileutils'
|
|
3
3
|
|
4
4
|
dir_config('io')
|
5
5
|
|
6
|
-
FileUtils.cp('io/extra.c', '.')
|
7
|
-
|
8
6
|
have_func('closefrom')
|
9
7
|
have_func('fdwalk')
|
10
8
|
have_func('directio')
|
9
|
+
have_func('pread')
|
10
|
+
have_func('pwrite')
|
11
11
|
|
12
|
-
create_makefile('io/extra')
|
12
|
+
create_makefile('io/extra', 'io')
|
data/ext/io/extra.c
CHANGED
@@ -84,7 +84,10 @@ static VALUE io_fdwalk(int argc, VALUE* argv, VALUE klass){
|
|
84
84
|
* current handle. The default is false.
|
85
85
|
*/
|
86
86
|
static VALUE io_get_directio(VALUE self){
|
87
|
-
VALUE v_advice =
|
87
|
+
VALUE v_advice = Qnil;
|
88
|
+
|
89
|
+
if(rb_ivar_defined(rb_cIO, rb_intern("@directio")))
|
90
|
+
v_advice = rb_iv_get(self, "directio");
|
88
91
|
|
89
92
|
if(NIL_P(v_advice))
|
90
93
|
v_advice = Qfalse;
|
@@ -124,6 +127,59 @@ static VALUE io_set_directio(VALUE self, VALUE v_advice){
|
|
124
127
|
}
|
125
128
|
#endif
|
126
129
|
|
130
|
+
#ifdef HAVE_PREAD
|
131
|
+
/*
|
132
|
+
* IO.pread(fd, length, offset)
|
133
|
+
*
|
134
|
+
* This is similar to the IO.read method, except that it reads from a given
|
135
|
+
* position in the file without changing the file pointer. And unlike IO.read,
|
136
|
+
* the +fd+, +length+ and +offset+ arguments are all mandatory.
|
137
|
+
*/
|
138
|
+
static VALUE s_io_pread(VALUE klass, VALUE v_fd, VALUE v_nbyte, VALUE v_offset){
|
139
|
+
size_t result;
|
140
|
+
int fd = NUM2INT(v_fd);
|
141
|
+
size_t nbyte = NUM2ULONG(v_nbyte);
|
142
|
+
off_t offset = NUM2ULONG(v_offset);
|
143
|
+
char buf[nbyte+1];
|
144
|
+
|
145
|
+
result = pread(fd, buf, nbyte, offset);
|
146
|
+
|
147
|
+
if(result == -1)
|
148
|
+
rb_sys_fail("pread");
|
149
|
+
|
150
|
+
return rb_str_new2(buf);
|
151
|
+
}
|
152
|
+
#endif
|
153
|
+
|
154
|
+
#ifdef HAVE_PWRITE
|
155
|
+
/*
|
156
|
+
* IO.pwrite(fd, buf, offset)
|
157
|
+
*
|
158
|
+
* This method writes the +buf+, starting at +offset+, to the given +fd+,
|
159
|
+
* which must be opened with write permissions.
|
160
|
+
*
|
161
|
+
* This is similar to a seek & write in standard Ruby but the difference,
|
162
|
+
* beyond being a singleton method, is that the file pointer is never moved.
|
163
|
+
*
|
164
|
+
* Returns the number of bytes written.
|
165
|
+
*/
|
166
|
+
static VALUE s_io_pwrite(VALUE klass, VALUE v_fd, VALUE v_buf, VALUE v_offset){
|
167
|
+
ssize_t result;
|
168
|
+
|
169
|
+
result = pwrite(
|
170
|
+
NUM2INT(v_fd),
|
171
|
+
RSTRING(v_buf)->ptr,
|
172
|
+
RSTRING(v_buf)->len,
|
173
|
+
NUM2INT(v_offset)
|
174
|
+
);
|
175
|
+
|
176
|
+
if(result == -1)
|
177
|
+
rb_sys_fail("pwrite");
|
178
|
+
|
179
|
+
return ULL2NUM(result);
|
180
|
+
}
|
181
|
+
#endif
|
182
|
+
|
127
183
|
/* Adds the IO.closefrom, IO.fdwalk class methods, as well as the IO#directio
|
128
184
|
* and IO#directio? instance methods (if supported on your platform).
|
129
185
|
*/
|
@@ -145,6 +201,14 @@ void Init_extra(){
|
|
145
201
|
rb_define_const(rb_cIO, "DIRECTIO_ON", UINT2NUM(DIRECTIO_ON));
|
146
202
|
#endif
|
147
203
|
|
148
|
-
|
149
|
-
|
204
|
+
#ifdef HAVE_PREAD
|
205
|
+
rb_define_singleton_method(rb_cIO, "pread", s_io_pread, 3);
|
206
|
+
#endif
|
207
|
+
|
208
|
+
#ifdef HAVE_PWRITE
|
209
|
+
rb_define_singleton_method(rb_cIO, "pwrite", s_io_pwrite, 3);
|
210
|
+
#endif
|
211
|
+
|
212
|
+
/* 1.1.0: The version of this package. This a string. */
|
213
|
+
rb_define_const(rb_cIO, "EXTRA_VERSION", rb_str_new2("1.1.0"));
|
150
214
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
###########################################################################
|
2
|
-
#
|
2
|
+
# test_io_extra.rb
|
3
3
|
#
|
4
|
-
# Test suite for the io-extra
|
4
|
+
# Test suite for the io-extra library. This test should be run via the
|
5
5
|
# 'rake test' task.
|
6
6
|
###########################################################################
|
7
7
|
require 'test/unit'
|
@@ -11,10 +11,11 @@ class TC_IO_Extra < Test::Unit::TestCase
|
|
11
11
|
def setup
|
12
12
|
@file = 'delete_this.txt'
|
13
13
|
@fh = File.open(@file, 'w+')
|
14
|
+
@fh.puts "The quick brown fox jumped over the lazy dog's back"
|
14
15
|
end
|
15
16
|
|
16
17
|
def test_version
|
17
|
-
assert_equal('1.0
|
18
|
+
assert_equal('1.1.0', IO::EXTRA_VERSION)
|
18
19
|
end
|
19
20
|
|
20
21
|
def test_directio
|
@@ -42,6 +43,18 @@ class TC_IO_Extra < Test::Unit::TestCase
|
|
42
43
|
assert_respond_to(IO, :closefrom)
|
43
44
|
assert_nothing_raised{ IO.closefrom(3) }
|
44
45
|
end
|
46
|
+
|
47
|
+
def test_pread
|
48
|
+
@fh.close rescue nil
|
49
|
+
@fh = File.open(@file)
|
50
|
+
assert_respond_to(IO, :pread)
|
51
|
+
assert_equal("quick", IO.pread(@fh.fileno, 5, 4))
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_pwrite
|
55
|
+
assert_respond_to(IO, :pwrite)
|
56
|
+
assert_nothing_raised{ IO.pwrite(@fh.fileno, "HAL", 0) }
|
57
|
+
end
|
45
58
|
|
46
59
|
def teardown
|
47
60
|
@fh.close rescue nil
|
metadata
CHANGED
@@ -1,53 +1,60 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.4
|
3
|
-
specification_version: 1
|
4
2
|
name: io-extra
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0
|
7
|
-
date: 2007-07-16 00:00:00 -06:00
|
8
|
-
summary: Adds the IO.closefrom and IO.fdwalk class methods as well as the IO#directio and IO#directio? instance methods (for those platforms that support them).
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: djberg96@gmail.com
|
12
|
-
homepage: http://www.rubyforge.org/projects/shards
|
13
|
-
rubyforge_project: shards
|
14
|
-
description: Adds the IO.closefrom and IO.fdwalk class methods as well as the IO#directio and IO#directio? instance methods (for those platforms that support them).
|
15
|
-
autorequire:
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 1.8.0
|
24
|
-
version:
|
4
|
+
version: 1.1.0
|
25
5
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
6
|
authors:
|
30
7
|
- Daniel J. Berger
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-28 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Adds the IO.closefrom and IO.fdwalk class methods as well as the IO#directio and IO#directio? instance methods (for those platforms that support them).
|
17
|
+
email: djberg96@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions:
|
21
|
+
- ext/extconf.rb
|
22
|
+
extra_rdoc_files:
|
23
|
+
- CHANGES
|
24
|
+
- README
|
25
|
+
- MANIFEST
|
31
26
|
files:
|
32
27
|
- doc/io_extra.txt
|
33
|
-
- test/
|
28
|
+
- test/test_io_extra.rb
|
34
29
|
- ext/io/extra.c
|
35
30
|
- CHANGES
|
36
31
|
- README
|
37
32
|
- MANIFEST
|
38
|
-
|
39
|
-
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://www.rubyforge.org/projects/shards
|
35
|
+
post_install_message:
|
40
36
|
rdoc_options: []
|
41
37
|
|
42
|
-
|
43
|
-
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.8.0
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
50
52
|
requirements: []
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
+
rubyforge_project: shards
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: Adds the IO.closefrom and IO.fdwalk class methods as well as the IO#directio and IO#directio? instance methods (for those platforms that support them).
|
59
|
+
test_files:
|
60
|
+
- test/test_io_extra.rb
|