2023-10-25 07:43:36 +03:00
|
|
|
|
#include "environment-variables.hh"
|
|
|
|
|
#include "file-system.hh"
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#include "file-path.hh"
|
2024-01-13 08:11:49 +02:00
|
|
|
|
#include "file-path-impl.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
|
#include "signals.hh"
|
|
|
|
|
#include "finally.hh"
|
|
|
|
|
#include "serialise.hh"
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <cerrno>
|
|
|
|
|
#include <climits>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
# include <io.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-10-25 07:43:36 +03:00
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
2024-01-14 21:30:25 +02:00
|
|
|
|
/**
|
|
|
|
|
* Treat the string as possibly an absolute path, by inspecting the
|
|
|
|
|
* start of it. Return whether it was probably intended to be
|
|
|
|
|
* absolute.
|
|
|
|
|
*/
|
2024-01-13 08:11:49 +02:00
|
|
|
|
static bool isAbsolute(PathView path)
|
|
|
|
|
{
|
2024-01-14 21:30:25 +02:00
|
|
|
|
return fs::path { path }.is_absolute();
|
2024-01-13 08:11:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-01-13 20:08:38 +02:00
|
|
|
|
Path absPath(PathView path, std::optional<PathView> dir, bool resolveSymlinks)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
{
|
2024-01-13 20:08:38 +02:00
|
|
|
|
std::string scratch;
|
|
|
|
|
|
2024-01-13 08:11:49 +02:00
|
|
|
|
if (!isAbsolute(path)) {
|
2024-01-13 20:08:38 +02:00
|
|
|
|
// In this case we need to call `canonPath` on a newly-created
|
|
|
|
|
// string. We set `scratch` to that string first, and then set
|
|
|
|
|
// `path` to `scratch`. This ensures the newly-created string
|
|
|
|
|
// lives long enough for the call to `canonPath`, and allows us
|
|
|
|
|
// to just accept a `std::string_view`.
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (!dir) {
|
|
|
|
|
#ifdef __GNU__
|
|
|
|
|
/* GNU (aka. GNU/Hurd) doesn't have any limitation on path
|
|
|
|
|
lengths and doesn't define `PATH_MAX'. */
|
|
|
|
|
char *buf = getcwd(NULL, 0);
|
|
|
|
|
if (buf == NULL)
|
|
|
|
|
#else
|
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
|
if (!getcwd(buf, sizeof(buf)))
|
|
|
|
|
#endif
|
|
|
|
|
throw SysError("cannot get cwd");
|
2024-01-13 20:08:38 +02:00
|
|
|
|
scratch = concatStrings(buf, "/", path);
|
2023-10-25 07:43:36 +03:00
|
|
|
|
#ifdef __GNU__
|
|
|
|
|
free(buf);
|
|
|
|
|
#endif
|
|
|
|
|
} else
|
2024-01-13 20:08:38 +02:00
|
|
|
|
scratch = concatStrings(*dir, "/", path);
|
|
|
|
|
path = scratch;
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
return canonPath(path, resolveSymlinks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Path canonPath(PathView path, bool resolveSymlinks)
|
|
|
|
|
{
|
|
|
|
|
assert(path != "");
|
|
|
|
|
|
2024-01-13 08:11:49 +02:00
|
|
|
|
if (!isAbsolute(path))
|
2023-10-25 07:43:36 +03:00
|
|
|
|
throw Error("not an absolute path: '%1%'", path);
|
|
|
|
|
|
2024-01-14 21:30:25 +02:00
|
|
|
|
// For Windows
|
|
|
|
|
auto rootName = fs::path { path }.root_name();
|
|
|
|
|
|
2024-01-13 08:11:49 +02:00
|
|
|
|
/* This just exists because we cannot set the target of `remaining`
|
|
|
|
|
(the callback parameter) directly to a newly-constructed string,
|
|
|
|
|
since it is `std::string_view`. */
|
2023-10-25 07:43:36 +03:00
|
|
|
|
std::string temp;
|
|
|
|
|
|
|
|
|
|
/* Count the number of times we follow a symlink and stop at some
|
|
|
|
|
arbitrary (but high) limit to prevent infinite loops. */
|
|
|
|
|
unsigned int followCount = 0, maxFollow = 1024;
|
|
|
|
|
|
2024-01-14 21:30:25 +02:00
|
|
|
|
auto ret = canonPathInner<NativePathTrait>(
|
2024-01-13 08:11:49 +02:00
|
|
|
|
path,
|
|
|
|
|
[&followCount, &temp, maxFollow, resolveSymlinks]
|
|
|
|
|
(std::string & result, std::string_view & remaining) {
|
2024-04-19 00:49:17 +03:00
|
|
|
|
if (resolveSymlinks && fs::is_symlink(result)) {
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (++followCount >= maxFollow)
|
2024-01-13 08:11:49 +02:00
|
|
|
|
throw Error("infinite symlink recursion in path '%0%'", remaining);
|
|
|
|
|
remaining = (temp = concatStrings(readLink(result), remaining));
|
|
|
|
|
if (isAbsolute(remaining)) {
|
|
|
|
|
/* restart for symlinks pointing to absolute path */
|
|
|
|
|
result.clear();
|
2023-10-25 07:43:36 +03:00
|
|
|
|
} else {
|
2024-01-13 08:11:49 +02:00
|
|
|
|
result = dirOf(result);
|
|
|
|
|
if (result == "/") {
|
|
|
|
|
/* we don’t want trailing slashes here, which `dirOf`
|
|
|
|
|
only produces if `result = /` */
|
|
|
|
|
result.clear();
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-13 08:11:49 +02:00
|
|
|
|
});
|
2024-01-14 21:30:25 +02:00
|
|
|
|
|
|
|
|
|
if (!rootName.empty())
|
|
|
|
|
ret = rootName.string() + std::move(ret);
|
|
|
|
|
return ret;
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Path dirOf(const PathView path)
|
|
|
|
|
{
|
2024-05-07 07:14:49 +03:00
|
|
|
|
Path::size_type pos = NativePathTrait::rfindPathSep(path);
|
2023-11-02 11:13:55 +02:00
|
|
|
|
if (pos == path.npos)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
return ".";
|
2024-05-07 07:14:49 +03:00
|
|
|
|
return fs::path{path}.parent_path().string();
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string_view baseNameOf(std::string_view path)
|
|
|
|
|
{
|
|
|
|
|
if (path.empty())
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
auto last = path.size() - 1;
|
2023-09-03 00:35:16 +03:00
|
|
|
|
while (last > 0 && NativePathTrait::isPathSep(path[last]))
|
2023-10-25 07:43:36 +03:00
|
|
|
|
last -= 1;
|
|
|
|
|
|
2023-09-03 00:35:16 +03:00
|
|
|
|
auto pos = NativePathTrait::rfindPathSep(path, last);
|
2023-11-02 11:13:55 +02:00
|
|
|
|
if (pos == path.npos)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
pos = 0;
|
|
|
|
|
else
|
|
|
|
|
pos += 1;
|
|
|
|
|
|
|
|
|
|
return path.substr(pos, last - pos + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool isInDir(std::string_view path, std::string_view dir)
|
|
|
|
|
{
|
|
|
|
|
return path.substr(0, 1) == "/"
|
|
|
|
|
&& path.substr(0, dir.size()) == dir
|
|
|
|
|
&& path.size() >= dir.size() + 2
|
|
|
|
|
&& path[dir.size()] == '/';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool isDirOrInDir(std::string_view path, std::string_view dir)
|
|
|
|
|
{
|
|
|
|
|
return path == dir || isInDir(path, dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct stat stat(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (stat(path.c_str(), &st))
|
|
|
|
|
throw SysError("getting status of '%1%'", path);
|
|
|
|
|
return st;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
# define STAT stat
|
|
|
|
|
#else
|
|
|
|
|
# define STAT lstat
|
|
|
|
|
#endif
|
2023-10-25 07:43:36 +03:00
|
|
|
|
|
|
|
|
|
struct stat lstat(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
2023-09-03 00:35:16 +03:00
|
|
|
|
if (STAT(path.c_str(), &st))
|
2023-10-25 07:43:36 +03:00
|
|
|
|
throw SysError("getting status of '%1%'", path);
|
|
|
|
|
return st;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-03-29 21:21:10 +02:00
|
|
|
|
std::optional<struct stat> maybeLstat(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
std::optional<struct stat> st{std::in_place};
|
2023-09-03 00:35:16 +03:00
|
|
|
|
if (STAT(path.c_str(), &*st))
|
2024-03-29 21:21:10 +02:00
|
|
|
|
{
|
|
|
|
|
if (errno == ENOENT || errno == ENOTDIR)
|
|
|
|
|
st.reset();
|
|
|
|
|
else
|
|
|
|
|
throw SysError("getting status of '%s'", path);
|
|
|
|
|
}
|
|
|
|
|
return st;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-25 07:43:36 +03:00
|
|
|
|
bool pathExists(const Path & path)
|
|
|
|
|
{
|
2024-03-29 21:21:10 +02:00
|
|
|
|
return maybeLstat(path).has_value();
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool pathAccessible(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return pathExists(path);
|
|
|
|
|
} catch (SysError & e) {
|
|
|
|
|
// swallow EPERM
|
|
|
|
|
if (e.errNo == EPERM) return false;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Path readLink(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
checkInterrupt();
|
2024-05-07 07:14:49 +03:00
|
|
|
|
return fs::read_symlink(path).string();
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-05-08 01:28:50 +03:00
|
|
|
|
std::vector<fs::directory_entry> readDirectory(const Path & path)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
{
|
2024-05-08 01:28:50 +03:00
|
|
|
|
std::vector<fs::directory_entry> entries;
|
2023-10-25 07:43:36 +03:00
|
|
|
|
entries.reserve(64);
|
|
|
|
|
|
2024-05-07 07:14:49 +03:00
|
|
|
|
for (auto & entry : fs::directory_iterator{path}) {
|
2023-10-25 07:43:36 +03:00
|
|
|
|
checkInterrupt();
|
2024-05-07 18:29:33 +03:00
|
|
|
|
entries.push_back(std::move(entry));
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-05-07 07:14:49 +03:00
|
|
|
|
fs::file_type getFileType(const Path & path)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
{
|
2024-05-07 07:14:49 +03:00
|
|
|
|
return fs::symlink_status(path).type();
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string readFile(const Path & path)
|
|
|
|
|
{
|
2023-09-03 00:35:16 +03:00
|
|
|
|
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_RDONLY
|
|
|
|
|
// TODO
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
| O_CLOEXEC
|
|
|
|
|
#endif
|
|
|
|
|
));
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (!fd)
|
|
|
|
|
throw SysError("opening file '%1%'", path);
|
|
|
|
|
return readFile(fd.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void readFile(const Path & path, Sink & sink)
|
|
|
|
|
{
|
2023-09-03 00:35:16 +03:00
|
|
|
|
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_RDONLY
|
|
|
|
|
// TODO
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
| O_CLOEXEC
|
|
|
|
|
#endif
|
|
|
|
|
));
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (!fd)
|
|
|
|
|
throw SysError("opening file '%s'", path);
|
|
|
|
|
drainFD(fd.get(), sink);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void writeFile(const Path & path, std::string_view s, mode_t mode, bool sync)
|
|
|
|
|
{
|
2023-09-03 00:35:16 +03:00
|
|
|
|
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT
|
|
|
|
|
// TODO
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
| O_CLOEXEC
|
|
|
|
|
#endif
|
|
|
|
|
, mode));
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (!fd)
|
|
|
|
|
throw SysError("opening file '%1%'", path);
|
|
|
|
|
try {
|
|
|
|
|
writeFull(fd.get(), s);
|
|
|
|
|
} catch (Error & e) {
|
|
|
|
|
e.addTrace({}, "writing file '%1%'", path);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
if (sync)
|
|
|
|
|
fd.fsync();
|
|
|
|
|
// Explicitly close to make sure exceptions are propagated.
|
|
|
|
|
fd.close();
|
|
|
|
|
if (sync)
|
|
|
|
|
syncParent(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void writeFile(const Path & path, Source & source, mode_t mode, bool sync)
|
|
|
|
|
{
|
2023-09-03 00:35:16 +03:00
|
|
|
|
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT
|
|
|
|
|
// TODO
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
| O_CLOEXEC
|
|
|
|
|
#endif
|
|
|
|
|
, mode));
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (!fd)
|
|
|
|
|
throw SysError("opening file '%1%'", path);
|
|
|
|
|
|
2023-12-26 18:40:55 +02:00
|
|
|
|
std::array<char, 64 * 1024> buf;
|
2023-10-25 07:43:36 +03:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
while (true) {
|
|
|
|
|
try {
|
|
|
|
|
auto n = source.read(buf.data(), buf.size());
|
|
|
|
|
writeFull(fd.get(), {buf.data(), n});
|
|
|
|
|
} catch (EndOfFile &) { break; }
|
|
|
|
|
}
|
|
|
|
|
} catch (Error & e) {
|
|
|
|
|
e.addTrace({}, "writing file '%1%'", path);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
if (sync)
|
|
|
|
|
fd.fsync();
|
|
|
|
|
// Explicitly close to make sure exceptions are propagated.
|
|
|
|
|
fd.close();
|
|
|
|
|
if (sync)
|
|
|
|
|
syncParent(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void syncParent(const Path & path)
|
|
|
|
|
{
|
2023-09-03 00:35:16 +03:00
|
|
|
|
AutoCloseFD fd = toDescriptor(open(dirOf(path).c_str(), O_RDONLY, 0));
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (!fd)
|
|
|
|
|
throw SysError("opening file '%1%'", path);
|
|
|
|
|
fd.fsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-05-08 01:28:50 +03:00
|
|
|
|
static void _deletePath(Descriptor parentfd, const fs::path & path, uint64_t & bytesFreed)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
{
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#ifndef _WIN32
|
2023-10-25 07:43:36 +03:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
2024-05-08 01:28:50 +03:00
|
|
|
|
std::string name(baseNameOf(path.native()));
|
2023-10-25 07:43:36 +03:00
|
|
|
|
|
|
|
|
|
struct stat st;
|
2023-09-03 00:35:16 +03:00
|
|
|
|
if (fstatat(parentfd, name.c_str(), &st,
|
|
|
|
|
AT_SYMLINK_NOFOLLOW) == -1) {
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (errno == ENOENT) return;
|
|
|
|
|
throw SysError("getting status of '%1%'", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
|
|
|
|
/* We are about to delete a file. Will it likely free space? */
|
|
|
|
|
|
|
|
|
|
switch (st.st_nlink) {
|
|
|
|
|
/* Yes: last link. */
|
|
|
|
|
case 1:
|
|
|
|
|
bytesFreed += st.st_size;
|
|
|
|
|
break;
|
|
|
|
|
/* Maybe: yes, if 'auto-optimise-store' or manual optimisation
|
|
|
|
|
was performed. Instead of checking for real let's assume
|
|
|
|
|
it's an optimised file and space will be freed.
|
|
|
|
|
|
|
|
|
|
In worst case we will double count on freed space for files
|
|
|
|
|
with exactly two hardlinks for unoptimised packages.
|
|
|
|
|
*/
|
|
|
|
|
case 2:
|
|
|
|
|
bytesFreed += st.st_size;
|
|
|
|
|
break;
|
|
|
|
|
/* No: 3+ links. */
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
|
/* Make the directory accessible. */
|
|
|
|
|
const auto PERM_MASK = S_IRUSR | S_IWUSR | S_IXUSR;
|
|
|
|
|
if ((st.st_mode & PERM_MASK) != PERM_MASK) {
|
|
|
|
|
if (fchmodat(parentfd, name.c_str(), st.st_mode | PERM_MASK, 0) == -1)
|
|
|
|
|
throw SysError("chmod '%1%'", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fd = openat(parentfd, path.c_str(), O_RDONLY);
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
throw SysError("opening directory '%1%'", path);
|
|
|
|
|
AutoCloseDir dir(fdopendir(fd));
|
|
|
|
|
if (!dir)
|
|
|
|
|
throw SysError("opening directory '%1%'", path);
|
2024-05-07 07:14:49 +03:00
|
|
|
|
|
|
|
|
|
struct dirent * dirent;
|
|
|
|
|
while (errno = 0, dirent = readdir(dir.get())) { /* sic */
|
|
|
|
|
checkInterrupt();
|
|
|
|
|
std::string childName = dirent->d_name;
|
|
|
|
|
if (childName == "." || childName == "..") continue;
|
|
|
|
|
_deletePath(dirfd(dir.get()), path + "/" + childName, bytesFreed);
|
|
|
|
|
}
|
|
|
|
|
if (errno) throw SysError("reading directory '%1%'", path);
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int flags = S_ISDIR(st.st_mode) ? AT_REMOVEDIR : 0;
|
|
|
|
|
if (unlinkat(parentfd, name.c_str(), flags) == -1) {
|
|
|
|
|
if (errno == ENOENT) return;
|
|
|
|
|
throw SysError("cannot unlink '%1%'", path);
|
|
|
|
|
}
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#else
|
|
|
|
|
// TODO implement
|
|
|
|
|
throw UnimplementedError("_deletePath");
|
|
|
|
|
#endif
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 01:28:50 +03:00
|
|
|
|
static void _deletePath(const fs::path & path, uint64_t & bytesFreed)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
{
|
2024-05-08 01:28:50 +03:00
|
|
|
|
Path dir = dirOf(path.string());
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (dir == "")
|
|
|
|
|
dir = "/";
|
|
|
|
|
|
2023-09-03 00:35:16 +03:00
|
|
|
|
AutoCloseFD dirfd = toDescriptor(open(dir.c_str(), O_RDONLY));
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (!dirfd) {
|
|
|
|
|
if (errno == ENOENT) return;
|
|
|
|
|
throw SysError("opening directory '%1%'", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_deletePath(dirfd.get(), path, bytesFreed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-05-08 01:28:50 +03:00
|
|
|
|
void deletePath(const fs::path & path)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
{
|
|
|
|
|
uint64_t dummy;
|
|
|
|
|
deletePath(path, dummy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Paths createDirs(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
Paths created;
|
|
|
|
|
if (path == "/") return created;
|
|
|
|
|
|
|
|
|
|
struct stat st;
|
2023-09-03 00:35:16 +03:00
|
|
|
|
if (STAT(path.c_str(), &st) == -1) {
|
2023-10-25 07:43:36 +03:00
|
|
|
|
created = createDirs(dirOf(path));
|
2023-09-03 00:35:16 +03:00
|
|
|
|
if (mkdir(path.c_str()
|
|
|
|
|
#ifndef _WIN32 // TODO abstract mkdir perms for Windows
|
|
|
|
|
, 0777
|
|
|
|
|
#endif
|
|
|
|
|
) == -1 && errno != EEXIST)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
throw SysError("creating directory '%1%'", path);
|
2023-09-03 00:35:16 +03:00
|
|
|
|
st = STAT(path);
|
2023-10-25 07:43:36 +03:00
|
|
|
|
created.push_back(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (S_ISLNK(st.st_mode) && stat(path.c_str(), &st) == -1)
|
|
|
|
|
throw SysError("statting symlink '%1%'", path);
|
|
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode)) throw Error("'%1%' is not a directory", path);
|
|
|
|
|
|
|
|
|
|
return created;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-05-08 01:28:50 +03:00
|
|
|
|
void deletePath(const fs::path & path, uint64_t & bytesFreed)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
{
|
|
|
|
|
//Activity act(*logger, lvlDebug, "recursively deleting path '%1%'", path);
|
|
|
|
|
bytesFreed = 0;
|
|
|
|
|
_deletePath(path, bytesFreed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
AutoDelete::AutoDelete() : del{false} {}
|
|
|
|
|
|
2024-05-08 01:28:50 +03:00
|
|
|
|
AutoDelete::AutoDelete(const fs::path & p, bool recursive) : _path(p)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
{
|
|
|
|
|
del = true;
|
|
|
|
|
this->recursive = recursive;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AutoDelete::~AutoDelete()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
if (del) {
|
|
|
|
|
if (recursive)
|
2024-05-08 01:28:50 +03:00
|
|
|
|
deletePath(_path);
|
2023-10-25 07:43:36 +03:00
|
|
|
|
else {
|
2024-05-08 01:28:50 +03:00
|
|
|
|
fs::remove(_path);
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (...) {
|
|
|
|
|
ignoreException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AutoDelete::cancel()
|
|
|
|
|
{
|
|
|
|
|
del = false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 01:28:50 +03:00
|
|
|
|
void AutoDelete::reset(const fs::path & p, bool recursive) {
|
|
|
|
|
_path = p;
|
2023-10-25 07:43:36 +03:00
|
|
|
|
this->recursive = recursive;
|
|
|
|
|
del = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2024-03-22 23:41:50 +02:00
|
|
|
|
std::string defaultTempDir() {
|
|
|
|
|
return getEnvNonEmpty("TMPDIR").value_or("/tmp");
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 07:43:36 +03:00
|
|
|
|
static Path tempName(Path tmpRoot, const Path & prefix, bool includePid,
|
|
|
|
|
std::atomic<unsigned int> & counter)
|
|
|
|
|
{
|
2024-03-22 23:41:50 +02:00
|
|
|
|
tmpRoot = canonPath(tmpRoot.empty() ? defaultTempDir() : tmpRoot, true);
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (includePid)
|
|
|
|
|
return fmt("%1%/%2%-%3%-%4%", tmpRoot, prefix, getpid(), counter++);
|
|
|
|
|
else
|
|
|
|
|
return fmt("%1%/%2%-%3%", tmpRoot, prefix, counter++);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Path createTempDir(const Path & tmpRoot, const Path & prefix,
|
|
|
|
|
bool includePid, bool useGlobalCounter, mode_t mode)
|
|
|
|
|
{
|
|
|
|
|
static std::atomic<unsigned int> globalCounter = 0;
|
|
|
|
|
std::atomic<unsigned int> localCounter = 0;
|
|
|
|
|
auto & counter(useGlobalCounter ? globalCounter : localCounter);
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
checkInterrupt();
|
|
|
|
|
Path tmpDir = tempName(tmpRoot, prefix, includePid, counter);
|
2023-09-03 00:35:16 +03:00
|
|
|
|
if (mkdir(tmpDir.c_str()
|
|
|
|
|
#ifndef _WIN32 // TODO abstract mkdir perms for Windows
|
|
|
|
|
, mode
|
|
|
|
|
#endif
|
|
|
|
|
) == 0) {
|
2023-10-25 07:43:36 +03:00
|
|
|
|
#if __FreeBSD__
|
|
|
|
|
/* Explicitly set the group of the directory. This is to
|
|
|
|
|
work around around problems caused by BSD's group
|
|
|
|
|
ownership semantics (directories inherit the group of
|
|
|
|
|
the parent). For instance, the group of /tmp on
|
|
|
|
|
FreeBSD is "wheel", so all directories created in /tmp
|
|
|
|
|
will be owned by "wheel"; but if the user is not in
|
|
|
|
|
"wheel", then "tar" will fail to unpack archives that
|
|
|
|
|
have the setgid bit set on directories. */
|
|
|
|
|
if (chown(tmpDir.c_str(), (uid_t) -1, getegid()) != 0)
|
|
|
|
|
throw SysError("setting group of directory '%1%'", tmpDir);
|
|
|
|
|
#endif
|
|
|
|
|
return tmpDir;
|
|
|
|
|
}
|
|
|
|
|
if (errno != EEXIST)
|
|
|
|
|
throw SysError("creating directory '%1%'", tmpDir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix)
|
|
|
|
|
{
|
2024-03-22 23:41:50 +02:00
|
|
|
|
Path tmpl(defaultTempDir() + "/" + prefix + ".XXXXXX");
|
2023-10-25 07:43:36 +03:00
|
|
|
|
// Strictly speaking, this is UB, but who cares...
|
|
|
|
|
// FIXME: use O_TMPFILE.
|
2023-09-03 00:35:16 +03:00
|
|
|
|
AutoCloseFD fd = toDescriptor(mkstemp((char *) tmpl.c_str()));
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (!fd)
|
|
|
|
|
throw SysError("creating temporary file '%s'", tmpl);
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#ifndef _WIN32
|
2023-10-25 07:43:36 +03:00
|
|
|
|
closeOnExec(fd.get());
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#endif
|
2023-10-25 07:43:36 +03:00
|
|
|
|
return {std::move(fd), tmpl};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void createSymlink(const Path & target, const Path & link)
|
|
|
|
|
{
|
2024-05-07 07:14:49 +03:00
|
|
|
|
fs::create_symlink(target, link);
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void replaceSymlink(const Path & target, const Path & link)
|
|
|
|
|
{
|
|
|
|
|
for (unsigned int n = 0; true; n++) {
|
|
|
|
|
Path tmp = canonPath(fmt("%s/.%d_%s", dirOf(link), n, baseNameOf(link)));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
createSymlink(target, tmp);
|
2024-05-07 07:14:49 +03:00
|
|
|
|
} catch (fs::filesystem_error & e) {
|
|
|
|
|
if (e.code() == std::errc::file_exists) continue;
|
2023-10-25 07:43:36 +03:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renameFile(tmp, link);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
static void setWriteTime(const fs::path & p, const struct stat & st)
|
2023-10-25 07:43:36 +03:00
|
|
|
|
{
|
|
|
|
|
struct timeval times[2];
|
|
|
|
|
times[0] = {
|
|
|
|
|
.tv_sec = st.st_atime,
|
|
|
|
|
.tv_usec = 0,
|
|
|
|
|
};
|
|
|
|
|
times[1] = {
|
|
|
|
|
.tv_sec = st.st_mtime,
|
|
|
|
|
.tv_usec = 0,
|
|
|
|
|
};
|
|
|
|
|
if (lutimes(p.c_str(), times) != 0)
|
|
|
|
|
throw SysError("changing modification time of '%s'", p);
|
|
|
|
|
}
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#endif
|
2023-10-25 07:43:36 +03:00
|
|
|
|
|
|
|
|
|
void copy(const fs::directory_entry & from, const fs::path & to, bool andDelete)
|
|
|
|
|
{
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#ifndef _WIN32
|
2023-10-25 07:43:36 +03:00
|
|
|
|
// TODO: Rewrite the `is_*` to use `symlink_status()`
|
|
|
|
|
auto statOfFrom = lstat(from.path().c_str());
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#endif
|
2023-10-25 07:43:36 +03:00
|
|
|
|
auto fromStatus = from.symlink_status();
|
|
|
|
|
|
|
|
|
|
// Mark the directory as writable so that we can delete its children
|
|
|
|
|
if (andDelete && fs::is_directory(fromStatus)) {
|
|
|
|
|
fs::permissions(from.path(), fs::perms::owner_write, fs::perm_options::add | fs::perm_options::nofollow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fs::is_symlink(fromStatus) || fs::is_regular_file(fromStatus)) {
|
|
|
|
|
fs::copy(from.path(), to, fs::copy_options::copy_symlinks | fs::copy_options::overwrite_existing);
|
|
|
|
|
} else if (fs::is_directory(fromStatus)) {
|
|
|
|
|
fs::create_directory(to);
|
|
|
|
|
for (auto & entry : fs::directory_iterator(from.path())) {
|
|
|
|
|
copy(entry, to / entry.path().filename(), andDelete);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw Error("file '%s' has an unsupported type", from.path());
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#ifndef _WIN32
|
2023-10-25 07:43:36 +03:00
|
|
|
|
setWriteTime(to, statOfFrom);
|
2023-09-03 00:35:16 +03:00
|
|
|
|
#endif
|
2023-10-25 07:43:36 +03:00
|
|
|
|
if (andDelete) {
|
|
|
|
|
if (!fs::is_symlink(fromStatus))
|
|
|
|
|
fs::permissions(from.path(), fs::perms::owner_write, fs::perm_options::add | fs::perm_options::nofollow);
|
|
|
|
|
fs::remove(from.path());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-13 09:28:02 +02:00
|
|
|
|
void copyFile(const Path & oldPath, const Path & newPath, bool andDelete)
|
|
|
|
|
{
|
|
|
|
|
return copy(fs::directory_entry(fs::path(oldPath)), fs::path(newPath), andDelete);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 07:43:36 +03:00
|
|
|
|
void renameFile(const Path & oldName, const Path & newName)
|
|
|
|
|
{
|
|
|
|
|
fs::rename(oldName, newName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void moveFile(const Path & oldName, const Path & newName)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
renameFile(oldName, newName);
|
|
|
|
|
} catch (fs::filesystem_error & e) {
|
|
|
|
|
auto oldPath = fs::path(oldName);
|
|
|
|
|
auto newPath = fs::path(newName);
|
|
|
|
|
// For the move to be as atomic as possible, copy to a temporary
|
|
|
|
|
// directory
|
2023-09-03 00:35:16 +03:00
|
|
|
|
fs::path temp = createTempDir(
|
|
|
|
|
os_string_to_string(PathViewNG { newPath.parent_path() }),
|
|
|
|
|
"rename-tmp");
|
2023-10-25 07:43:36 +03:00
|
|
|
|
Finally removeTemp = [&]() { fs::remove(temp); };
|
|
|
|
|
auto tempCopyTarget = temp / "copy-target";
|
|
|
|
|
if (e.code().value() == EXDEV) {
|
|
|
|
|
fs::remove(newPath);
|
|
|
|
|
warn("Can’t rename %s as %s, copying instead", oldName, newName);
|
|
|
|
|
copy(fs::directory_entry(oldPath), tempCopyTarget, true);
|
2023-09-03 00:35:16 +03:00
|
|
|
|
renameFile(
|
|
|
|
|
os_string_to_string(PathViewNG { tempCopyTarget }),
|
|
|
|
|
os_string_to_string(PathViewNG { newPath }));
|
2023-10-25 07:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
}
|