Quick Links: |
Provide methods for filesystem access with multi-language names. More...
Namespaces | |
namespace | bdls |
bdls::FilesystemUtil | namespace for filesystem access methods |
bdls::FilesystemUtil
namespace is a thin wrapper on top of the operating system's own filesystem access functions, providing a consistent and unambiguous interface for handling files on all supported platforms. open
method is governed by three sets of enumerations: bdls::FilesystemUtil::FileOpenPolicy
governs whether open
creates a new file or opens an existing one. The following values are possible: e_OPEN
: e_CREATE
: e_CREATE_PRIVATE
: e_OPEN_OR_CREATE
: bdls::FilesystemUtil::FileIOPolicy
governs what Input/Output operations are allowed on a file after it is opened. The following values are possible: e_READ_ONLY
: e_WRITE_ONLY
: e_READ_WRITE
: e_APPEND_ONLY
: e_READ_APPEND
: bdls::FilesystemUtil::FileTruncatePolicy
governs whether open
deletes the existing contents of a file when it is opened. The following values are possible: e_TRUNCATE
: e_KEEP
: seek
method is governed by an enumeration that determines the point from which the seek operation starts: e_SEEK_FROM_BEGINNING
: e_SEEK_FROM_CURRENT
: e_SEEK_FROM_END
: bdls::FilesystemUtil::read
and bdls::FilesystemUtil::write
methods add no atomicity guarantees for reading and writing to those provided (if any) by the underlying platform's methods for reading and writing (see http://lwn.net/articles/180387/
). On Windows, methods of bdls::FilesystemUtil
that take a file or directory name or pattern as a char*
or bsl::string
type assume that the name is encoded in UTF-8. The routines attempt to convert the name to a UTF-16 wchar_t
string via bdlde::CharConvertUtf16::utf8ToUtf16
, and if the conversion succeeds, call the Windows wide-character W
APIs with the UTF-16 name. If the conversion fails, the method fails. Similarly, file searches returning file names call the Windows wide-character W
APIs and convert the resulting UTF-16 names to UTF-8.
utf8ToUtf16
nor the Windows W
APIs do any normalization of the UTF-16 strings resulting from UTF-8 conversion, and it is therefore possible to have sets of file names that have the same visual representation but are treated as different names by the filesystem. bdls::FilesystemUtil
as a char*
or bsl::string
type is passed unchanged to the underlying system file APIs. Because the file names and patterns are passed unchanged, bdls::FilesystemUtil
methods will work correctly on Posix with any encoding, but will interoperate only with processes that use the same encoding as the current process. open
method is called, file truncation is allowed only if the client requests an openPolicy
containing the word CREATE
and/or an ioPolicy
containing the word WRITE
. #ifdef BSLS_PLATFORM_OS_WINDOWS bsl::string logPath = "temp.1\\logs"; #else bsl::string logPath = "temp.1/logs"; #endif
bsl::string oldPath(logPath), newPath(logPath); bdls::PathUtil::appendRaw(&oldPath, "old"); bdls::PathUtil::appendRaw(&newPath, "new"); int rc = bdls::FilesystemUtil::createDirectories(oldPath, true); assert(0 == rc); rc = bdls::FilesystemUtil::createDirectories(newPath, true); assert(0 == rc);
bdls::PathUtil::appendRaw(&logPath, "*.log"); bsl::vector<bsl::string> logFiles; bdls::FilesystemUtil::findMatchingPaths(&logFiles, logPath.c_str());
bdlt::Datetime modTime; bsl::string fileName; for (bsl::vector<bsl::string>::iterator it = logFiles.begin(); it != logFiles.end(); ++it) { assert(0 == bdls::FilesystemUtil::getLastModificationTime(&modTime, *it)); assert(0 == bdls::PathUtil::getLeaf(&fileName, *it)); bsl::string *whichDirectory = 2 < (bdlt::CurrentTime::utc() - modTime).totalDays() ? &oldPath : &newPath; bdls::PathUtil::appendRaw(whichDirectory, fileName.c_str()); assert(0 == bdls::FilesystemUtil::move(it->c_str(), whichDirectory->c_str())); bdls::PathUtil::popLeaf(whichDirectory); }
bdls::FilesystemUtil::visitPaths
enables clients to define a function object to operate on file paths that match a specified pattern. In this example, we create a function that can be used to filter out files that have a last modified time within a particular time frame. void getFilesWithinTimeframe(bsl::vector<bsl::string> *vector, const char *item, const bdlt::Datetime& start, const bdlt::Datetime& end) { bdlt::Datetime datetime; int ret = bdls::FilesystemUtil::getLastModificationTime(&datetime, item); if (ret) { return; // RETURN } if (datetime < start || datetime > end) { return; // RETURN } vector->push_back(item); }
bdls::FilesystemUtil::visitPaths
and bdlf::BindUtil::bind
, we create a function for finding all file paths that match a specified pattern and have a last modified time within a specified start and end time (both specified as a bdlt::Datetime
): void findMatchingFilesInTimeframe(bsl::vector<bsl::string> *result, const char *pattern, const bdlt::Datetime& start, const bdlt::Datetime& end) { result->clear(); bdls::FilesystemUtil::visitPaths( pattern, bdlf::BindUtil::bind(&getFilesWithinTimeframe, result, bdlf::PlaceHolders::_1, start, end)); }