1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-27 07:33:05 +00:00

add backup when write data

This commit is contained in:
nishiki 2016-05-07 14:53:37 +02:00
parent 22676a8b79
commit 812a02d60e

View file

@ -77,9 +77,9 @@ class MPW
end end
# Encrypt a file # Encrypt a file
# TODO backup and restore file with raise
def write_data def write_data
data = {} data = {}
tmp_file = "#{@wallet_file}.tmp"
@data.each do |item| @data.each do |item|
next if item.empty? next if item.empty?
@ -98,7 +98,7 @@ class MPW
) )
end end
Gem::Package::TarWriter.new(File.open(@wallet_file, 'w+')) do |tar| Gem::Package::TarWriter.new(File.open(tmp_file, 'w+')) do |tar|
data_encrypt = encrypt(YAML::dump(data)) data_encrypt = encrypt(YAML::dump(data))
tar.add_file_simple('wallet/meta.gpg', 0400, data_encrypt.length) do |io| tar.add_file_simple('wallet/meta.gpg', 0400, data_encrypt.length) do |io|
io.write(data_encrypt) io.write(data_encrypt)
@ -116,6 +116,12 @@ class MPW
end end
end end
end end
File.rename(tmp_file, @wallet_file)
rescue Exception => e
File.unlink(tmp_file)
raise "#{I18n.t('error.mpw_file.write_data')}\n#{e}"
end end
# Get a password # Get a password