tmpdir 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of tmpdir might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.github/workflows/test.yml +24 -0
- data/.gitignore +8 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +23 -0
- data/README.md +58 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tmpdir.rb +147 -0
- data/tmpdir.gemspec +23 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 85004f3b0d2caff8af5b16c3cecd842adfd8ba7eaab2b4129aea2334121c5a7d
|
4
|
+
data.tar.gz: 2d1b2624b97a9673af669a05cc30ecde60acb1abada6e0e6fb8044ff5c8ac942
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b6900648a6f5005b35e2809a6aca0d2e4f0ec858374a14b9d56a1f000b25f159eb3d42b39a4c6895782eb122896d8febfd56488bf16a46592449543e47acd9cc
|
7
|
+
data.tar.gz: 7d1a75bcaa076e6a3134faf0cb5b5d7682f7bde1e6ad41200bafa9e2e8c01de6a9f725b8c219cb7f4053b3ca7e622f040584f5c652f4a601ab8a8caf97b5ff7e
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: ubuntu
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
name: build (${{ matrix.ruby }} / ${{ matrix.os }})
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby: [ 2.7, 2.6, 2.5, 2.4, head ]
|
11
|
+
os: [ ubuntu-latest, macos-latest ]
|
12
|
+
runs-on: ${{ matrix.os }}
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@master
|
15
|
+
- name: Set up Ruby
|
16
|
+
uses: ruby/setup-ruby@v1
|
17
|
+
with:
|
18
|
+
ruby-version: ${{ matrix.ruby }}
|
19
|
+
- name: Install dependencies
|
20
|
+
run: |
|
21
|
+
gem install bundler --no-document
|
22
|
+
bundle install
|
23
|
+
- name: Run test
|
24
|
+
run: rake test
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
tmpdir (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
power_assert (1.1.5)
|
10
|
+
rake (12.3.3)
|
11
|
+
test-unit (3.3.5)
|
12
|
+
power_assert
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
rake
|
19
|
+
test-unit
|
20
|
+
tmpdir!
|
21
|
+
|
22
|
+
BUNDLED WITH
|
23
|
+
2.1.4
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# tmpdir
|
2
|
+
|
3
|
+
retrieve temporary directory path
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'tmpdir'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install tmpdir
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Dir.mktmpdir creates a temporary directory.
|
24
|
+
The directory is created with 0700 permission.
|
25
|
+
|
26
|
+
Application should not change the permission to make the temporary directory accessible from other users.
|
27
|
+
|
28
|
+
The prefix and suffix of the name of the directory is specified by
|
29
|
+
the optional first argument, <i>prefix_suffix</i>.
|
30
|
+
|
31
|
+
- If it is not specified or nil, "d" is used as the prefix and no suffix is used.
|
32
|
+
- If it is a string, it is used as the prefix and no suffix is used.
|
33
|
+
- If it is an array, first element is used as the prefix and second element is used as a suffix.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
Dir.mktmpdir {|dir| dir is ".../d..." }
|
37
|
+
Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
|
38
|
+
Dir.mktmpdir(["foo", "bar"]) {|dir| dir is ".../foo...bar" }
|
39
|
+
```
|
40
|
+
|
41
|
+
The directory is created under Dir.tmpdir or
|
42
|
+
the optional second argument <i>tmpdir</i> if non-nil value is given.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
|
46
|
+
Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
|
47
|
+
```
|
48
|
+
|
49
|
+
## Development
|
50
|
+
|
51
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
52
|
+
|
53
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/tmpdir.
|
58
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tmpdir"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/tmpdir.rb
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# tmpdir - retrieve temporary directory path
|
4
|
+
#
|
5
|
+
# $Id$
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'fileutils'
|
9
|
+
begin
|
10
|
+
require 'etc.so'
|
11
|
+
rescue LoadError # rescue LoadError for miniruby
|
12
|
+
end
|
13
|
+
|
14
|
+
class Dir
|
15
|
+
|
16
|
+
@@systmpdir ||= defined?(Etc.systmpdir) ? Etc.systmpdir : '/tmp'
|
17
|
+
|
18
|
+
##
|
19
|
+
# Returns the operating system's temporary file path.
|
20
|
+
|
21
|
+
def self.tmpdir
|
22
|
+
tmp = nil
|
23
|
+
[ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', '.'].each do |dir|
|
24
|
+
next if !dir
|
25
|
+
dir = File.expand_path(dir)
|
26
|
+
if stat = File.stat(dir) and stat.directory? and stat.writable? and
|
27
|
+
(!stat.world_writable? or stat.sticky?)
|
28
|
+
tmp = dir
|
29
|
+
break
|
30
|
+
end rescue nil
|
31
|
+
end
|
32
|
+
raise ArgumentError, "could not find a temporary directory" unless tmp
|
33
|
+
tmp
|
34
|
+
end
|
35
|
+
|
36
|
+
# Dir.mktmpdir creates a temporary directory.
|
37
|
+
#
|
38
|
+
# The directory is created with 0700 permission.
|
39
|
+
# Application should not change the permission to make the temporary directory accessible from other users.
|
40
|
+
#
|
41
|
+
# The prefix and suffix of the name of the directory is specified by
|
42
|
+
# the optional first argument, <i>prefix_suffix</i>.
|
43
|
+
# - If it is not specified or nil, "d" is used as the prefix and no suffix is used.
|
44
|
+
# - If it is a string, it is used as the prefix and no suffix is used.
|
45
|
+
# - If it is an array, first element is used as the prefix and second element is used as a suffix.
|
46
|
+
#
|
47
|
+
# Dir.mktmpdir {|dir| dir is ".../d..." }
|
48
|
+
# Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
|
49
|
+
# Dir.mktmpdir(["foo", "bar"]) {|dir| dir is ".../foo...bar" }
|
50
|
+
#
|
51
|
+
# The directory is created under Dir.tmpdir or
|
52
|
+
# the optional second argument <i>tmpdir</i> if non-nil value is given.
|
53
|
+
#
|
54
|
+
# Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
|
55
|
+
# Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
|
56
|
+
#
|
57
|
+
# If a block is given,
|
58
|
+
# it is yielded with the path of the directory.
|
59
|
+
# The directory and its contents are removed
|
60
|
+
# using FileUtils.remove_entry before Dir.mktmpdir returns.
|
61
|
+
# The value of the block is returned.
|
62
|
+
#
|
63
|
+
# Dir.mktmpdir {|dir|
|
64
|
+
# # use the directory...
|
65
|
+
# open("#{dir}/foo", "w") { ... }
|
66
|
+
# }
|
67
|
+
#
|
68
|
+
# If a block is not given,
|
69
|
+
# The path of the directory is returned.
|
70
|
+
# In this case, Dir.mktmpdir doesn't remove the directory.
|
71
|
+
#
|
72
|
+
# dir = Dir.mktmpdir
|
73
|
+
# begin
|
74
|
+
# # use the directory...
|
75
|
+
# open("#{dir}/foo", "w") { ... }
|
76
|
+
# ensure
|
77
|
+
# # remove the directory.
|
78
|
+
# FileUtils.remove_entry dir
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
def self.mktmpdir(prefix_suffix=nil, *rest, **options)
|
82
|
+
base = nil
|
83
|
+
path = Tmpname.create(prefix_suffix || "d", *rest, **options) {|path, _, _, d|
|
84
|
+
base = d
|
85
|
+
mkdir(path, 0700)
|
86
|
+
}
|
87
|
+
if block_given?
|
88
|
+
begin
|
89
|
+
yield path
|
90
|
+
ensure
|
91
|
+
unless base
|
92
|
+
stat = File.stat(File.dirname(path))
|
93
|
+
if stat.world_writable? and !stat.sticky?
|
94
|
+
raise ArgumentError, "parent directory is world writable but not sticky"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
FileUtils.remove_entry path
|
98
|
+
end
|
99
|
+
else
|
100
|
+
path
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
module Tmpname # :nodoc:
|
105
|
+
module_function
|
106
|
+
|
107
|
+
def tmpdir
|
108
|
+
Dir.tmpdir
|
109
|
+
end
|
110
|
+
|
111
|
+
UNUSABLE_CHARS = [File::SEPARATOR, File::ALT_SEPARATOR, File::PATH_SEPARATOR, ":"].uniq.join("").freeze
|
112
|
+
|
113
|
+
class << (RANDOM = Random.new)
|
114
|
+
MAX = 36**6 # < 0x100000000
|
115
|
+
def next
|
116
|
+
rand(MAX).to_s(36)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
private_constant :RANDOM
|
120
|
+
|
121
|
+
def create(basename, tmpdir=nil, max_try: nil, **opts)
|
122
|
+
origdir = tmpdir
|
123
|
+
tmpdir ||= tmpdir()
|
124
|
+
n = nil
|
125
|
+
prefix, suffix = basename
|
126
|
+
prefix = (String.try_convert(prefix) or
|
127
|
+
raise ArgumentError, "unexpected prefix: #{prefix.inspect}")
|
128
|
+
prefix = prefix.delete(UNUSABLE_CHARS)
|
129
|
+
suffix &&= (String.try_convert(suffix) or
|
130
|
+
raise ArgumentError, "unexpected suffix: #{suffix.inspect}")
|
131
|
+
suffix &&= suffix.delete(UNUSABLE_CHARS)
|
132
|
+
begin
|
133
|
+
t = Time.now.strftime("%Y%m%d")
|
134
|
+
path = "#{prefix}#{t}-#{$$}-#{RANDOM.next}"\
|
135
|
+
"#{n ? %[-#{n}] : ''}#{suffix||''}"
|
136
|
+
path = File.join(tmpdir, path)
|
137
|
+
yield(path, n, opts, origdir)
|
138
|
+
rescue Errno::EEXIST
|
139
|
+
n ||= 0
|
140
|
+
n += 1
|
141
|
+
retry if !max_try or n < max_try
|
142
|
+
raise "cannot generate temporary name using `#{basename}' under `#{tmpdir}'"
|
143
|
+
end
|
144
|
+
path
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
data/tmpdir.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "tmpdir"
|
3
|
+
spec.version = "0.1.0"
|
4
|
+
spec.authors = ["Yukihiro Matsumoto"]
|
5
|
+
spec.email = ["matz@ruby-lang.org"]
|
6
|
+
|
7
|
+
spec.summary = %q{Extends the Dir class to manage the OS temporary file path.}
|
8
|
+
spec.description = %q{Extends the Dir class to manage the OS temporary file path.}
|
9
|
+
spec.homepage = "https://github.com/ruby/tmpdir"
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
11
|
+
|
12
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
13
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tmpdir
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yukihiro Matsumoto
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Extends the Dir class to manage the OS temporary file path.
|
14
|
+
email:
|
15
|
+
- matz@ruby-lang.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".github/workflows/test.yml"
|
21
|
+
- ".gitignore"
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- lib/tmpdir.rb
|
29
|
+
- tmpdir.gemspec
|
30
|
+
homepage: https://github.com/ruby/tmpdir
|
31
|
+
licenses: []
|
32
|
+
metadata:
|
33
|
+
homepage_uri: https://github.com/ruby/tmpdir
|
34
|
+
source_code_uri: https://github.com/ruby/tmpdir
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.3.0
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubygems_version: 3.2.0.pre1
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: Extends the Dir class to manage the OS temporary file path.
|
54
|
+
test_files: []
|