scclib
Stable Cloud Computing C++ Library
|
Common file system utilities. More...
#include <fs.h>
Static Public Member Functions | |
static std::map< std::string, FileType > | scan_dir (const std::string &, std::function< bool(const std::string &, FileType)>=default_scan_filter, std::system_error *=nullptr) |
Scan a directory, and return a map of names and file types. More... | |
static FileType | file_type (const std::string &, std::system_error *=nullptr) |
Get the file type. More... | |
static FileStat | file_stat (const std::string &, std::system_error *=nullptr) |
Get the file stat. More... | |
static void | set_mode (const std::string &, unsigned, std::system_error *=nullptr) |
Set the file mode. More... | |
static void | set_times (const std::string &, uint64_t, uint64_t, std::system_error *=nullptr) |
Set the file times. More... | |
static void | set_ids (const std::string &, int, int, std::system_error *=nullptr) |
Set the user id and/or group id. More... | |
static void | set_size (const std::string &, off_t, std::system_error *=nullptr) |
Set the file size. More... | |
static std::map< off_t, off_t > | sparse_map (const std::string &, std::system_error *=nullptr) |
Map of file sparseness. More... | |
static void | remove (const std::string &, std::system_error *=nullptr) |
Remove the file or directory. More... | |
static void | rename (const std::string &, const std::string &, std::system_error *=nullptr) |
Rename the file or directory. More... | |
static void | remove_all (const std::string &fn, std::system_error *err=nullptr) |
Recursively removes the file or directory. More... | |
static void | create_dir (const std::string &, std::system_error *=nullptr) |
Create a directory. More... | |
static void | create_reg (const std::string &, std::system_error *=nullptr) |
Create a regular file. More... | |
static std::string | create_tmp_reg (const std::string &, std::system_error *=nullptr) |
Create a temporary regular file. More... | |
static void | create_symlink (const std::string &, const std::string &, std::system_error *=nullptr) |
Create a symbolic link. More... | |
static void | create_link (const std::string &, const std::string &, std::system_error *=nullptr) |
Create a hard link. More... | |
static void | create_fifo (const std::string &, std::system_error *=nullptr) |
Create a named pipe (FIFO). More... | |
static void | change_dir (const std::string &, std::system_error *=nullptr) |
Change working directory. More... | |
static std::string | get_current_dir (std::system_error *=nullptr) |
Get working directory. More... | |
static std::string | read_symlink (const std::string &, std::system_error *=nullptr) |
Read the location of a symbolic link target. More... | |
static std::string | norm_path (const std::string &, const std::string &) noexcept |
Normalize a path with base directory. More... | |
static std::string | norm_path (const std::string &path) noexcept |
Normalize a path. More... | |
Common file system utilities.
Utility to manipulate files and the filesystem.
|
static |
Change working directory.
On error, throws an exception or sets err if provided.
|
static |
Create a directory.
On error, throws an exception or sets err if provided.
name | File name. |
err | Exception override. |
The directory will be created if necessary with mode 0700.
|
static |
Create a named pipe (FIFO).
On error, throws an exception or sets err if provided.
The file will be created if necessary with mode 0600.
|
static |
Create a hard link.
Results in a second inode that points to the original file.
orig_name | Original file name. |
new_name | New file name. |
err | Exception override. |
On error, throws an exception or sets err if provided.
|
static |
Create a regular file.
name | File name. |
err | Exception override. |
The file will be created if necessary with mode 0600.
If the file exists, and cannot be opened for writing, throws an error.
On error, throws an exception or sets err if provided.
|
static |
Create a symbolic link.
target | Target of the link. |
name | Path name of the link file. |
err | Exception override. |
E.g. create_symlink("../reg", "dir/link") creates a symlink: dir/link -> ../reg
On error, throws an exception or sets err if provided.
|
static |
Create a temporary regular file.
Return the filename, which will be prefix plus a random string.
On error, throws an exception or sets err if provided.
|
static |
Get the file stat.
On error, throws an exception or sets err if provided.
|
static |
|
static |
Get working directory.
On error, throws an exception or sets err if provided.
|
staticnoexcept |
Normalize a path with base directory.
If path is relative, will append to the base dir, and return a normalized version.
If path is absolute, ignores base_dir, and returns a normalized version of path.
Normalized version removes all consecutive slashes, and unneccesary . and .. directories.
Returned paths starting with:
An input of "" paths will result in output "."
Conserves trailing slash, if any.
base_dir | Base directory |
path | Path |
|
inlinestaticnoexcept |
|
static |
Read the location of a symbolic link target.
name | The symbolic link name |
err | Exception override. |
Example if "dir/link" -> "dir/regular", read_symlink(dir/link) will return "dir/regular"
On error, throws an exception or sets err if provided.
|
static |
Remove the file or directory.
On error, throws an exception or sets err if provided.
|
inlinestatic |
|
static |
|
static |
Scan a directory, and return a map of names and file types.
Does not scan recursively.
dirname | The directory name. |
filter | Scan filter function which is called with parameters (name, FileType), and returns true if the file should be included. |
err | If set, on error will set this value and the exception will not be thrown. |
Example in scclib/util/unittest/fs.cc
|
static |
|
static |
Set the file mode.
name | File name. |
mode | File mode, e.g. 0664. |
err | Exception override. |
File mode can be specified in octal format:
Calling this api on a symbolic link will result in an error.
On error, throws an exception or sets err if provided.
|
static |
Set the file size.
name | File name. |
size | New file size. |
err | Exception override. |
If size is less than current size, file is truncated.
If size is greater than current size, a sparse file is created.
Example in scclib/util/unittest/fs.cc
|
static |
Set the file times.
Times are in nanoseconds since epoch (divide by 1e9 to get time_t)
name | File name. |
access_time | File access time. |
mod_time | File modification time. |
err | Exception override. |
On error, throws an exception or sets err if provided.
|
static |
Map of file sparseness.
name | File name. |
err | Exception override. |
A sparse file will contain data only in blocks which have been written. When data is read from an unwritten "hole", 0 is returned.
For example, a 16 K sparse file with a single 0 written at the front and end will have map:
<4096, 12287> - block from 4096 to 12287 is hole
This file reads all 0, but takes up 8192 bytes on disk instead of 16384.
Example in scclib/util/unittest/fs.cc