forked from lix-project/lix
libutil: Don't use std::filesystem
Just in case making libutil depend on std::filesystem is unacceptable, here is the non-filesystem approach.
This commit is contained in:
parent
435848cef1
commit
7f5caaa7c0
|
@ -10,7 +10,6 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <filesystem>
|
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
@ -1691,7 +1690,7 @@ void setStackSize(size_t stackSize)
|
||||||
|
|
||||||
#if __linux__
|
#if __linux__
|
||||||
static AutoCloseFD fdSavedMountNamespace;
|
static AutoCloseFD fdSavedMountNamespace;
|
||||||
std::optional<std::filesystem::path> savedCwd;
|
std::optional<Path> savedCwd;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void saveMountNamespace()
|
void saveMountNamespace()
|
||||||
|
@ -1699,7 +1698,11 @@ void saveMountNamespace()
|
||||||
#if __linux__
|
#if __linux__
|
||||||
static std::once_flag done;
|
static std::once_flag done;
|
||||||
std::call_once(done, []() {
|
std::call_once(done, []() {
|
||||||
savedCwd.emplace(std::filesystem::current_path());
|
char* cwd = getcwd(NULL, 0);
|
||||||
|
if (cwd == NULL) throw SysError("getting cwd");
|
||||||
|
savedCwd.emplace(cwd);
|
||||||
|
free(cwd);
|
||||||
|
|
||||||
AutoCloseFD fd = open("/proc/self/ns/mnt", O_RDONLY);
|
AutoCloseFD fd = open("/proc/self/ns/mnt", O_RDONLY);
|
||||||
if (!fd)
|
if (!fd)
|
||||||
throw SysError("saving parent mount namespace");
|
throw SysError("saving parent mount namespace");
|
||||||
|
@ -1717,11 +1720,8 @@ void restoreMountNamespace()
|
||||||
} catch (Error & e) {
|
} catch (Error & e) {
|
||||||
debug(e.msg());
|
debug(e.msg());
|
||||||
}
|
}
|
||||||
try {
|
if (savedCwd && chdir(savedCwd->c_str()) == -1) {
|
||||||
if (savedCwd)
|
throw SysError("restoring cwd");
|
||||||
std::filesystem::current_path(*savedCwd);
|
|
||||||
} catch (std::filesystem::filesystem_error const &e) {
|
|
||||||
debug(e.what());
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue