Skip to content

Commit

Permalink
Fix a couple issues with saving files in the snap / flatpak packages
Browse files Browse the repository at this point in the history
- Copy the temp file instead of renaming, which doesn't work when the temp folder is on a different volume
- Use the application's data folder for the backup file, which is more reliable (we also use this for auto backup) than the temp folder

Fixes: #498
  • Loading branch information
cameronwhite committed Nov 19, 2024
1 parent 33935d8 commit eaa9425
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Thanks to the following contributors who worked on this release:

### Fixed
- Fixed AppStream validation issues in `xdg/powertabeditor.metainfo.xml`
- Fixed a bug which could prevent barlines from being deleted
- Fixed a bug which could prevent barlines from being deleted (#496)
- Fixed errors with saving files in the Snap or Flatpak packages (#498)

## [2.0.20] - 2024-11-03

Expand Down
2 changes: 1 addition & 1 deletion source/app/powertabeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ bool PowerTabEditor::saveFile(int doc_index, QString path)

try
{
myFileFormatManager->exportFile(doc.getScore(), path_str, *format);
myFileFormatManager->exportFile(doc.getScore(), path_str, Paths::getBackupDir(), *format);
}
catch (const std::exception &e)
{
Expand Down
2 changes: 1 addition & 1 deletion source/dialogs/bulkconverterdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ convertFile(const std::filesystem::path &src, const std::filesystem::path &dst,

try
{
ffm->exportFile(score, dst, export_format);
ffm->exportFile(score, dst, Paths::getBackupDir(), export_format);
}
catch (const std::exception &)
{
Expand Down
17 changes: 12 additions & 5 deletions source/formats/fileformatmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,27 @@ std::string FileFormatManager::exportFileFilter() const
return filter;
}

void FileFormatManager::exportFile(const Score &score,
const std::filesystem::path &filename,
const FileFormat &format)
void
FileFormatManager::exportFile(const Score &score, const std::filesystem::path &filename,
const std::filesystem::path &backup_folder, const FileFormat &format)
{
// Write to a temporary file and then swap, to avoid data loss if e.g. the exporter crashes.
std::filesystem::path temp_file = std::filesystem::temp_directory_path() / filename.filename();
std::filesystem::create_directories(backup_folder);
std::filesystem::path temp_file = backup_folder / filename.filename();
temp_file += ".new";

for (auto &exporter : myExporters)
{
if (exporter->fileFormat() == format)
{
exporter->save(temp_file, score);
std::filesystem::rename(temp_file, filename);

// Note that filesystem::rename() doesn't work if the backup folder is on a different
// volume, so just copy.
std::filesystem::copy(temp_file, filename,
std::filesystem::copy_options::overwrite_existing);
std::filesystem::remove(temp_file);

return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/formats/fileformatmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class FileFormatManager
/// Exports the given score to a file.
/// @throws std::exception
void exportFile(const Score &score, const std::filesystem::path &filename,
const FileFormat &format);
const std::filesystem::path &backup_folder, const FileFormat &format);

// Checks to see if there is an importer for the designated extension
bool extensionImportSupported(const std::string& extension) const;
Expand Down

0 comments on commit eaa9425

Please sign in to comment.