libfoedus-core
FOEDUS Core Library
|
Filesystem wrapper, an analogue of boost::filesystem. More...
Filesystem wrapper, an analogue of boost::filesystem.
These methods abstract accesses to filesystems like boost::filesystem and std::filesystem in C++1z(?). We should not directly call POSIX or Windows filesystem APIs in other modules. Instead, all of them should go through this package.
![]() |
Files | |
file | fwd.hpp |
Forward declarations of classes in filesystem package. | |
Classes | |
struct | foedus::fs::DeviceEmulationOptions |
Set of configurations to emulate slower devices for some experiments. More... | |
class | foedus::fs::DirectIoFile |
Represents an I/O stream on one file without filesystem caching. More... | |
struct | foedus::fs::FileStatus |
Analogue of boost::filesystem::file_status. More... | |
struct | foedus::fs::SpaceInfo |
Analogue of boost::filesystem::space_info. More... | |
class | foedus::fs::Path |
Analogue of boost::filesystem::path. More... | |
Typedefs | |
typedef assorted::FixedString< 508 > | foedus::fs::FixedPath |
Represents a fixed (thus can be placed in shared memory) path string. More... | |
Enumerations | |
enum | foedus::fs::FileType { foedus::fs::kStatusError = 0, foedus::fs::kFileNotFound, foedus::fs::kRegularFile, foedus::fs::kDirectoryFile, foedus::fs::kTypeUnknown } |
Analogue of boost::filesystem::file_type. More... | |
enum | foedus::fs::FilePermission { foedus::fs::kNoPerms = 0, foedus::fs::kOwnerRead = 0400, foedus::fs::kOwnerWrite = 0200, foedus::fs::kOwnerExe = 0100, foedus::fs::kOwnerAll = 0700, foedus::fs::kGroupRead = 040, foedus::fs::kGroupWrite = 020, foedus::fs::kGroupExe = 010, foedus::fs::kGroupAll = 070, foedus::fs::kOthersRead = 04, foedus::fs::kOthersWrite = 02, foedus::fs::kOthersExe = 01, foedus::fs::kOthersAll = 07, foedus::fs::kAllAll = kOwnerAll|kGroupAll|kOthersAll, foedus::fs::kPermsNotKnown = 0xFFFF } |
Analogue of boost::filesystem::perm. More... | |
Functions | |
FileStatus | foedus::fs::status (const Path &p) |
Returns the status of the file. More... | |
bool | foedus::fs::exists (const Path &p) |
Returns if the file exists. More... | |
bool | foedus::fs::is_directory (const Path &p) |
Returns if the file is a directory. More... | |
bool | foedus::fs::is_regular_file (const Path &p) |
Returns if the file is a regular file. More... | |
Path | foedus::fs::current_path () |
Returns the current working directory. More... | |
Path | foedus::fs::home_path () |
Returns the absolute path of the home directory of the user running this process. More... | |
Path | foedus::fs::absolute (const std::string &p) |
Returns the absolue path of the specified path. More... | |
bool | foedus::fs::create_directories (const Path &p, bool sync=false) |
Recursive mkdir (mkdirs). More... | |
bool | foedus::fs::create_directory (const Path &p, bool sync=false) |
mkdir. More... | |
uint64_t | foedus::fs::file_size (const Path &p) |
Returns size of the file. More... | |
bool | foedus::fs::remove (const Path &p) |
Deletes a regular file or an empty directory. More... | |
uint64_t | foedus::fs::remove_all (const Path &p) |
Recursively deletes a directory. More... | |
SpaceInfo | foedus::fs::space (const Path &p) |
Returns free space information for the device the file is on. More... | |
std::string | foedus::fs::unique_name (uint64_t differentiator=0) |
Equivalent to unique_path("%%%%-%%%%-%%%%-%%%%"). More... | |
std::string | foedus::fs::unique_name (const std::string &model, uint64_t differentiator=0) |
Returns a randomly generated file name with the given template. More... | |
bool | foedus::fs::fsync (const Path &path, bool sync_parent_directory=false) |
Makes the content and metadata of the file durable all the way up to devices. More... | |
bool | foedus::fs::atomic_rename (const Path &old_path, const Path &new_path) |
Renames the old file to the new file with the POSIX atomic-rename semantics. More... | |
bool | foedus::fs::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. More... | |
bool | foedus::fs::rename (const Path &old_path, const Path &new_path) |
Just a synonym of atomic_rename() to avoid confusion. More... | |
typedef assorted::FixedString<508> foedus::fs::FixedPath |
Represents a fixed (thus can be placed in shared memory) path string.
Definition at line 83 of file filesystem.hpp.
Analogue of boost::filesystem::perm.
Enumerator | |
---|---|
kNoPerms | |
kOwnerRead | |
kOwnerWrite | |
kOwnerExe | |
kOwnerAll | |
kGroupRead | |
kGroupWrite | |
kGroupExe | |
kGroupAll | |
kOthersRead | |
kOthersWrite | |
kOthersExe | |
kOthersAll | |
kAllAll | |
kPermsNotKnown |
Definition at line 51 of file filesystem.hpp.
enum foedus::fs::FileType |
Analogue of boost::filesystem::file_type.
Enumerator | |
---|---|
kStatusError | |
kFileNotFound | |
kRegularFile | |
kDirectoryFile | |
kTypeUnknown |
Definition at line 38 of file filesystem.hpp.
Path foedus::fs::absolute | ( | const std::string & | p | ) |
Returns the absolue path of the specified path.
Definition at line 62 of file filesystem.cpp.
Renames the old file to the new file with the POSIX atomic-rename semantics.
[in] | old_path | path of the file to rename |
[in] | new_path | path after rename |
This is analogus to boost::filesystem::rename(), but we named this atomic_rename to clarify that this implementation guarantees the POSIX atomic-rename semantics. When new_path already exists, this method atomically swaps the file on filesystem with the old_path file, appropriately deleting the old file. This is an essential semantics to achieve safe and fault-tolerant file writes. And, for that usecase, do NOT forget to also call fsync before/after rename, too. Use durable_atomic_rename() to make sure.
Definition at line 199 of file filesystem.cpp.
References foedus::fs::Path::c_str(), and foedus::fs::rename().
Referenced by foedus::fs::durable_atomic_rename(), and foedus::fs::rename().
bool foedus::fs::create_directories | ( | const Path & | p, |
bool | sync = false |
||
) |
Recursive mkdir (mkdirs).
[in] | p | path of the directory to create |
[in] | sync | (optional, default false) wheter to call fsync() on the created directories and their parents. This is required to make sure the new directory entries become durable. |
Definition at line 89 of file filesystem.cpp.
References foedus::fs::create_directory(), foedus::fs::Path::empty(), foedus::fs::exists(), and foedus::fs::Path::parent_path().
Referenced by foedus::log::MetaLogger::initialize_once(), foedus::log::LogManagerPimpl::initialize_once(), foedus::fs::DirectIoFile::open(), foedus::externalize::Externalizable::save_to_file(), and foedus::snapshot::SnapshotManagerPimpl::snapshot_metadata().
bool foedus::fs::create_directory | ( | const Path & | p, |
bool | sync = false |
||
) |
mkdir.
[in] | p | path of the directory to create |
[in] | sync | (optional, default false) wheter to call fsync() on the created directory and its parent. This is required to make sure the new directory entry becomes durable. |
Definition at line 108 of file filesystem.cpp.
References foedus::fs::Path::c_str(), and foedus::fs::fsync().
Referenced by foedus::fs::create_directories().
Path foedus::fs::current_path | ( | ) |
Returns the current working directory.
Definition at line 66 of file filesystem.cpp.
References ASSERT_ND.
Referenced by foedus::fs::Path::Path().
fsync() on source file before rename, then fsync() on the parent folder after rename.
This method makes 2 fsync calls, one on old file before rename and another on the parent directory after rename.
Note that we don't need fsync on parent directory before rename assuming old_path and new_path is in the same folder (if not, you have to call fsync yourself before calling this method). Even if a crash happens right after rename, we still see the old content of new_path.
Also, we don't need fsync on new_path after rename because POSIX rename doesn't change the inode of renamed file. It's already there as soon as parent folder's fsync is done.
Quite complex and expensive, but this is required to make it durable regardless of filesystems. Fortunately, we have to call this method only once per epoch-advance.
Definition at line 236 of file filesystem.cpp.
References foedus::fs::atomic_rename(), foedus::fs::fsync(), and foedus::fs::Path::parent_path().
Referenced by foedus::externalize::Externalizable::save_to_file().
|
inline |
Returns if the file exists.
Definition at line 128 of file filesystem.hpp.
References foedus::fs::FileStatus::exists(), and foedus::fs::status().
Referenced by foedus::memory::SharedMemory::alloc(), foedus::memory::SharedMemory::attach(), foedus::fs::create_directories(), foedus::log::MetaLogger::initialize_once(), foedus::log::LogManagerPimpl::initialize_once(), foedus::savepoint::SavepointManagerPimpl::initialize_once(), foedus::externalize::Externalizable::load_from_file(), foedus::fs::DirectIoFile::open(), foedus::storage::MetadataSerializer::save_all_storages_to_xml(), foedus::externalize::Externalizable::save_to_file(), and foedus::snapshot::SnapshotManagerPimpl::snapshot_metadata().
uint64_t foedus::fs::file_size | ( | const Path & | p | ) |
Returns size of the file.
Definition at line 120 of file filesystem.cpp.
References foedus::fs::Path::c_str().
Referenced by foedus::snapshot::LogMapper::handle_process(), foedus::snapshot::SnapshotWriter::open(), foedus::fs::DirectIoFile::open(), foedus::snapshot::SnapshotManagerPimpl::read_snapshot_metadata(), foedus::restart::RestartManagerPimpl::redo_meta_logs(), and foedus::snapshot::SnapshotManagerPimpl::snapshot_metadata().
bool foedus::fs::fsync | ( | const Path & | path, |
bool | sync_parent_directory = false |
||
) |
Makes the content and metadata of the file durable all the way up to devices.
[in] | path | path of the file to make durable |
[in] | sync_parent_directory | (optional, default false) whether to also call fsync on the parent directory to make sure the directory entry is written to device. This is required when you create a new file, rename, etc. |
Surprisingly, there is no analogus method in boost::filesystem. This method provides the fundamental building block of fault-tolerant systems; fsync. We so far don't provide fdatasync (no metadata sync), but this should suffice.
Definition at line 203 of file filesystem.cpp.
References foedus::fs::Path::c_str(), foedus::fs::is_directory(), and foedus::fs::Path::parent_path().
Referenced by foedus::snapshot::SnapshotWriter::close(), foedus::fs::create_directory(), foedus::fs::durable_atomic_rename(), foedus::externalize::Externalizable::save_to_file(), foedus::snapshot::SnapshotManagerPimpl::snapshot_metadata(), foedus::fs::DirectIoFile::sync(), and foedus::fs::DirectIoFile::truncate().
Path foedus::fs::home_path | ( | ) |
Returns the absolute path of the home directory of the user running this process.
So far this checks only HOME environment variable, which might not be set in some environment. In that case, this returns an empty path. A truly crossplatform home_path is not in standard C++, unfortunately.
Definition at line 80 of file filesystem.cpp.
Referenced by foedus::fs::Path::Path().
|
inline |
Returns if the file is a directory.
Definition at line 133 of file filesystem.hpp.
References foedus::fs::FileStatus::is_directory(), and foedus::fs::status().
Referenced by foedus::fs::Path::child_paths(), and foedus::fs::fsync().
|
inline |
Returns if the file is a regular file.
Definition at line 138 of file filesystem.hpp.
References foedus::fs::FileStatus::is_regular_file(), and foedus::fs::status().
bool foedus::fs::remove | ( | const Path & | p | ) |
Deletes a regular file or an empty directory.
Definition at line 132 of file filesystem.cpp.
References foedus::fs::FileStatus::exists(), foedus::fs::FileStatus::is_regular_file(), and foedus::fs::status().
Referenced by foedus::memory::SharedMemory::release_block().
uint64_t foedus::fs::remove_all | ( | const Path & | p | ) |
Recursively deletes a directory.
Definition at line 148 of file filesystem.cpp.
References foedus::fs::Path::child_paths().
Just a synonym of atomic_rename() to avoid confusion.
Definition at line 286 of file filesystem.hpp.
References foedus::fs::atomic_rename().
Referenced by foedus::fs::atomic_rename().
SpaceInfo foedus::fs::space | ( | const Path & | p | ) |
Returns free space information for the device the file is on.
Definition at line 158 of file filesystem.cpp.
References foedus::fs::Path::c_str(), and foedus::fs::SpaceInfo::capacity_.
FileStatus foedus::fs::status | ( | const Path & | p | ) |
Returns the status of the file.
Definition at line 45 of file filesystem.cpp.
References foedus::fs::Path::c_str(), foedus::fs::kDirectoryFile, foedus::fs::kFileNotFound, foedus::fs::kRegularFile, foedus::fs::kStatusError, and foedus::fs::kTypeUnknown.
Referenced by foedus::storage::sequential::SequentialComposer::compose(), foedus::assorted::demangle_type_name(), foedus::fs::exists(), foedus::snapshot::LogMapper::handle_process(), foedus::soc::SocManagerPimpl::initialize_master(), foedus::EnginePimpl::initialize_once(), foedus::snapshot::MergeSort::initialize_once(), foedus::fs::is_directory(), foedus::fs::is_regular_file(), foedus::fs::remove(), foedus::soc::SocManagerPimpl::report_engine_fatal_error(), foedus::soc::SocManagerPimpl::wait_for_child_attach(), foedus::soc::SocManagerPimpl::wait_for_child_terminate(), foedus::soc::SocManagerPimpl::wait_for_children_module(), and foedus::soc::SocManagerPimpl::wait_for_master_module().
std::string foedus::fs::unique_name | ( | uint64_t | differentiator = 0 | ) |
Equivalent to unique_path("%%%%-%%%%-%%%%-%%%%").
Definition at line 174 of file filesystem.cpp.
Referenced by foedus::externalize::Externalizable::save_to_file().
std::string foedus::fs::unique_name | ( | const std::string & | model, |
uint64_t | differentiator = 0 |
||
) |
Returns a randomly generated file name with the given template.
[in] | model | file name template where % will be replaced with random hex numbers. |
[in] | differentiator | optional parameter to further randomize this method. |
We use std::chrono::high_resolution_clock::now() to get a random seed. However, even high_resolution_clock sometimes has low precision depending on environment. When you are concerned with a conflict (eg running many concurrent testcases), also give a differentiator.
Definition at line 177 of file filesystem.cpp.