From: Pádraig Brady Date: Mon, 3 Apr 2023 17:06:22 +0000 (+0100) Subject: backupfile: fix bug when renaming from subdirectory X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=9bd1c5b4162fef251659ab16e244a821dc339b0a;p=gnulib.git backupfile: fix bug when renaming from subdirectory * lib/backupfile.c (backup_internal): Ensure we use the appropriate offset if operating on a subdirectory, i.e., on an updated sdir. Fixes https://bugs.gnu.org/62607 --- diff --git a/ChangeLog b/ChangeLog index 57357aa917..ac2d04b644 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2023-04-04 Pádraig Brady + + backupfile: fix bug when renaming from subdirectory + * lib/backupfile.c (backup_internal): Ensure we use the + appropriate offset if operating on a subdirectory, + i.e., on an updated sdir. + Fixes https://bugs.gnu.org/62607 + 2023-04-04 Bruno Haible c32rtomb: Fix an autoconf test. diff --git a/lib/backupfile.c b/lib/backupfile.c index d9f465a3e0..48fbee01ac 100644 --- a/lib/backupfile.c +++ b/lib/backupfile.c @@ -1,6 +1,6 @@ /* backupfile.c -- make Emacs style backup file names - Copyright (C) 1990-2006, 2009-2022 Free Software Foundation, Inc. + Copyright (C) 1990-2006, 2009-2023 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -332,7 +332,7 @@ backupfile_internal (int dir_fd, char const *file, return s; DIR *dirp = NULL; - int sdir = dir_fd; + int sdir = -1; idx_t base_max = 0; while (true) { @@ -371,9 +371,10 @@ backupfile_internal (int dir_fd, char const *file, if (! rename) break; - idx_t offset = backup_type == simple_backups ? 0 : base_offset; + dir_fd = sdir < 0 ? dir_fd : sdir; + idx_t offset = sdir < 0 ? 0 : base_offset; unsigned flags = backup_type == simple_backups ? 0 : RENAME_NOREPLACE; - if (renameatu (sdir, file + offset, sdir, s + offset, flags) == 0) + if (renameatu (dir_fd, file + offset, dir_fd, s + offset, flags) == 0) break; int e = errno; if (! (e == EEXIST && extended))