summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/cgi/session.rb17
-rw-r--r--lib/date.rb5
-rw-r--r--lib/e2mmap.rb3
-rw-r--r--lib/getopts.rb4
-rw-r--r--lib/mathn.rb6
-rw-r--r--lib/mutex_m.rb3
-rw-r--r--lib/pstore.rb46
-rw-r--r--lib/singleton.rb2
-rw-r--r--lib/sync.rb3
-rw-r--r--lib/tempfile.rb49
10 files changed, 80 insertions, 58 deletions
diff --git a/lib/cgi/session.rb b/lib/cgi/session.rb
index 1120fb50f0..1a3379b88a 100644
--- a/lib/cgi/session.rb
+++ b/lib/cgi/session.rb
@@ -96,10 +96,19 @@ class CGI
end
class FileStore
+ def check_id(id)
+ /[^0-9a-zA-Z]/ =~ id.to_s ? false : true
+ end
+ module_function :check_id
+
def initialize(session, option={})
dir = option['tmpdir'] || ENV['TMP'] || '/tmp'
prefix = option['prefix'] || ''
- path = dir+"/"+prefix+session.session_id
+ id = session.session_id
+ unless check_id(id)
+ raise ArgumentError, "session_id `%s' is invalid" % id
+ end
+ path = dir+"/"+prefix+id
path.untaint
unless File::exist? path
@hash = {}
@@ -149,9 +158,9 @@ class CGI
class MemoryStore
GLOBAL_HASH_TABLE = {}
- def initialize(session, option={})
+ def initialize(session, option=nil)
@session_id = session.session_id
- GLOBAL_HASH_TABLE[@session_id] = {}
+ GLOBAL_HASH_TABLE[@session_id] ||= {}
end
def restore
@@ -167,7 +176,7 @@ class CGI
end
def delete
- GLOBAL_HASH_TABLE[@session_id] = nil
+ GLOBAL_HASH_TABLE.delete(@session_id)
end
end
end
diff --git a/lib/date.rb b/lib/date.rb
index 3422121298..cd0b997179 100644
--- a/lib/date.rb
+++ b/lib/date.rb
@@ -202,7 +202,10 @@ class Date
alias_method :__#{id.to_i}__, :#{id.id2name}
private :__#{id.to_i}__
def #{id.id2name}(*args, &block)
- (@__#{id.to_i}__ ||= [__#{id.to_i}__(*args, &block)])[0]
+ unless defined? @__#{id.to_i}__
+ @__#{id.to_i}__ = __#{id.to_i}__(*args, &block)
+ end
+ @__#{id.to_i}__
end
end;
end
diff --git a/lib/e2mmap.rb b/lib/e2mmap.rb
index 2ae934578d..6739c42518 100644
--- a/lib/e2mmap.rb
+++ b/lib/e2mmap.rb
@@ -73,8 +73,7 @@ module Exception2MessageMapper
end
alias Fail Raise
- def self.append_features(mod)
- super
+ def self.included(mod)
mod.extend Exception2MessageMapper
end
]
diff --git a/lib/getopts.rb b/lib/getopts.rb
index 490523b878..8a5917e794 100644
--- a/lib/getopts.rb
+++ b/lib/getopts.rb
@@ -77,8 +77,8 @@ end
elsif boolopts.key? opt then # ruby --verbose
boolopts[ opt ] = true
else
- return nil
- end
+ return nil
+ end
c += 1
when /\A-(.+)/
diff --git a/lib/mathn.rb b/lib/mathn.rb
index 8d92272159..f3f7a95a48 100644
--- a/lib/mathn.rb
+++ b/lib/mathn.rb
@@ -122,7 +122,11 @@ end
class Rational
Unify = true
-
+
+ def inspect
+ format "%s/%s", @numerator.inspect, @denominator.inspect
+ end
+
alias power! **
def ** (other)
diff --git a/lib/mutex_m.rb b/lib/mutex_m.rb
index e0fcf0f209..1a3cd05856 100644
--- a/lib/mutex_m.rb
+++ b/lib/mutex_m.rb
@@ -16,8 +16,7 @@
#
module Mutex_m
- def Mutex_m.append_features(cl)
- super
+ def Mutex_m.included(cl)
unless cl.instance_of?(Module)
cl.module_eval %q{
alias locked? mu_locked?
diff --git a/lib/pstore.rb b/lib/pstore.rb
index b3e1df8284..d74d712a56 100644
--- a/lib/pstore.rb
+++ b/lib/pstore.rb
@@ -13,6 +13,7 @@
# end
require "ftools"
+require "md5"
class PStore
class Error < StandardError
@@ -41,11 +42,10 @@ class PStore
def [](name)
in_transaction
- value = @table[name]
- if value == nil
+ unless @table.key? name
raise PStore::Error, format("undefined root name `%s'", name)
end
- value
+ @table[name]
end
def []=(name, value)
in_transaction
@@ -69,10 +69,12 @@ class PStore
end
def commit
+ in_transaction
@abort = false
throw :pstore_abort_transaction
end
def abort
+ in_transaction
@abort = true
throw :pstore_abort_transaction
end
@@ -83,18 +85,21 @@ class PStore
@transaction = true
value = nil
backup = @filename+"~"
- if File::exist?(@filename)
+ begin
file = File::open(@filename, "r+")
orig = true
- else
- @table = {}
+ rescue Errno::ENOENT
file = File::open(@filename, "w+")
- Marshal::dump(@table, file)
end
file.flock(File::LOCK_EX)
if orig
- File::copy @filename, backup
- @table = Marshal::load(file)
+ content = file.read
+ @table = Marshal::load(content)
+ size = content.size
+ md5 = MD5.new(content).digest
+ content = nil # unreference huge data
+ else
+ @table = {}
end
begin
catch(:pstore_abort_transaction) do
@@ -105,13 +110,18 @@ class PStore
raise
ensure
unless @abort
- begin
- file.rewind
- Marshal::dump(@table, file)
- file.truncate(file.pos)
- rescue
- File::rename backup, @filename if File::exist?(backup)
- raise
+ file.rewind
+ content = Marshal::dump(@table)
+ if !md5 || size != content.size || md5 != MD5.new(content).digest
+ File::copy @filename, backup
+ begin
+ file.write(content)
+ file.truncate(file.pos)
+ content = nil # unreference huge data
+ rescue
+ File::rename backup, @filename if File::exist?(backup)
+ raise
+ end
end
end
@abort = false
@@ -139,4 +149,8 @@ if __FILE__ == $0
p db["root"][0]
end
end
+
+ db.transaction do
+ p db["root"]
+ end
end
diff --git a/lib/singleton.rb b/lib/singleton.rb
index aa245b32b2..f5c2d8346b 100644
--- a/lib/singleton.rb
+++ b/lib/singleton.rb
@@ -11,7 +11,7 @@
# a = SomeSingletonClass.new # error (`new' is private)
module Singleton
- def Singleton.append_features(klass)
+ def Singleton.included(klass)
klass.private_class_method(:new)
klass.instance_eval %{
@__instance__ = nil
diff --git a/lib/sync.rb b/lib/sync.rb
index f1410af1a9..6ddd837dad 100644
--- a/lib/sync.rb
+++ b/lib/sync.rb
@@ -76,8 +76,7 @@ module Sync_m
end
end
- def Sync_m.append_features(cl)
- super
+ def Sync_m.included(cl)
unless cl.instance_of?(Module)
# do nothing for Modules
# make aliases and include the proper module.
diff --git a/lib/tempfile.rb b/lib/tempfile.rb
index 0b22de260a..d4876f9c61 100644
--- a/lib/tempfile.rb
+++ b/lib/tempfile.rb
@@ -4,7 +4,7 @@
# The class for temporary files.
# o creates a temporary file, which name is "basename.pid.n" with mode "w+".
# o Tempfile objects can be used like IO object.
-# o with tmpfile.close(true) created temporary files are removed.
+# o with tempfile.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.
@@ -35,36 +35,31 @@ class Tempfile < SimpleDelegator
if $SAFE > 0 and tmpdir.tainted?
tmpdir = '/tmp'
end
- umask = File.umask(0177)
- begin
- n = 0
- while true
- begin
- tmpname = sprintf('%s/%s%d.%d', tmpdir, basename, $$, n)
- lock = tmpname + '.lock'
- unless File.exist?(tmpname) or File.exist?(lock)
- Dir.mkdir(lock)
- break
- end
- rescue
- raise "cannot generate tmpfile `%s'" % tmpname if n >= Max_try
- #sleep(1)
+ n = 0
+ while true
+ begin
+ tmpname = sprintf('%s/%s%d.%d', tmpdir, basename, $$, n)
+ lock = tmpname + '.lock'
+ unless File.exist?(tmpname) or File.exist?(lock)
+ Dir.mkdir(lock)
+ break
end
- n += 1
+ rescue
+ raise "cannot generate tempfile `%s'" % tmpname if n >= Max_try
+ #sleep(1)
end
+ n += 1
+ end
- @protect = []
- @clean_files = Tempfile.callback(tmpname, @protect)
- ObjectSpace.define_finalizer(self, @clean_files)
+ @protect = []
+ @clean_files = Tempfile.callback(tmpname, @protect)
+ ObjectSpace.define_finalizer(self, @clean_files)
- @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL)
- @protect[0] = @tmpfile
- @tmpname = tmpname
- super(@tmpfile)
- Dir.rmdir(lock)
- ensure
- File.umask(umask)
- end
+ @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL)
+ @protect[0] = @tmpfile
+ @tmpname = tmpname
+ super(@tmpfile)
+ Dir.rmdir(lock)
end
def Tempfile.open(*args)