2014-08-16 06:04:21 +08:00
|
|
|
//===-- FileSystem.cpp ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2014-08-16 06:04:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Host/FileSystem.h"
|
|
|
|
|
|
|
|
// C includes
|
2015-06-28 07:11:34 +08:00
|
|
|
#include <dirent.h>
|
2018-11-03 01:34:16 +08:00
|
|
|
#include <fcntl.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>
|
2017-07-18 21:14:01 +08:00
|
|
|
#include <unistd.h>
|
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/Host/Host.h"
|
2017-05-12 12:51:55 +08:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/StreamString.h"
|
2014-08-16 06:04:21 +08:00
|
|
|
|
2019-03-22 03:35:55 +08:00
|
|
|
#include "llvm/Support/Errno.h"
|
2017-03-09 01:56:08 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
|
2014-08-16 06:04:21 +08:00
|
|
|
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
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
|
|
|
|
Status error;
|
2016-09-07 04:57:50 +08:00
|
|
|
if (::symlink(dst.GetCString(), src.GetCString()) == -1)
|
|
|
|
error.SetErrorToErrno();
|
|
|
|
return error;
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status FileSystem::Readlink(const FileSpec &src, FileSpec &dst) {
|
|
|
|
Status error;
|
2016-09-07 04:57:50 +08:00
|
|
|
char buf[PATH_MAX];
|
|
|
|
ssize_t count = ::readlink(src.GetCString(), buf, sizeof(buf) - 1);
|
|
|
|
if (count < 0)
|
|
|
|
error.SetErrorToErrno();
|
|
|
|
else {
|
|
|
|
buf[count] = '\0'; // Success
|
2018-11-02 05:05:36 +08:00
|
|
|
dst.SetFile(buf, FileSpec::Style::native);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
return error;
|
2014-08-16 06:04:21 +08:00
|
|
|
}
|
2015-02-24 07:47:09 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) {
|
2016-09-07 04:57:50 +08:00
|
|
|
char resolved_path[PATH_MAX];
|
|
|
|
if (!src.GetPath(resolved_path, sizeof(resolved_path))) {
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status("Couldn't get the canonical path for %s", src.GetCString());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char real_path[PATH_MAX + 1];
|
|
|
|
if (realpath(resolved_path, real_path) == nullptr) {
|
2017-05-12 12:51:55 +08:00
|
|
|
Status err;
|
2016-09-07 04:57:50 +08:00
|
|
|
err.SetErrorToErrno();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-11-02 05:05:36 +08:00
|
|
|
dst = FileSpec(real_path);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status();
|
2015-09-19 06:24:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
FILE *FileSystem::Fopen(const char *path, const char *mode) {
|
2019-03-22 03:35:55 +08:00
|
|
|
return llvm::sys::RetryAfterSignal(nullptr, ::fopen, path, mode);
|
2016-03-23 01:58:09 +08:00
|
|
|
}
|
2018-11-03 01:34:16 +08:00
|
|
|
|
|
|
|
int FileSystem::Open(const char *path, int flags, int mode) {
|
2019-03-22 03:35:55 +08:00
|
|
|
return llvm::sys::RetryAfterSignal(-1, ::open, path, flags, mode);
|
2018-11-03 01:34:16 +08:00
|
|
|
}
|