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:
parent
22676a8b79
commit
812a02d60e
1 changed files with 9 additions and 3 deletions
|
@ -77,9 +77,9 @@ class MPW
|
|||
end
|
||||
|
||||
# Encrypt a file
|
||||
# TODO backup and restore file with raise
|
||||
def write_data
|
||||
data = {}
|
||||
data = {}
|
||||
tmp_file = "#{@wallet_file}.tmp"
|
||||
|
||||
@data.each do |item|
|
||||
next if item.empty?
|
||||
|
@ -98,7 +98,7 @@ class MPW
|
|||
)
|
||||
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))
|
||||
tar.add_file_simple('wallet/meta.gpg', 0400, data_encrypt.length) do |io|
|
||||
io.write(data_encrypt)
|
||||
|
@ -116,6 +116,12 @@ class MPW
|
|||
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
|
||||
|
||||
# Get a password
|
||||
|
|
Loading…
Reference in a new issue