diff --git lib/pstore.rb lib/pstore.rb index 9fb0249..cc9e70d 100644 --- lib/pstore.rb +++ lib/pstore.rb @@ -9,7 +9,7 @@ require "fileutils" -require "digest/md5" +require "zlib" require "thread" # @@ -349,7 +349,7 @@ class PStore # Constant for relieving Ruby's garbage collector. EMPTY_STRING = "" EMPTY_MARSHAL_DATA = Marshal.dump({}) - EMPTY_MARSHAL_CHECKSUM = Digest::MD5.digest(EMPTY_MARSHAL_DATA) + EMPTY_MARSHAL_CHECKSUM = Zlib.crc32(EMPTY_MARSHAL_DATA) # # Open the specified filename (either in read-only mode or in @@ -406,7 +406,7 @@ class PStore size = empty_marshal_data.size else table = load(data) - checksum = Digest::MD5.digest(data) + checksum = Zlib.crc32(data) size = data.size if !table.is_a?(Hash) raise Error, "PStore file seems to be corrupted." @@ -428,33 +428,12 @@ class PStore is_windows end - # Check whether Marshal.dump supports the 'canonical' option. This option - # makes sure that Marshal.dump always dumps data structures in the same order. - # This is important because otherwise, the checksums that we generate may differ. - def marshal_dump_supports_canonical_option? - begin - Marshal.dump(nil, -1, true) - result = true - rescue - result = false - end - self.class.__send__(:define_method, :marshal_dump_supports_canonical_option?) do - result - end - result - end - def save_data(original_checksum, original_file_size, file) # We only want to save the new data if the size or checksum has changed. # This results in less filesystem calls, which is good for performance. - if marshal_dump_supports_canonical_option? - new_data = Marshal.dump(@table, -1, true) - else - new_data = dump(@table) - end - new_checksum = Digest::MD5.digest(new_data) + new_data = dump(@table) - if new_data.size != original_file_size || new_checksum != original_checksum + if new_data.size != original_file_size || Zlib.crc32(new_data) != original_checksum if @ultra_safe && !on_windows? # Windows doesn't support atomic file renames. save_data_with_atomic_file_rename_strategy(new_data, file) @@ -484,11 +463,10 @@ class PStore def save_data_with_fast_strategy(data, file) file.rewind - file.truncate(0) file.write(data) + file.truncate(data.size) end - # This method is just a wrapped around Marshal.dump # to allow subclass overriding used in YAML::Store. def dump(table) # :nodoc: @@ -511,7 +489,7 @@ end # :enddoc: -if __FILE__ == $0 +if __FILE__ == $PROGRAM_NAME db = PStore.new("/tmp/foo") db.transaction do p db.roots