# HG changeset patch # User Markus Mützel # Date 1683389579 -7200 # Node ID a4fbcbaa0aa2cc8f9ee2f275b99dbca87876d087 # Parent 3c608abd55f5b98576c7ab4d76e06e6a4a18e410 rename: Consistent behavior cross-platform for existing target file (bug #63803). * liboctave/system/file-ops.cc (octave::sys:rename): Manually enforce consistent behavior on different platforms because the STL allow implementation defined behavior if the target already exists. diff -r 3c608abd55f5 -r a4fbcbaa0aa2 liboctave/system/file-ops.cc --- a/liboctave/system/file-ops.cc Sat May 06 17:48:27 2023 +0200 +++ b/liboctave/system/file-ops.cc Sat May 06 18:12:59 2023 +0200 @@ -569,6 +569,26 @@ msg = ""; + // Do nothing if source and target are the same file. + if (same_file (to, from)) + return 0; + + // The behavior of std::rename with existing target is not defined by the + // standard. Implementations differ vastly. For Octave, use the following + // for the case that the target already exists: + // If the source and the target are regular files, overwrite the target. + // In other cases, fail. + if (file_exists (to)) + { + if (file_exists (to, false) && file_exists (from, false)) + unlink (to); + else + { + msg = "Target already exists."; + return status; + } + } + #if defined (OCTAVE_USE_WINDOWS_API) std::wstring wfrom = u8_to_wstring (from); std::wstring wto = u8_to_wstring (to);