Module: Webby::MkTemp

Defined in:
lib/webby/stelan/mktemp.rb

Constant Summary collapse

VALID_TMPNAM_CHARS =
(?a..?z).to_a + (?A..?Z).to_a

Class Method Summary collapse

Class Method Details

.mktemp(filename, mask = true) ⇒ Object

This routine works similarly to mkstemp(3) in that it gets a new file, and returns a file handle for that file. The mask parameter determines whether or not to process the filename as a mask by calling the tmpnam() routine in this module. This routine will continue until it finds a valid filename, which may not do what you expect.

While all attempts have been made to keep this as secure as possible, due to a few problems with Ruby’s file handling code, we are required to allow a few concessions. If a 0-length file is created before we attempt to create ours, we have no choice but to accept it. Do not rely on this code for any expected level of security, even though we have taken all the measures we can to handle that situation.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/webby/stelan/mktemp.rb', line 88

def mktemp(filename, mask=true)
  fh = nil

  begin 
    loop do
      fn = mask ? tmpnam(filename) : filename

      if File.exist? fn
        fail "Unable to create a temporary filename" unless mask
        next
      end

      fh = File.new(fn, "a", 0600)
      fh.seek(0, IO::SEEK_END)
      break if fh.pos == 0 

      fail "Unable to create a temporary filename" unless mask
      fh.close
    end
  rescue Exception => e
    # in the case that we hit a locked file...
    fh.close if fh
    raise e unless mask
  end
  
  return fh
end

.mktempdir(filename, mask = true) ⇒ Object

Create a directory. If mask is true (default), it will use the random name generation rules from the tmpnam() call in this module.



124
125
126
127
128
# File 'lib/webby/stelan/mktemp.rb', line 124

def mktempdir(filename, mask=true)
  fn = mask ? tmpnam(filename) : filename
  Dir.mkdir(fn)
  return fn
end

.tmpnam(filename) ⇒ Object

This routine just generates a temporary file similar to the routines from ‘mktemp’. A trailing series of ‘X’ characters will be transformed into a randomly-generated set of alphanumeric characters.

This routine performs no file testing, at all. It is not suitable for anything beyond that.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/webby/stelan/mktemp.rb', line 55

def tmpnam(filename)
  m = filename.match(/(X*)$/)
  
  retnam = filename.dup
  
  if m[1]
    mask = ""
    m[1].length.times { mask += VALID_TMPNAM_CHARS[rand(52)].chr }
    retnam.sub!(/(X*)$/, mask) 
  end

  return retnam
end