GithubHelp home page GithubHelp logo

tmpdir's Introduction

tmpdir

retrieve temporary directory path

Installation

Add this line to your application's Gemfile:

gem 'tmpdir'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install tmpdir

Usage

Dir.mktmpdir creates a temporary directory. The directory is created with 0700 permission.

Application should not change the permission to make the temporary directory accessible from other users.

The prefix and suffix of the name of the directory is specified by the optional first argument, prefix_suffix.

  • If it is not specified or nil, "d" is used as the prefix and no suffix is used.
  • If it is a string, it is used as the prefix and no suffix is used.
  • If it is an array, first element is used as the prefix and second element is used as a suffix.
Dir.mktmpdir {|dir| dir is ".../d..." }
Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
Dir.mktmpdir(["foo", "bar"]) {|dir| dir is ".../foo...bar" }

The directory is created under Dir.tmpdir or the optional second argument tmpdir if non-nil value is given.

Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }

Development

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.

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/tmpdir.

tmpdir's People

Contributors

adam12 avatar akr avatar dependabot[bot] avatar drbrain avatar eban avatar hsbt avatar jeremyevans avatar k-tsj avatar k0kubun avatar ko1 avatar mame avatar nobu avatar nurse avatar pvdb avatar unak avatar znz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tmpdir's Issues

Warn when value from environment is being ignored by `Dir.tmpdir`

Currently, if you set ENV["TMPDIR"] to a value that doesn't meet Dir.tmpdir criteria to become the temporary folder, the environment variable is silently ignored and the next variable in the chain is checked.

This makes issues quite hard to debug, because the temporary folder can get silently set to a value you don't expect.

An example of this is: rubygems/rubygems#3649.

And I believe current failures in ruby/ruby#3211 might be caused by the same issue.

Would it be possible to replace the following "silent rescue" to introduce a warning when it's hit?

end rescue nil

I'm happy to create a PR if this is accepted.

unexpected behaviour when environment variable is set but empty ... bug or feature?

I noticed some unexpected behaviour when one of the environment variables considered by Dir.tmpdir (ie. ENV["TEMPDIR"], ENV["TMP"] and ENV["TEMP"]) is set but empty.

Some examples will hopefully clarify what I mean! ๐Ÿ˜…

For reasons that will become clear, it's worth noting that they are all executed in the following directory, into which I cloned the ruby/tmpdir repository:

$ pwd
/usr/local/src/ruby/tmpdir
$ _

This first example makes sense, as it is the expected behaviour:

$ TMPDIR=/tmp ruby -I lib -r tmpdir -e 'puts Dir.tmpdir'
/tmp
$ _

... but the behaviour in this second example is quite unexpected:

$ TMPDIR= TMP=/tmp ruby -I lib -r tmpdir -e 'puts Dir.tmpdir'
/usr/local/src/ruby/tmpdir
$ _

... as instead of returning the /tmp directory (from ENV["TMP"]) it returns the current working directory instead. ๐Ÿค”

From going through the source code, it's easy to understand why it returns the working directory (see footnote below), but it is unclear (from reading the documentation and from looking at the tests) if this Dir.tmpdir behaviour is a bug or a feature; either way it is rather unexpected, even if it can be explained by the current implementation. ๐Ÿ˜ƒ

Note that it is only an issue when the environment variable is set to an empty string; when the environment variable is not set at all, as in this third example, the behaviour is again as expected:

$ ( unset TMPDIR ; TMP=/tmp ruby -I lib -r tmpdir -e 'puts Dir.tmpdir' )
/tmp
$ _

Also note that I have been using two environment variables in my examples to prove the point, even though that's not very likely to happen IRL; but the unexpected behaviour also manifests itself in other scenarios:

$ TMPDIR= ruby -I lib -r tmpdir -e 'puts Dir.tmpdir'
/usr/local/src/ruby/tmpdir
$ _

Again it is returning the current working directory, even though it would make more sense for it to return Etc.systmpdir in this case, which on my system would be:

$ ruby -r etc -e 'puts Etc.systmpdir'
/var/folders/fc/_sw268z16wgf3pq66_szc44w0000gp/T/
$ _

Assuming for argument's sake that this behaviour is a bug, then it's easy enough to fix:

$ git diff -- lib/tmpdir.rb
diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb
index 3b67164..ba8b6dc 100644
--- a/lib/tmpdir.rb
+++ b/lib/tmpdir.rb
@@ -21,7 +21,7 @@ class Dir
   def self.tmpdir
     tmp = nil
     ['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].each do |name, dir = ENV[name]|
-      next if !dir
+      next if !dir || dir.empty?
       dir = File.expand_path(dir)
       stat = File.stat(dir) rescue next
       case
$ _

... after which the examples from above behave as expected:

$ TMPDIR= TMP=/tmp ruby -I lib -r tmpdir -e 'puts Dir.tmpdir'
/tmp
$ _

... as well as:

$ TMPDIR= ruby -I lib -r tmpdir -e 'puts Dir.tmpdir'
/var/folders/fc/_sw268z16wgf3pq66_szc44w0000gp/T
$ _

If this behaviour is indeed deemed to be a bug in the current implementation, then I am more than happy to put together a proper PR for the above fix (including relevant tests) but I thought I'd submit it as an issue for discussion first.


Footnote:

['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].each do |name, dir = ENV[name]|
  next if !dir
  dir = File.expand_path(dir)
  ...
end

Because ENV["TMPDIR"] is the empty string ("") it passes the nil-check and File.expand_path("") in turn returns the current working directory of the process:

$ TMPDIR= ruby -e 'puts File.expand_path(ENV["TMPDIR"])'
/usr/local/src/ruby/tmpdir
$ _

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.