I need to walk a directory tree and get stat values for every file. I want to do this safely while the filesystem is being modified.
In Python, the best option is os.fwalk
, which gives access to the fd for the directory being traversed; I can then os.stat
with the dir_fd (fstatat
) and get current stat values. This is as race-free as it can be made on Linux (if the contents of this directory are being modified, I may have to rescan it). In C, there is nftw
, which is implemented similarly, and fts
, which in glibc uses a plain (l)stat and therefore is racy (it reduces the race window by changing directories, which is inconvenient).
C++ has a new filesystem API graduated from boost, which caches stat
values but doesn't expose them (and I need access to st_dev). This isn't purely a header library, so I can't work around that.
Am I missing a decent C++ option, that uses fstatat
and isn't bound by Boost's ideal of not exposing platform-specific calls? Or is my best option to wrap nftw
(or even find
)?