Skip to content

Commit 1ccfb50

Browse files
committed
FreeImage_Save: more accurate removing of tmp file; fixed copy operation.
1 parent 8468a71 commit 1ccfb50

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

Source/FreeImage/Plugin.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,11 @@ namespace {
809809
if (!err) {
810810
return true;
811811
}
812-
return std::filesystem::copy_file(oldp, newp);
812+
const auto copied = std::filesystem::copy_file(oldp, newp, std::filesystem::copy_options::overwrite_existing);
813+
if (copied) {
814+
std::filesystem::remove(oldp);
815+
}
816+
return copied;
813817
}
814818

815819

@@ -825,12 +829,15 @@ namespace {
825829
fclose(handle);
826830

827831
if (success) {
828-
if (!MoveOrCopy(tmpname, filename)) {
832+
success = MoveOrCopy(tmpname, filename);
833+
if (!success) {
829834
FreeImage_OutputMessageProc((int)fif, "FreeImage_Save: failed to rename output file %s", filename.u8string().c_str());
830835
}
831836
}
832837

833-
std::filesystem::remove(tmpname);
838+
if (!success) {
839+
std::filesystem::remove(tmpname);
840+
}
834841
}
835842
else {
836843
FreeImage_OutputMessageProc((int)fif, "FreeImage_Save: failed to open file %s", filename.u8string().c_str());

0 commit comments

Comments
 (0)