mktemp 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mktemp.rb +131 -0
- metadata +45 -0
data/lib/mktemp.rb
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
#
|
2
|
+
# Skeleton module for the 'mktemp' routine.
|
3
|
+
#
|
4
|
+
# Ideally, one would do this in their code to import the "mktemp" call
|
5
|
+
# directly into their current namespace:
|
6
|
+
#
|
7
|
+
# require 'mktemp'
|
8
|
+
# include MkTemp
|
9
|
+
# # do something with mktemp()
|
10
|
+
#
|
11
|
+
#
|
12
|
+
# It is recommended that you look at the documentation for the mktemp()
|
13
|
+
# call directly for specific usage.
|
14
|
+
#
|
15
|
+
#--
|
16
|
+
#
|
17
|
+
# The compilation of software known as mktemp.rb is distributed under the
|
18
|
+
# following terms:
|
19
|
+
# Copyright (C) 2005-2006 Erik Hollensbe. All rights reserved.
|
20
|
+
#
|
21
|
+
# Redistribution and use in source form, with or without
|
22
|
+
# modification, are permitted provided that the following conditions
|
23
|
+
# are met:
|
24
|
+
# 1. Redistributions of source code must retain the above copyright
|
25
|
+
# notice, this list of conditions and the following disclaimer.
|
26
|
+
#
|
27
|
+
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
28
|
+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
29
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
30
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
31
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
32
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
33
|
+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
34
|
+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
35
|
+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
36
|
+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
37
|
+
# SUCH DAMAGE.
|
38
|
+
#
|
39
|
+
#++
|
40
|
+
|
41
|
+
module MkTemp
|
42
|
+
VALID_TMPNAM_CHARS = (?a..?z).to_a + (?A..?Z).to_a
|
43
|
+
|
44
|
+
#
|
45
|
+
# This routine just generates a temporary file similar to the
|
46
|
+
# routines from 'mktemp'. A trailing series of 'X' characters will
|
47
|
+
# be transformed into a randomly-generated set of alphanumeric
|
48
|
+
# characters.
|
49
|
+
#
|
50
|
+
# This routine performs no file testing, at all. It is not suitable
|
51
|
+
# for anything beyond that.
|
52
|
+
#
|
53
|
+
|
54
|
+
def tmpnam(filename)
|
55
|
+
m = filename.match(/(X*)$/)
|
56
|
+
|
57
|
+
retnam = filename.dup
|
58
|
+
|
59
|
+
if m[1]
|
60
|
+
mask = ""
|
61
|
+
m[1].length.times { mask += VALID_TMPNAM_CHARS[rand(52)].chr }
|
62
|
+
retnam.sub!(/(X*)$/, mask)
|
63
|
+
end
|
64
|
+
|
65
|
+
return retnam
|
66
|
+
end
|
67
|
+
|
68
|
+
module_function :tmpnam
|
69
|
+
|
70
|
+
#
|
71
|
+
# This routine works similarly to mkstemp(3) in that it gets a new
|
72
|
+
# file, and returns a file handle for that file. The mask parameter
|
73
|
+
# determines whether or not to process the filename as a mask by
|
74
|
+
# calling the tmpnam() routine in this module. This routine will
|
75
|
+
# continue until it finds a valid filename, which may not do what
|
76
|
+
# you expect.
|
77
|
+
#
|
78
|
+
# While all attempts have been made to keep this as secure as
|
79
|
+
# possible, due to a few problems with Ruby's file handling code, we
|
80
|
+
# are required to allow a few concessions. If a 0-length file is
|
81
|
+
# created before we attempt to create ours, we have no choice but to
|
82
|
+
# accept it. Do not rely on this code for any expected level of
|
83
|
+
# security, even though we have taken all the measures we can to
|
84
|
+
# handle that situation.
|
85
|
+
#
|
86
|
+
|
87
|
+
def mktemp(filename, mask=true)
|
88
|
+
fh = nil
|
89
|
+
|
90
|
+
begin
|
91
|
+
loop do
|
92
|
+
fn = mask ? tmpnam(filename) : filename
|
93
|
+
|
94
|
+
if File.exist? fn
|
95
|
+
fail "Unable to create a temporary filename" unless mask
|
96
|
+
next
|
97
|
+
end
|
98
|
+
|
99
|
+
fh = File.new(fn, "a", 0600)
|
100
|
+
fh.seek(0, IO::SEEK_END)
|
101
|
+
break if fh.pos == 0
|
102
|
+
|
103
|
+
fail "Unable to create a temporary filename" unless mask
|
104
|
+
fh.close
|
105
|
+
end
|
106
|
+
rescue Exception => e
|
107
|
+
# in the case that we hit a locked file...
|
108
|
+
fh.close if fh
|
109
|
+
raise e unless mask
|
110
|
+
end
|
111
|
+
|
112
|
+
return fh
|
113
|
+
end
|
114
|
+
|
115
|
+
module_function :mktemp
|
116
|
+
|
117
|
+
#
|
118
|
+
# Create a directory. If mask is true (default), it will use the
|
119
|
+
# random name generation rules from the tmpnam() call in this
|
120
|
+
# module.
|
121
|
+
#
|
122
|
+
|
123
|
+
def mktempdir(filename, mask=true)
|
124
|
+
fn = mask ? tmpnam(filename) : filename
|
125
|
+
Dir.mkdir(fn)
|
126
|
+
return fn
|
127
|
+
end
|
128
|
+
|
129
|
+
module_function :mktempdir
|
130
|
+
end
|
131
|
+
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: mktemp
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2006-04-09 00:00:00 -07:00
|
8
|
+
summary: Module to emulate the 'mktemp' utility from a unix system
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: erik@hollensbe.org
|
12
|
+
homepage:
|
13
|
+
rubyforge_project: shell-tools
|
14
|
+
description:
|
15
|
+
autorequire: mktemp
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- Erik Hollensbe
|
30
|
+
files:
|
31
|
+
- lib/mktemp.rb
|
32
|
+
test_files: []
|
33
|
+
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
extra_rdoc_files: []
|
37
|
+
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
requirements: []
|
43
|
+
|
44
|
+
dependencies: []
|
45
|
+
|