mkdtemp 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ # Copyright 2007 Wincent Colaiuta
2
+ # This program is free software: you can redistribute it and/or modify
3
+ # it under the terms of the GNU General Public License as published by
4
+ # the Free Software Foundation, either version 3 of the License, or
5
+ # (at your option) any later version.
6
+ #
7
+ # This program is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
+ # GNU General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU General Public License
13
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
+
15
+ require 'mkmf'
16
+ create_makefile('mkdtemp')
17
+
@@ -0,0 +1,54 @@
1
+ /*
2
+ Copyright 2007-2008 Wincent Colaiuta
3
+ This program is free software: you can redistribute it and/or modify
4
+ it under the terms of the GNU General Public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+
8
+ This program is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU General Public License for more details.
12
+
13
+ You should have received a copy of the GNU General Public License
14
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+ */
16
+
17
+ #include <ruby.h>
18
+ #include <errno.h>
19
+ #include <unistd.h>
20
+
21
+ /*
22
+
23
+ call-seq:
24
+ Dir.mkdtemp([string]) -> String or nil
25
+
26
+ This method securely creates temporary directories. It is a wrapper for the mkdtemp() function in the standard C library. It takes
27
+ an optional String parameter as a template describing the desired form of the directory name and overwriting the template in-place;
28
+ if no template is supplied then "/tmp/temp.XXXXXX" is used as a default.
29
+
30
+ Note that the exact implementation of mkdtemp() may vary depending on the target system. For example, on Mac OS X at the time of
31
+ writing, the man page states that the template may contain "some number" of "Xs" on the end of the string, whereas on Red Hat
32
+ Enterprise Linux it states that the template suffix "must be XXXXXX".
33
+
34
+ */
35
+ static VALUE dir_mkdtemp_m(int argc, VALUE *argv, VALUE self)
36
+ {
37
+ VALUE template;
38
+ char *path;
39
+ if (rb_scan_args(argc, argv, "01", &template) == 0) /* check for 0 mandatory arguments, 1 optional argument */
40
+ template = Qnil; /* default to nil if no argument passed */
41
+ if (NIL_P(template))
42
+ template = rb_str_new2("/tmp/temp.XXXXXX"); /* fallback to this template if passed nil */
43
+ SafeStringValue(template); /* raises if template is tainted and SAFE level > 0 */
44
+ template = StringValue(template); /* duck typing support */
45
+ path = mkdtemp(RSTRING(template)->ptr);
46
+ if (path == NULL)
47
+ rb_raise(rb_eSystemCallError, "mkdtemp failed (error: %d)", errno);
48
+ return template;
49
+ }
50
+
51
+ void Init_mkdtemp()
52
+ {
53
+ rb_define_module_function(rb_cDir, "mkdtemp", dir_mkdtemp_m, -1);
54
+ }
@@ -0,0 +1,19 @@
1
+ # Copyright 2008 Wincent Colaiuta
2
+ # This program is free software: you can redistribute it and/or modify
3
+ # it under the terms of the GNU General Public License as published by
4
+ # the Free Software Foundation, either version 3 of the License, or
5
+ # (at your option) any later version.
6
+ #
7
+ # This program is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
+ # GNU General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU General Public License
13
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
+
15
+ class Dir
16
+ module Mkdtemp
17
+ VERSION = '1.0'
18
+ end # module Mkdtemp
19
+ end # class Dir
@@ -0,0 +1,78 @@
1
+ # Copyright 2008 Wincent Colaiuta
2
+ # This program is free software: you can redistribute it and/or modify
3
+ # it under the terms of the GNU General Public License as published by
4
+ # the Free Software Foundation, either version 3 of the License, or
5
+ # (at your option) any later version.
6
+ #
7
+ # This program is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
+ # GNU General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU General Public License
13
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
+
15
+ require File.join(File.dirname(__FILE__), 'spec_helper')
16
+
17
+ describe 'Dir.mkdtemp' do
18
+ before do
19
+ if not File.exist? '/tmp'
20
+ pending "cannot run spec because '/tmp' does not exist"
21
+ elsif not File.directory? '/tmp'
22
+ pending "cannot run spec because '/tmp' is not a directory"
23
+ elsif not File.writable? '/tmp'
24
+ pending "cannot run spec because '/tmp' is not writeable"
25
+ end
26
+ end
27
+
28
+ it 'should return the modified template path' do
29
+ path = Dir.mkdtemp
30
+ path.should be_kind_of(String)
31
+ path.should_not == '/tmp/temp.XXXXXX'
32
+ end
33
+
34
+ it 'should create the directory corresponding to the template' do
35
+ path = Dir.mkdtemp
36
+ File.exist?(path).should == true
37
+ File.directory?(path).should == true
38
+ end
39
+
40
+ it 'should create the directory with write permissions' do
41
+ path = Dir.mkdtemp
42
+ File.writable?(path).should == true
43
+ end
44
+
45
+ it 'should create the directory with read permissions' do
46
+ path = Dir.mkdtemp
47
+ File.readable?(path).should == true
48
+ end
49
+
50
+ it 'should create the directory with secure ownership (same user as caller)' do
51
+ path = Dir.mkdtemp
52
+ File.owned?(path).should == true
53
+ end
54
+
55
+ it 'should complain if passed a template that includes non-existentent parent directories' do
56
+ non_existent_dir = '/temp-which-does-not-exist'
57
+ if File.exist? non_existent_dir
58
+ pending "cannot run because '#{non_existent_dir}' exists"
59
+ end
60
+ lambda { Dir.mkdtemp "/#{non_existent_dir}/temp.XXXXXX" }.should raise_error(SystemCallError)
61
+ end
62
+
63
+ it 'should use "/tmp/temp.XXXXXX" as a template if passed nil' do
64
+ path = Dir.mkdtemp
65
+ path.should match(%r{\A/tmp/temp\..{6}\z})
66
+ path.should_not == '/tmp/temp.XXXXXX'
67
+ end
68
+
69
+ it 'should use substitute random characters for the trailing "Xs" in the template' do
70
+ path = Dir.mkdtemp '/tmp/test.XXXXXX'
71
+ path.should_not == '/tmp/test.XXXXXX'
72
+ end
73
+
74
+ it 'should leave the prefix portion of the template unchanged' do
75
+ path = Dir.mkdtemp '/tmp/test.XXXXXX'
76
+ path.should match(%r{\A/tmp/test\..{6}\z})
77
+ end
78
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright 2008 Wincent Colaiuta
2
+ # This program is free software: you can redistribute it and/or modify
3
+ # it under the terms of the GNU General Public License as published by
4
+ # the Free Software Foundation, either version 3 of the License, or
5
+ # (at your option) any later version.
6
+ #
7
+ # This program is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
+ # GNU General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU General Public License
13
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
+
15
+ require 'pathname'
16
+ require 'rubygems'
17
+ require 'spec'
18
+ require File.join(File.dirname(__FILE__), '..', 'ext', 'mkdtemp')
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mkdtemp
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Wincent Colaiuta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-18 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: mkdtemp is a C extension that wraps the Standard C Library function of the same name to make secure creation of temporary directories easily available from within Ruby.
17
+ email: win@wincent.com
18
+ executables: []
19
+
20
+ extensions:
21
+ - ext/extconf.rb
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/mkdtemp
26
+ - lib/mkdtemp/version.rb
27
+ - spec/mkdtemp_spec.rb
28
+ - spec/spec_helper.rb
29
+ - ext/mkdtemp.c
30
+ - ext/extconf.rb
31
+ has_rdoc: true
32
+ homepage: http://mkdtemp.rubyforge.org/
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - ext
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project: mkdtemp
54
+ rubygems_version: 1.3.1
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: Secure creation of temporary directories
58
+ test_files: []
59
+