Class: FileTemp

Inherits:
File
  • Object
show all
Defined in:
ext/temp.c

Constant Summary collapse

TMPDIR =

Your system’s tmpdir

ENV['P_tmpdir']
VERSION =

The version of this library

0.1.2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(delete = true, template = 'rb_file_temp_XXXXXX') ⇒ File

Creates a new, anonymous temporary file in your FileTemp::TMPDIR directory, or /tmp if that cannot be accessed. If your $TMPDIR environment variable is set, it will be used instead. If $TMPDIR is not writable by the process, it will resort back to FileTemp::TMPDIR or /tmp.

If the delete option is set to true (the default) then the temporary file will be deleted automatically as soon as all references to it are closed. Otherwise, the file will live on in your $TMPDIR.

If the delete option is set to false, then the file is not deleted. In addition, you can supply a string template that the system replaces with a unique filename. This template should end with 3 to 6 ‘X’ characters. The default template is ‘rb_file_temp_XXXXXX’. In this case the temporary file lives in the directory where it was created.

The template argument is ignored if the delete argument is true.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/temp.c', line 41

static VALUE tempfile_init(int argc, VALUE* argv, VALUE self){
   VALUE v_args[2];
   VALUE v_delete;
   VALUE v_template;

   rb_scan_args(argc, argv, "02", &v_delete, &v_template);

   if(NIL_P(v_delete))
      v_delete = Qtrue;

   if(RTEST(v_delete)){
#ifdef HAVE_TMPFILE_S
      FILE* fptr;

      if(tmpfile_s(&fptr))
         rb_sys_fail("tmpfile_s()");
#else
      FILE* fptr = tmpfile();
      if(!fptr)
         rb_sys_fail("tmpfile()");
#endif
      
      v_args[0] = INT2FIX(fileno(fptr));
   }
   else{
      int fd;
      
      if(NIL_P(v_template))
         v_template = rb_str_new2("rb_file_temp_XXXXXX");
         
      fd = mkstemp(StringValuePtr(v_template));

      if(fd < 0)
         rb_sys_fail("mkstemp()");

      v_args[0] = INT2FIX(fd);
   }

   /* This bit of explicitness is necessary for MS Windows */
   v_args[1] = rb_str_new2("wb");

   return rb_call_super(2, v_args);
}

Class Method Details

.tmpnamObject

Generates a unique file name, prefixed with the value of FileTemp::TMPDIR. Note that a file is not actually generated on the filesystem.



89
90
91
92
# File 'ext/temp.c', line 89

static VALUE tempfile_tmpnam(VALUE klass){
   char buf[L_tmpnam];
   return rb_str_new2(tmpnam(buf));
}