diff options
Diffstat (limited to 'gdbsupport/filestuff.cc')
-rw-r--r-- | gdbsupport/filestuff.cc | 156 |
1 files changed, 74 insertions, 82 deletions
diff --git a/gdbsupport/filestuff.cc b/gdbsupport/filestuff.cc index d733210..535a064 100644 --- a/gdbsupport/filestuff.cc +++ b/gdbsupport/filestuff.cc @@ -57,8 +57,6 @@ #define SOCK_CLOEXEC 0 #endif - - #ifndef HAVE_FDWALK #include <dirent.h> @@ -86,51 +84,51 @@ fdwalk (int (*func) (void *, int), void *arg) int result = 0; for (entry = readdir (dir); entry != NULL; entry = readdir (dir)) - { - long fd; - char *tail; - - errno = 0; - fd = strtol (entry->d_name, &tail, 10); - if (*tail != '\0' || errno != 0) - continue; - if ((int) fd != fd) - { - /* What can we do here really? */ - continue; - } - - if (fd == dirfd (dir)) - continue; - - result = func (arg, fd); - if (result != 0) - break; - } + { + long fd; + char *tail; + + errno = 0; + fd = strtol (entry->d_name, &tail, 10); + if (*tail != '\0' || errno != 0) + continue; + if ((int) fd != fd) + { + /* What can we do here really? */ + continue; + } + + if (fd == dirfd (dir)) + continue; + + result = func (arg, fd); + if (result != 0) + break; + } closedir (dir); return result; } - /* We may fall through to the next case. */ + /* We may fall through to the next case. */ #endif #ifdef HAVE_KINFO_GETFILE int nfd; - gdb::unique_xmalloc_ptr<struct kinfo_file[]> fdtbl - (kinfo_getfile (getpid (), &nfd)); + gdb::unique_xmalloc_ptr<struct kinfo_file[]> fdtbl ( + kinfo_getfile (getpid (), &nfd)); if (fdtbl != NULL) { for (int i = 0; i < nfd; i++) - { - if (fdtbl[i].kf_fd >= 0) - { - int result = func (arg, fdtbl[i].kf_fd); - if (result != 0) - return result; - } - } + { + if (fdtbl[i].kf_fd >= 0) + { + int result = func (arg, fdtbl[i].kf_fd); + if (result != 0) + return result; + } + } return 0; } - /* We may fall through to the next case. */ + /* We may fall through to the next case. */ #endif { @@ -139,31 +137,32 @@ fdwalk (int (*func) (void *, int), void *arg) #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) struct rlimit rlim; - if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY) + if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 + && rlim.rlim_max != RLIM_INFINITY) max = rlim.rlim_max; else #endif { #ifdef _SC_OPEN_MAX - max = sysconf (_SC_OPEN_MAX); + max = sysconf (_SC_OPEN_MAX); #else - /* Whoops. */ - return 0; + /* Whoops. */ + return 0; #endif /* _SC_OPEN_MAX */ } for (fd = 0; fd < max; ++fd) { - struct stat sb; - int result; + struct stat sb; + int result; - /* Only call FUNC for open fds. */ - if (fstat (fd, &sb) == -1) - continue; + /* Only call FUNC for open fds. */ + if (fstat (fd, &sb) == -1) + continue; - result = func (arg, fd); - if (result != 0) - return result; + result = func (arg, fd); + if (result != 0) + return result; } return 0; @@ -172,8 +171,6 @@ fdwalk (int (*func) (void *, int), void *arg) #endif /* HAVE_FDWALK */ - - /* A vector holding all the fds open when notice_open_fds was called. We don't use a hashtab because we don't expect there to be many open fds. */ @@ -227,10 +224,10 @@ do_close (void *ignore, int fd) for (int val : open_fds) { if (fd == val) - { - /* Keep this one open. */ - return 0; - } + { + /* Keep this one open. */ + return 0; + } } close (fd); @@ -245,8 +242,6 @@ close_most_fds (void) fdwalk (do_close, NULL); } - - /* This is a tri-state flag. When zero it means we haven't yet tried O_CLOEXEC. When positive it means that O_CLOEXEC works on this host. When negative, it means that O_CLOEXEC doesn't work. We @@ -270,12 +265,12 @@ mark_cloexec (int fd) fcntl (fd, F_SETFD, old | FD_CLOEXEC); if (trust_o_cloexec == 0) - { - if ((old & FD_CLOEXEC) != 0) - trust_o_cloexec = 1; - else - trust_o_cloexec = -1; - } + { + if ((old & FD_CLOEXEC) != 0) + trust_o_cloexec = 1; + else + trust_o_cloexec = -1; + } } #endif /* HAVE_F_GETFD */ } @@ -302,8 +297,6 @@ socket_mark_cloexec (int fd) #endif - - /* See filestuff.h. */ scoped_fd @@ -330,8 +323,8 @@ gdb_fopen_cloexec (const char *filename, const char *opentype) unknown modes. Assume that if O_CLOEXEC is zero, then "e" isn't supported. On MinGW, O_CLOEXEC is an alias of O_NOINHERIT, and "e" isn't supported. */ - static int fopen_e_ever_failed_einval = - O_CLOEXEC == 0 || O_CLOEXEC == O_NOINHERIT; + static int fopen_e_ever_failed_einval + = O_CLOEXEC == 0 || O_CLOEXEC == O_NOINHERIT; if (!fopen_e_ever_failed_einval) { @@ -345,11 +338,11 @@ gdb_fopen_cloexec (const char *filename, const char *opentype) result = fopen (filename, copy); if (result == NULL && errno == EINVAL) - { - result = fopen (filename, opentype); - if (result != NULL) - fopen_e_ever_failed_einval = 1; - } + { + result = fopen (filename, opentype); + if (result != NULL) + fopen_e_ever_failed_einval = 1; + } } else result = fopen (filename, opentype); @@ -364,8 +357,7 @@ gdb_fopen_cloexec (const char *filename, const char *opentype) /* See filestuff.h. */ int -gdb_socketpair_cloexec (int domain, int style, int protocol, - int filedes[2]) +gdb_socketpair_cloexec (int domain, int style, int protocol, int filedes[2]) { #ifdef HAVE_SOCKETPAIR int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes); @@ -418,7 +410,7 @@ gdb_pipe_cloexec (int filedes[2]) mark_cloexec (filedes[0]); mark_cloexec (filedes[1]); } -#else /* HAVE_PIPE */ +#else /* HAVE_PIPE */ gdb_assert_not_reached ("pipe not available on this host"); #endif /* HAVE_PIPE */ #endif /* HAVE_PIPE2 */ @@ -442,7 +434,7 @@ is_regular_file (const char *name, int *errno_ptr) if (status != 0) { if (errno != ENOENT) - return true; + return true; *errno_ptr = ENOENT; return false; } @@ -463,7 +455,7 @@ bool mkdir_recursive (const char *dir) { auto holder = make_unique_xstrdup (dir); - char * const start = holder.get (); + char *const start = holder.get (); char *component_start = start; char *component_end = start; @@ -471,16 +463,16 @@ mkdir_recursive (const char *dir) { /* Find the beginning of the next component. */ while (*component_start == '/') - component_start++; + component_start++; /* Are we done? */ if (*component_start == '\0') - return true; + return true; /* Find the slash or null-terminator after this component. */ component_end = component_start; while (*component_end != '/' && *component_end != '\0') - component_end++; + component_end++; /* Temporarily replace the slash with a null terminator, so we can create the directory up to this component. */ @@ -493,8 +485,8 @@ mkdir_recursive (const char *dir) component, the caller will fail with ENOTDIR when trying to open/create a file under that path. */ if (mkdir (start, 0700) != 0) - if (errno != EEXIST) - return false; + if (errno != EEXIST) + return false; /* Restore the overwritten char. */ *component_end = saved_char; @@ -522,14 +514,14 @@ read_text_file_to_string (const char *path) int n = fread (&res[start_size], 1, chunk_size, file.get ()); if (n == chunk_size) - continue; + continue; gdb_assert (n < chunk_size); /* Less than CHUNK means EOF or error. If it's an error, return no value. */ if (ferror (file.get ())) - return {}; + return {}; /* Resize the string according to the data we read. */ res.resize (start_size + n); |