25 #include <sys/types.h>
46 struct stat path_stat;
47 int ret = ::stat(p.
c_str(), &path_stat);
50 if (errno == ENOENT || errno == ENOTDIR) {
54 }
else if (S_ISDIR(path_stat.st_mode)) {
56 }
else if (S_ISREG(path_stat.st_mode)) {
68 for (
size_t path_max = 128;; path_max *=2) {
69 std::vector<char> buf(path_max);
70 if (::getcwd(&buf[0], path_max) == 0) {
73 cur = std::string(&buf[0]);
81 const char *home = ::getenv(
"HOME");
109 int ret = ::mkdir(p.
c_str(), S_IRWXU);
114 return fsync(p,
true);
121 struct stat path_stat;
122 int ret = ::stat(p.
c_str(), &path_stat);
124 return static_cast<uint64_t
>(-1);
126 if (!S_ISREG(path_stat.st_mode)) {
127 return static_cast<uint64_t
>(-1);
129 return static_cast<uint64_t
>(path_stat.st_size);
139 int ret = ::rmdir(p.c_str());
151 for (
Path child : child_paths) {
161 int ret = ::statfs(p.
c_str(), &vfs);
163 info.
capacity_ =
static_cast<uint64_t
>(vfs.f_blocks) * vfs.f_bsize;
164 info.free_ = static_cast<uint64_t>(vfs.f_bfree) * vfs.f_bsize;
165 info.available_ =
static_cast<uint64_t
>(vfs.f_bavail) * vfs.f_bsize;
175 return unique_name(
"%%%%-%%%%-%%%%-%%%%", differentiator);
177 std::string
unique_name(
const std::string& model, uint64_t differentiator) {
178 const char* kHexChars =
"0123456789abcdef";
179 uint64_t seed64 = std::chrono::high_resolution_clock::now().time_since_epoch().count();
180 seed64 += ::getpid();
181 seed64 ^= differentiator;
182 uint32_t seed32 = (seed64 >> 32) ^ seed64;
183 std::string s(model);
184 for (
size_t i = 0; i < s.size(); ++i) {
186 seed32 = ::rand_r(&seed32);
187 s[i] = kHexChars[seed32 & 0xf];
195 <<
", free_=" << v.
free_;
203 bool fsync(
const Path& path,
bool sync_parent_directory) {
206 int descriptor = ::open(path.
c_str(), O_RDONLY);
207 if (descriptor < 0) {
210 sync_ret =
::fsync(descriptor);
213 DIR *dir = ::opendir(path.
c_str());
217 int descriptor = ::dirfd(dir);
218 if (descriptor < 0) {
222 sync_ret =
::fsync(descriptor);
230 if (sync_parent_directory) {
237 if (!
fsync(old_path,
false)) {
bool remove(const Path &p)
Deletes a regular file or an empty directory.
std::ostream & operator<<(std::ostream &o, const DirectIoFile &v)
uint64_t available_
Less than free_.
bool is_directory(const Path &p)
Returns if the file is a directory.
Analogue of boost::filesystem::space_info.
uint64_t remove_all(const Path &p)
Recursively deletes a directory.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Path home_path()
Returns the absolute path of the home directory of the user running this process. ...
FileStatus status(const Path &p)
Returns the status of the file.
Path current_path()
Returns the current working directory.
bool create_directory(const Path &p, bool sync=false)
mkdir.
std::vector< Path > child_paths() const
Analogue of boost::filesystem::path.
bool durable_atomic_rename(const Path &old_path, const Path &new_path)
fsync() on source file before rename, then fsync() on the parent folder after rename.
Path absolute(const std::string &p)
Returns the absolue path of the specified path.
bool create_directories(const Path &p, bool sync=false)
Recursive mkdir (mkdirs).
bool exists(const Path &p)
Returns if the file exists.
SpaceInfo space(const Path &p)
Returns free space information for the device the file is on.
bool is_regular_file() const
bool rename(const Path &old_path, const Path &new_path)
Just a synonym of atomic_rename() to avoid confusion.
uint64_t file_size(const Path &p)
Returns size of the file.
const char * c_str() const
std::string unique_name(uint64_t differentiator=0)
Equivalent to unique_path("%%%%-%%%%-%%%%-%%%%").
Analogue of boost::filesystem::file_status.
bool atomic_rename(const Path &old_path, const Path &new_path)
Renames the old file to the new file with the POSIX atomic-rename semantics.
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
bool fsync(const Path &path, bool sync_parent_directory=false)
Makes the content and metadata of the file durable all the way up to devices.
uint64_t free_
Less than capacity_.