Allow creating a new file in PasswordFile::save()

to ease saving a file under a different location
This commit is contained in:
Martchus 2019-07-03 23:59:07 +02:00
parent b6ad06faa2
commit a1b694a50b
2 changed files with 11 additions and 2 deletions

View File

@ -405,7 +405,15 @@ void PasswordFile::save(PasswordFileSaveFlags options)
if (m_file.is_open()) {
m_file.close();
}
m_file.open(m_path, ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary);
try {
m_file.open(m_path, ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary);
} catch (const ios_base::failure &) {
// try to create a new file if configured via \a options
if (!(options & PasswordFileSaveFlags::AllowToCreateNewFile)) {
throw;
}
m_file.open(m_path, ios_base::out | ios_base::trunc | ios_base::binary);
}
}
write(options);

View File

@ -48,7 +48,8 @@ enum class PasswordFileSaveFlags : std::uint64_t {
Encryption = 1,
Compression = 2,
PasswordHashing = 4,
Default = Encryption | Compression | PasswordHashing,
AllowToCreateNewFile = 8,
Default = Encryption | Compression | PasswordHashing | AllowToCreateNewFile,
};
std::string PASSWORD_FILE_EXPORT flagsToString(PasswordFileSaveFlags flags);