Path: blob/main/contrib/llvm-project/lldb/source/Host/posix/FileSystemPosix.cpp
39606 views
//===-- FileSystemPosix.cpp -----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Host/FileSystem.h"910// C includes11#include <dirent.h>12#include <fcntl.h>13#include <sys/mount.h>14#include <sys/param.h>15#include <sys/stat.h>16#include <sys/types.h>17#include <unistd.h>18#if defined(__NetBSD__)19#include <sys/statvfs.h>20#endif2122// lldb Includes23#include "lldb/Host/Host.h"24#include "lldb/Utility/Status.h"25#include "lldb/Utility/StreamString.h"2627#include "llvm/Support/Errno.h"28#include "llvm/Support/FileSystem.h"2930using namespace lldb;31using namespace lldb_private;3233const char *FileSystem::DEV_NULL = "/dev/null";3435Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {36Status error;37if (::symlink(dst.GetPath().c_str(), src.GetPath().c_str()) == -1)38error.SetErrorToErrno();39return error;40}4142Status FileSystem::Readlink(const FileSpec &src, FileSpec &dst) {43Status error;44char buf[PATH_MAX];45ssize_t count = ::readlink(src.GetPath().c_str(), buf, sizeof(buf) - 1);46if (count < 0)47error.SetErrorToErrno();48else {49buf[count] = '\0'; // Success50dst.SetFile(buf, FileSpec::Style::native);51}52return error;53}5455Status FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) {56char resolved_path[PATH_MAX];57if (!src.GetPath(resolved_path, sizeof(resolved_path))) {58return Status("Couldn't get the canonical path for %s",59src.GetPath().c_str());60}6162char real_path[PATH_MAX + 1];63if (realpath(resolved_path, real_path) == nullptr) {64Status err;65err.SetErrorToErrno();66return err;67}6869dst = FileSpec(real_path);7071return Status();72}7374FILE *FileSystem::Fopen(const char *path, const char *mode) {75return llvm::sys::RetryAfterSignal(nullptr, ::fopen, path, mode);76}7778int FileSystem::Open(const char *path, int flags, int mode) {79// Call ::open in a lambda to avoid overload resolution in RetryAfterSignal80// when open is overloaded, such as in Bionic.81auto lambda = [&]() { return ::open(path, flags, mode); };82return llvm::sys::RetryAfterSignal(-1, lambda);83}848586