Skip to content

Instantly share code, notes, and snippets.

@theaboutbox
Created September 12, 2011 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theaboutbox/1211614 to your computer and use it in GitHub Desktop.
Save theaboutbox/1211614 to your computer and use it in GitHub Desktop.
PStore File Encoding Issue
require 'pstore'
# Some code (like Rails) sets this so that strings are represented in Unicode
Encoding.default_internal = 'UTF-8'
# But if we do that, PStore randomly blows up
begin
print "Trying to store some values with PStore"
store = PStore.new('test.pstore')
(1..1000).each do |i|
store.transaction {
store["Key#{i}"] = "value #{i}"
print '.'
}
end
puts "Success!"
rescue => err
puts "O NOES! Received error: #{err.to_s}"
end
# Why is that?
file = File.open("file_test", File::CREAT | File::RDWR | File::BINARY) do |f|
puts "Using IO Constants, File Encoding: #{f.external_encoding.to_s}"
# UTF-8? But...I told Ruby this was a BINARY file...what's going on?
end
# But if I do it this way
file = File.open("file_test", "w+b") do |f|
puts "Using String mode, File Encoding: #{f.external_encoding.to_s}"
# Yeah, ASCII-8BIT - that's more like it
end
# So if we monkey-patch PStore thusly:
class PStore
def open_and_lock_file(filename, read_only)
if read_only
begin
file = File.new(filename, 'rb')
begin
file.flock(File::LOCK_SH)
return file
rescue
file.close
raise
end
rescue Errno::ENOENT
return nil
end
else
file = File.new(filename, 'w+b')
file.flock(File::LOCK_EX)
return file
end
end
end
# And if we try our little PStore test again:
begin
print "Trying to store some values with PStore"
store = PStore.new('test2.pstore')
(1..1000).each do |i|
store.transaction {
store["Key#{i}"] = "value #{i}"
print '.'
}
end
puts "Success!"
rescue => err
puts "O NOES! Received error: #{err.to_s}"
end
# Everything works!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment