1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

tempfile.rb modified

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@50 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 1998-01-23 10:39:02 +00:00
parent db34086b5b
commit 30ce83d0ab
3 changed files with 28 additions and 21 deletions

View file

@ -41,4 +41,7 @@ class SimpleDelegater<Delegater
@obj @obj
end end
def __setobj__(obj)
@obj = obj
end
end end

View file

@ -1,12 +1,12 @@
# #
# $Id$ # $Id$
# Copyright (C) 1998 akira yamada. All rights reserved. #
# This file can be distributed under the terms of the Ruby.
# The class for temporary files. # The class for temporary files.
# o creates a temporary file, which name is "basename.pid.n" with mode "w+". # o creates a temporary file, which name is "basename.pid.n" with mode "w+".
# o Tempfile objects can be used like IO object. # o Tempfile objects can be used like IO object.
# o created temporary files are removed on closing or script termination. # o with tmpfile.close(true) created temporary files are removed.
# o created files are also removed on script termination.
# o with Tempfile#open, you can reopen the temporary file.
# o file mode of the temporary files are 0600. # o file mode of the temporary files are 0600.
require 'delegate' require 'delegate'
@ -16,18 +16,14 @@ class Tempfile < SimpleDelegater
Max_try = 10 Max_try = 10
def initialize(basename, tmpdir = '/tmp') def initialize(basename, tmpdir = '/tmp')
@tmpdir = tmpdir
umask = File.umask(0177) umask = File.umask(0177)
cwd = Dir.getwd
Dir.chdir(@tmpdir)
begin begin
n = 0 n = 0
while true while true
begin begin
@tmpname = sprintf('%s.%d.%d', basename, $$, n) @tmpname = sprintf('%s/%s.%d.%d', tmpdir, basename, $$, n)
unless File.exist?(@tmpname) unless File.exist?(@tmpname)
File.symlink('.', @tmpname + '.lock') File.symlink(tmpdir, @tmpname + '.lock')
break break
end end
rescue rescue
@ -38,21 +34,20 @@ class Tempfile < SimpleDelegater
end end
@clean_files = proc {|id| @clean_files = proc {|id|
if File.exist?(@tmpdir + '/' + @tmpname) if File.exist?(@tmpname)
File.unlink(@tmpdir + '/' + @tmpname) File.unlink(@tmpname)
end end
if File.exist?(@tmpdir + '/' + @tmpname + '.lock') if File.exist?(@tmpname + '.lock')
File.unlink(@tmpdir + '/' + @tmpname + '.lock') File.unlink(@tmpname + '.lock')
end end
} }
ObjectSpace.define_finalizer(self, @clean_files) ObjectSpace.define_finalizer(self, @clean_files)
@tmpfile = open(@tmpname, 'w+') @tmpfile = File.open(@tmpname, 'w+')
super(@tmpfile) super(@tmpfile)
File.unlink(@tmpname + '.lock') File.unlink(@tmpname + '.lock')
ensure ensure
File.umask(umask) File.umask(umask)
Dir.chdir(cwd)
end end
end end
@ -60,9 +55,18 @@ class Tempfile < SimpleDelegater
Tempfile.new(*args) Tempfile.new(*args)
end end
def close def open
@tmpfile.close @tmpfile.close if @tmpfile
@tmpfile = File.open(@tmpname, 'r+')
__setobj__(@tmpfile)
end
def close(real=false)
@tmpfile.close if @tmpfile
@tmpfile = nil
if real
@clean_files.call @clean_files.call
ObjectSpace.undefine_finalizer(self) ObjectSpace.undefine_finalizer(self)
end end
end
end end

View file

@ -632,7 +632,7 @@ An end of a defun is found by moving forward from the beginning of one."
'("\\(^\\|[^_]\\)\\b\\([A-Z]+[a-zA-Z0-9_]*\\)" '("\\(^\\|[^_]\\)\\b\\([A-Z]+[a-zA-Z0-9_]*\\)"
2 font-lock-type-face) 2 font-lock-type-face)
;; functions ;; functions
'("^\\s *def[ \t]+.*$" '("^\\s *def[ \t]+[^ \t(]*"
0 font-lock-function-name-face t)) 0 font-lock-function-name-face t))
"*Additional expressions to highlight in ruby mode.") "*Additional expressions to highlight in ruby mode.")
(if (and (>= (string-to-int emacs-version) 20) (if (and (>= (string-to-int emacs-version) 20)