158 lines
3.7 KiB
C++
158 lines
3.7 KiB
C++
//===-- FileSystem.cpp ------------------------------------------*- C++ -*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "lldb/Host/FileSystem.h"
|
|
|
|
// C includes
|
|
#include <dirent.h>
|
|
#include <sys/mount.h>
|
|
#include <sys/param.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#ifdef __linux__
|
|
#include <linux/magic.h>
|
|
#include <sys/mount.h>
|
|
#include <sys/statfs.h>
|
|
#endif
|
|
#if defined(__NetBSD__)
|
|
#include <sys/statvfs.h>
|
|
#endif
|
|
|
|
// lldb Includes
|
|
#include "lldb/Host/Host.h"
|
|
#include "lldb/Utility/Error.h"
|
|
#include "lldb/Utility/StreamString.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
using namespace lldb;
|
|
using namespace lldb_private;
|
|
|
|
const char *FileSystem::DEV_NULL = "/dev/null";
|
|
|
|
FileSpec::PathSyntax FileSystem::GetNativePathSyntax() {
|
|
return FileSpec::ePathSyntaxPosix;
|
|
}
|
|
|
|
lldb::user_id_t FileSystem::GetFileSize(const FileSpec &file_spec) {
|
|
return file_spec.GetByteSize();
|
|
}
|
|
|
|
bool FileSystem::GetFileExists(const FileSpec &file_spec) {
|
|
return file_spec.Exists();
|
|
}
|
|
|
|
Error FileSystem::Hardlink(const FileSpec &src, const FileSpec &dst) {
|
|
Error error;
|
|
if (::link(dst.GetCString(), src.GetCString()) == -1)
|
|
error.SetErrorToErrno();
|
|
return error;
|
|
}
|
|
|
|
int FileSystem::GetHardlinkCount(const FileSpec &file_spec) {
|
|
struct stat file_stat;
|
|
if (::stat(file_spec.GetCString(), &file_stat) == 0)
|
|
return file_stat.st_nlink;
|
|
|
|
return -1;
|
|
}
|
|
|
|
Error FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
|
|
Error error;
|
|
if (::symlink(dst.GetCString(), src.GetCString()) == -1)
|
|
error.SetErrorToErrno();
|
|
return error;
|
|
}
|
|
|
|
Error FileSystem::Unlink(const FileSpec &file_spec) {
|
|
Error error;
|
|
if (::unlink(file_spec.GetCString()) == -1)
|
|
error.SetErrorToErrno();
|
|
return error;
|
|
}
|
|
|
|
Error FileSystem::Readlink(const FileSpec &src, FileSpec &dst) {
|
|
Error error;
|
|
char buf[PATH_MAX];
|
|
ssize_t count = ::readlink(src.GetCString(), buf, sizeof(buf) - 1);
|
|
if (count < 0)
|
|
error.SetErrorToErrno();
|
|
else {
|
|
buf[count] = '\0'; // Success
|
|
dst.SetFile(buf, false);
|
|
}
|
|
return error;
|
|
}
|
|
|
|
Error FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) {
|
|
char resolved_path[PATH_MAX];
|
|
if (!src.GetPath(resolved_path, sizeof(resolved_path))) {
|
|
return Error("Couldn't get the canonical path for %s", src.GetCString());
|
|
}
|
|
|
|
char real_path[PATH_MAX + 1];
|
|
if (realpath(resolved_path, real_path) == nullptr) {
|
|
Error err;
|
|
err.SetErrorToErrno();
|
|
return err;
|
|
}
|
|
|
|
dst = FileSpec(real_path, false);
|
|
|
|
return Error();
|
|
}
|
|
|
|
#if defined(__NetBSD__)
|
|
static bool IsLocal(const struct statvfs &info) {
|
|
return (info.f_flag & MNT_LOCAL) != 0;
|
|
}
|
|
#else
|
|
static bool IsLocal(const struct statfs &info) {
|
|
#ifdef __linux__
|
|
#define CIFS_MAGIC_NUMBER 0xFF534D42
|
|
switch ((uint32_t)info.f_type) {
|
|
case NFS_SUPER_MAGIC:
|
|
case SMB_SUPER_MAGIC:
|
|
case CIFS_MAGIC_NUMBER:
|
|
return false;
|
|
default:
|
|
return true;
|
|
}
|
|
#else
|
|
return (info.f_flags & MNT_LOCAL) != 0;
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
#if defined(__NetBSD__)
|
|
bool FileSystem::IsLocal(const FileSpec &spec) {
|
|
struct statvfs statfs_info;
|
|
std::string path(spec.GetPath());
|
|
if (statvfs(path.c_str(), &statfs_info) == 0)
|
|
return ::IsLocal(statfs_info);
|
|
return false;
|
|
}
|
|
#else
|
|
bool FileSystem::IsLocal(const FileSpec &spec) {
|
|
struct statfs statfs_info;
|
|
std::string path(spec.GetPath());
|
|
if (statfs(path.c_str(), &statfs_info) == 0)
|
|
return ::IsLocal(statfs_info);
|
|
return false;
|
|
}
|
|
#endif
|
|
|
|
FILE *FileSystem::Fopen(const char *path, const char *mode) {
|
|
return ::fopen(path, mode);
|
|
}
|
|
|
|
int FileSystem::Stat(const char *path, struct stat *stats) {
|
|
return ::stat(path, stats);
|
|
}
|