Skip to content

Commit 6b07782

Browse files
committed
Use non-throwing remove() and rename() in MultiPage and CacheFile
1 parent 5430ab2 commit 6b07782

4 files changed

Lines changed: 21 additions & 19 deletions

File tree

Source/FreeImage/CacheFile.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ CacheFile::close() {
8383
m_file = nullptr;
8484

8585
// delete the file
86-
std::filesystem::remove(m_filename);
86+
std::error_code err{};
87+
std::filesystem::remove(m_filename, err);
8788
}
8889
}
8990

Source/FreeImage/MultiPage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,10 @@ FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags) {
518518
// applies changes to the destination file
519519

520520
if (success) {
521-
std::filesystem::remove(header->m_filename);
522-
std::filesystem::rename(spool_name, header->m_filename);
521+
success = MoveOrCopy(spool_name, header->m_filename);
523522
} else {
524-
std::filesystem::remove(spool_name);
523+
std::error_code err{};
524+
std::filesystem::remove(spool_name, err);
525525
}
526526
} catch (std::exception& err) {
527527
FreeImage_OutputMessageProc(header->fif, "Exception: %s", err.what());

Source/FreeImage/Plugin.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -802,21 +802,6 @@ namespace {
802802
}
803803

804804

805-
bool MoveOrCopy(const std::filesystem::path& oldp, const std::filesystem::path& newp) noexcept
806-
{
807-
std::error_code err{};
808-
std::filesystem::rename(oldp, newp, err);
809-
if (!err) {
810-
return true;
811-
}
812-
const auto copied = std::filesystem::copy_file(oldp, newp, std::filesystem::copy_options::overwrite_existing, err);
813-
if (copied) {
814-
std::filesystem::remove(oldp, err);
815-
}
816-
return copied;
817-
}
818-
819-
820805
FIBOOL SaveImpl(FREE_IMAGE_FORMAT fif, FIBITMAP* dib, const std::filesystem::path& filename, int flags)
821806
try
822807
{

Source/Utilities.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ FILE* FreeImage_FOpen(const std::filesystem::path& filename, const std::filesyst
8888
}
8989

9090

91+
inline
92+
bool MoveOrCopy(const std::filesystem::path& oldp, const std::filesystem::path& newp) noexcept
93+
{
94+
std::error_code err{};
95+
std::filesystem::rename(oldp, newp, err);
96+
if (!err) {
97+
return true;
98+
}
99+
const auto copied = std::filesystem::copy_file(oldp, newp, std::filesystem::copy_options::overwrite_existing, err);
100+
if (copied) {
101+
std::filesystem::remove(oldp, err);
102+
}
103+
return copied;
104+
}
105+
106+
91107
// these structs are for file I/O and should not be confused with similar
92108
// structs in FreeImage.h which are for in-memory bitmap handling
93109

0 commit comments

Comments
 (0)