ucommon
7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
![]() ![]() |
#include <fsys.h>
Public Types | |
enum | { OWNER_READONLY = 0400, GROUP_READONLY = 0440, PUBLIC_READONLY = 0444, OWNER_PRIVATE = 0600, OWNER_PUBLIC = 0644, GROUP_PRIVATE = 0660, GROUP_PUBLIC = 0664, EVERYONE = 0666, DIR_TEMPORARY = 01777 } |
enum | access_t { RDONLY, WRONLY, REWRITE, RDWR = REWRITE, APPEND, SHARED, EXCLUSIVE, DEVICE, STREAM, RANDOM } |
typedef struct stat | fileinfo_t |
typedef long | offset_t |
Public Member Functions | |
fsys () | |
fsys (fd_t handle) | |
fsys (const fsys &descriptor) | |
fsys (const char *path, access_t access) | |
fsys (const char *path, unsigned permission, access_t access) | |
~fsys () | |
fd_t | operator* () const |
operator fd_t () const | |
void | reset (void) |
operator bool () const | |
bool | operator! () const |
fsys & | operator= (const fsys &descriptor) |
fsys & | operator*= (fd_t &descriptor) |
fsys & | operator= (fd_t descriptor) |
fd_t | handle (void) const |
void | set (fd_t descriptor) |
fd_t | release (void) |
int | seek (offset_t offset) |
int | drop (offset_t size=0) |
bool | is_tty (void) const |
ssize_t | read (void *buffer, size_t count) |
ssize_t | write (const void *buffer, size_t count) |
int | info (fileinfo_t *buffer) |
int | trunc (offset_t offset) |
int | sync (void) |
void | open (const char *path, access_t access) |
void | assign (fd_t descriptor) |
void | open (const char *path, unsigned mode, access_t access) |
int | close (void) |
int | err (void) const |
Static Public Member Functions | |
static int | remapError (void) |
static bool | is_tty (fd_t fd) |
static int | prefix (const char *path) |
static int | prefix (char *path, size_t size) |
static stringref_t | prefix (void) |
static int | info (const char *path, fileinfo_t *buffer) |
static int | erase (const char *path) |
static int | copy (const char *source, const char *target, size_t size=1024) |
static int | rename (const char *oldpath, const char *newpath) |
static int | mode (const char *path, unsigned value) |
static bool | is_exists (const char *path) |
static bool | is_readable (const char *path) |
static bool | is_writable (const char *path) |
static bool | is_executable (const char *path) |
static bool | is_file (const char *path) |
static bool | is_dir (const char *path) |
static bool | is_link (const char *path) |
static bool | is_device (const char *path) |
static bool | is_hidden (const char *path) |
static void | assign (fsys &object, fd_t descriptor) |
static int | unlink (const char *path) |
static int | link (const char *path, const char *target) |
static int | hardlink (const char *path, const char *target) |
static int | linkinfo (const char *path, char *buffer, size_t size) |
static fd_t | input (const char *path) |
static fd_t | output (const char *path) |
static fd_t | append (const char *path) |
static void | release (fd_t descriptor) |
static int | pipe (fd_t &input, fd_t &output, size_t size=0) |
static int | inherit (fd_t &descriptor, bool enable) |
static fd_t | null (void) |
static int | load (const char *path) |
static int | exec (const char *path, char **argv, char **envp=NULL) |
static bool | is_file (struct stat *inode) |
static bool | is_dir (struct stat *inode) |
static bool | is_link (struct stat *inode) |
static bool | is_dev (struct stat *inode) |
static bool | is_char (struct stat *inode) |
static bool | is_disk (struct stat *inode) |
static bool | is_sys (struct stat *inode) |
Static Public Attributes | |
static const offset_t | end = (offset_t)(-1) |
Protected Attributes | |
fd_t | fd |
int | error |
A container for generic and o/s portable threadsafe file system functions. These are based roughly on their posix equivilents. For libpth, the system calls are wrapped. The native file descriptor or handle may be used, but it is best to use "class fsys" instead because it can capture the errno of a file operation in a threadsafe and platform independent manner, including for mswindows targets.
typedef struct stat ucommon::fsys::fileinfo_t |
typedef long ucommon::fsys::offset_t |
anonymous enum |
ucommon::fsys::fsys | ( | ) |
Construct an unattached fsys descriptor.
Definition at line 1171 of file fsys.cpp.
References error, fd, and INVALID_HANDLE_VALUE.
ucommon::fsys::fsys | ( | fd_t | handle | ) |
Contruct fsys from raw file handle.
Definition at line 1762 of file fsys.cpp.
References ucommon::dso::error.
ucommon::fsys::fsys | ( | const fsys & | descriptor | ) |
Copy (dup) an existing fsys descriptor.
descriptor | to copy from. |
Definition at line 1061 of file fsys.cpp.
References copy(), ucommon::dup(), error, fd, and INVALID_HANDLE_VALUE.
ucommon::fsys::fsys | ( | const char * | path, |
access_t | access | ||
) |
Create a fsys descriptor by opening an existing file or directory.
path | of file to open for created descriptor. |
access | mode of file. |
Definition at line 1184 of file fsys.cpp.
References fd, INVALID_HANDLE_VALUE, and open().
ucommon::fsys::fsys | ( | const char * | path, |
unsigned | permission, | ||
access_t | access | ||
) |
Create a fsys descriptor by creating a file.
path | of file to create for descriptor. |
access | mode of file access. |
permission | mode of file. |
Definition at line 1190 of file fsys.cpp.
References fd, INVALID_HANDLE_VALUE, and open().
ucommon::fsys::~fsys | ( | ) |
|
static |
|
inline |
Assign descriptor directly.
descriptor | to assign. |
Definition at line 491 of file fsys.h.
Referenced by digest(), and ucommon::pipestream::open().
int ucommon::fsys::close | ( | void | ) |
Close a fsys resource.
Definition at line 820 of file fsys.cpp.
References error, fd, INVALID_HANDLE_VALUE, and remapError().
Referenced by ucommon::filestream::close(), copy(), digest(), open(), operator*=(), operator=(), release(), scrub(), and ~fsys().
|
static |
Copy a file.
source | file. |
target | file. |
size | of buffer. |
Definition at line 1374 of file fsys.cpp.
References buffer, close(), end, err(), GROUP_PUBLIC, ucommon::is(), open(), read(), result(), STREAM, and write().
Referenced by fsys(), and ucommon::secure::oscerts().
int ucommon::fsys::drop | ( | offset_t | size = 0 | ) |
Drop cached data from start of file.
size | of region to drop or until end of file. |
Definition at line 1110 of file fsys.cpp.
References error, fd, and remapError().
|
static |
Erase (remove) a file only.
path | of file. |
Definition at line 1341 of file fsys.cpp.
References is_device(), and remapError().
|
inline |
Get last error.
Definition at line 557 of file fsys.h.
References error.
Referenced by copy(), digest(), ucommon::filestream::err(), ucommon::dir::err(), scrub(), and zerofill().
|
static |
Execute a process and get exit code.
path | to execute. |
argv | list. |
optional | env. |
Definition at line 1784 of file fsys.cpp.
References ucommon::shell::spawn(), and ucommon::shell::wait().
|
inline |
|
static |
Create a hard link.
path | to create link to. |
target | of link. |
Definition at line 1252 of file fsys.cpp.
References link(), and remapError().
Referenced by link().
|
static |
Stat a file.
path | of file to stat. |
buffer | to save stat info. |
int ucommon::fsys::info | ( | fileinfo_t * | buffer | ) |
Get status of open descriptor.
buffer | to save status info in. |
Referenced by digest(), encodefile(), scrub(), and zerofill().
|
static |
Changle inheritable handle. On windows this is done by creating a duplicate handle and then closing the original. Elsewhere this is done simply by setting flags.
descriptor | to modify. |
enable | child process inheritence. |
Definition at line 1073 of file fsys.cpp.
References fd, and remapError().
Referenced by ucommon::pipestream::open().
|
static |
|
inlinestatic |
|
inlinestatic |
|
static |
Test if path is a device path.
path | to test. |
Definition at line 1697 of file fsys.cpp.
References ucommon::is_dir(), and strnicmp().
Referenced by erase(), ucommon::is_device(), and ucommon::dir::remove().
|
static |
Test if path is a directory.
path | to test. |
Definition at line 1500 of file fsys.cpp.
Referenced by dirpath(), ucommon::is_dir(), is_executable(), ucommon::DirPager::load(), main(), process(), scan(), and scrub().
|
inlinestatic |
|
inlinestatic |
|
static |
Test if path is executable.
path | to test. |
Definition at line 1050 of file fsys.cpp.
References is_dir().
Referenced by ucommon::is_executable().
|
static |
Test if path exists.
path | to test. |
Definition at line 1026 of file fsys.cpp.
Referenced by ucommon::is_exists().
|
static |
Test if path is a file.
path | to test. |
Definition at line 1453 of file fsys.cpp.
References S_ISREG.
Referenced by ucommon::is_file(), and process().
|
inlinestatic |
|
static |
|
static |
Test if path is a symlink.
path | to test. |
Definition at line 1478 of file fsys.cpp.
References S_ISLNK.
Referenced by dirpath(), ucommon::is_link(), linkinfo(), scan(), scrub(), and unlink().
|
inlinestatic |
|
static |
Test if path readable.
path | to test. |
Definition at line 1034 of file fsys.cpp.
Referenced by ucommon::is_readable().
|
inlinestatic |
|
static |
bool ucommon::fsys::is_tty | ( | void | ) | const |
See if current file stream is a tty device.
Definition at line 801 of file fsys.cpp.
References fd.
Referenced by encodestream(), ucommon::shell::getline(), and ucommon::shell::inkey().
|
static |
Test if path writable.
path | to test. |
Definition at line 1042 of file fsys.cpp.
Referenced by ucommon::is_writable().
|
static |
Create a symbolic link.
path | to create. |
target | of link. |
Definition at line 1265 of file fsys.cpp.
References hardlink(), INVALID_HANDLE_VALUE, and remapError().
Referenced by hardlink().
|
static |
Read a symbolic link to get it's target.
path | of link. |
buffer | to save target into. |
size | of buffer. |
Definition at line 1210 of file fsys.cpp.
References buffer, INVALID_HANDLE_VALUE, is_link(), remapError(), and ucommon::String::set().
|
static |
Load a library into memory.
path | to plugin. |
Definition at line 1433 of file fsys.cpp.
References ucommon::dso::error, ucommon::dso::map(), ucommon::dso::ptr, and remapError().
|
static |
Change file access mode.
path | to change. |
value | of mode to assign. |
Definition at line 1019 of file fsys.cpp.
References remapError().
Referenced by seek().
|
static |
Create inheritable /dev/null handle.
Definition at line 778 of file fsys.cpp.
Referenced by ucommon::pipestream::open().
void ucommon::fsys::open | ( | const char * | path, |
access_t | access | ||
) |
Open a file or directory.
path | of file to open. |
access | mode of descriptor. |
Definition at line 919 of file fsys.cpp.
References APPEND, close(), DEVICE, error, EXCLUSIVE, fd, INVALID_HANDLE_VALUE, O_NONBLOCK, RANDOM, RDONLY, remapError(), REWRITE, SHARED, STREAM, and WRONLY.
Referenced by copy(), digest(), fsys(), ucommon::filestream::open(), open(), scrub(), and zerofill().
void ucommon::fsys::open | ( | const char * | path, |
unsigned | mode, | ||
access_t | access | ||
) |
Open a file descriptor directly.
path | of file to create. |
access | mode of descriptor. |
mode | of file if created. |
Definition at line 854 of file fsys.cpp.
References APPEND, close(), DEVICE, error, EXCLUSIVE, fd, INVALID_HANDLE_VALUE, open(), RANDOM, RDONLY, remapError(), REWRITE, SHARED, STREAM, and WRONLY.
|
inline |
Test if file descriptor is open.
Definition at line 246 of file fsys.h.
References INVALID_HANDLE_VALUE.
|
inline |
|
inline |
Test if file descriptor is closed.
Definition at line 254 of file fsys.h.
References INVALID_HANDLE_VALUE.
|
inline |
Replace current file descriptor with an external descriptor. This does not create a duplicate. The external descriptor object is marked as invalid.
Definition at line 1201 of file fsys.cpp.
References close(), fd, and INVALID_HANDLE_VALUE.
Assign file descriptor by duplicating another descriptor.
descriptor | to dup from. |
Definition at line 1099 of file fsys.cpp.
References close(), ucommon::dup(), error, fd, INVALID_HANDLE_VALUE, and remapError().
Assing file descriptor from system descriptor.
descriptor | to dup from. |
Definition at line 1088 of file fsys.cpp.
References close(), ucommon::dup(), error, fd, INVALID_HANDLE_VALUE, and remapError().
|
static |
Create pipe. These are created inheritable by default.
input | descriptor. |
output | descriptor. |
size | of buffer if supported. |
Definition at line 783 of file fsys.cpp.
References input(), output(), and remapError().
Referenced by ucommon::pipestream::open().
|
static |
Get current directory prefix (pwd).
path | to save directory into. |
size | of path we can save. |
Definition at line 1012 of file fsys.cpp.
References remapError().
|
static |
Set directory prefix (chdir).
path | to change to. |
Definition at line 1005 of file fsys.cpp.
References remapError().
|
static |
Definition at line 1803 of file fsys.cpp.
References ucommon::stringref< R >::create(), ucommon::typeref< const char * >::expand(), and out.
Referenced by ucommon::shell::set0().
ssize_t ucommon::fsys::read | ( | void * | buffer, |
size_t | count | ||
) |
Read data from descriptor or scan directory.
buffer | to read into. |
count | of bytes to read. |
Definition at line 750 of file fsys.cpp.
References error, fd, and remapError().
Referenced by copy(), digest(), ucommon::temporary< uint8_t * >::read(), ucommon::pipestream::underflow(), and ucommon::filestream::underflow().
|
static |
fd_t ucommon::fsys::release | ( | void | ) |
Release descriptor, do not close.
Definition at line 1775 of file fsys.cpp.
References ucommon::dso::error, and INVALID_HANDLE_VALUE.
Referenced by ucommon::pipestream::open(), and ucommon::pipestream::release().
|
inlinestatic |
Definition at line 152 of file fsys.h.
Referenced by close(), ucommon::dir::close(), ucommon::dir::create(), drop(), erase(), hardlink(), inherit(), link(), linkinfo(), load(), mode(), open(), ucommon::dir::open(), operator=(), pipe(), prefix(), read(), ucommon::dir::remove(), rename(), seek(), sync(), trunc(), unlink(), and write().
|
static |
Rename a file.
oldpath | to rename from. |
newpath | to rename to. |
Definition at line 1426 of file fsys.cpp.
References remapError().
|
inline |
Reset error flag.
Definition at line 238 of file fsys.h.
References error.
Referenced by ucommon::dir::reset().
int ucommon::fsys::seek | ( | offset_t | offset | ) |
Set the position of a file descriptor.
offset | from start of file or "end" to append. |
Definition at line 1124 of file fsys.cpp.
References end, error, fd, mode(), and remapError().
Referenced by ucommon::filestream::rewind(), scrub(), ucommon::filestream::seek(), trunc(), and zerofill().
void ucommon::fsys::set | ( | fd_t | descriptor | ) |
Set with external descriptor. Closes existing file if open.
descriptor | of open file. |
Definition at line 1768 of file fsys.cpp.
References ucommon::dso::error.
int ucommon::fsys::sync | ( | void | ) |
Commit changes to the filesystem.
Definition at line 759 of file fsys.cpp.
References error, fd, and remapError().
int ucommon::fsys::trunc | ( | offset_t | offset | ) |
Truncate file to specified length. The file pointer is positioned to the new end of file.
offset | to truncate to. |
Definition at line 987 of file fsys.cpp.
References remapError(), and seek().
Referenced by scrub().
|
static |
Remove a symbolic link explicitly. Other kinds of files are also deleted. This should be used when uncertain about symlinks requiring special support.
path | to remove. |
Definition at line 1314 of file fsys.cpp.
References INVALID_HANDLE_VALUE, is_link(), and remapError().
Referenced by scrub().
ssize_t ucommon::fsys::write | ( | const void * | buffer, |
size_t | count | ||
) |
Write data to descriptor.
buffer | to write from. |
count | of bytes to write. |
Definition at line 769 of file fsys.cpp.
References error, fd, and remapError().
Referenced by copy(), ucommon::pipestream::overflow(), ucommon::filestream::overflow(), scrub(), ucommon::temporary< uint8_t * >::write(), and zerofill().
|
static |
|
mutableprotected |
Definition at line 129 of file fsys.h.
Referenced by close(), ucommon::dir::close(), drop(), fsys(), open(), ucommon::dir::open(), operator=(), read(), ucommon::dir::remove(), seek(), sync(), and write().
|
protected |