Skip to content

Instantly share code, notes, and snippets.

@doxavore
Created November 2, 2012 15:37
Show Gist options
  • Save doxavore/4002071 to your computer and use it in GitHub Desktop.
Save doxavore/4002071 to your computer and use it in GitHub Desktop.
JRuby Dir.tmpdir
def Dir::tmpdir
tmp = '.'
if $SAFE > 0
tmp = @@systmpdir
else
# Search a directory which isn't world-writable first. In JRuby,
# FileUtils.remove_entry_secure(dir) crashes when a dir is under
# a world-writable directory because it tries to open directory.
# Opening directory is not allowed in Java.
dirs = [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', tmp]
for dir in dirs
if dir and stat = File.stat(dir) and stat.directory? and stat.writable? and !stat.world_writable?
return File.expand_path(dir)
end rescue nil
end
for dir in dirs
if dir and stat = File.stat(dir) and stat.directory? and stat.writable?
tmp = dir
break
end rescue nil
end
File.expand_path(tmp)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment