-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());
+std::error_code remove_directories(const Twine &path, bool IgnoreErrors) {
+ auto EC = remove_directories_impl(path, IgnoreErrors);
+ if (EC && !IgnoreErrors)
+ return EC;
+ EC = fs::remove(path, true);
+ if (EC && !IgnoreErrors)
+ return EC;
+ return std::error_code();
+}