2019-08-21 00:14:12 +00:00
|
|
|
#ifndef FILESYSTEM_TEST_HELPER_H
|
|
|
|
#define FILESYSTEM_TEST_HELPER_H
|
2016-06-17 19:46:40 +00:00
|
|
|
|
2023-11-28 18:41:59 -05:00
|
|
|
#include <filesystem>
|
2019-01-15 19:14:15 +00:00
|
|
|
|
2020-10-15 10:32:09 -04:00
|
|
|
#include <sys/stat.h> // for stat, mkdir, mkfifo
|
2020-10-13 21:42:31 +03:00
|
|
|
#ifndef _WIN32
|
2020-05-25 19:08:49 +03:00
|
|
|
#include <unistd.h> // for ftruncate, link, symlink, getcwd, chdir
|
2020-10-21 13:42:48 +03:00
|
|
|
#include <sys/statvfs.h>
|
2020-10-13 21:42:31 +03:00
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#include <direct.h>
|
|
|
|
#include <windows.h> // for CreateSymbolicLink, CreateHardLink
|
|
|
|
#endif
|
2019-01-15 19:14:15 +00:00
|
|
|
|
2016-06-17 19:46:40 +00:00
|
|
|
#include <cassert>
|
2023-03-19 23:10:37 +01:00
|
|
|
#include <cerrno>
|
2021-12-23 12:03:36 +01:00
|
|
|
#include <chrono>
|
2022-12-01 14:48:20 -08:00
|
|
|
#include <cstdint>
|
2016-06-17 19:46:40 +00:00
|
|
|
#include <cstdio> // for printf
|
2024-07-12 09:17:00 -05:00
|
|
|
#include <cstring>
|
2016-06-17 19:46:40 +00:00
|
|
|
#include <string>
|
2021-12-23 12:03:36 +01:00
|
|
|
#include <system_error>
|
2023-02-12 12:32:36 +01:00
|
|
|
#include <type_traits>
|
[libc++] Implement Directory Entry Caching -- Sort of.
Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.
The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:
* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`. This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.
As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.
@BillyONeal has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.
Some additional semantics which differ from the Windows implementation:
1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).
It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).
If the changes to the specification don't get accepted, we'll be able to make the changes later.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html
Reviewers: mclow.lists, gromer, ldionne, aaron.ballman
Subscribers: BillyONeal, christof, cfe-commits
Differential Revision: https://reviews.llvm.org/D49530
llvm-svn: 337516
2018-07-20 01:22:32 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-01-21 13:35:30 +01:00
|
|
|
#include "assert_macros.h"
|
2020-12-16 09:43:14 +01:00
|
|
|
#include "make_string.h"
|
2018-07-23 02:00:52 +00:00
|
|
|
#include "test_macros.h"
|
2019-08-21 00:14:12 +00:00
|
|
|
#include "format_string.h"
|
2016-06-17 19:46:40 +00:00
|
|
|
|
2020-03-30 15:53:16 -04:00
|
|
|
// For creating socket files
|
2020-10-19 11:07:31 +03:00
|
|
|
#if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
|
2020-03-30 15:53:16 -04:00
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <sys/un.h>
|
|
|
|
#endif
|
2023-11-28 18:41:59 -05:00
|
|
|
namespace fs = std::filesystem;
|
2020-03-30 15:53:16 -04:00
|
|
|
|
2020-10-15 10:32:09 -04:00
|
|
|
namespace utils {
|
2020-10-13 21:42:31 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
inline int mkdir(const char* path, int mode) { (void)mode; return ::_mkdir(path); }
|
|
|
|
inline int symlink(const char* oldname, const char* newname, bool is_dir) {
|
|
|
|
DWORD flags = is_dir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0;
|
|
|
|
if (CreateSymbolicLinkA(newname, oldname,
|
|
|
|
flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE))
|
|
|
|
return 0;
|
|
|
|
if (GetLastError() != ERROR_INVALID_PARAMETER)
|
|
|
|
return 1;
|
|
|
|
return !CreateSymbolicLinkA(newname, oldname, flags);
|
|
|
|
}
|
|
|
|
inline int link(const char *oldname, const char* newname) {
|
|
|
|
return !CreateHardLinkA(newname, oldname, NULL);
|
|
|
|
}
|
2020-10-18 23:19:29 +03:00
|
|
|
inline int setenv(const char *var, const char *val, int overwrite) {
|
|
|
|
(void)overwrite;
|
|
|
|
return ::_putenv((std::string(var) + "=" + std::string(val)).c_str());
|
|
|
|
}
|
|
|
|
inline int unsetenv(const char *var) {
|
|
|
|
return ::_putenv((std::string(var) + "=").c_str());
|
|
|
|
}
|
2020-10-21 13:42:48 +03:00
|
|
|
inline bool space(std::string path, std::uintmax_t &capacity,
|
|
|
|
std::uintmax_t &free, std::uintmax_t &avail) {
|
|
|
|
ULARGE_INTEGER FreeBytesAvailableToCaller, TotalNumberOfBytes,
|
|
|
|
TotalNumberOfFreeBytes;
|
|
|
|
if (!GetDiskFreeSpaceExA(path.c_str(), &FreeBytesAvailableToCaller,
|
|
|
|
&TotalNumberOfBytes, &TotalNumberOfFreeBytes))
|
|
|
|
return false;
|
|
|
|
capacity = TotalNumberOfBytes.QuadPart;
|
|
|
|
free = TotalNumberOfFreeBytes.QuadPart;
|
|
|
|
avail = FreeBytesAvailableToCaller.QuadPart;
|
|
|
|
assert(capacity > 0);
|
|
|
|
assert(free > 0);
|
|
|
|
assert(avail > 0);
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-13 21:42:31 +03:00
|
|
|
#else
|
|
|
|
using ::mkdir;
|
|
|
|
inline int symlink(const char* oldname, const char* newname, bool is_dir) { (void)is_dir; return ::symlink(oldname, newname); }
|
|
|
|
using ::link;
|
2020-11-06 09:34:17 +02:00
|
|
|
using ::setenv;
|
|
|
|
using ::unsetenv;
|
2020-10-21 13:42:48 +03:00
|
|
|
inline bool space(std::string path, std::uintmax_t &capacity,
|
|
|
|
std::uintmax_t &free, std::uintmax_t &avail) {
|
|
|
|
struct statvfs expect;
|
|
|
|
if (::statvfs(path.c_str(), &expect) == -1)
|
|
|
|
return false;
|
|
|
|
assert(expect.f_bavail > 0);
|
|
|
|
assert(expect.f_bfree > 0);
|
|
|
|
assert(expect.f_bsize > 0);
|
|
|
|
assert(expect.f_blocks > 0);
|
|
|
|
assert(expect.f_frsize > 0);
|
|
|
|
auto do_mult = [&](std::uintmax_t val) {
|
|
|
|
std::uintmax_t fsize = expect.f_frsize;
|
|
|
|
std::uintmax_t new_val = val * fsize;
|
|
|
|
assert(new_val / fsize == val); // Test for overflow
|
|
|
|
return new_val;
|
|
|
|
};
|
|
|
|
capacity = do_mult(expect.f_blocks);
|
|
|
|
free = do_mult(expect.f_bfree);
|
|
|
|
avail = do_mult(expect.f_bavail);
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-13 21:42:31 +03:00
|
|
|
#endif
|
|
|
|
|
2022-12-01 14:48:20 -08:00
|
|
|
// N.B. libc might define some of the foo[64] identifiers using macros from
|
|
|
|
// foo64 -> foo or vice versa.
|
|
|
|
#if defined(_WIN32)
|
2023-03-12 17:11:29 +01:00
|
|
|
using off64_t = std::int64_t;
|
2022-12-01 14:48:20 -08:00
|
|
|
#elif defined(__MVS__) || defined(__LP64__)
|
|
|
|
using off64_t = ::off_t;
|
|
|
|
#else
|
|
|
|
using ::off64_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
inline FILE* fopen64(const char* pathname, const char* mode) {
|
|
|
|
// Bionic does not distinguish between fopen and fopen64, but fopen64
|
|
|
|
// wasn't added until API 24.
|
|
|
|
#if defined(_WIN32) || defined(__MVS__) || defined(__LP64__) || defined(__BIONIC__)
|
|
|
|
return ::fopen(pathname, mode);
|
|
|
|
#else
|
|
|
|
return ::fopen64(pathname, mode);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int ftruncate64(int fd, off64_t length) {
|
|
|
|
#if defined(_WIN32)
|
|
|
|
// _chsize_s sets errno on failure and also returns the error number.
|
|
|
|
return ::_chsize_s(fd, length) ? -1 : 0;
|
|
|
|
#elif defined(__MVS__) || defined(__LP64__)
|
|
|
|
return ::ftruncate(fd, length);
|
|
|
|
#else
|
|
|
|
return ::ftruncate64(fd, length);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-15 10:32:09 -04:00
|
|
|
inline std::string getcwd() {
|
|
|
|
// Assume that path lengths are not greater than this.
|
|
|
|
// This should be fine for testing purposes.
|
|
|
|
char buf[4096];
|
|
|
|
char* ret = ::getcwd(buf, sizeof(buf));
|
|
|
|
assert(ret && "getcwd failed");
|
|
|
|
return std::string(ret);
|
|
|
|
}
|
2018-07-22 02:00:53 +00:00
|
|
|
|
2020-10-15 10:32:09 -04:00
|
|
|
inline bool exists(std::string const& path) {
|
|
|
|
struct ::stat tmp;
|
|
|
|
return ::stat(path.c_str(), &tmp) == 0;
|
|
|
|
}
|
2024-09-05 12:39:05 -04:00
|
|
|
} // namespace utils
|
2018-07-22 02:00:53 +00:00
|
|
|
|
2016-06-17 19:46:40 +00:00
|
|
|
struct scoped_test_env
|
|
|
|
{
|
2020-10-15 10:32:09 -04:00
|
|
|
scoped_test_env() : test_root(available_cwd_path()) {
|
2020-10-13 21:42:31 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Windows mkdir can create multiple recursive directories
|
|
|
|
// if needed.
|
|
|
|
std::string cmd = "mkdir " + test_root.string();
|
|
|
|
#else
|
|
|
|
std::string cmd = "mkdir -p " + test_root.string();
|
|
|
|
#endif
|
2020-03-30 15:53:16 -04:00
|
|
|
int ret = std::system(cmd.c_str());
|
|
|
|
assert(ret == 0);
|
2020-03-24 17:06:29 -04:00
|
|
|
|
|
|
|
// Ensure that the root_path is fully resolved, i.e. it contains no
|
|
|
|
// symlinks. The filesystem tests depend on that. We do this after
|
|
|
|
// creating the root_path, because `fs::canonical` requires the
|
|
|
|
// path to exist.
|
|
|
|
test_root = fs::canonical(test_root);
|
|
|
|
}
|
2016-06-17 19:46:40 +00:00
|
|
|
|
2020-03-30 15:53:16 -04:00
|
|
|
~scoped_test_env() {
|
2020-10-13 21:42:31 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
std::string cmd = "rmdir /s /q " + test_root.string();
|
2020-10-26 16:33:15 -04:00
|
|
|
int ret = std::system(cmd.c_str());
|
|
|
|
assert(ret == 0);
|
2021-12-13 11:00:56 -05:00
|
|
|
#else
|
|
|
|
#if defined(__MVS__)
|
|
|
|
// The behaviour of chmod -R on z/OS prevents recursive
|
|
|
|
// permission change for directories that do not have read permission.
|
|
|
|
std::string cmd = "find " + test_root.string() + " -exec chmod 777 {} \\;";
|
2020-10-13 21:42:31 +03:00
|
|
|
#else
|
|
|
|
std::string cmd = "chmod -R 777 " + test_root.string();
|
2021-12-13 11:00:56 -05:00
|
|
|
#endif // defined(__MVS__)
|
2020-03-30 15:53:16 -04:00
|
|
|
int ret = std::system(cmd.c_str());
|
2023-10-19 17:27:01 -07:00
|
|
|
# if !defined(_AIX) && !defined(__ANDROID__)
|
2022-01-21 11:05:31 -05:00
|
|
|
// On AIX the chmod command will return non-zero when trying to set
|
|
|
|
// the permissions on a directory that contains a bad symlink. This triggers
|
|
|
|
// the assert, despite being able to delete everything with the following
|
|
|
|
// `rm -r` command.
|
2023-10-19 17:27:01 -07:00
|
|
|
//
|
|
|
|
// Android's chmod was buggy in old OSs, but skipping this assert is
|
|
|
|
// sufficient to ensure that the `rm -rf` succeeds for almost all tests:
|
|
|
|
// - Android L: chmod aborts after one error
|
|
|
|
// - Android L and M: chmod -R tries to set permissions of a symlink
|
|
|
|
// target.
|
|
|
|
// LIBCXX-ANDROID-FIXME: Other fixes to consider: place a toybox chmod
|
|
|
|
// onto old devices, re-enable this assert for devices running Android N
|
|
|
|
// and up, rewrite this chmod+rm in C or C++.
|
2020-03-30 15:53:16 -04:00
|
|
|
assert(ret == 0);
|
2023-10-19 17:27:01 -07:00
|
|
|
# endif
|
2020-03-30 15:53:16 -04:00
|
|
|
|
2021-12-13 11:00:56 -05:00
|
|
|
cmd = "rm -rf " + test_root.string();
|
2020-03-30 15:53:16 -04:00
|
|
|
ret = std::system(cmd.c_str());
|
|
|
|
assert(ret == 0);
|
2020-10-26 16:33:15 -04:00
|
|
|
#endif
|
2020-03-30 15:53:16 -04:00
|
|
|
}
|
2016-06-17 19:46:40 +00:00
|
|
|
|
|
|
|
scoped_test_env(scoped_test_env const &) = delete;
|
|
|
|
scoped_test_env & operator=(scoped_test_env const &) = delete;
|
|
|
|
|
|
|
|
fs::path make_env_path(std::string p) { return sanitize_path(p); }
|
|
|
|
|
|
|
|
std::string sanitize_path(std::string raw) {
|
|
|
|
assert(raw.find("..") == std::string::npos);
|
2020-10-13 21:42:31 +03:00
|
|
|
std::string root = test_root.string();
|
2016-06-17 19:46:40 +00:00
|
|
|
if (root.compare(0, root.size(), raw, 0, root.size()) != 0) {
|
|
|
|
assert(raw.front() != '\\');
|
|
|
|
fs::path tmp(test_root);
|
|
|
|
tmp /= raw;
|
2020-10-13 21:42:31 +03:00
|
|
|
return tmp.string();
|
2016-06-17 19:46:40 +00:00
|
|
|
}
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
2019-01-15 19:14:15 +00:00
|
|
|
// Purposefully using a size potentially larger than off_t here so we can
|
|
|
|
// test the behavior of libc++fs when it is built with _FILE_OFFSET_BITS=64
|
|
|
|
// but the caller is not (std::filesystem also uses uintmax_t rather than
|
|
|
|
// off_t). On a 32-bit system this allows us to create a file larger than
|
|
|
|
// 2GB.
|
2023-03-24 17:18:11 +01:00
|
|
|
std::string create_file(fs::path filename_path, std::uintmax_t size = 0) {
|
2022-12-01 14:48:20 -08:00
|
|
|
std::string filename = sanitize_path(filename_path.string());
|
2019-01-15 19:14:15 +00:00
|
|
|
|
2020-11-26 08:58:44 +01:00
|
|
|
if (size >
|
2022-12-01 14:48:20 -08:00
|
|
|
static_cast<typename std::make_unsigned<utils::off64_t>::type>(
|
|
|
|
std::numeric_limits<utils::off64_t>::max())) {
|
2024-07-12 09:17:00 -05:00
|
|
|
std::fprintf(stderr, "create_file(%s, %ju) too large\n",
|
|
|
|
filename.c_str(), size);
|
|
|
|
std::abort();
|
2016-06-17 19:46:40 +00:00
|
|
|
}
|
2019-01-15 19:14:15 +00:00
|
|
|
|
2021-11-18 15:57:50 -05:00
|
|
|
#if defined(_WIN32) || defined(__MVS__)
|
|
|
|
# define FOPEN_CLOEXEC_FLAG ""
|
2020-10-13 21:42:31 +03:00
|
|
|
#else
|
2021-11-18 15:57:50 -05:00
|
|
|
# define FOPEN_CLOEXEC_FLAG "e"
|
2020-10-13 21:42:31 +03:00
|
|
|
#endif
|
2022-12-01 14:48:20 -08:00
|
|
|
FILE* file = utils::fopen64(filename.c_str(), "w" FOPEN_CLOEXEC_FLAG);
|
2019-01-15 19:14:15 +00:00
|
|
|
if (file == nullptr) {
|
2024-07-12 09:17:00 -05:00
|
|
|
std::fprintf(stderr, "fopen %s failed: %s\n", filename.c_str(),
|
|
|
|
std::strerror(errno));
|
|
|
|
std::abort();
|
2019-01-15 19:14:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 14:48:20 -08:00
|
|
|
if (utils::ftruncate64(
|
|
|
|
fileno(file), static_cast<utils::off64_t>(size)) == -1) {
|
2024-07-12 09:17:00 -05:00
|
|
|
std::fprintf(stderr, "ftruncate %s %ju failed: %s\n", filename.c_str(),
|
|
|
|
size, std::strerror(errno));
|
|
|
|
std::fclose(file);
|
|
|
|
std::abort();
|
2019-01-15 19:14:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 09:17:00 -05:00
|
|
|
std::fclose(file);
|
2016-06-17 19:46:40 +00:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:42:31 +03:00
|
|
|
std::string create_dir(fs::path filename_path) {
|
|
|
|
std::string filename = filename_path.string();
|
2016-06-17 19:46:40 +00:00
|
|
|
filename = sanitize_path(std::move(filename));
|
2020-10-13 21:42:31 +03:00
|
|
|
int ret = utils::mkdir(filename.c_str(), 0777); // rwxrwxrwx mode
|
2020-03-30 15:53:16 -04:00
|
|
|
assert(ret == 0);
|
2016-06-17 19:46:40 +00:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:59:13 +02:00
|
|
|
std::string create_file_dir_symlink(fs::path source_path,
|
|
|
|
fs::path to_path,
|
|
|
|
bool sanitize_source = true,
|
|
|
|
bool is_dir = false) {
|
2020-10-13 21:42:31 +03:00
|
|
|
std::string source = source_path.string();
|
|
|
|
std::string to = to_path.string();
|
2020-05-25 19:08:49 +03:00
|
|
|
if (sanitize_source)
|
|
|
|
source = sanitize_path(std::move(source));
|
2016-06-17 19:46:40 +00:00
|
|
|
to = sanitize_path(std::move(to));
|
2020-10-13 21:42:31 +03:00
|
|
|
int ret = utils::symlink(source.c_str(), to.c_str(), is_dir);
|
2020-03-30 15:53:16 -04:00
|
|
|
assert(ret == 0);
|
2016-06-17 19:46:40 +00:00
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:59:13 +02:00
|
|
|
std::string create_symlink(fs::path source_path,
|
|
|
|
fs::path to_path,
|
|
|
|
bool sanitize_source = true) {
|
|
|
|
return create_file_dir_symlink(source_path, to_path, sanitize_source,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string create_directory_symlink(fs::path source_path,
|
|
|
|
fs::path to_path,
|
|
|
|
bool sanitize_source = true) {
|
|
|
|
return create_file_dir_symlink(source_path, to_path, sanitize_source,
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:42:31 +03:00
|
|
|
std::string create_hardlink(fs::path source_path, fs::path to_path) {
|
|
|
|
std::string source = source_path.string();
|
|
|
|
std::string to = to_path.string();
|
2016-06-17 19:46:40 +00:00
|
|
|
source = sanitize_path(std::move(source));
|
|
|
|
to = sanitize_path(std::move(to));
|
2020-10-13 21:42:31 +03:00
|
|
|
int ret = utils::link(source.c_str(), to.c_str());
|
2020-03-30 15:53:16 -04:00
|
|
|
assert(ret == 0);
|
2016-06-17 19:46:40 +00:00
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:42:31 +03:00
|
|
|
#ifndef _WIN32
|
2016-06-17 19:46:40 +00:00
|
|
|
std::string create_fifo(std::string file) {
|
|
|
|
file = sanitize_path(std::move(file));
|
2020-05-25 19:08:49 +03:00
|
|
|
int ret = ::mkfifo(file.c_str(), 0666); // rw-rw-rw- mode
|
2020-03-30 15:53:16 -04:00
|
|
|
assert(ret == 0);
|
2016-06-17 19:46:40 +00:00
|
|
|
return file;
|
|
|
|
}
|
2020-10-13 21:42:31 +03:00
|
|
|
#endif
|
2016-06-17 19:46:40 +00:00
|
|
|
|
2020-10-19 11:07:31 +03:00
|
|
|
// Some platforms doesn't support socket files so we shouldn't even
|
2016-06-17 19:46:40 +00:00
|
|
|
// allow tests to call this unguarded.
|
2020-10-19 11:07:31 +03:00
|
|
|
#if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
|
2016-06-17 19:46:40 +00:00
|
|
|
std::string create_socket(std::string file) {
|
2024-01-09 11:15:44 -08:00
|
|
|
file = sanitize_path(std::move(file));
|
|
|
|
|
|
|
|
::sockaddr_un address;
|
|
|
|
address.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
// If file.size() is too big, try to create a file directly inside
|
|
|
|
// /tmp to make sure file path is short enough.
|
|
|
|
// Android platform warns about tmpnam, since the problem does not appear
|
|
|
|
// on Android, let's not apply it for Android.
|
|
|
|
# if !defined(__ANDROID__)
|
2024-01-12 08:48:34 -08:00
|
|
|
if (file.size() > sizeof(address.sun_path)) {
|
2024-01-09 11:15:44 -08:00
|
|
|
file = std::tmpnam(nullptr);
|
2016-06-17 19:46:40 +00:00
|
|
|
}
|
2024-01-09 11:15:44 -08:00
|
|
|
# endif
|
|
|
|
assert(file.size() <= sizeof(address.sun_path));
|
|
|
|
::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
|
|
|
|
int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
|
|
|
|
return file;
|
|
|
|
}
|
2016-06-17 19:46:40 +00:00
|
|
|
#endif
|
|
|
|
|
2020-03-24 17:06:29 -04:00
|
|
|
fs::path test_root;
|
2016-06-17 19:46:40 +00:00
|
|
|
|
|
|
|
private:
|
2020-10-15 10:32:09 -04:00
|
|
|
// This could potentially introduce a filesystem race if multiple
|
|
|
|
// scoped_test_envs were created concurrently in the same test (hence
|
|
|
|
// sharing the same cwd). However, it is fairly unlikely to happen as
|
|
|
|
// we generally don't use scoped_test_env from multiple threads, so
|
|
|
|
// this is deemed acceptable.
|
[libcxx] [test] Avoid race conditions between tests regarding temp directories
Prior to e0d01294bc124211a8ffb55e69162eb34a242680, all tests used a
random directory name, but now it is deterministic, based on the
test name. This change was done under the assumption that the filename
portion of the cwd is unique across tests that use the filesystem
test temporary directories.
When running tests locally, the cwd of the test is something like
"<build-dir>/test/<test path>/Output/copy_assign.pass.cpp.dir",
and the filename portion, "copy_assign.pass.cpp.dir", is used as
base for the temp directory names.
The change noted that there's a risk for race conditions if multiple
threads within one test try to create temp directories in parallel, but
that doesn't really happen in practice.
However, if running tests with a large number of parallel workers,
multiple tests with the same filename portion, e.g. "copy_assign.pass.cpp.dir",
can run in parallel, leading to race conditions across processes.
Therefore, add a hash of the full cwd to distinguish such cases
from each other.
Secondly, don't use two separate levels of temporary directories
(<base>/static_env.0). When cleaning up, only the individual
directory is removed, leaving the empty intermediate directory
behind littering the temp directory.
Differential Revision: https://reviews.llvm.org/D98703
2021-03-16 14:37:28 +02:00
|
|
|
// The cwd.filename() itself isn't unique across all tests in the suite,
|
|
|
|
// so start the numbering from a hash of the full cwd, to avoid
|
|
|
|
// different tests interfering with each other.
|
2020-10-15 10:32:09 -04:00
|
|
|
static inline fs::path available_cwd_path() {
|
|
|
|
fs::path const cwd = utils::getcwd();
|
|
|
|
fs::path const tmp = fs::temp_directory_path();
|
[libcxx] [test] Avoid race conditions between tests regarding temp directories
Prior to e0d01294bc124211a8ffb55e69162eb34a242680, all tests used a
random directory name, but now it is deterministic, based on the
test name. This change was done under the assumption that the filename
portion of the cwd is unique across tests that use the filesystem
test temporary directories.
When running tests locally, the cwd of the test is something like
"<build-dir>/test/<test path>/Output/copy_assign.pass.cpp.dir",
and the filename portion, "copy_assign.pass.cpp.dir", is used as
base for the temp directory names.
The change noted that there's a risk for race conditions if multiple
threads within one test try to create temp directories in parallel, but
that doesn't really happen in practice.
However, if running tests with a large number of parallel workers,
multiple tests with the same filename portion, e.g. "copy_assign.pass.cpp.dir",
can run in parallel, leading to race conditions across processes.
Therefore, add a hash of the full cwd to distinguish such cases
from each other.
Secondly, don't use two separate levels of temporary directories
(<base>/static_env.0). When cleaning up, only the individual
directory is removed, leaving the empty intermediate directory
behind littering the temp directory.
Differential Revision: https://reviews.llvm.org/D98703
2021-03-16 14:37:28 +02:00
|
|
|
std::string base = cwd.filename().string();
|
2023-03-14 21:27:03 +01:00
|
|
|
std::size_t i = std::hash<std::string>()(cwd.string());
|
[libcxx] [test] Avoid race conditions between tests regarding temp directories
Prior to e0d01294bc124211a8ffb55e69162eb34a242680, all tests used a
random directory name, but now it is deterministic, based on the
test name. This change was done under the assumption that the filename
portion of the cwd is unique across tests that use the filesystem
test temporary directories.
When running tests locally, the cwd of the test is something like
"<build-dir>/test/<test path>/Output/copy_assign.pass.cpp.dir",
and the filename portion, "copy_assign.pass.cpp.dir", is used as
base for the temp directory names.
The change noted that there's a risk for race conditions if multiple
threads within one test try to create temp directories in parallel, but
that doesn't really happen in practice.
However, if running tests with a large number of parallel workers,
multiple tests with the same filename portion, e.g. "copy_assign.pass.cpp.dir",
can run in parallel, leading to race conditions across processes.
Therefore, add a hash of the full cwd to distinguish such cases
from each other.
Secondly, don't use two separate levels of temporary directories
(<base>/static_env.0). When cleaning up, only the individual
directory is removed, leaving the empty intermediate directory
behind littering the temp directory.
Differential Revision: https://reviews.llvm.org/D98703
2021-03-16 14:37:28 +02:00
|
|
|
fs::path p = tmp / (base + "-static_env." + std::to_string(i));
|
2020-10-13 21:42:31 +03:00
|
|
|
while (utils::exists(p.string())) {
|
[libcxx] [test] Avoid race conditions between tests regarding temp directories
Prior to e0d01294bc124211a8ffb55e69162eb34a242680, all tests used a
random directory name, but now it is deterministic, based on the
test name. This change was done under the assumption that the filename
portion of the cwd is unique across tests that use the filesystem
test temporary directories.
When running tests locally, the cwd of the test is something like
"<build-dir>/test/<test path>/Output/copy_assign.pass.cpp.dir",
and the filename portion, "copy_assign.pass.cpp.dir", is used as
base for the temp directory names.
The change noted that there's a risk for race conditions if multiple
threads within one test try to create temp directories in parallel, but
that doesn't really happen in practice.
However, if running tests with a large number of parallel workers,
multiple tests with the same filename portion, e.g. "copy_assign.pass.cpp.dir",
can run in parallel, leading to race conditions across processes.
Therefore, add a hash of the full cwd to distinguish such cases
from each other.
Secondly, don't use two separate levels of temporary directories
(<base>/static_env.0). When cleaning up, only the individual
directory is removed, leaving the empty intermediate directory
behind littering the temp directory.
Differential Revision: https://reviews.llvm.org/D98703
2021-03-16 14:37:28 +02:00
|
|
|
p = tmp / (base + "-static_env." + std::to_string(++i));
|
2016-06-17 19:46:40 +00:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-25 19:08:49 +03:00
|
|
|
/// This class generates the following tree:
|
|
|
|
///
|
|
|
|
/// static_test_env
|
2022-08-18 17:41:13 -04:00
|
|
|
/// |-- bad_symlink -> dne
|
|
|
|
/// |-- dir1
|
|
|
|
/// | |-- dir2
|
|
|
|
/// | | |-- afile3
|
|
|
|
/// | | |-- dir3
|
|
|
|
/// | | | `-- file5
|
|
|
|
/// | | |-- file4
|
|
|
|
/// | | `-- symlink_to_dir3 -> dir3
|
|
|
|
/// | `-- file1
|
|
|
|
/// | `-- file2
|
|
|
|
/// |-- empty_file
|
|
|
|
/// |-- non_empty_file
|
|
|
|
/// |-- symlink_to_dir -> dir1
|
|
|
|
/// `-- symlink_to_empty_file -> empty_file
|
2020-05-25 19:08:49 +03:00
|
|
|
///
|
|
|
|
class static_test_env {
|
|
|
|
scoped_test_env env_;
|
|
|
|
public:
|
|
|
|
static_test_env() {
|
|
|
|
env_.create_symlink("dne", "bad_symlink", false);
|
|
|
|
env_.create_dir("dir1");
|
|
|
|
env_.create_dir("dir1/dir2");
|
|
|
|
env_.create_file("dir1/dir2/afile3");
|
|
|
|
env_.create_dir("dir1/dir2/dir3");
|
|
|
|
env_.create_file("dir1/dir2/dir3/file5");
|
|
|
|
env_.create_file("dir1/dir2/file4");
|
2020-11-09 13:59:13 +02:00
|
|
|
env_.create_directory_symlink("dir3", "dir1/dir2/symlink_to_dir3", false);
|
2020-05-25 19:08:49 +03:00
|
|
|
env_.create_file("dir1/file1");
|
|
|
|
env_.create_file("dir1/file2", 42);
|
|
|
|
env_.create_file("empty_file");
|
|
|
|
env_.create_file("non_empty_file", 42);
|
2020-11-09 13:59:13 +02:00
|
|
|
env_.create_directory_symlink("dir1", "symlink_to_dir", false);
|
2020-05-25 19:08:49 +03:00
|
|
|
env_.create_symlink("empty_file", "symlink_to_empty_file", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const fs::path Root = env_.test_root;
|
|
|
|
|
|
|
|
fs::path makePath(fs::path const& p) const {
|
|
|
|
// env_path is expected not to contain symlinks.
|
|
|
|
fs::path const& env_path = Root;
|
|
|
|
return env_path / p;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<fs::path> TestFileList = {
|
|
|
|
makePath("empty_file"),
|
|
|
|
makePath("non_empty_file"),
|
|
|
|
makePath("dir1/file1"),
|
|
|
|
makePath("dir1/file2")
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::vector<fs::path> TestDirList = {
|
|
|
|
makePath("dir1"),
|
|
|
|
makePath("dir1/dir2"),
|
|
|
|
makePath("dir1/dir2/dir3")
|
|
|
|
};
|
|
|
|
|
|
|
|
const fs::path File = TestFileList[0];
|
|
|
|
const fs::path Dir = TestDirList[0];
|
|
|
|
const fs::path Dir2 = TestDirList[1];
|
|
|
|
const fs::path Dir3 = TestDirList[2];
|
|
|
|
const fs::path SymlinkToFile = makePath("symlink_to_empty_file");
|
|
|
|
const fs::path SymlinkToDir = makePath("symlink_to_dir");
|
|
|
|
const fs::path BadSymlink = makePath("bad_symlink");
|
|
|
|
const fs::path DNE = makePath("DNE");
|
|
|
|
const fs::path EmptyFile = TestFileList[0];
|
|
|
|
const fs::path NonEmptyFile = TestFileList[1];
|
|
|
|
const fs::path CharFile = "/dev/null"; // Hopefully this exists
|
|
|
|
|
|
|
|
const std::vector<fs::path> DirIterationList = {
|
|
|
|
makePath("dir1/dir2"),
|
|
|
|
makePath("dir1/file1"),
|
|
|
|
makePath("dir1/file2")
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::vector<fs::path> DirIterationListDepth1 = {
|
|
|
|
makePath("dir1/dir2/afile3"),
|
|
|
|
makePath("dir1/dir2/dir3"),
|
|
|
|
makePath("dir1/dir2/symlink_to_dir3"),
|
|
|
|
makePath("dir1/dir2/file4"),
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::vector<fs::path> RecDirIterationList = {
|
|
|
|
makePath("dir1/dir2"),
|
|
|
|
makePath("dir1/file1"),
|
|
|
|
makePath("dir1/file2"),
|
|
|
|
makePath("dir1/dir2/afile3"),
|
|
|
|
makePath("dir1/dir2/dir3"),
|
|
|
|
makePath("dir1/dir2/symlink_to_dir3"),
|
|
|
|
makePath("dir1/dir2/file4"),
|
|
|
|
makePath("dir1/dir2/dir3/file5")
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::vector<fs::path> RecDirFollowSymlinksIterationList = {
|
|
|
|
makePath("dir1/dir2"),
|
|
|
|
makePath("dir1/file1"),
|
|
|
|
makePath("dir1/file2"),
|
|
|
|
makePath("dir1/dir2/afile3"),
|
|
|
|
makePath("dir1/dir2/dir3"),
|
|
|
|
makePath("dir1/dir2/file4"),
|
|
|
|
makePath("dir1/dir2/dir3/file5"),
|
|
|
|
makePath("dir1/dir2/symlink_to_dir3"),
|
|
|
|
makePath("dir1/dir2/symlink_to_dir3/file5"),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CWDGuard {
|
2020-10-15 10:32:09 -04:00
|
|
|
std::string oldCwd_;
|
|
|
|
CWDGuard() : oldCwd_(utils::getcwd()) { }
|
2020-10-13 15:47:31 -04:00
|
|
|
~CWDGuard() {
|
2020-10-15 10:32:09 -04:00
|
|
|
int ret = ::chdir(oldCwd_.c_str());
|
2020-05-25 19:08:49 +03:00
|
|
|
assert(ret == 0 && "chdir failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
CWDGuard(CWDGuard const&) = delete;
|
|
|
|
CWDGuard& operator=(CWDGuard const&) = delete;
|
|
|
|
};
|
|
|
|
|
2016-06-17 19:46:40 +00:00
|
|
|
// We often need to test that the error_code was cleared if no error occurs
|
2017-01-18 20:10:25 +00:00
|
|
|
// this function returns an error_code which is set to an error that will
|
2016-06-17 19:46:40 +00:00
|
|
|
// never be returned by the filesystem functions.
|
[libc++] Implement Directory Entry Caching -- Sort of.
Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.
The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:
* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`. This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.
As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.
@BillyONeal has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.
Some additional semantics which differ from the Windows implementation:
1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).
It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).
If the changes to the specification don't get accepted, we'll be able to make the changes later.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html
Reviewers: mclow.lists, gromer, ldionne, aaron.ballman
Subscribers: BillyONeal, christof, cfe-commits
Differential Revision: https://reviews.llvm.org/D49530
llvm-svn: 337516
2018-07-20 01:22:32 +00:00
|
|
|
inline std::error_code GetTestEC(unsigned Idx = 0) {
|
|
|
|
using std::errc;
|
|
|
|
auto GetErrc = [&]() {
|
|
|
|
switch (Idx) {
|
|
|
|
case 0:
|
|
|
|
return errc::address_family_not_supported;
|
|
|
|
case 1:
|
|
|
|
return errc::address_not_available;
|
|
|
|
case 2:
|
|
|
|
return errc::address_in_use;
|
|
|
|
case 3:
|
|
|
|
return errc::argument_list_too_long;
|
|
|
|
default:
|
|
|
|
assert(false && "Idx out of range");
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return std::make_error_code(GetErrc());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ErrorIsImp(const std::error_code& ec,
|
|
|
|
std::vector<std::errc> const& errors) {
|
2020-10-29 12:40:13 +02:00
|
|
|
std::error_condition cond = ec.default_error_condition();
|
[libc++] Implement Directory Entry Caching -- Sort of.
Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.
The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:
* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`. This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.
As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.
@BillyONeal has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.
Some additional semantics which differ from the Windows implementation:
1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).
It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).
If the changes to the specification don't get accepted, we'll be able to make the changes later.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html
Reviewers: mclow.lists, gromer, ldionne, aaron.ballman
Subscribers: BillyONeal, christof, cfe-commits
Differential Revision: https://reviews.llvm.org/D49530
llvm-svn: 337516
2018-07-20 01:22:32 +00:00
|
|
|
for (auto errc : errors) {
|
2020-10-29 12:40:13 +02:00
|
|
|
if (cond.value() == static_cast<int>(errc))
|
[libc++] Implement Directory Entry Caching -- Sort of.
Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.
The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:
* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`. This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.
As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.
@BillyONeal has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.
Some additional semantics which differ from the Windows implementation:
1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).
It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).
If the changes to the specification don't get accepted, we'll be able to make the changes later.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html
Reviewers: mclow.lists, gromer, ldionne, aaron.ballman
Subscribers: BillyONeal, christof, cfe-commits
Differential Revision: https://reviews.llvm.org/D49530
llvm-svn: 337516
2018-07-20 01:22:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... ErrcT>
|
|
|
|
inline bool ErrorIs(const std::error_code& ec, std::errc First, ErrcT... Rest) {
|
|
|
|
std::vector<std::errc> errors = {First, Rest...};
|
|
|
|
return ErrorIsImp(ec, errors);
|
2016-06-17 19:46:40 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 21:44:26 +00:00
|
|
|
// Provide our own Sleep routine since std::this_thread::sleep_for is not
|
|
|
|
// available in single-threaded mode.
|
2021-05-03 00:13:51 +03:00
|
|
|
template <class Dur> void SleepFor(Dur dur) {
|
2016-06-17 21:44:26 +00:00
|
|
|
using namespace std::chrono;
|
2016-06-18 18:32:26 +00:00
|
|
|
#if defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
|
|
|
|
using Clock = system_clock;
|
|
|
|
#else
|
|
|
|
using Clock = steady_clock;
|
|
|
|
#endif
|
|
|
|
const auto wake_time = Clock::now() + dur;
|
|
|
|
while (Clock::now() < wake_time)
|
2016-06-17 21:44:26 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2021-02-26 14:37:26 +02:00
|
|
|
inline fs::perms NormalizeExpectedPerms(fs::perms P) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
// On Windows, fs::perms only maps down to one bit stored in the filesystem,
|
|
|
|
// a boolean readonly flag.
|
|
|
|
// Normalize permissions to the format it gets returned; all fs entries are
|
|
|
|
// read+exec for all users; writable ones also have the write bit set for
|
|
|
|
// all users.
|
|
|
|
P |= fs::perms::owner_read | fs::perms::group_read | fs::perms::others_read;
|
|
|
|
P |= fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec;
|
|
|
|
fs::perms Write =
|
|
|
|
fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
|
|
|
|
if ((P & Write) != fs::perms::none)
|
|
|
|
P |= Write;
|
|
|
|
#endif
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
|
[libc++] Implement Directory Entry Caching -- Sort of.
Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.
The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:
* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`. This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.
As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.
@BillyONeal has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.
Some additional semantics which differ from the Windows implementation:
1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).
It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).
If the changes to the specification don't get accepted, we'll be able to make the changes later.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html
Reviewers: mclow.lists, gromer, ldionne, aaron.ballman
Subscribers: BillyONeal, christof, cfe-commits
Differential Revision: https://reviews.llvm.org/D49530
llvm-svn: 337516
2018-07-20 01:22:32 +00:00
|
|
|
struct ExceptionChecker {
|
2018-07-23 02:00:52 +00:00
|
|
|
std::errc expected_err;
|
[libc++] Implement Directory Entry Caching -- Sort of.
Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.
The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:
* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`. This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.
As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.
@BillyONeal has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.
Some additional semantics which differ from the Windows implementation:
1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).
It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).
If the changes to the specification don't get accepted, we'll be able to make the changes later.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html
Reviewers: mclow.lists, gromer, ldionne, aaron.ballman
Subscribers: BillyONeal, christof, cfe-commits
Differential Revision: https://reviews.llvm.org/D49530
llvm-svn: 337516
2018-07-20 01:22:32 +00:00
|
|
|
fs::path expected_path1;
|
|
|
|
fs::path expected_path2;
|
2018-07-23 02:00:52 +00:00
|
|
|
unsigned num_paths;
|
|
|
|
const char* func_name;
|
|
|
|
std::string opt_message;
|
|
|
|
|
2020-10-30 11:19:07 -04:00
|
|
|
explicit ExceptionChecker(std::errc first_err, const char* fun_name,
|
2018-07-23 02:00:52 +00:00
|
|
|
std::string opt_msg = {})
|
2020-10-30 11:19:07 -04:00
|
|
|
: expected_err{first_err}, num_paths(0), func_name(fun_name),
|
2018-07-23 02:00:52 +00:00
|
|
|
opt_message(opt_msg) {}
|
|
|
|
explicit ExceptionChecker(fs::path p, std::errc first_err,
|
2020-10-30 11:19:07 -04:00
|
|
|
const char* fun_name, std::string opt_msg = {})
|
2018-07-23 02:00:52 +00:00
|
|
|
: expected_err(first_err), expected_path1(p), num_paths(1),
|
2020-10-30 11:19:07 -04:00
|
|
|
func_name(fun_name), opt_message(opt_msg) {}
|
[libc++] Implement Directory Entry Caching -- Sort of.
Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.
The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:
* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`. This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.
As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.
@BillyONeal has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.
Some additional semantics which differ from the Windows implementation:
1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).
It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).
If the changes to the specification don't get accepted, we'll be able to make the changes later.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html
Reviewers: mclow.lists, gromer, ldionne, aaron.ballman
Subscribers: BillyONeal, christof, cfe-commits
Differential Revision: https://reviews.llvm.org/D49530
llvm-svn: 337516
2018-07-20 01:22:32 +00:00
|
|
|
|
|
|
|
explicit ExceptionChecker(fs::path p1, fs::path p2, std::errc first_err,
|
2020-10-30 11:19:07 -04:00
|
|
|
const char* fun_name, std::string opt_msg = {})
|
2018-07-23 02:00:52 +00:00
|
|
|
: expected_err(first_err), expected_path1(p1), expected_path2(p2),
|
2020-10-30 11:19:07 -04:00
|
|
|
num_paths(2), func_name(fun_name), opt_message(opt_msg) {}
|
[libc++] Implement Directory Entry Caching -- Sort of.
Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.
The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:
* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`. This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.
As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.
@BillyONeal has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.
Some additional semantics which differ from the Windows implementation:
1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).
It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).
If the changes to the specification don't get accepted, we'll be able to make the changes later.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html
Reviewers: mclow.lists, gromer, ldionne, aaron.ballman
Subscribers: BillyONeal, christof, cfe-commits
Differential Revision: https://reviews.llvm.org/D49530
llvm-svn: 337516
2018-07-20 01:22:32 +00:00
|
|
|
|
2018-07-23 02:00:52 +00:00
|
|
|
void operator()(fs::filesystem_error const& Err) {
|
2023-01-21 13:35:30 +01:00
|
|
|
assert(ErrorIsImp(Err.code(), {expected_err}));
|
|
|
|
assert(Err.path1() == expected_path1);
|
|
|
|
assert(Err.path2() == expected_path2);
|
2018-07-23 02:00:52 +00:00
|
|
|
LIBCPP_ONLY(check_libcxx_string(Err));
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_libcxx_string(fs::filesystem_error const& Err) {
|
|
|
|
std::string message = std::make_error_code(expected_err).message();
|
|
|
|
|
|
|
|
std::string additional_msg = "";
|
|
|
|
if (!opt_message.empty()) {
|
|
|
|
additional_msg = opt_message + ": ";
|
|
|
|
}
|
|
|
|
auto transform_path = [](const fs::path& p) {
|
2021-03-05 20:13:35 -05:00
|
|
|
return "\"" + p.string() + "\"";
|
2018-07-23 02:00:52 +00:00
|
|
|
};
|
|
|
|
std::string format = [&]() -> std::string {
|
|
|
|
switch (num_paths) {
|
|
|
|
case 0:
|
|
|
|
return format_string("filesystem error: in %s: %s%s", func_name,
|
|
|
|
additional_msg, message);
|
|
|
|
case 1:
|
|
|
|
return format_string("filesystem error: in %s: %s%s [%s]", func_name,
|
|
|
|
additional_msg, message,
|
2020-10-13 21:42:31 +03:00
|
|
|
transform_path(expected_path1).c_str());
|
2018-07-23 02:00:52 +00:00
|
|
|
case 2:
|
|
|
|
return format_string("filesystem error: in %s: %s%s [%s] [%s]",
|
|
|
|
func_name, additional_msg, message,
|
2020-10-13 21:42:31 +03:00
|
|
|
transform_path(expected_path1).c_str(),
|
|
|
|
transform_path(expected_path2).c_str());
|
2018-07-23 02:00:52 +00:00
|
|
|
default:
|
2023-01-21 13:35:30 +01:00
|
|
|
TEST_FAIL("unexpected case");
|
2018-07-23 02:00:52 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}();
|
2023-01-21 13:35:30 +01:00
|
|
|
assert(format == Err.what());
|
2018-07-23 02:00:52 +00:00
|
|
|
if (format != Err.what()) {
|
2024-07-12 09:17:00 -05:00
|
|
|
std::fprintf(stderr, "filesystem_error::what() does not match expected output:\n");
|
|
|
|
std::fprintf(stderr, " expected: \"%s\"\n", format.c_str());
|
|
|
|
std::fprintf(stderr, " actual: \"%s\"\n\n", Err.what());
|
2018-07-23 02:00:52 +00:00
|
|
|
}
|
[libc++] Implement Directory Entry Caching -- Sort of.
Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.
The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:
* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`. This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.
As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.
@BillyONeal has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.
Some additional semantics which differ from the Windows implementation:
1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).
It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).
If the changes to the specification don't get accepted, we'll be able to make the changes later.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html
Reviewers: mclow.lists, gromer, ldionne, aaron.ballman
Subscribers: BillyONeal, christof, cfe-commits
Differential Revision: https://reviews.llvm.org/D49530
llvm-svn: 337516
2018-07-20 01:22:32 +00:00
|
|
|
}
|
2018-07-23 02:00:52 +00:00
|
|
|
|
|
|
|
ExceptionChecker(ExceptionChecker const&) = delete;
|
|
|
|
ExceptionChecker& operator=(ExceptionChecker const&) = delete;
|
|
|
|
|
[libc++] Implement Directory Entry Caching -- Sort of.
Summary:
This patch implements directory_entry caching *almost* as specified in P0317r1. However, I explicitly chose to deviate from the standard as I'll explain below.
The approach I decided to take is a fully caching one. When `refresh()` is called, the cache is populated by calls to `stat` and `lstat` as needed.
During directory iteration the cache is only populated with the `file_type` as reported by `readdir`.
The cache can be in the following states:
* `_Empty`: There is nothing in the cache (likely due to an error)
* `_IterSymlink`: Created by directory iteration when we walk onto a symlink only the symlink file type is known.
* `_IterNonSymlink`: Created by directory iteration when we walk onto a non-symlink. Both the regular file type and symlink file type are known.
* `_RefreshSymlink` and `_RefreshNonSymlink`: A full cache created by `refresh()`. This case includes dead symlinks.
* `_RefreshSymlinkUnresolved`: A partial cache created by refresh when we fail to resolve the file pointed to by a symlink (likely due to permissions). Symlink attributes are cached, but attributes about the linked entity are not.
As mentioned, this implementation purposefully deviates from the standard. According to some readings of the specification, and the Windows filesystem implementation, the constructors and modifiers which don't pass an `error_code` must throw when the `directory_entry` points to a entity which doesn't exist. or when attribute resolution fails for another reason.
@BillyONeal has proposed a more reasonable set of requirements, where modifiers other than refresh ignore errors. This is the behavior libc++ currently implements, with the expectation some form of the new language will be accepted into the standard.
Some additional semantics which differ from the Windows implementation:
1. `refresh` will not throw when the entry doesn't exist. In this case we can still meet the functions specification, so we don't treat it as an error.
2. We don't clear the path name when a constructor fails via refresh (this will hopefully be changed in the standard as well).
It should be noted that libstdc++'s current implementation has the same behavior as libc++, except for point (2).
If the changes to the specification don't get accepted, we'll be able to make the changes later.
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0317r1.html
Reviewers: mclow.lists, gromer, ldionne, aaron.ballman
Subscribers: BillyONeal, christof, cfe-commits
Differential Revision: https://reviews.llvm.org/D49530
llvm-svn: 337516
2018-07-20 01:22:32 +00:00
|
|
|
};
|
|
|
|
|
2021-03-08 12:03:30 +02:00
|
|
|
inline fs::path GetWindowsInaccessibleDir() {
|
|
|
|
// Only makes sense on windows, but the code can be compiled for
|
|
|
|
// any platform.
|
|
|
|
const fs::path dir("C:\\System Volume Information");
|
|
|
|
std::error_code ec;
|
|
|
|
const fs::path root("C:\\");
|
2021-03-09 11:37:44 +02:00
|
|
|
for (const auto &ent : fs::directory_iterator(root, ec)) {
|
|
|
|
if (ent != dir)
|
|
|
|
continue;
|
|
|
|
// Basic sanity checks on the directory_entry
|
|
|
|
if (!ent.exists() || !ent.is_directory()) {
|
2024-07-12 09:17:00 -05:00
|
|
|
std::fprintf(stderr, "The expected inaccessible directory \"%s\" was found "
|
|
|
|
"but doesn't behave as expected, skipping tests "
|
|
|
|
"regarding it\n", dir.string().c_str());
|
2021-03-09 11:37:44 +02:00
|
|
|
return fs::path();
|
2021-03-08 12:03:30 +02:00
|
|
|
}
|
2021-03-09 11:37:44 +02:00
|
|
|
// Check that it indeed is inaccessible as expected
|
|
|
|
(void)fs::exists(ent, ec);
|
|
|
|
if (!ec) {
|
2024-07-12 09:17:00 -05:00
|
|
|
std::fprintf(stderr, "The expected inaccessible directory \"%s\" was found "
|
|
|
|
"but seems to be accessible, skipping tests "
|
|
|
|
"regarding it\n", dir.string().c_str());
|
2021-03-09 11:37:44 +02:00
|
|
|
return fs::path();
|
|
|
|
}
|
|
|
|
return ent;
|
2021-03-08 12:03:30 +02:00
|
|
|
}
|
2024-07-12 09:17:00 -05:00
|
|
|
std::fprintf(stderr, "No inaccessible directory \"%s\" found, skipping tests "
|
|
|
|
"regarding it\n", dir.string().c_str());
|
2021-03-08 12:03:30 +02:00
|
|
|
return fs::path();
|
|
|
|
}
|
|
|
|
|
2022-02-04 15:37:01 -05:00
|
|
|
#endif /* FILESYSTEM_TEST_HELPER_H */
|