llvm/llvm/lib/Support/Unix/Path.inc

//===- llvm/Support/Unix/Path.inc - Unix Path Implementation ----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the Unix specific implementation of the Path API.
//
//===----------------------------------------------------------------------===//

//===----------------------------------------------------------------------===//
//=== WARNING: Implementation here must contain only generic UNIX code that
//===          is guaranteed to work on *all* UNIX variants.
//===----------------------------------------------------------------------===//

#include "Unix.h"
#include <limits.h>
#include <stdio.h>
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif

#include <dirent.h>
#include <pwd.h>
#include <sys/file.h>

#ifdef __APPLE__
#include <copyfile.h>
#include <mach-o/dyld.h>
#include <sys/attr.h>
#if __has_include(<sys/clonefile.h>)
#include <sys/clonefile.h>
#endif
#elif defined(__FreeBSD__)
#include <osreldate.h>
#if __FreeBSD_version >= 1300057
#include <sys/auxv.h>
#else
#include <machine/elf.h>
extern char **environ;
#endif
#elif defined(__DragonFly__)
#include <sys/mount.h>
#elif defined(__MVS__)
#include "llvm/Support/AutoConvert.h"
#include <sys/ps.h>
#endif

// Both stdio.h and cstdio are included via different paths and
// stdcxx's cstdio doesn't include stdio.h, so it doesn't #undef the macros
// either.
#undef ferror
#undef feof

#if !defined(PATH_MAX)
// For GNU Hurd
#if defined(__GNU__)
#define PATH_MAX
#elif defined(__MVS__)
#define PATH_MAX
#endif
#endif

#include <sys/types.h>
#if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__FreeBSD__) &&   \
    !defined(__linux__) && !defined(__FreeBSD_kernel__) && !defined(_AIX)
#include <sys/statvfs.h>
#define STATVFS
#define FSTATVFS
#define STATVFS_F_FRSIZE
#else
#if defined(__OpenBSD__) || defined(__FreeBSD__)
#include <sys/mount.h>
#include <sys/param.h>
#elif defined(__linux__)
#if defined(HAVE_LINUX_MAGIC_H)
#include <linux/magic.h>
#else
#if defined(HAVE_LINUX_NFS_FS_H)
#include <linux/nfs_fs.h>
#endif
#if defined(HAVE_LINUX_SMB_H)
#include <linux/smb.h>
#endif
#endif
#include <sys/vfs.h>
#elif defined(_AIX)
#include <sys/statfs.h>

// <sys/vmount.h> depends on `uint` to be a typedef from <sys/types.h> to
// `uint_t`; however, <sys/types.h> does not always declare `uint`. We provide
// the typedef prior to including <sys/vmount.h> to work around this issue.
typedef uint_t uint;
#include <sys/vmount.h>
#else
#include <sys/mount.h>
#endif
#define STATVFS
#define FSTATVFS
#define STATVFS_F_FRSIZE(vfs)
#endif

#if defined(__NetBSD__) || defined(__DragonFly__) || defined(__GNU__) ||       \
    defined(__MVS__)
#define STATVFS_F_FLAG
#else
#define STATVFS_F_FLAG(vfs)
#endif

usingnamespacellvm;

namespace llvm {
namespace sys {
namespace fs {

const file_t kInvalidFile =;

#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) ||      \
    defined(__FreeBSD_kernel__) || defined(__linux__) || defined(__CYGWIN__) || \
    defined(__DragonFly__) || defined(_AIX) || defined(__GNU__) ||              \
    (defined(__sun__) && defined(__svr4__) || defined(__HAIKU__))
static int test_dir(char ret[PATH_MAX], const char *dir, const char *bin) {}

static char *getprogpath(char ret[PATH_MAX], const char *bin) {}
#endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__

/// GetMainExecutable - Return the path to the main executable, given the
/// value of argv[0] from program startup.
std::string getMainExecutable(const char *argv0, void *MainAddr) {}

TimePoint<> basic_file_status::getLastAccessedTime() const {}

TimePoint<> basic_file_status::getLastModificationTime() const {}

UniqueID file_status::getUniqueID() const {}

uint32_t file_status::getLinkCount() const {}

ErrorOr<space_info> disk_space(const Twine &Path) {}

std::error_code current_path(SmallVectorImpl<char> &result) {}

std::error_code set_current_path(const Twine &path) {}

std::error_code create_directory(const Twine &path, bool IgnoreExisting,
                                 perms Perms) {}

// Note that we are using symbolic link because hard links are not supported by
// all filesystems (SMB doesn't).
std::error_code create_link(const Twine &to, const Twine &from) {}

std::error_code create_hard_link(const Twine &to, const Twine &from) {}

std::error_code remove(const Twine &path, bool IgnoreNonExisting) {}

static bool is_local_impl(struct STATVFS &Vfs) {}

std::error_code is_local(const Twine &Path, bool &Result) {}

std::error_code is_local(int FD, bool &Result) {}

std::error_code rename(const Twine &from, const Twine &to) {}

std::error_code resize_file(int FD, uint64_t Size) {}

static int convertAccessMode(AccessMode Mode) {}

std::error_code access(const Twine &Path, AccessMode Mode) {}

bool can_execute(const Twine &Path) {}

bool equivalent(file_status A, file_status B) {}

std::error_code equivalent(const Twine &A, const Twine &B, bool &result) {}

static void expandTildeExpr(SmallVectorImpl<char> &Path) {}

void expand_tilde(const Twine &path, SmallVectorImpl<char> &dest) {}

static file_type typeForMode(mode_t Mode) {}

static std::error_code fillStatus(int StatRet, const struct stat &Status,
                                  file_status &Result) {}

std::error_code status(const Twine &Path, file_status &Result, bool Follow) {}

std::error_code status(int FD, file_status &Result) {}

unsigned getUmask() {}

std::error_code setPermissions(const Twine &Path, perms Permissions) {}

std::error_code setPermissions(int FD, perms Permissions) {}

std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime,
                                                 TimePoint<> ModificationTime) {}

std::error_code mapped_file_region::init(int FD, uint64_t Offset,
                                         mapmode Mode) {}

mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length,
                                       uint64_t offset, std::error_code &ec)
    :{}

void mapped_file_region::unmapImpl() {}

void mapped_file_region::dontNeedImpl() {}

int mapped_file_region::alignment() {}

std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
                                                     StringRef path,
                                                     bool follow_symlinks) {}

