1 // Copyright 2007-2011 Wincent Colaiuta. All rights reserved.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
12 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
16 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22 // POSSIBILITY OF SUCH DAMAGE.
27 #include "ruby_compat.h"
29 // helper function needed by rb_iterate; see:
30 // http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/144100
31 VALUE call_chdir(VALUE dir)
33 return rb_funcall(rb_cDir, rb_intern("chdir"), 1, dir);
36 // helper function needed by rb_iterate
37 VALUE yield_block(VALUE ignored, VALUE block)
39 return rb_funcall(block, rb_intern("call"), 0);
43 * @overload mkdtemp(template)
44 * Securely create a temporary directory.
46 * This method securely creates temporary directories. It is a wrapper for the
47 * <code>mkdtemp()</code> function in the standard C library.
49 * If supplied a block, performs a <code>Dir.chdir</code> into the created
50 * directory and yields to the block:
53 * # this: # is a shorthand for:
54 * Dir.mkdtemp do # dir = Dir.mkdtemp
55 * puts Dir.pwd # Dir.chdir dir do
59 * @yield an optional block to perform operations inside the created directory.
60 * @param [String, nil] template a template describing the desired form of the
61 * directory name. If no template is supplied then "/tmp/temp.XXXXXX" is used
63 * @return [String] the path to the created directory
64 * @raise [TypeError] if <code>template</code> is not a String or cannot be
66 * @raise [SecurityError] if <code>template</code> is tainted and
67 * <code>$SAFE</code> is > 0
68 * @raise [NoMemoryError] if temporary storage for the template could not be
70 * @raise [SystemCallError] if the call to <code>mkdtemp()</code> fails
73 * Note that the exact implementation of <code>mkdtemp()</code> may vary
74 * depending on the target system. For example, on Mac OS X at the time of
75 * writing, the man page states that the template may contain "some number" of
76 * "Xs" on the end of the string, whereas on Red Hat Enterprise Linux it states
77 * that the template suffix "must be XXXXXX".
79 static VALUE dir_mkdtemp_m(int argc, VALUE *argv, VALUE self)
81 VALUE template, block;
86 if (rb_scan_args(argc, argv, "01&", &template, &block) == 0) // 0 mandatory, 1 optional, 1 block
87 template = Qnil; // default to nil if no argument passed
89 template = rb_str_new2("/tmp/temp.XXXXXX"); // fallback to this template if passed nil
90 SafeStringValue(template); // raises if template is tainted and SAFE level > 0
91 template = StringValue(template); // duck typing support
93 // create temporary storage
94 c_template = malloc(RSTRING_LEN(template) + 1);
96 rb_raise(rb_eNoMemError, "failed to allocate %ld bytes of template storage", RSTRING_LEN(template) + 1);
97 strncpy(c_template, RSTRING_PTR(template), RSTRING_LEN(template));
98 c_template[RSTRING_LEN(template)] = 0; // NUL-terminate
101 path = mkdtemp(c_template);
103 template = rb_str_new2(path);
106 rb_raise(rb_eSystemCallError, "mkdtemp failed (error #%d: %s)", errno, strerror(errno));
108 // yield to block if given, inside Dir.chdir
109 if (rb_block_given_p())
110 rb_iterate(call_chdir, template, yield_block, block);
117 // for YARD, need to fake this here
118 VALUE rb_cDir = rb_define_class("Dir", rb_cObject);
120 rb_define_singleton_method(rb_cDir, "mkdtemp", dir_mkdtemp_m, -1);