UCommon
|
A container for generic and o/s portable threadsafe file system functions. More...
#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 } |
Most of the common chmod values are predefined. More... | |
enum | access_t { RDONLY , WRONLY , REWRITE , RDWR = REWRITE , APPEND , SHARED , EXCLUSIVE , DEVICE , STREAM , RANDOM } |
Enumerated file access modes. More... | |
typedef struct stat | fileinfo_t |
typedef long | offset_t |
File offset type. | |
Public Member Functions | |
void | assign (fd_t descriptor) |
Assign descriptor directly. | |
int | close (void) |
Close a fsys resource. | |
int | drop (offset_t size=0) |
Drop cached data from start of file. | |
int | err (void) const |
Get last error. | |
fsys () | |
Construct an unattached fsys descriptor. | |
fsys (const char *path, access_t access) | |
Create a fsys descriptor by opening an existing file or directory. | |
fsys (const char *path, unsigned permission, access_t access) | |
Create a fsys descriptor by creating a file. | |
fsys (const fsys &descriptor) | |
Copy (dup) an existing fsys descriptor. | |
fsys (fd_t handle) | |
Contruct fsys from raw file handle. | |
fd_t | handle (void) const |
Get the native system descriptor handle of the file descriptor. | |
int | info (fileinfo_t *buffer) |
Get status of open descriptor. | |
bool | is_tty (void) const |
See if current file stream is a tty device. | |
void | open (const char *path, access_t access) |
Open a file or directory. | |
void | open (const char *path, unsigned mode, access_t access) |
Open a file descriptor directly. | |
operator bool () const | |
Test if file descriptor is open. | |
operator fd_t () const | |
Get the descriptor from the object by casting reference. | |
bool | operator! () const |
Test if file descriptor is closed. | |
fd_t | operator* () const |
Get the descriptor from the object by pointer reference. | |
fsys & | operator*= (fd_t &descriptor) |
Replace current file descriptor with an external descriptor. | |
fsys & | operator= (const fsys &descriptor) |
Assign file descriptor by duplicating another descriptor. | |
fsys & | operator= (fd_t descriptor) |
Assing file descriptor from system descriptor. | |
ssize_t | read (void *buffer, size_t count) |
Read data from descriptor or scan directory. | |
fd_t | release (void) |
Release descriptor, do not close. | |
void | reset (void) |
Reset error flag. | |
int | seek (offset_t offset) |
Set the position of a file descriptor. | |
void | set (fd_t descriptor) |
Set with external descriptor. | |
int | sync (void) |
Commit changes to the filesystem. | |
int | trunc (offset_t offset) |
Truncate file to specified length. | |
ssize_t | write (const void *buffer, size_t count) |
Write data to descriptor. | |
~fsys () | |
Close and release a file descriptor. | |
Static Public Member Functions | |
static fd_t | append (const char *path) |
Direct means to create or append a writable path and return descriptor. | |
static void | assign (fsys &object, fd_t descriptor) |
Assign a descriptor directly. | |
static int | copy (const char *source, const char *target, size_t size=1024) |
Copy a file. | |
static int | erase (const char *path) |
Erase (remove) a file only. | |
static int | exec (const char *path, char **argv, char **envp=NULL) |
Execute a process and get exit code. | |
static int | hardlink (const char *path, const char *target) |
Create a hard link. | |
static int | info (const char *path, fileinfo_t *buffer) |
Stat a file. | |
static int | inherit (fd_t &descriptor, bool enable) |
Changle inheritable handle. | |
static fd_t | input (const char *path) |
Direct means to open a read-only file path and return a descriptor. | |
static bool | is_char (struct stat *inode) |
static bool | is_dev (struct stat *inode) |
static bool | is_device (const char *path) |
Test if path is a device path. | |
static bool | is_dir (const char *path) |
Test if path is a directory. | |
static bool | is_dir (struct stat *inode) |
static bool | is_disk (struct stat *inode) |
static bool | is_executable (const char *path) |
Test if path is executable. | |
static bool | is_exists (const char *path) |
Test if path exists. | |
static bool | is_file (const char *path) |
Test if path is a file. | |
static bool | is_file (struct stat *inode) |
static bool | is_hidden (const char *path) |
Test if path is a hidden file. | |
static bool | is_link (const char *path) |
Test if path is a symlink. | |
static bool | is_link (struct stat *inode) |
static bool | is_readable (const char *path) |
Test if path readable. | |
static bool | is_sys (struct stat *inode) |
static bool | is_tty (fd_t fd) |
See if the file handle is a tty device. | |
static bool | is_writable (const char *path) |
Test if path writable. | |
static int | link (const char *path, const char *target) |
Create a symbolic link. | |
static int | linkinfo (const char *path, char *buffer, size_t size) |
Read a symbolic link to get it's target. | |
static int | load (const char *path) |
Load a library into memory. | |
static int | mode (const char *path, unsigned value) |
Change file access mode. | |
static fd_t | null (void) |
Create inheritable /dev/null handle. | |
static fd_t | output (const char *path) |
Direct means to create or access a writable path and return descriptor. | |
static int | pipe (fd_t &input, fd_t &output, size_t size=0) |
Create pipe. | |
static int | prefix (char *path, size_t size) |
Get current directory prefix (pwd). | |
static int | prefix (const char *path) |
Set directory prefix (chdir). | |
static stringref_t | prefix (void) |
static void | release (fd_t descriptor) |
Release a file descriptor. | |
static int | remapError (void) |
static int | rename (const char *oldpath, const char *newpath) |
Rename a file. | |
static int | unlink (const char *path) |
Remove a symbolic link explicitly. | |
Static Public Attributes | |
static const offset_t | end |
Used to mark "append" in set position operations. | |
Protected Attributes | |
int | error |
fd_t | fd |
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 long ucommon::fsys::offset_t |
anonymous enum |
ucommon::fsys::fsys | ( | const fsys & | descriptor | ) |
Copy (dup) an existing fsys descriptor.
descriptor | to copy from. |
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. |
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. |
|
static |
Direct means to create or append a writable path and return descriptor.
path | to create. |
|
inline |
|
inlinestatic |
int ucommon::fsys::close | ( | void | ) |
Close a fsys resource.
|
static |
Copy a file.
source | file. |
target | file. |
size | of buffer. |
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. |
|
static |
Erase (remove) a file only.
path | of file. |
|
inline |
|
static |
Execute a process and get exit code.
path | to execute. |
argv | list. |
optional | env. |
|
inline |
|
static |
Create a hard link.
path | to create link to. |
target | of 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. |
|
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. |
|
static |
Direct means to open a read-only file path and return a descriptor.
path | to open. |
|
inlinestatic |
|
inlinestatic |
|
static |
Test if path is a device path.
path | to test. |
|
static |
Test if path is a directory.
path | to test. |
|
inlinestatic |
|
inlinestatic |
|
static |
Test if path is executable.
path | to test. |
|
static |
Test if path exists.
path | to test. |
|
static |
Test if path is a file.
path | to test. |
|
inlinestatic |
|
static |
Test if path is a hidden file.
path | to test. |
|
static |
Test if path is a symlink.
path | to test. |
|
inlinestatic |
|
static |
Test if path readable.
path | to test. |
|
inlinestatic |
|
static |
See if the file handle is a tty device.
bool ucommon::fsys::is_tty | ( | void | ) | const |
See if current file stream is a tty device.
|
static |
Test if path writable.
path | to test. |
|
static |
Create a symbolic link.
path | to create. |
target | of link. |
|
static |
Read a symbolic link to get it's target.
path | of link. |
buffer | to save target into. |
size | of buffer. |
|
static |
Load a library into memory.
path | to plugin. |
|
static |
Change file access mode.
path | to change. |
value | of mode to assign. |
|
static |
Create inheritable /dev/null handle.
void ucommon::fsys::open | ( | const char * | path, |
access_t | access ) |
Open a file or directory.
path | of file to open. |
access | mode of descriptor. |
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. |
|
inline |
|
inline |
|
inline |
|
inline |
fsys & ucommon::fsys::operator*= | ( | fd_t & | descriptor | ) |
Replace current file descriptor with an external descriptor.
This does not create a duplicate. The external descriptor object is marked as invalid.
Assign file descriptor by duplicating another descriptor.
descriptor | to dup from. |
fsys & ucommon::fsys::operator= | ( | fd_t | descriptor | ) |
Assing file descriptor from system descriptor.
descriptor | to dup from. |
|
static |
Direct means to create or access a writable path and return descriptor.
path | to create. |
|
static |
Create pipe.
These are created inheritable by default.
input | descriptor. |
output | descriptor. |
size | of buffer if supported. |
|
static |
Get current directory prefix (pwd).
path | to save directory into. |
size | of path we can save. |
|
static |
Set directory prefix (chdir).
path | to change to. |
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. |
|
static |
Release a file descriptor.
descriptor | to release. |
fd_t ucommon::fsys::release | ( | void | ) |
Release descriptor, do not close.
|
static |
Rename a file.
oldpath | to rename from. |
newpath | to rename to. |
int ucommon::fsys::seek | ( | offset_t | offset | ) |
Set the position of a file descriptor.
offset | from start of file or "end" to append. |
void ucommon::fsys::set | ( | fd_t | descriptor | ) |
Set with external descriptor.
Closes existing file if open.
descriptor | of open file. |
int ucommon::fsys::sync | ( | void | ) |
Commit changes to the filesystem.
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. |
|
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. |
ssize_t ucommon::fsys::write | ( | const void * | buffer, |
size_t | count ) |
Write data to descriptor.
buffer | to write from. |
count | of bytes to write. |
|
static |