bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] backupfile: fix bug when renaming not from wd


From: Paul Eggert
Subject: [PATCH] backupfile: fix bug when renaming not from wd
Date: Mon, 10 Jan 2022 10:38:22 -0800

* lib/backupfile.c (backupfile_internal): Fix bug when DIR_FD
does not specify the working directory, and when RENAME.
Without the bug fix, FILE is treated as relative to the working
directory, not relative to DIR_FD, when renaming FILE.
This bug was introduced when DIR_FD and RENAME were introduced,
in 2018-10-24T02:10:21Z!eggert@cs.ucla.edu.
While we’re at it, when SDIR is nonnegative improve performance a
bit by passing an SDIR-relative old name to renameatu.
---
 ChangeLog        | 12 ++++++++++++
 lib/backupfile.c | 11 ++++-------
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index aa3b04035d..33dbeb7bc6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2022-01-10  Paul Eggert  <eggert@cs.ucla.edu>
+
+       backupfile: fix bug when renaming not from wd
+       * lib/backupfile.c (backupfile_internal): Fix bug when DIR_FD
+       does not specify the working directory, and when RENAME.
+       Without the bug fix, FILE is treated as relative to the working
+       directory, not relative to DIR_FD, when renaming FILE.
+       This bug was introduced when DIR_FD and RENAME were introduced,
+       in 2018-10-24T02:10:21Z!eggert@cs.ucla.edu.
+       While we’re at it, when SDIR is nonnegative improve performance a
+       bit by passing an SDIR-relative old name to renameatu.
+
 2022-01-09  Bruno Haible  <bruno@clisp.org>
 
        host-cpu-c-abi: Add support for LoongArch64 CPU.
diff --git a/lib/backupfile.c b/lib/backupfile.c
index ac4b0f4677..1e9290a187 100644
--- a/lib/backupfile.c
+++ b/lib/backupfile.c
@@ -332,7 +332,7 @@ backupfile_internal (int dir_fd, char const *file,
     return s;
 
   DIR *dirp = NULL;
-  int sdir = -1;
+  int sdir = AT_FDCWD;
   idx_t base_max = 0;
   while (true)
     {
@@ -371,13 +371,10 @@ backupfile_internal (int dir_fd, char const *file,
       if (! rename)
         break;
 
-      if (sdir < 0)
-        {
-          sdir = AT_FDCWD;
-          base_offset = 0;
-        }
+      int olddirfd = sdir < 0 ? dir_fd : sdir;
+      idx_t offset = sdir < 0 ? 0 : base_offset;
       unsigned flags = backup_type == simple_backups ? 0 : RENAME_NOREPLACE;
-      if (renameatu (AT_FDCWD, file, sdir, s + base_offset, flags) == 0)
+      if (renameatu (olddirfd, file + offset, sdir, s + offset, flags) == 0)
         break;
       int e = errno;
       if (! (e == EEXIST && extended))
-- 
2.32.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]