18dffb485Schristos /* scoped_fd, automatically close a file descriptor 28dffb485Schristos 3*5ba1f45fSchristos Copyright (C) 2018-2024 Free Software Foundation, Inc. 48dffb485Schristos 58dffb485Schristos This file is part of GDB. 68dffb485Schristos 78dffb485Schristos This program is free software; you can redistribute it and/or modify 88dffb485Schristos it under the terms of the GNU General Public License as published by 98dffb485Schristos the Free Software Foundation; either version 3 of the License, or 108dffb485Schristos (at your option) any later version. 118dffb485Schristos 128dffb485Schristos This program is distributed in the hope that it will be useful, 138dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 148dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 158dffb485Schristos GNU General Public License for more details. 168dffb485Schristos 178dffb485Schristos You should have received a copy of the GNU General Public License 188dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 198dffb485Schristos 208dffb485Schristos #ifndef COMMON_SCOPED_FD_H 218dffb485Schristos #define COMMON_SCOPED_FD_H 228dffb485Schristos 238dffb485Schristos #include <unistd.h> 244b169a6bSchristos #include "gdb_file.h" 258dffb485Schristos 268dffb485Schristos /* A smart-pointer-like class to automatically close a file descriptor. */ 278dffb485Schristos 288dffb485Schristos class scoped_fd 298dffb485Schristos { 308dffb485Schristos public: 318dffb485Schristos explicit scoped_fd (int fd = -1) noexcept : m_fd (fd) {} 328dffb485Schristos 338dffb485Schristos scoped_fd (scoped_fd &&other) noexcept 348dffb485Schristos : m_fd (other.m_fd) 358dffb485Schristos { 368dffb485Schristos other.m_fd = -1; 378dffb485Schristos } 388dffb485Schristos 398dffb485Schristos ~scoped_fd () 408dffb485Schristos { 418dffb485Schristos if (m_fd >= 0) 428dffb485Schristos close (m_fd); 438dffb485Schristos } 448dffb485Schristos 458dffb485Schristos scoped_fd &operator= (scoped_fd &&other) 468dffb485Schristos { 478dffb485Schristos if (m_fd != other.m_fd) 488dffb485Schristos { 498dffb485Schristos if (m_fd >= 0) 508dffb485Schristos close (m_fd); 518dffb485Schristos m_fd = other.m_fd; 528dffb485Schristos other.m_fd = -1; 538dffb485Schristos } 548dffb485Schristos return *this; 558dffb485Schristos } 568dffb485Schristos 578dffb485Schristos DISABLE_COPY_AND_ASSIGN (scoped_fd); 588dffb485Schristos 598dffb485Schristos ATTRIBUTE_UNUSED_RESULT int release () noexcept 608dffb485Schristos { 618dffb485Schristos int fd = m_fd; 628dffb485Schristos m_fd = -1; 638dffb485Schristos return fd; 648dffb485Schristos } 658dffb485Schristos 668dffb485Schristos /* Like release, but return a gdb_file_up that owns the file 678dffb485Schristos descriptor. On success, this scoped_fd will be released. On 688dffb485Schristos failure, return NULL and leave this scoped_fd in possession of 698dffb485Schristos the fd. */ 708dffb485Schristos gdb_file_up to_file (const char *mode) noexcept 718dffb485Schristos { 728dffb485Schristos gdb_file_up result (fdopen (m_fd, mode)); 738dffb485Schristos if (result != nullptr) 748dffb485Schristos m_fd = -1; 758dffb485Schristos return result; 768dffb485Schristos } 778dffb485Schristos 788dffb485Schristos int get () const noexcept 798dffb485Schristos { 808dffb485Schristos return m_fd; 818dffb485Schristos } 828dffb485Schristos 838dffb485Schristos private: 848dffb485Schristos int m_fd; 858dffb485Schristos }; 868dffb485Schristos 878dffb485Schristos #endif /* COMMON_SCOPED_FD_H */ 88