std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) {}

static file_type direntType(dirent *Entry) {}

std::error_code detail::directory_iterator_increment(detail::DirIterState &It) {}

ErrorOr<basic_file_status> directory_entry::status() const {}

//
// FreeBSD optionally provides /proc/self/fd, but it is incompatible with
// Linux. The thing to use is realpath.
//
#if !defined(__FreeBSD__) && !defined(__OpenBSD__)
#define TRY_PROC_SELF_FD
#endif

#if !defined(F_GETPATH) && defined(TRY_PROC_SELF_FD)
static bool hasProcSelfFD() {}
#endif

static int nativeOpenFlags(CreationDisposition Disp, OpenFlags Flags,
                           FileAccess Access) {}

std::error_code openFile(const Twine &Name, int &ResultFD,
                         CreationDisposition Disp, FileAccess Access,
                         OpenFlags Flags, unsigned Mode) {}

Expected<int> openNativeFile(const Twine &Name, CreationDisposition Disp,
                             FileAccess Access, OpenFlags Flags,
                             unsigned Mode) {}

std::error_code openFileForRead(const Twine &Name, int &ResultFD,
                                OpenFlags Flags,
                                SmallVectorImpl<char> *RealPath) {}

Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags,
                                       SmallVectorImpl<char> *RealPath) {}

file_t getStdinHandle() {}
file_t getStdoutHandle() {}
file_t getStderrHandle() {}

Expected<size_t> readNativeFile(file_t FD, MutableArrayRef<char> Buf) {}

Expected<size_t> readNativeFileSlice(file_t FD, MutableArrayRef<char> Buf,
                                     uint64_t Offset) {}

std::error_code tryLockFile(int FD, std::chrono::milliseconds Timeout) {}

std::error_code lockFile(int FD) {}

std::error_code unlockFile(int FD) {}

std::error_code closeFile(file_t &F) {}

template <typename T>
static std::error_code remove_directories_impl(const T &Entry,
                                               bool IgnoreErrors) {}

std::error_code remove_directories(const Twine &path, bool IgnoreErrors) {}

std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest,
                          bool expand_tilde) {}

std::error_code changeFileOwnership(int FD, uint32_t Owner, uint32_t Group) {}

} // end namespace fs

namespace path {

bool home_directory(SmallVectorImpl<char> &result) {}

static bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) {}

bool user_config_directory(SmallVectorImpl<char> &result) {}

bool cache_directory(SmallVectorImpl<char> &result) {}

static const char *getEnvTempDir() {}

static const char *getDefaultTempDir(bool ErasedOnReboot) {}

void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl<char> &Result) {}

} // end namespace path

namespace fs {

#ifdef __APPLE__
/// This implementation tries to perform an APFS CoW clone of the file,
/// which can be much faster and uses less space.
/// Unfortunately fcopyfile(3) does not support COPYFILE_CLONE, so the
/// file descriptor variant of this function still uses the default
/// implementation.
std::error_code copy_file(const Twine &From, const Twine &To) {
  std::string FromS = From.str();
  std::string ToS = To.str();
#if __has_builtin(__builtin_available)
  if (__builtin_available(macos 10.12, *)) {
    // Optimistically try to use clonefile() and handle errors, rather than
    // calling stat() to see if it'll work.
    //
    // Note: It's okay if From is a symlink. In contrast to the behaviour of
    // copyfile() with COPYFILE_CLONE, clonefile() clones targets (not the
    // symlink itself) unless the flag CLONE_NOFOLLOW is passed.
    if (!clonefile(FromS.c_str(), ToS.c_str(), 0))
      return std::error_code();

    auto Errno = errno;
    switch (Errno) {
    case EEXIST:  // To already exists.
    case ENOTSUP: // Device does not support cloning.
    case EXDEV:   // From and To are on different devices.
      break;
    default:
      // Anything else will also break copyfile().
      return std::error_code(Errno, std::generic_category());
    }

    // TODO: For EEXIST, profile calling fs::generateUniqueName() and
    // clonefile() in a retry loop (then rename() on success) before falling
    // back to copyfile(). Depending on the size of the file this could be
    // cheaper.
  }
#endif
  if (!copyfile(FromS.c_str(), ToS.c_str(), /*State=*/NULL, COPYFILE_DATA))
    return std::error_code();
  return errnoAsErrorCode();
}
#endif // __APPLE__

} // end namespace fs

} // end namespace sys
} // end namespace llvm