Class: Dir

Inherits:
Object
  • Object
show all
Defined in:
(unknown)

Class Method Summary collapse

Class Method Details

.mkdtemp([string]) ⇒ String?

This method securely creates temporary directories. It is a wrapper for the mkdtemp() function in the standard C library. It takes an optional String parameter as a template describing the desired form of the directory name; if no template is supplied then “/tmp/temp.XXXXXX” is used as a default.

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'ext/mkdtemp/mkdtemp.c', line 22

static VALUE walrus_dir_mkdtemp_m(int argc, VALUE *argv, VALUE self)
{
    VALUE template;
    if (rb_scan_args(argc, argv, "01", &template) == 0)                         /* check for 0 mandatory arguments, 1 optional argument */
        template = Qnil;                                                        /* default to nil if no argument passed */
    if (NIL_P(template))
        template = rb_str_new2("/tmp/temp.XXXXXX");                             /* fallback to this template if passed nil */
    SafeStringValue(template);                                                  /* raises if template is tainted and SAFE level > 0 */
    VALUE safe  = StringValue(template);                                        /* duck typing support */
    char *path  = mkdtemp(RSTRING(safe)->ptr);
    if (path == NULL)
        rb_raise(rb_eSystemCallError, "mkdtemp failed (error: %d)", errno);
    return rb_str_new2(path);
}