]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/compile: Use std::filesystem::remove_all in cleanup
authorLancelot SIX <lancelot.six@amd.com>
Sun, 3 Mar 2024 16:47:56 +0000 (16:47 +0000)
committerLancelot SIX <lancelot.six@amd.com>
Wed, 3 Apr 2024 12:53:32 +0000 (13:53 +0100)
In a previous review, I noticed that some code in gdb/compile/compile.c
could use c++17's `std::filesystem::remove_all` instead of using some
`system ("rm -rf ...");`.

This patch implements this.

Note that I use the noexcept overload of std::filesystem::remove_all and
explicitly check for an error code.  This means that this code called
during the cleanup procedure cannot throw, and does not risk preventing
other cleanup functions to be called.

Tested on x86_64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31420
Change-Id: If5668bf3e15e66c020e5c3b4fa999f861690e4cf
Approved-By: Tom Tromey <tom@tromey.com>
gdb/compile/compile.c

index 2d97a1b2005fc4d965e1fc719117fea25c0f36c6..bff69a5092dc7dbe78f6db3a2a33ce2942c54cd0 100644 (file)
@@ -39,7 +39,9 @@
 #include "osabi.h"
 #include "gdbsupport/gdb_wait.h"
 #include "valprint.h"
+#include <filesystem>
 #include <optional>
+#include <system_error>
 #include "gdbsupport/gdb_unlinker.h"
 #include "gdbsupport/pathstuff.h"
 #include "gdbsupport/scoped_ignore_signal.h"
@@ -449,15 +451,11 @@ get_compile_file_tempdir (void)
   tempdir_name = xstrdup (tempdir_name);
   add_final_cleanup ([] ()
     {
-      char *zap;
-      int wstat;
-
-      gdb_assert (startswith (tempdir_name, TMP_PREFIX));
-      zap = concat ("rm -rf ", tempdir_name, (char *) NULL);
-      wstat = system (zap);
-      if (wstat == -1 || !WIFEXITED (wstat) || WEXITSTATUS (wstat) != 0)
-       warning (_("Could not remove temporary directory %s"), tempdir_name);
-      XDELETEVEC (zap);
+      std::error_code error;
+      if (std::filesystem::remove_all (tempdir_name, error)
+         == static_cast<std::uintmax_t> (-1))
+       warning (_("Could not remove temporary directory %s (%s)"),
+                tempdir_name, error.message ().c_str ());
     });
   return tempdir_name;
 }