BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdls_filesystemutil

Detailed Description

Outline

Purpose

Provide methods for filesystem access with multi-language names.

Classes

See also
bdls_pathutil

Description

This component provides a platform-independent interface to filesystem utility methods, supporting multi-language file and path names. Each method in the 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.

Methods in this component can be used to manipulate files with any name in any language on all supported platforms. To provide such support, the following restrictions are applied to file names and patterns passed to methods of this component: On Windows, all file names and patterns must be passed as UTF-8-encoded strings; file search results will similarly be encoded as UTF-8. On Posix, file names and patterns may be passed in any encoding, but all processes accessing a given file must encode its name in the same encoding. On modern Posix installations, this effectively means that file names and patterns should be encoded in UTF-8, just as on Windows. See the section "Platform-Specific File Name Encoding Caveats" below.

Policies for open

The behavior of the open method is governed by three sets of enumerations:

OpenCreate Policy: bdls::FilesystemUtil::FileOpenPolicy

bdls::FilesystemUtil::FileOpenPolicy governs whether open creates a new file or opens an existing one. The following values are possible:

InputOutput Access Policy: bdls::FilesystemUtil::FileIOPolicy

bdls::FilesystemUtil::FileIOPolicy governs what Input/Output operations are allowed on a file after it is opened. The following values are possible:

Truncation Policy: bdls::FilesystemUtil::FileTruncatePolicy

bdls::FilesystemUtil::FileTruncatePolicy governs whether open deletes the existing contents of a file when it is opened. The following values are possible:

Starting Points for seek

The behavior of the seek method is governed by an enumeration that determines the point from which the seek operation starts:

Platform-Specific File Locking Caveats

Locking has the following caveats for the following operating systems:

Platform-Specific Atomicity Caveats

The 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/).

Platform-Specific File Name Encoding Caveats

File-name encodings have the following caveats for the following operating systems:

File Truncation Caveats

In order to provide consistent behavior across both Posix and Windows platforms, when the 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.

Usage

This section illustrates intended use of this component.

Example 1: General Usage

In this example, we start with a (relative) native path to a directory containing log files:

#ifdef BSLS_PLATFORM_OS_WINDOWS
bsl::string logPath = "temp.1\\logs";
#else
bsl::string logPath = "temp.1/logs";
#endif
Definition bslstl_string.h:1281

Suppose that we want to separate files into "old" and "new" subdirectories on the basis of modification time. We will provide paths representing these locations, and create the directories if they do not exist:

bsl::string oldPath(logPath), newPath(logPath);
bdls::PathUtil::appendRaw(&oldPath, "old");
bdls::PathUtil::appendRaw(&newPath, "new");
assert(0 == rc);
assert(0 == rc);
static int createDirectories(const char *path, bool isLeafDirectoryFlag=false)
static void appendRaw(bsl::string *path, const char *filename, int length=-1, int rootEnd=-1)

We know that all of our log files match the pattern "*.log", so let's search for all such files in the log directory:

bdls::PathUtil::appendRaw(&logPath, "*.log");
const CHAR_TYPE * c_str() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6705
Definition bslstl_vector.h:1025
static int findMatchingPaths(bsl::vector< bsl::string > *result, const char *pattern)

Now for each of these files, we will get the modification time. Files that are older than 2 days will be moved to "old", and the rest will be moved to "new":

bsl::string fileName;
it != logFiles.end(); ++it) {
assert(0 ==
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);
}
Definition bdlt_datetime.h:331
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2511
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2519
VALUE_TYPE * iterator
Definition bslstl_vector.h:1057
static int getLastModificationTime(bdlt::Datetime *time, const char *path)
static int move(const char *oldPath, const char *newPath)
static int popLeaf(bsl::string *path, int rootEnd=-1)
static int getLeaf(bsl::string *leaf, const bsl::string_view &path, int rootEnd=-1)
static Datetime utc()
Definition bdlt_currenttime.h:296

Example 2: Using bdls::FilesystemUtil::visitPaths

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.

First we define our filtering function:

void getFilesWithinTimeframe(bsl::vector<bsl::string> *vector,
const char *item,
const bdlt::Datetime& start,
const bdlt::Datetime& end)
{
bdlt::Datetime datetime;
item);
if (ret) {
return; // RETURN
}
if (datetime < start || datetime > end) {
return; // RETURN
}
vector->push_back(item);
}
void push_back(const VALUE_TYPE &value)
Definition bslstl_vector.h:3760

Then, with the help of 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();
pattern,
bdlf::BindUtil::bind(&getFilesWithinTimeframe,
result,
start,
end));
}
static Bind< bslmf::Nil, t_FUNC, Bind_BoundTuple0 > bind(t_FUNC func)
Definition bdlf_bind.h:1830
void swap(vector &other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(AllocatorTraits void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:1712
const PlaceHolder< 1 > _1
static int visitPaths(const char *pattern, const bsl::function< void(const char *path)> &visitor)