diff options
Diffstat (limited to 'lib/Support/Unix/Path.inc')
-rw-r--r-- | lib/Support/Unix/Path.inc | 92 |
1 files changed, 22 insertions, 70 deletions
diff --git a/lib/Support/Unix/Path.inc b/lib/Support/Unix/Path.inc index 45097eb918b7..2ecb97316c87 100644 --- a/lib/Support/Unix/Path.inc +++ b/lib/Support/Unix/Path.inc @@ -108,10 +108,9 @@ using namespace llvm; namespace llvm { namespace sys { namespace fs { -#if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \ - defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \ - defined(__linux__) || defined(__CYGWIN__) || defined(__DragonFly__) || \ - defined(_AIX) +#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \ + defined(__minix) || defined(__FreeBSD_kernel__) || defined(__linux__) || \ + defined(__CYGWIN__) || defined(__DragonFly__) || defined(_AIX) static int test_dir(char ret[PATH_MAX], const char *dir, const char *bin) { @@ -180,9 +179,9 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) { if (realpath(exe_path, link_path)) return link_path; } -#elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \ - defined(__OpenBSD__) || defined(__minix) || defined(__DragonFly__) || \ - defined(__FreeBSD_kernel__) || defined(_AIX) +#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \ + defined(__minix) || defined(__DragonFly__) || \ + defined(__FreeBSD_kernel__) || defined(_AIX) char exe_path[PATH_MAX]; if (getprogpath(exe_path, argv0) != NULL) @@ -218,11 +217,11 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) { return ""; } -TimePoint<> file_status::getLastAccessedTime() const { +TimePoint<> basic_file_status::getLastAccessedTime() const { return toTimePoint(fs_st_atime); } -TimePoint<> file_status::getLastModificationTime() const { +TimePoint<> basic_file_status::getLastModificationTime() const { return toTimePoint(fs_st_mtime); } @@ -427,7 +426,7 @@ std::error_code resize_file(int FD, uint64_t Size) { // If we have posix_fallocate use it. Unlike ftruncate it always allocates // space, so we get an error if the disk is full. if (int Err = ::posix_fallocate(FD, 0, Size)) { - if (Err != EOPNOTSUPP) + if (Err != EINVAL && Err != EOPNOTSUPP) return std::error_code(Err, std::generic_category()); } #endif @@ -642,15 +641,9 @@ std::error_code mapped_file_region::init(int FD, uint64_t Offset, return std::error_code(); } -mapped_file_region::mapped_file_region(int fd, mapmode mode, uint64_t length, +mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length, uint64_t offset, std::error_code &ec) : Size(length), Mapping() { - // Make sure that the requested size fits within SIZE_T. - if (length > std::numeric_limits<size_t>::max()) { - ec = make_error_code(errc::invalid_argument); - return; - } - ec = init(fd, offset, mode); if (ec) Mapping = nullptr; @@ -661,7 +654,7 @@ mapped_file_region::~mapped_file_region() { ::munmap(Mapping, Size); } -uint64_t mapped_file_region::size() const { +size_t mapped_file_region::size() const { assert(Mapping && "Mapping failed but used anyway!"); return Size; } @@ -720,6 +713,13 @@ std::error_code detail::directory_iterator_increment(detail::DirIterState &it) { return std::error_code(); } +ErrorOr<basic_file_status> directory_entry::status() const { + file_status s; + if (auto EC = fs::status(Path, s, FollowSymlinks)) + return EC; + return s; +} + #if !defined(F_GETPATH) static bool hasProcSelfFD() { // If we have a /proc filesystem mounted, we can quickly establish the @@ -808,53 +808,6 @@ std::error_code openFileForWrite(const Twine &Name, int &ResultFD, return std::error_code(); } -std::error_code getPathFromOpenFD(int FD, SmallVectorImpl<char> &ResultPath) { - if (FD < 0) - return make_error_code(errc::bad_file_descriptor); - -#if defined(F_GETPATH) - // When F_GETPATH is availble, it is the quickest way to get - // the path from a file descriptor. - ResultPath.reserve(MAXPATHLEN); - if (::fcntl(FD, F_GETPATH, ResultPath.begin()) == -1) - return std::error_code(errno, std::generic_category()); - - ResultPath.set_size(strlen(ResultPath.begin())); -#else - // If we have a /proc filesystem mounted, we can quickly establish the - // real name of the file with readlink. Otherwise, we don't know how to - // get the filename from a file descriptor. Give up. - if (!fs::hasProcSelfFD()) - return make_error_code(errc::function_not_supported); - - ResultPath.reserve(PATH_MAX); - char ProcPath[64]; - snprintf(ProcPath, sizeof(ProcPath), "/proc/self/fd/%d", FD); - ssize_t CharCount = ::readlink(ProcPath, ResultPath.begin(), ResultPath.capacity()); - if (CharCount < 0) - return std::error_code(errno, std::generic_category()); - - // Was the filename truncated? - if (static_cast<size_t>(CharCount) == ResultPath.capacity()) { - // Use lstat to get the size of the filename - struct stat sb; - if (::lstat(ProcPath, &sb) < 0) - return std::error_code(errno, std::generic_category()); - - ResultPath.reserve(sb.st_size + 1); - CharCount = ::readlink(ProcPath, ResultPath.begin(), ResultPath.capacity()); - if (CharCount < 0) - return std::error_code(errno, std::generic_category()); - - // Test for race condition: did the link size change? - if (CharCount > sb.st_size) - return std::error_code(ENAMETOOLONG, std::generic_category()); - } - ResultPath.set_size(static_cast<size_t>(CharCount)); -#endif - return std::error_code(); -} - template <typename T> static std::error_code remove_directories_impl(const T &Entry, bool IgnoreErrors) { @@ -863,12 +816,11 @@ static std::error_code remove_directories_impl(const T &Entry, directory_iterator End; while (Begin != End) { auto &Item = *Begin; - file_status st; - EC = Item.status(st); - if (EC && !IgnoreErrors) - return EC; + ErrorOr<basic_file_status> st = Item.status(); + if (!st && !IgnoreErrors) + return st.getError(); - if (is_directory(st)) { + if (is_directory(*st)) { EC = remove_directories_impl(Item, IgnoreErrors); if (EC && !IgnoreErrors) return EC; |