Move mkdir_recursive to common/filestuff.c
[deliverable/binutils-gdb.git] / gdb / common / filestuff.h
CommitLineData
614c279d 1/* Low-level file-handling.
e2882c85 2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
614c279d
TT
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#ifndef FILESTUFF_H
20#define FILESTUFF_H
21
f0b3976b
TT
22#include <dirent.h>
23
614c279d
TT
24/* Note all the file descriptors which are open when this is called.
25 These file descriptors will not be closed by close_most_fds. */
26
27extern void notice_open_fds (void);
28
21ff4686
TT
29/* Mark a file descriptor as inheritable across an exec. */
30
31extern void mark_fd_no_cloexec (int fd);
32
33/* Mark a file descriptor as no longer being inheritable across an
34 exec. This is only meaningful when FD was previously passed to
35 mark_fd_no_cloexec. */
36
37extern void unmark_fd_no_cloexec (int fd);
38
614c279d
TT
39/* Close all open file descriptors other than those marked by
40 'notice_open_fds', and stdin, stdout, and stderr. Errors that
41 occur while closing are ignored. */
42
43extern void close_most_fds (void);
44
45/* Like 'open', but ensures that the returned file descriptor has the
46 close-on-exec flag set. */
47
5d71132c
TT
48extern int gdb_open_cloexec (const char *filename, int flags,
49 /* mode_t */ unsigned long mode);
614c279d 50
528e1572
SM
51/* Convenience wrapper for the above, which takes the filename as an
52 std::string. */
53
54static inline int
55gdb_open_cloexec (const std::string &filename, int flags,
56 /* mode_t */ unsigned long mode)
57{
58 return gdb_open_cloexec (filename.c_str (), flags, mode);
59}
60
d419f42d
TT
61struct gdb_file_deleter
62{
63 void operator() (FILE *file) const
64 {
65 fclose (file);
66 }
67};
68
69/* A unique pointer to a FILE. */
70
71typedef std::unique_ptr<FILE, gdb_file_deleter> gdb_file_up;
72
614c279d
TT
73/* Like 'fopen', but ensures that the returned file descriptor has the
74 close-on-exec flag set. */
75
d419f42d
TT
76extern gdb_file_up gdb_fopen_cloexec (const char *filename,
77 const char *opentype);
614c279d 78
528e1572
SM
79/* Convenience wrapper for the above, which takes the filename as an
80 std::string. */
81
82static inline gdb_file_up
83gdb_fopen_cloexec (const std::string &filename, const char *opentype)
84{
85 return gdb_fopen_cloexec (filename.c_str (), opentype);
86}
87
614c279d
TT
88/* Like 'socketpair', but ensures that the returned file descriptors
89 have the close-on-exec flag set. */
90
fe978cb0 91extern int gdb_socketpair_cloexec (int domain, int style, int protocol,
614c279d
TT
92 int filedes[2]);
93
94/* Like 'socket', but ensures that the returned file descriptor has
95 the close-on-exec flag set. */
96
fe978cb0 97extern int gdb_socket_cloexec (int domain, int style, int protocol);
614c279d
TT
98
99/* Like 'pipe', but ensures that the returned file descriptors have
100 the close-on-exec flag set. */
101
102extern int gdb_pipe_cloexec (int filedes[2]);
103
ca095836
GB
104/* Return a new cleanup that closes FD. */
105
106extern struct cleanup *make_cleanup_close (int fd);
107
f0b3976b
TT
108struct gdb_dir_deleter
109{
110 void operator() (DIR *dir) const
111 {
112 closedir (dir);
113 }
114};
115
116/* A unique pointer to a DIR. */
117
118typedef std::unique_ptr<DIR, gdb_dir_deleter> gdb_dir_up;
119
3c025cfe
SDJ
120/* Return true if the file NAME exists and is a regular file.
121 If the result is false then *ERRNO_PTR is set to a useful value assuming
122 we're expecting a regular file. */
123extern bool is_regular_file (const char *name, int *errno_ptr);
124
e418a61a
TT
125
126/* A cheap (as in low-quality) recursive mkdir. Try to create all the
127 parents directories up to DIR and DIR itself. Stop if we hit an
128 error along the way. There is no attempt to remove created
129 directories in case of failure.
130
131 Returns false on failure and sets errno. */
132
133extern bool mkdir_recursive (const char *dir);
134
614c279d 135#endif /* FILESTUFF_H */
This page took 0.40214 seconds and 4 git commands to generate.