]> 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 11:01:04 +0000 (13:01 +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 57357aa917726f64956b2a18f5a2a39c7f1e96e2..ac2d04b644b68c20723f2752abbcc16fc1f9755f 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 d9f465a3e0eaa8bdd0764247dbf285a5b14455c8..48fbee01ace346b0fbbe0e4ead99933a372e0df9 100644 (file)
@@ -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))