]> Savannah Git Hosting - gnulib.git/commitdiff
backupfile: fix bug when renaming from subdirectory
authorPádraig Brady <P@draigBrady.com>
Mon, 3 Apr 2023 17:06:22 +0000 (18:06 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 8 Apr 2023 10:38:00 +0000 (12:38 +0200)
* 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

ChangeLog
lib/backupfile.c

index c1b9eac82cd89bf1ba940eebf0470cedb9e1fc22..698d7b31dbf86a4b1e8c09087b071e3d6bd6b7db 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-04-04  Pádraig Brady  <P@draigBrady.com>
+
+       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  <bruno@clisp.org>
 
        c32rtomb: Fix an autoconf test.
index 9cca271343390a14735be66507de7da105b4352e..5bcf924414666ea24c77968bbc0aa74f9d715c04 100644 (file)
@@ -331,7 +331,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)
     {
@@ -370,9 +370,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))