2014-08-16 06:04:21 +08:00
|
|
|
//===-- 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
|
2015-06-28 07:11:34 +08:00
|
|
|
#include <dirent.h>
|
2015-02-24 07:47:09 +08:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/param.h>
|
2014-08-16 06:04:21 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2015-02-24 13:14:49 +08:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <linux/magic.h>
|
2016-09-07 04:57:50 +08:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/statfs.h>
|
2015-02-24 13:14:49 +08:00
|
|
|
#endif
|
2015-09-09 09:19:05 +08:00
|
|
|
#if defined(__NetBSD__)
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#endif
|
2014-08-16 06:04:21 +08:00
|
|
|
|
|
|
|
// lldb Includes
|
|
|
|
#include "lldb/Core/Error.h"
|
|
|
|
#include "lldb/Core/StreamString.h"
|
|
|
|
#include "lldb/Host/Host.h"
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const char *FileSystem::DEV_NULL = "/dev/null";
|
2015-10-15 05:37:36 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
FileSpec::PathSyntax FileSystem::GetNativePathSyntax() {
|
|
|
|
return FileSpec::ePathSyntaxPosix;
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Error FileSystem::MakeDirectory(const FileSpec &file_spec,
|
|
|
|
uint32_t file_permissions) {
|
|
|
|
if (file_spec) {
|
|
|
|
Error error;
|
|
|
|
if (::mkdir(file_spec.GetCString(), file_permissions) == -1) {
|
|
|
|
error.SetErrorToErrno();
|
|
|
|
errno = 0;
|
|
|
|
switch (error.GetError()) {
|
|
|
|
case ENOENT: {
|
|
|
|
// Parent directory doesn't exist, so lets make it if we can
|
|
|
|
// Make the parent directory and try again
|
|
|
|
FileSpec parent_file_spec{file_spec.GetDirectory().GetCString(), false};
|
|
|
|
error = MakeDirectory(parent_file_spec, file_permissions);
|
|
|
|
if (error.Fail())
|
|
|
|
return error;
|
|
|
|
// Try and make the directory again now that the parent directory was
|
|
|
|
// made successfully
|
|
|
|
if (::mkdir(file_spec.GetCString(), file_permissions) == -1) {
|
|
|
|
error.SetErrorToErrno();
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
2015-05-30 03:52:29 +08:00
|
|
|
return error;
|
2016-09-07 04:57:50 +08:00
|
|
|
} break;
|
|
|
|
case EEXIST: {
|
|
|
|
if (file_spec.IsDirectory())
|
2016-11-11 12:29:25 +08:00
|
|
|
return Error(); // It is a directory and it already exists
|
2016-09-07 04:57:50 +08:00
|
|
|
} break;
|
|
|
|
}
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
return Error("empty path");
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Error FileSystem::DeleteDirectory(const FileSpec &file_spec, bool recurse) {
|
|
|
|
Error error;
|
|
|
|
if (file_spec) {
|
|
|
|
if (recurse) {
|
|
|
|
// Save all sub directories in a list so we don't recursively call this
|
|
|
|
// function
|
|
|
|
// and possibly run out of file descriptors if the directory is too deep.
|
|
|
|
std::vector<FileSpec> sub_directories;
|
2015-06-30 02:29:00 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
FileSpec::ForEachItemInDirectory(
|
|
|
|
file_spec.GetCString(),
|
|
|
|
[&error, &sub_directories](
|
|
|
|
FileSpec::FileType file_type,
|
|
|
|
const FileSpec &spec) -> FileSpec::EnumerateDirectoryResult {
|
|
|
|
if (file_type == FileSpec::eFileTypeDirectory) {
|
|
|
|
// Save all directorires and process them after iterating through
|
|
|
|
// this directory
|
|
|
|
sub_directories.push_back(spec);
|
|
|
|
} else {
|
|
|
|
// Update sub_spec to point to the current file and delete it
|
|
|
|
error = FileSystem::Unlink(spec);
|
2015-06-28 07:11:34 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
// If anything went wrong, stop iterating, else process the next
|
|
|
|
// file
|
|
|
|
if (error.Fail())
|
|
|
|
return FileSpec::eEnumerateDirectoryResultQuit;
|
|
|
|
else
|
|
|
|
return FileSpec::eEnumerateDirectoryResultNext;
|
|
|
|
});
|
2015-06-30 02:29:00 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (error.Success()) {
|
|
|
|
// Now delete all sub directories with separate calls that aren't
|
|
|
|
// recursively calling into this function _while_ this function is
|
|
|
|
// iterating through the current directory.
|
|
|
|
for (const auto &sub_directory : sub_directories) {
|
|
|
|
error = DeleteDirectory(sub_directory, recurse);
|
|
|
|
if (error.Fail())
|
|
|
|
break;
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (error.Success()) {
|
|
|
|
if (::rmdir(file_spec.GetCString()) != 0)
|
2014-08-16 06:04:21 +08:00
|
|
|
error.SetErrorToErrno();
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
} else {
|
|
|
|
error.SetErrorString("empty path");
|
|
|
|
}
|
|
|
|
return error;
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Error FileSystem::GetFilePermissions(const FileSpec &file_spec,
|
|
|
|
uint32_t &file_permissions) {
|
|
|
|
Error error;
|
|
|
|
struct stat file_stats;
|
|
|
|
if (::stat(file_spec.GetCString(), &file_stats) == 0) {
|
|
|
|
// The bits in "st_mode" currently match the definitions
|
|
|
|
// for the file mode bits in unix.
|
|
|
|
file_permissions = file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
|
|
|
|
} else {
|
|
|
|
error.SetErrorToErrno();
|
|
|
|
}
|
|
|
|
return error;
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Error FileSystem::SetFilePermissions(const FileSpec &file_spec,
|
|
|
|
uint32_t file_permissions) {
|
|
|
|
Error error;
|
|
|
|
if (::chmod(file_spec.GetCString(), file_permissions) != 0)
|
|
|
|
error.SetErrorToErrno();
|
|
|
|
return error;
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::user_id_t FileSystem::GetFileSize(const FileSpec &file_spec) {
|
|
|
|
return file_spec.GetByteSize();
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool FileSystem::GetFileExists(const FileSpec &file_spec) {
|
|
|
|
return file_spec.Exists();
|
2015-05-09 07:54:34 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
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;
|
2015-09-19 02:12:39 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return -1;
|
2015-09-19 02:12:39 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Error FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
|
|
|
|
Error error;
|
|
|
|
if (::symlink(dst.GetCString(), src.GetCString()) == -1)
|
|
|
|
error.SetErrorToErrno();
|
|
|
|
return error;
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Error FileSystem::Unlink(const FileSpec &file_spec) {
|
|
|
|
Error error;
|
|
|
|
if (::unlink(file_spec.GetCString()) == -1)
|
|
|
|
error.SetErrorToErrno();
|
|
|
|
return error;
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
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;
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
2015-02-24 07:47:09 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
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);
|
|
|
|
|
2016-11-11 12:29:25 +08:00
|
|
|
return Error();
|
2015-09-19 06:24:57 +08:00
|
|
|
}
|
|
|
|
|
2015-09-09 09:19:05 +08:00
|
|
|
#if defined(__NetBSD__)
|
2016-09-07 04:57:50 +08:00
|
|
|
static bool IsLocal(const struct statvfs &info) {
|
|
|
|
return (info.f_flag & MNT_LOCAL) != 0;
|
2015-09-09 09:19:05 +08:00
|
|
|
}
|
|
|
|
#else
|
2016-09-07 04:57:50 +08:00
|
|
|
static bool IsLocal(const struct statfs &info) {
|
2015-02-24 13:14:49 +08:00
|
|
|
#ifdef __linux__
|
2016-09-07 04:57:50 +08:00
|
|
|
#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;
|
|
|
|
}
|
2015-02-24 13:14:49 +08:00
|
|
|
#else
|
2016-09-07 04:57:50 +08:00
|
|
|
return (info.f_flags & MNT_LOCAL) != 0;
|
2015-02-24 13:14:49 +08:00
|
|
|
#endif
|
|
|
|
}
|
2015-09-09 09:19:05 +08:00
|
|
|
#endif
|
2015-02-24 13:14:49 +08:00
|
|
|
|
2015-09-09 09:19:05 +08:00
|
|
|
#if defined(__NetBSD__)
|
2016-09-07 04:57:50 +08:00
|
|
|
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;
|
2015-09-09 09:19:05 +08:00
|
|
|
}
|
|
|
|
#else
|
2016-09-07 04:57:50 +08:00
|
|
|
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;
|
2015-02-24 07:47:09 +08:00
|
|
|
}
|
2015-09-09 09:19:05 +08:00
|
|
|
#endif
|
2016-03-23 01:58:09 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
FILE *FileSystem::Fopen(const char *path, const char *mode) {
|
|
|
|
return ::fopen(path, mode);
|
2016-03-23 01:58:09 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
int FileSystem::Stat(const char *path, struct stat *stats) {
|
|
|
|
return ::stat(path, stats);
|
2016-03-23 01:58:09 +08:00
|
|
|
}
|