BloombergLP::bdls::FilesystemUtil

This struct provides a namespace for utility functions dealing with platform-independent filesystem access.

Synopsis

Declared in <bdls_filesystemutil.h>

struct FilesystemUtil;

Type Aliases

NameDescription
FileDescriptor FileDescriptor is an alias for the operating system's native file descriptor / file handle type.
Offset Offset is an alias for a signed value, representing the offset of a location within a file.

Enums

NameDescription
Unnamed enum Default growth increment for file operations.
ErrorType Enumeration of error codes returned by certain filesystem operations.
FileIOPolicy Enumeration used to distinguish between different sets of actions permitted on an open file descriptor.
FileOpenPolicy Enumeration used to determine whether 'open' should open an existing file, or create a new file.
FileTruncatePolicy Enumeration used to distinguish between different ways to handle the contents, if any, of an existing file immediately upon opening the file.
Whence Enumeration used to distinguish among different starting points for a seek operation.

Static Member Functions

NameDescription
close Close the specified descriptor. Return 0 on success and a non-zero value otherwise. A return value of k_BAD_FILE_DESCRIPTOR indicates that the supplied descriptor is invalid.
createDirectories createDirectories overloads
createPrivateDirectory createPrivateDirectory overloads
createTemporaryDirectory createTemporaryDirectory overloads
createTemporaryFile createTemporaryFile overloads
createTemporarySubdirectory createTemporarySubdirectory overloads
exists exists overloads
findMatchingPaths findMatchingPaths overloads
getAvailableSpace getAvailableSpace overloads
getFileSize getFileSize overloads
getFileSizeLimit Return the file size limit for this process, k_OFFSET_MAX if no limit is set, or a negative value if an error occurs. Note that if you are doing any calculations involving the returned value, it is recommended to check for k_OFFSET_MAX specifically to avoid integer overflow in your calculations.
getLastModificationTime getLastModificationTime overloads
getSymbolicLinkTarget getSymbolicLinkTarget overloads
getSystemTemporaryDirectory getSystemTemporaryDirectory overloads
getWorkingDirectory getWorkingDirectory overloads
growFile Grow the file with the specified descriptor to the size of at least the specified size bytes. Return 0 on success, and a non-zero value otherwise. If the optionally specified reserveFlag is true, make sure the space on disk is preallocated and not allocated on demand, preventing a possible out-of-disk-space error when accessing the data on file systems with sparse file support. Preallocation is done by writing unspecified data to file in blocks of the optionally specified increment or a default value if increment is zero or unspecified. Note that if the size of the file is greater than or equal to size, this function has no effect. Also note that the contents of the newly grown portion of the file is undefined.
isDirectory isDirectory overloads
isRegularFile isRegularFile overloads
isSymbolicLink isSymbolicLink overloads
lock Acquire a lock for the file with the specified descriptor. If lockWriteFlag is true, acquire an exclusive write lock; otherwise acquire a (possibly) shared read lock. The calling thread will block until the lock is acquired. Return 0 on success, and a non-zero value otherwise. Note that this operation locks the indicated file for use by the current process, but the behavior is unspecified (and platform-dependent) when either attempting to lock descriptor multiple times, or attempting to lock another descriptor referring to the same file, within a single process.
makeUnsafeTemporaryFilename makeUnsafeTemporaryFilename overloads
map Map the region of the specified size bytes, starting at the specified offset bytes into the file with the specified descriptor to memory, and load into the specified address of the mapped area. Return 0 on success, and a non-zero value otherwise. The access permissions for mapping memory are defined by the specified mode, which may be a combination of MemoryUtil::k_ACCESS_READ, MemoryUtil::k_ACCESS_WRITE and MemoryUtil::k_ACCESS_EXECUTE. Note that on failure, the value of address is undefined. Also note that mapping will succeed even if there are fewer than offset + size bytes in the specified file, and an attempt to access the mapped memory beyond the end of the file will result in undefined behavior (i.e., this function does not grow the file to guarantee it can accommodate the mapped region). Also note that mapping past the end of file may return 0, but any access of the resulting mapped memory may segfault.
mapChecked Map the region of the specified size bytes, starting at the specified offset bytes into the file with the specified descriptor to memory, and load into the specified address of the mapped area. Return 0 on success, k_ERROR_PAST_EOF if an attempt is made to map past the end of file, and a non-zero value otherwise. The access permissions for mapping memory are defined by the specified mode, which may be a combination of MemoryUtil::k_ACCESS_READ, MemoryUtil::k_ACCESS_WRITE and MemoryUtil::k_ACCESS_EXECUTE, though on some platforms they must be a subset of the file permissions. The behavior is undefined unless bits in mode other than MemoryUtil::k_ACCESS_READ_WRITE_EXECUTE are all clear, unless 0 <= offset, and unless 0 < size, and unless the offset is a multiple of MemoryUtil::pageSize(). Note that on failure, the value of address is undefined. Also note that the check against mapping past the end of file and all assertions are done before the call to map the file.
move move overloads
open open overloads
read Read the specified numBytes bytes beginning at the file pointer of the file with the specified descriptor into the specified buffer. Return numBytes on success; the number of bytes read if there were not enough available; or a negative number on some other error.
remove remove overloads
rollFileChain Remove the file at the specified path appended with the specified maxSuffix using a . as a separator. Then move the files with the suffixes .1 to .maxSuffix-1 so they have new suffixes from .2 to .maxSuffix. Finally, move path to path with a .1 suffix. Return 0 on success, and non-zero otherwise.
seek Set the file pointer associated with the specified descriptor (used by calls to the read and write system calls) according to the specified whence behavior: ` * If 'whence' is e_SEEK_FROM_BEGINNING, set the pointer to 'offset' bytes from the beginning of the file. * If 'whence' is e_SEEK_FROM_CURRENT, advance the pointer by 'offset' bytes * If 'whence' is e_SEEK_FROM_END, set the pointer to 'offset' bytes beyond the end of the file. ` Return the new location of the file pointer, in bytes from the beginning of the file, on success; and -1 otherwise. The effect on the file pointer is undefined unless the file is on a device capable of seeking. Note that seek does not change the size of the file if the pointer advances beyond the end of the file; instead, the next write at the pointer will increase the file size.
setWorkingDirectory setWorkingDirectory overloads
sync Synchronize the contents of the specified numBytes of mapped memory beginning at the specified address with the underlying file on disk. If the specified syncFlag is true, block until all writes to nonvolatile media have actually completed, otherwise, return once they have been scheduled. Return 0 on success, and a non-zero value otherwise. The behavior is undefined unless address is aligned on a page boundary, numBytes is a multiple of pageSize(), and 0 <= numBytes.
truncateFileSize Set the size of the file referred to by the specified descriptor to the specified size. descriptor must be open for writing. After the function call, the position is set to the end of the file. Return 0 on success and a non-zero value otherwise. The behavior is undefined if the file is currently mapped, or if size is greater than the existing size of the file.
tryLock Acquire a lock for the file with the specified descriptor if it is currently available. If the specified lockWriteFlag is true, acquire an exclusive write lock unless another process has any type of lock on the file. If lockWriteFlag is false, acquire a shared read lock unless a process has a write lock. This method will not block. Return 0 on success, k_ERROR_LOCKING_CONFLICT if the platform reports the lock could not be acquired because another process holds a conflicting lock, and a negative value for any other kind of error. Note that this operation locks the indicated file for the current process, but the behavior is unspecified (and platform-dependent) when either attempting to lock descriptor multiple times, or attempting to lock another descriptor referring to the same file, within a single process.
unlock Release any lock this process holds on the file with the specified descriptor. Return 0 on success, and a non-zero value otherwise.
unmap Unmap the memory mapping with the specified base address and specified size. Return 0 on success, and a non-zero value otherwise. The behavior is undefined unless this area with address and size was previously mapped with a map call.
visitPaths visitPaths overloads
visitTree Recursively traverse the directory tree starting at the specified root for files whose leaf names match the specified pattern, and run the specified function visitor, passing it the full path starting with root to each pattern matching file. See findMatchingPaths for a discussion of how pattern is interpreted. If the specified sortFlag is true, traverse the files in the tree in sorted order, sorted by the full path name, otherwise the order in which the files will be visited is unspecified. UTF-8 paths will be sorted by strcmp, which sorts by chars, not unicode code points. Found . and .. directories are ignored, except that root may be . or ... Return 0 on success, and a non-zero value otherwise. This function will fail if root does not specify a directory, of if pattern contains / on Unix or '' on Windows. Note that both directories and plain files whose names match pattern will be visited, while other files such as symlinks will not be visited or followed. No file or directory that is not matched will be visited. All directories are traversed, regardless of whether they are matched. If a directory is matched and sortFlag is true, it is visited immediately before it is traversed. Also note that root is never visited, even if it matches pattern. Also note that no pattern matching is done on root -- if it contains wildcards, they are not interpreted as such and must exactly match the characters in the name of the directory.
write Write the specified numBytes from the specified buffer address to the file with the specified descriptor. Return numBytes on success; the number of bytes written if space was exhausted; or a negative value on some other error.

Static Data Members

NameDescription
k_INVALID_FD FileDescriptor value representing no file, used as the error return for open.
k_OFFSET_MAX maximum representable file offset value
k_OFFSET_MIN minimum representable file offset value