From 36033ef57cd048588f9a3d5523712147066421f2 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 20 Sep 2018 16:30:47 -0600 Subject: Do not reopen temporary files The current callers of mkostemp close the file descriptor and then re-open it with fopen. It seemed better to me to continue to use the already-opened file descriptor, so this patch rearranges the code a little in order to do so. It takes care to ensure that the files are only unlinked after the file descriptor in question is closed, as before. gdb/ChangeLog 2018-10-27 Tom Tromey * unittests/scoped_fd-selftests.c (test_to_file): New function. (run_tests): Call test_to_file. * dwarf-index-write.c (write_psymtabs_to_index): Do not reopen temporary files. * common/scoped_fd.h (scoped_fd::to_file): New method. --- gdb/common/scoped_fd.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'gdb/common') diff --git a/gdb/common/scoped_fd.h b/gdb/common/scoped_fd.h index c2125bd1afe..d20e18a2c09 100644 --- a/gdb/common/scoped_fd.h +++ b/gdb/common/scoped_fd.h @@ -21,6 +21,7 @@ #define SCOPED_FD_H #include +#include "filestuff.h" /* A smart-pointer-like class to automatically close a file descriptor. */ @@ -43,6 +44,18 @@ public: return fd; } + /* Like release, but return a gdb_file_up that owns the file + descriptor. On success, this scoped_fd will be released. On + failure, return NULL and leave this scoped_fd in possession of + the fd. */ + gdb_file_up to_file (const char *mode) noexcept + { + gdb_file_up result (fdopen (m_fd, mode)); + if (result != nullptr) + m_fd = -1; + return result; + } + int get () const noexcept { return m_fd; -- cgit v1.2.1