On Sat, Dec 03, 2022 at 08:54:42PM +0200, Eli Zaretskii wrote:
> I suggest the other way around: try renaming, and if it fails, remove and
> retry renaming. This way, if remove fails, it fails the entire renaming
> operation, and you don't need to deal with remove failing due to unrelated
> reasons.
I've done this:
--- a/install-info/install-info.c
+++ b/install-info/install-info.c
@@ -1054,7 +1054,20 @@ output_dirfile (char *dirfile, int dir_nlines, struct
line_data *dir_lines,
/* Update dir file atomically. This stops the dir file being corrupted
if install-info is interrupted. */
if (rename (tempname, dirfile) == -1)
- perror (tempname);
+ {
+ /* Try to delete target file and try again. On some platforms you
+ may not rename to an existing file. */
+ if (remove (dirfile) == -1)
+ {
+ perror (dirfile);
+ remove (tempname);
+ }
+ else if (rename (tempname, dirfile) == -1)
+ {
+ perror (tempname);
+ remove (tempname);
+ }
+ }
}