* 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/trunk@10895 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2006-09-08 19:56:37 +00:00
parent b000ee600f
commit fbe8d44d5c
2 changed files with 15 additions and 11 deletions

View File

@ -1,3 +1,8 @@
Sat Sep 9 04:55:59 2006 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/pstore.rb: open all in binary mode, and get rid of the quirk of
msvcrt. fixed: [ruby-dev:29518]
Sat Sep 9 04:47:45 2006 Nobuyoshi Nakada <nobu@ruby-lang.org> Sat Sep 9 04:47:45 2006 Nobuyoshi Nakada <nobu@ruby-lang.org>
* Makefile.in, win32/Makefile.sub (MINIRUBY): append MINIRUBYOPT. * Makefile.in, win32/Makefile.sub (MINIRUBY): append MINIRUBYOPT.

View File

@ -78,6 +78,11 @@ require "digest/md5"
# end # end
# #
class PStore 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. # The error type thrown by all PStore methods.
class Error < StandardError class Error < StandardError
end end
@ -287,17 +292,15 @@ class PStore
content = nil content = nil
unless read_only unless read_only
file = File.open(@filename, File::RDWR | File::CREAT) file = File.open(@filename, RDWR_ACCESS)
file.binmode
file.flock(File::LOCK_EX) file.flock(File::LOCK_EX)
commit_new(file) if FileTest.exist?(new_file) commit_new(file) if FileTest.exist?(new_file)
content = file.read() content = file.read()
else else
begin begin
file = File.open(@filename, File::RDONLY) file = File.open(@filename, RD_ACCESS)
file.binmode
file.flock(File::LOCK_SH) 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 rescue Errno::ENOENT
content = "" content = ""
end end
@ -326,10 +329,7 @@ class PStore
tmp_file = @filename + ".tmp" tmp_file = @filename + ".tmp"
content = dump(@table) content = dump(@table)
if !md5 || size != content.size || md5 != Digest::MD5.digest(content) if !md5 || size != content.size || md5 != Digest::MD5.digest(content)
File.open(tmp_file, "w") {|t| File.open(tmp_file, WR_ACCESS) {|t| t.write(content)}
t.binmode
t.write(content)
}
File.rename(tmp_file, new_file) File.rename(tmp_file, new_file)
commit_new(file) commit_new(file)
end end
@ -365,8 +365,7 @@ class PStore
f.truncate(0) f.truncate(0)
f.rewind f.rewind
new_file = @filename + ".new" new_file = @filename + ".new"
File.open(new_file) do |nf| File.open(new_file, RD_ACCESS) do |nf|
nf.binmode
FileUtils.copy_stream(nf, f) FileUtils.copy_stream(nf, f)
end end
File.unlink(new_file) File.unlink(new_file)