summaryrefslogtreecommitdiff
path: root/lib/pstore.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-09-08 19:56:37 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-09-08 19:56:37 +0000
commit74af63a39021bfbcae3c96605af9b9d542a6d517 (patch)
tree6f61eecb7f261e8bd9534a7c5a470370fdbad44f /lib/pstore.rb
parent12491fb96660a451a3a55b5b9a7cc9b024bcadf3 (diff)
* lib/pstore.rb: open all in binary mode, and get rid of the quirk of
msvcrt. fixed: [ruby-dev:29518] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10895 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/pstore.rb')
-rw-r--r--lib/pstore.rb21
1 files changed, 10 insertions, 11 deletions
diff --git a/lib/pstore.rb b/lib/pstore.rb
index 46123bf..6df6447 100644
--- a/lib/pstore.rb
+++ b/lib/pstore.rb
@@ -78,6 +78,11 @@ require "digest/md5"
# end
#
class PStore
+ binmode = defined?(File::BINARY) ? File::BINARY : 0
+ RDWR_ACCESS = File::RDWR | File::CREAT | binmode
+ RD_ACCESS = File::RDONLY | binmode
+ WR_ACCESS = File::WRONLY | File::CREAT | File::TRUNC | binmode
+
# The error type thrown by all PStore methods.
class Error < StandardError
end
@@ -287,17 +292,15 @@ class PStore
content = nil
unless read_only
- file = File.open(@filename, File::RDWR | File::CREAT)
- file.binmode
+ file = File.open(@filename, RDWR_ACCESS)
file.flock(File::LOCK_EX)
commit_new(file) if FileTest.exist?(new_file)
content = file.read()
else
begin
- file = File.open(@filename, File::RDONLY)
- file.binmode
+ file = File.open(@filename, RD_ACCESS)
file.flock(File::LOCK_SH)
- content = (File.read(new_file) rescue file.read())
+ content = (File.open(new_file, RD_ACCESS) {|n| n.read} rescue file.read())
rescue Errno::ENOENT
content = ""
end
@@ -326,10 +329,7 @@ class PStore
tmp_file = @filename + ".tmp"
content = dump(@table)
if !md5 || size != content.size || md5 != Digest::MD5.digest(content)
- File.open(tmp_file, "w") {|t|
- t.binmode
- t.write(content)
- }
+ File.open(tmp_file, WR_ACCESS) {|t| t.write(content)}
File.rename(tmp_file, new_file)
commit_new(file)
end
@@ -365,8 +365,7 @@ class PStore
f.truncate(0)
f.rewind
new_file = @filename + ".new"
- File.open(new_file) do |nf|
- nf.binmode
+ File.open(new_file, RD_ACCESS) do |nf|
FileUtils.copy_stream(nf, f)
end
File.unlink(new_file)