'Snapshot pointers to a directory with std::filesystem

With the std::filesystem API, is it possible to get a pointer to a directory and execute reads via that pointer such that the pointer remains valid and points to a valid directory even if the directory gets deleted in the background?

Eg.

auto directory = getPointerToDirectory();
// something expensive
auto contents = readFile(directory, filename);

such that if someone deletes the entire directory while "something expensive" is running, the contents of the file can still be read as if the directory was not deleted? Suboptimally, is this possible with the posix filesystem API?



Solution 1:[1]

Afraid not. std::filesystem is just a wrapper on the native file system API. It makes no such guarantees. Unless the native file system supports this (and I don't know of any that do), then this would not work.

You would need to basically write some sort of transactional wrapper on the files yourself (e.g. to manually copy files to a backup area when you got the "pointer"). I don't see that working out very well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joe