open-temporary 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +1 -1
- data/README.rdoc +31 -14
- data/VERSION +1 -1
- data/ext/open_temporary/open_temporary.c +32 -2
- data/lib/open-temporary.rb +1 -1
- data/open-temporary.gemspec +3 -3
- data/spec/spec_helper.rb +1 -1
- metadata +4 -4
data/.document
CHANGED
data/README.rdoc
CHANGED
@@ -1,19 +1,36 @@
|
|
1
|
-
= open-temporary
|
1
|
+
= open-temporary the "real file" temporary one.
|
2
2
|
|
3
|
-
|
3
|
+
OK, so you dropped into a tempfile pitfall. Like Rails.
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
-
* Fork the project.
|
10
|
-
* Start a feature/bugfix branch.
|
11
|
-
* Commit and push until you are happy with your contribution.
|
12
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
5
|
+
https://github.com/rails/rails/pull/2068
|
14
6
|
|
15
|
-
|
7
|
+
Why not just fix the situation so that you can safely dup?
|
16
8
|
|
17
|
-
|
18
|
-
further details.
|
9
|
+
= How to use it?
|
19
10
|
|
11
|
+
You first add
|
12
|
+
|
13
|
+
gem "open-temporary"
|
14
|
+
|
15
|
+
to your Gemfile, then from your .rb file,
|
16
|
+
|
17
|
+
require 'open-temporary'
|
18
|
+
|
19
|
+
And that's it. Now you have the "open" method extended, just like
|
20
|
+
the open-uri standard library.
|
21
|
+
|
22
|
+
File.open # => gives you a temporary file
|
23
|
+
|
24
|
+
This time the null (zero-arity) argument indicates this is a temporary
|
25
|
+
file. You get a REAL instance of File, no proxy, no tweak. It can be
|
26
|
+
dup'ed, it can be used with process forks, and still, you can just
|
27
|
+
leave it, and the GC reclaims the whole resources.
|
28
|
+
|
29
|
+
= Great! So why this isn't a standard?
|
30
|
+
|
31
|
+
Because it has absolutely zero portability. The magic behind the
|
32
|
+
scene is that we just let your operating system to reclaim what's
|
33
|
+
necessary. That way, the behaviour detail is much much OS specific.
|
34
|
+
Especially, this kind of API is not doable on a Windows environment.
|
35
|
+
Might not also on Java (not sure). So this can never be a part of
|
36
|
+
gospel ruby.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
@@ -23,10 +23,27 @@
|
|
23
23
|
#include <stdlib.h>
|
24
24
|
#include <unistd.h>
|
25
25
|
#include <fcntl.h>
|
26
|
+
#include <errno.h>
|
27
|
+
|
28
|
+
#define preserving_errno(stmts) \
|
29
|
+
do {int saved_errno = errno; stmts; errno = saved_errno;} while (0)
|
26
30
|
|
27
31
|
static VALUE tmpdir = Qundef;
|
28
32
|
static VALUE tmpsuf = Qundef;
|
29
33
|
|
34
|
+
struct fdopen_args {
|
35
|
+
int fd;
|
36
|
+
int mod;
|
37
|
+
const char *str;
|
38
|
+
};
|
39
|
+
|
40
|
+
static VALUE
|
41
|
+
call_fdopen(VALUE args)
|
42
|
+
{
|
43
|
+
struct fdopen_args *argp = (struct fdopen_args *)args;
|
44
|
+
return rb_io_fdopen(argp->fd, argp->mod, argp->str);
|
45
|
+
}
|
46
|
+
|
30
47
|
static VALUE
|
31
48
|
open_temporary(int argc, VALUE* argv, VALUE klass)
|
32
49
|
{
|
@@ -41,7 +58,7 @@ open_temporary(int argc, VALUE* argv, VALUE klass)
|
|
41
58
|
|
42
59
|
if (dir == Qnil) dir = tmpdir;
|
43
60
|
if (suf == Qnil) suf = tmpsuf;
|
44
|
-
|
61
|
+
|
45
62
|
SafeStringValue(dir);
|
46
63
|
SafeStringValue(suf);
|
47
64
|
|
@@ -51,14 +68,27 @@ open_temporary(int argc, VALUE* argv, VALUE klass)
|
|
51
68
|
rb_sys_fail("mkstemp(3)");
|
52
69
|
}
|
53
70
|
else if (unlink(str) == -1) {
|
71
|
+
preserving_errno((void)close(fd));
|
54
72
|
/* unlink failed, no way to reclaim */
|
55
73
|
rb_sys_fail("unlink(2)");
|
56
74
|
}
|
57
75
|
else if ((mod = fcntl(fd, F_GETFL)) == -1) {
|
76
|
+
preserving_errno((void)close(fd));
|
58
77
|
rb_sys_fail("fcntl(2)");
|
59
78
|
}
|
60
79
|
else {
|
61
|
-
|
80
|
+
int state;
|
81
|
+
VALUE io;
|
82
|
+
struct fdopen_args args;
|
83
|
+
args.fd = fd;
|
84
|
+
args.mod = mod;
|
85
|
+
args.str = str;
|
86
|
+
io = rb_protect(call_fdopen, (VALUE)&args, &state);
|
87
|
+
if (state) {
|
88
|
+
(void)close(fd);
|
89
|
+
rb_jump_tag(state);
|
90
|
+
}
|
91
|
+
return io;
|
62
92
|
}
|
63
93
|
}
|
64
94
|
|
data/lib/open-temporary.rb
CHANGED
data/open-temporary.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "open-temporary"
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Urabe, Shyouhei"]
|
12
|
-
s.date = "2012-09-
|
12
|
+
s.date = "2012-09-06"
|
13
13
|
s.description = "A variation of tempfile standard lib, that actually creates a File instance."
|
14
14
|
s.email = "shyouhei@ruby-lang.org"
|
15
15
|
s.extensions = ["ext/open_temporary/extconf.rb"]
|
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
|
|
37
37
|
s.homepage = "http://github.com/shyouhei/open-temporary"
|
38
38
|
s.licenses = ["MIT"]
|
39
39
|
s.require_paths = ["lib"]
|
40
|
-
s.rubygems_version = "1.8.
|
40
|
+
s.rubygems_version = "1.8.24"
|
41
41
|
s.summary = "A variant of Tempfile that makes File instead"
|
42
42
|
|
43
43
|
if s.respond_to? :specification_version then
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open-temporary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -162,7 +162,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
162
162
|
version: '0'
|
163
163
|
segments:
|
164
164
|
- 0
|
165
|
-
hash:
|
165
|
+
hash: 1506962448648116596
|
166
166
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
167
|
none: false
|
168
168
|
requirements:
|
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
171
|
version: '0'
|
172
172
|
requirements: []
|
173
173
|
rubyforge_project:
|
174
|
-
rubygems_version: 1.8.
|
174
|
+
rubygems_version: 1.8.24
|
175
175
|
signing_key:
|
176
176
|
specification_version: 3
|
177
177
|
summary: A variant of Tempfile that makes File instead
|