mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-15 04:31:49 +08:00
268a13a5a3
This is the next patch in the ongoing series to move gdbsever to the top level. This patch just renames the "common" directory. The idea is to do this move in two parts: first rename the directory (this patch), then move the directory to the top. This approach makes the patches a bit more tractable. I chose the name "gdbsupport" for the directory. However, as this patch was largely written by sed, we could pick a new name without too much difficulty. Tested by the buildbot. gdb/ChangeLog 2019-07-09 Tom Tromey <tom@tromey.com> * contrib/ari/gdb_ari.sh: Change common to gdbsupport. * configure: Rebuild. * configure.ac: Change common to gdbsupport. * gdbsupport: Rename from common. * acinclude.m4: Change common to gdbsupport. * Makefile.in (CONFIG_SRC_SUBDIR, COMMON_SFILES) (HFILES_NO_SRCDIR, stamp-version, ALLDEPFILES): Change common to gdbsupport. * aarch64-tdep.c, ada-lang.c, ada-lang.h, agent.c, alloc.c, amd64-darwin-tdep.c, amd64-dicos-tdep.c, amd64-fbsd-nat.c, amd64-fbsd-tdep.c, amd64-linux-nat.c, amd64-linux-tdep.c, amd64-nbsd-tdep.c, amd64-obsd-tdep.c, amd64-sol2-tdep.c, amd64-tdep.c, amd64-windows-tdep.c, arch-utils.c, arch/aarch64-insn.c, arch/aarch64.c, arch/aarch64.h, arch/amd64.c, arch/amd64.h, arch/arm-get-next-pcs.c, arch/arm-linux.c, arch/arm.c, arch/i386.c, arch/i386.h, arch/ppc-linux-common.c, arch/riscv.c, arch/riscv.h, arch/tic6x.c, arm-tdep.c, auto-load.c, auxv.c, ax-gdb.c, ax-general.c, ax.h, breakpoint.c, breakpoint.h, btrace.c, btrace.h, build-id.c, build-id.h, c-lang.h, charset.c, charset.h, cli/cli-cmds.c, cli/cli-cmds.h, cli/cli-decode.c, cli/cli-dump.c, cli/cli-option.h, cli/cli-script.c, coff-pe-read.c, command.h, compile/compile-c-support.c, compile/compile-c.h, compile/compile-cplus-symbols.c, compile/compile-cplus-types.c, compile/compile-cplus.h, compile/compile-loc2c.c, compile/compile.c, completer.c, completer.h, contrib/ari/gdb_ari.sh, corefile.c, corelow.c, cp-support.c, cp-support.h, cp-valprint.c, csky-tdep.c, ctf.c, darwin-nat.c, debug.c, defs.h, disasm-selftests.c, disasm.c, disasm.h, dtrace-probe.c, dwarf-index-cache.c, dwarf-index-cache.h, dwarf-index-write.c, dwarf2-frame.c, dwarf2expr.c, dwarf2loc.c, dwarf2read.c, event-loop.c, event-top.c, exceptions.c, exec.c, extension.h, fbsd-nat.c, features/aarch64-core.c, features/aarch64-fpu.c, features/aarch64-pauth.c, features/aarch64-sve.c, features/i386/32bit-avx.c, features/i386/32bit-avx512.c, features/i386/32bit-core.c, features/i386/32bit-linux.c, features/i386/32bit-mpx.c, features/i386/32bit-pkeys.c, features/i386/32bit-segments.c, features/i386/32bit-sse.c, features/i386/64bit-avx.c, features/i386/64bit-avx512.c, features/i386/64bit-core.c, features/i386/64bit-linux.c, features/i386/64bit-mpx.c, features/i386/64bit-pkeys.c, features/i386/64bit-segments.c, features/i386/64bit-sse.c, features/i386/x32-core.c, features/riscv/32bit-cpu.c, features/riscv/32bit-csr.c, features/riscv/32bit-fpu.c, features/riscv/64bit-cpu.c, features/riscv/64bit-csr.c, features/riscv/64bit-fpu.c, features/tic6x-c6xp.c, features/tic6x-core.c, features/tic6x-gp.c, filename-seen-cache.h, findcmd.c, findvar.c, fork-child.c, gcore.c, gdb_bfd.c, gdb_bfd.h, gdb_proc_service.h, gdb_regex.c, gdb_select.h, gdb_usleep.c, gdbarch-selftests.c, gdbthread.h, gdbtypes.h, gnu-nat.c, go32-nat.c, guile/guile.c, guile/scm-ports.c, guile/scm-safe-call.c, guile/scm-type.c, i386-fbsd-nat.c, i386-fbsd-tdep.c, i386-go32-tdep.c, i386-linux-nat.c, i386-linux-tdep.c, i386-tdep.c, i387-tdep.c, ia64-libunwind-tdep.c, ia64-linux-nat.c, inf-child.c, inf-ptrace.c, infcall.c, infcall.h, infcmd.c, inferior-iter.h, inferior.c, inferior.h, inflow.c, inflow.h, infrun.c, infrun.h, inline-frame.c, language.h, linespec.c, linux-fork.c, linux-nat.c, linux-tdep.c, linux-thread-db.c, location.c, machoread.c, macrotab.h, main.c, maint.c, maint.h, memattr.c, memrange.h, mi/mi-cmd-break.h, mi/mi-cmd-env.c, mi/mi-cmd-stack.c, mi/mi-cmd-var.c, mi/mi-interp.c, mi/mi-main.c, mi/mi-parse.h, minsyms.c, mips-linux-tdep.c, namespace.h, nat/aarch64-linux-hw-point.c, nat/aarch64-linux-hw-point.h, nat/aarch64-linux.c, nat/aarch64-sve-linux-ptrace.c, nat/amd64-linux-siginfo.c, nat/fork-inferior.c, nat/linux-btrace.c, nat/linux-btrace.h, nat/linux-namespaces.c, nat/linux-nat.h, nat/linux-osdata.c, nat/linux-personality.c, nat/linux-procfs.c, nat/linux-ptrace.c, nat/linux-ptrace.h, nat/linux-waitpid.c, nat/mips-linux-watch.c, nat/mips-linux-watch.h, nat/ppc-linux.c, nat/x86-dregs.c, nat/x86-dregs.h, nat/x86-linux-dregs.c, nat/x86-linux.c, nto-procfs.c, nto-tdep.c, objfile-flags.h, objfiles.c, objfiles.h, obsd-nat.c, observable.h, osdata.c, p-valprint.c, parse.c, parser-defs.h, ppc-linux-nat.c, printcmd.c, probe.c, proc-api.c, procfs.c, producer.c, progspace.h, psymtab.h, python/py-framefilter.c, python/py-inferior.c, python/py-ref.h, python/py-type.c, python/python.c, record-btrace.c, record-full.c, record.c, record.h, regcache-dump.c, regcache.c, regcache.h, remote-fileio.c, remote-fileio.h, remote-sim.c, remote.c, riscv-tdep.c, rs6000-aix-tdep.c, rust-exp.y, s12z-tdep.c, selftest-arch.c, ser-base.c, ser-event.c, ser-pipe.c, ser-tcp.c, ser-unix.c, skip.c, solib-aix.c, solib-target.c, solib.c, source-cache.c, source.c, source.h, sparc-nat.c, spu-linux-nat.c, stack.c, stap-probe.c, symfile-add-flags.h, symfile.c, symfile.h, symtab.c, symtab.h, target-descriptions.c, target-descriptions.h, target-memory.c, target.c, target.h, target/waitstatus.c, target/waitstatus.h, thread-iter.h, thread.c, tilegx-tdep.c, top.c, top.h, tracefile-tfile.c, tracefile.c, tracepoint.c, tracepoint.h, tui/tui-io.c, ui-file.c, ui-out.h, unittests/array-view-selftests.c, unittests/child-path-selftests.c, unittests/cli-utils-selftests.c, unittests/common-utils-selftests.c, unittests/copy_bitwise-selftests.c, unittests/environ-selftests.c, unittests/format_pieces-selftests.c, unittests/function-view-selftests.c, unittests/lookup_name_info-selftests.c, unittests/memory-map-selftests.c, unittests/memrange-selftests.c, unittests/mkdir-recursive-selftests.c, unittests/observable-selftests.c, unittests/offset-type-selftests.c, unittests/optional-selftests.c, unittests/parse-connection-spec-selftests.c, unittests/ptid-selftests.c, unittests/rsp-low-selftests.c, unittests/scoped_fd-selftests.c, unittests/scoped_mmap-selftests.c, unittests/scoped_restore-selftests.c, unittests/string_view-selftests.c, unittests/style-selftests.c, unittests/tracepoint-selftests.c, unittests/unpack-selftests.c, unittests/utils-selftests.c, unittests/xml-utils-selftests.c, utils.c, utils.h, valarith.c, valops.c, valprint.c, value.c, value.h, varobj.c, varobj.h, windows-nat.c, x86-linux-nat.c, xml-support.c, xml-support.h, xml-tdesc.h, xstormy16-tdep.c, xtensa-linux-nat.c, dwarf2read.h: Change common to gdbsupport. gdb/gdbserver/ChangeLog 2019-07-09 Tom Tromey <tom@tromey.com> * configure: Rebuild. * configure.ac: Change common to gdbsupport. * acinclude.m4: Change common to gdbsupport. * Makefile.in (SFILES, OBS, GDBREPLAY_OBS, IPA_OBJS) (version-generated.c, gdbsupport/%-ipa.o, gdbsupport/%.o): Change common to gdbsupport. * ax.c, event-loop.c, fork-child.c, gdb_proc_service.h, gdbreplay.c, gdbthread.h, hostio-errno.c, hostio.c, i387-fp.c, inferiors.c, inferiors.h, linux-aarch64-tdesc-selftest.c, linux-amd64-ipa.c, linux-i386-ipa.c, linux-low.c, linux-tic6x-low.c, linux-x86-low.c, linux-x86-tdesc-selftest.c, linux-x86-tdesc.c, lynx-i386-low.c, lynx-low.c, mem-break.h, nto-x86-low.c, regcache.c, regcache.h, remote-utils.c, server.c, server.h, spu-low.c, symbol.c, target.h, tdesc.c, tdesc.h, thread-db.c, tracepoint.c, win32-i386-low.c, win32-low.c: Change common to gdbsupport.
504 lines
11 KiB
C
504 lines
11 KiB
C
/* Low-level file-handling.
|
||
Copyright (C) 2012-2019 Free Software Foundation, Inc.
|
||
|
||
This file is part of GDB.
|
||
|
||
This program is free software; you can redistribute it and/or modify
|
||
it under the terms of the GNU General Public License as published by
|
||
the Free Software Foundation; either version 3 of the License, or
|
||
(at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU General Public License for more details.
|
||
|
||
You should have received a copy of the GNU General Public License
|
||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||
|
||
#include "common-defs.h"
|
||
#include "filestuff.h"
|
||
#include "gdb_vecs.h"
|
||
#include <fcntl.h>
|
||
#include <unistd.h>
|
||
#include <sys/types.h>
|
||
#include <sys/stat.h>
|
||
#include <algorithm>
|
||
|
||
#ifdef USE_WIN32API
|
||
#include <winsock2.h>
|
||
#include <windows.h>
|
||
#define HAVE_SOCKETS 1
|
||
#elif defined HAVE_SYS_SOCKET_H
|
||
#include <sys/socket.h>
|
||
/* Define HAVE_F_GETFD if we plan to use F_GETFD. */
|
||
#define HAVE_F_GETFD F_GETFD
|
||
#define HAVE_SOCKETS 1
|
||
#endif
|
||
|
||
#ifdef HAVE_KINFO_GETFILE
|
||
#include <sys/user.h>
|
||
#include <libutil.h>
|
||
#endif
|
||
|
||
#ifdef HAVE_SYS_RESOURCE_H
|
||
#include <sys/resource.h>
|
||
#endif /* HAVE_SYS_RESOURCE_H */
|
||
|
||
#ifndef O_CLOEXEC
|
||
#define O_CLOEXEC 0
|
||
#endif
|
||
|
||
#ifndef O_NOINHERIT
|
||
#define O_NOINHERIT 0
|
||
#endif
|
||
|
||
#ifndef SOCK_CLOEXEC
|
||
#define SOCK_CLOEXEC 0
|
||
#endif
|
||
|
||
|
||
|
||
#ifndef HAVE_FDWALK
|
||
|
||
#include <dirent.h>
|
||
|
||
/* Replacement for fdwalk, if the system doesn't define it. Walks all
|
||
open file descriptors (though this implementation may walk closed
|
||
ones as well, depending on the host platform's capabilities) and
|
||
call FUNC with ARG. If FUNC returns non-zero, stops immediately
|
||
and returns the same value. Otherwise, returns zero when
|
||
finished. */
|
||
|
||
static int
|
||
fdwalk (int (*func) (void *, int), void *arg)
|
||
{
|
||
/* Checking __linux__ isn't great but it isn't clear what would be
|
||
better. There doesn't seem to be a good way to check for this in
|
||
configure. */
|
||
#ifdef __linux__
|
||
DIR *dir;
|
||
|
||
dir = opendir ("/proc/self/fd");
|
||
if (dir != NULL)
|
||
{
|
||
struct dirent *entry;
|
||
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;
|
||
}
|
||
|
||
closedir (dir);
|
||
return result;
|
||
}
|
||
/* 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));
|
||
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;
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
/* We may fall through to the next case. */
|
||
#endif
|
||
|
||
{
|
||
int max, fd;
|
||
|
||
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
|
||
struct rlimit rlim;
|
||
|
||
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);
|
||
#else
|
||
/* Whoops. */
|
||
return 0;
|
||
#endif /* _SC_OPEN_MAX */
|
||
}
|
||
|
||
for (fd = 0; fd < max; ++fd)
|
||
{
|
||
struct stat sb;
|
||
int result;
|
||
|
||
/* Only call FUNC for open fds. */
|
||
if (fstat (fd, &sb) == -1)
|
||
continue;
|
||
|
||
result = func (arg, fd);
|
||
if (result != 0)
|
||
return result;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
#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. */
|
||
|
||
static std::vector<int> open_fds;
|
||
|
||
/* An fdwalk callback function used by notice_open_fds. It puts the
|
||
given file descriptor into the vec. */
|
||
|
||
static int
|
||
do_mark_open_fd (void *ignore, int fd)
|
||
{
|
||
open_fds.push_back (fd);
|
||
return 0;
|
||
}
|
||
|
||
/* See filestuff.h. */
|
||
|
||
void
|
||
notice_open_fds (void)
|
||
{
|
||
fdwalk (do_mark_open_fd, NULL);
|
||
}
|
||
|
||
/* See filestuff.h. */
|
||
|
||
void
|
||
mark_fd_no_cloexec (int fd)
|
||
{
|
||
do_mark_open_fd (NULL, fd);
|
||
}
|
||
|
||
/* See filestuff.h. */
|
||
|
||
void
|
||
unmark_fd_no_cloexec (int fd)
|
||
{
|
||
auto it = std::remove (open_fds.begin (), open_fds.end (), fd);
|
||
|
||
if (it != open_fds.end ())
|
||
open_fds.erase (it);
|
||
else
|
||
gdb_assert_not_reached (_("fd not found in open_fds"));
|
||
}
|
||
|
||
/* Helper function for close_most_fds that closes the file descriptor
|
||
if appropriate. */
|
||
|
||
static int
|
||
do_close (void *ignore, int fd)
|
||
{
|
||
for (int val : open_fds)
|
||
{
|
||
if (fd == val)
|
||
{
|
||
/* Keep this one open. */
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
close (fd);
|
||
return 0;
|
||
}
|
||
|
||
/* See filestuff.h. */
|
||
|
||
void
|
||
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
|
||
track this state because, while gdb might have been compiled
|
||
against a libc that supplies O_CLOEXEC, there is no guarantee that
|
||
the kernel supports it. */
|
||
|
||
static int trust_o_cloexec;
|
||
|
||
/* Mark FD as close-on-exec, ignoring errors. Update
|
||
TRUST_O_CLOEXEC. */
|
||
|
||
static void
|
||
mark_cloexec (int fd)
|
||
{
|
||
#ifdef HAVE_F_GETFD
|
||
int old = fcntl (fd, F_GETFD, 0);
|
||
|
||
if (old != -1)
|
||
{
|
||
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;
|
||
}
|
||
}
|
||
#endif /* HAVE_F_GETFD */
|
||
}
|
||
|
||
/* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
|
||
|
||
static void
|
||
maybe_mark_cloexec (int fd)
|
||
{
|
||
if (trust_o_cloexec <= 0)
|
||
mark_cloexec (fd);
|
||
}
|
||
|
||
#ifdef HAVE_SOCKETS
|
||
|
||
/* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
|
||
|
||
static void
|
||
socket_mark_cloexec (int fd)
|
||
{
|
||
if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0)
|
||
mark_cloexec (fd);
|
||
}
|
||
|
||
#endif
|
||
|
||
|
||
|
||
/* See filestuff.h. */
|
||
|
||
int
|
||
gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
|
||
{
|
||
int fd = open (filename, flags | O_CLOEXEC, mode);
|
||
|
||
if (fd >= 0)
|
||
maybe_mark_cloexec (fd);
|
||
|
||
return fd;
|
||
}
|
||
|
||
/* See filestuff.h. */
|
||
|
||
gdb_file_up
|
||
gdb_fopen_cloexec (const char *filename, const char *opentype)
|
||
{
|
||
FILE *result;
|
||
/* Probe for "e" support once. But, if we can tell the operating
|
||
system doesn't know about close on exec mode "e" without probing,
|
||
skip it. E.g., the Windows runtime issues an "Invalid parameter
|
||
passed to C runtime function" OutputDebugString warning for
|
||
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;
|
||
|
||
if (!fopen_e_ever_failed_einval)
|
||
{
|
||
char *copy;
|
||
|
||
copy = (char *) alloca (strlen (opentype) + 2);
|
||
strcpy (copy, opentype);
|
||
/* This is a glibc extension but we try it unconditionally on
|
||
this path. */
|
||
strcat (copy, "e");
|
||
result = fopen (filename, copy);
|
||
|
||
if (result == NULL && errno == EINVAL)
|
||
{
|
||
result = fopen (filename, opentype);
|
||
if (result != NULL)
|
||
fopen_e_ever_failed_einval = 1;
|
||
}
|
||
}
|
||
else
|
||
result = fopen (filename, opentype);
|
||
|
||
if (result != NULL)
|
||
maybe_mark_cloexec (fileno (result));
|
||
|
||
return gdb_file_up (result);
|
||
}
|
||
|
||
#ifdef HAVE_SOCKETS
|
||
/* See filestuff.h. */
|
||
|
||
int
|
||
gdb_socketpair_cloexec (int domain, int style, int protocol,
|
||
int filedes[2])
|
||
{
|
||
#ifdef HAVE_SOCKETPAIR
|
||
int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes);
|
||
|
||
if (result != -1)
|
||
{
|
||
socket_mark_cloexec (filedes[0]);
|
||
socket_mark_cloexec (filedes[1]);
|
||
}
|
||
|
||
return result;
|
||
#else
|
||
gdb_assert_not_reached (_("socketpair not available on this host"));
|
||
#endif
|
||
}
|
||
|
||
/* See filestuff.h. */
|
||
|
||
int
|
||
gdb_socket_cloexec (int domain, int style, int protocol)
|
||
{
|
||
int result = socket (domain, style | SOCK_CLOEXEC, protocol);
|
||
|
||
if (result != -1)
|
||
socket_mark_cloexec (result);
|
||
|
||
return result;
|
||
}
|
||
#endif
|
||
|
||
/* See filestuff.h. */
|
||
|
||
int
|
||
gdb_pipe_cloexec (int filedes[2])
|
||
{
|
||
int result;
|
||
|
||
#ifdef HAVE_PIPE2
|
||
result = pipe2 (filedes, O_CLOEXEC);
|
||
if (result != -1)
|
||
{
|
||
maybe_mark_cloexec (filedes[0]);
|
||
maybe_mark_cloexec (filedes[1]);
|
||
}
|
||
#else
|
||
#ifdef HAVE_PIPE
|
||
result = pipe (filedes);
|
||
if (result != -1)
|
||
{
|
||
mark_cloexec (filedes[0]);
|
||
mark_cloexec (filedes[1]);
|
||
}
|
||
#else /* HAVE_PIPE */
|
||
gdb_assert_not_reached (_("pipe not available on this host"));
|
||
#endif /* HAVE_PIPE */
|
||
#endif /* HAVE_PIPE2 */
|
||
|
||
return result;
|
||
}
|
||
|
||
/* See gdbsupport/filestuff.h. */
|
||
|
||
bool
|
||
is_regular_file (const char *name, int *errno_ptr)
|
||
{
|
||
struct stat st;
|
||
const int status = stat (name, &st);
|
||
|
||
/* Stat should never fail except when the file does not exist.
|
||
If stat fails, analyze the source of error and return true
|
||
unless the file does not exist, to avoid returning false results
|
||
on obscure systems where stat does not work as expected. */
|
||
|
||
if (status != 0)
|
||
{
|
||
if (errno != ENOENT)
|
||
return true;
|
||
*errno_ptr = ENOENT;
|
||
return false;
|
||
}
|
||
|
||
if (S_ISREG (st.st_mode))
|
||
return true;
|
||
|
||
if (S_ISDIR (st.st_mode))
|
||
*errno_ptr = EISDIR;
|
||
else
|
||
*errno_ptr = EINVAL;
|
||
return false;
|
||
}
|
||
|
||
/* See gdbsupport/filestuff.h. */
|
||
|
||
bool
|
||
mkdir_recursive (const char *dir)
|
||
{
|
||
auto holder = make_unique_xstrdup (dir);
|
||
char * const start = holder.get ();
|
||
char *component_start = start;
|
||
char *component_end = start;
|
||
|
||
while (1)
|
||
{
|
||
/* Find the beginning of the next component. */
|
||
while (*component_start == '/')
|
||
component_start++;
|
||
|
||
/* Are we done? */
|
||
if (*component_start == '\0')
|
||
return true;
|
||
|
||
/* Find the slash or null-terminator after this component. */
|
||
component_end = component_start;
|
||
while (*component_end != '/' && *component_end != '\0')
|
||
component_end++;
|
||
|
||
/* Temporarily replace the slash with a null terminator, so we can create
|
||
the directory up to this component. */
|
||
char saved_char = *component_end;
|
||
*component_end = '\0';
|
||
|
||
/* If we get EEXIST and the existing path is a directory, then we're
|
||
happy. If it exists, but it's a regular file and this is not the last
|
||
component, we'll fail at the next component. If this is the last
|
||
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;
|
||
|
||
/* Restore the overwritten char. */
|
||
*component_end = saved_char;
|
||
component_start = component_end;
|
||
}
|
||
}
|