lib/dbox/db.rb in dbox-0.4.3 vs lib/dbox/db.rb in dbox-0.4.4

- old
+ new

@@ -1,13 +1,15 @@ module Dbox class MissingDatabase < RuntimeError; end + class CorruptDatabase < RuntimeError; end class BadPath < RuntimeError; end class DB include Loggable DB_FILE = ".dropbox.db" + DB_TMPFILE = ".dropbox.db.tmp" attr_accessor :local_path def self.create(remote_path, local_path) api.create_dir(remote_path) @@ -36,13 +38,23 @@ def self.exists?(local_path) File.exists?(db_file(local_path)) end + def self.corrupt?(local_path) + begin + load(local_path) + false + rescue CorruptDatabase + true + end + end + def self.load(local_path) if exists?(local_path) db = File.open(db_file(local_path), "r") {|f| YAML::load(f.read) } + raise CorruptDatabase unless db && db.kind_of?(DB) db.local_path = local_path db else raise MissingDatabase, "No DB file found in #{local_path}" end @@ -58,11 +70,12 @@ save end def save self.class.saving_timestamp(@local_path) do - File.open(db_file, "w") {|f| f << YAML::dump(self) } + File.open(db_tmpfile, "w") {|f| f << YAML::dump(self) } + FileUtils.mv(db_tmpfile, db_file) end end def pull @root.pull @@ -93,19 +106,19 @@ raise BadPath, "Not a remote path: #{path}" end end def relative_to_local_path(path) - if path.any? + if path && path.length > 0 File.join(@local_path, path) else @local_path end end def relative_to_remote_path(path) - if path.any? + if path && path.length > 0 File.join(@remote_path, path) else @remote_path end end @@ -126,11 +139,19 @@ def self.db_file(local_path) File.join(local_path, DB_FILE) end + def self.db_tmpfile(local_path) + File.join(local_path, DB_TMPFILE) + end + def db_file self.class.db_file(@local_path) + end + + def db_tmpfile + self.class.db_tmpfile(@local_path) end class DropboxBlob include Loggable