The FileUtils class provides static functions to easily access and handle files and the file system. More...
#include <FileUtils.h>
Static Public Member Functions | |
static bool | read (const String &fname, String &contents) |
Reads a whole file into a String. More... | |
static ssize_t | read (int fd, void *vptr, size_t n) |
Reads data from a file descriptor into a buffer. More... | |
static ssize_t | pread (int fd, void *vptr, size_t n, off_t offset) |
Reads positional data from a file descriptor into a buffer. More... | |
static ssize_t | write (const String &fname, const std::string &contents) |
Writes a String buffer to a file; the file is overwritten if it already exists. More... | |
static ssize_t | write (int fd, const void *vptr, size_t n) |
Writes a memory buffer to a file descriptor. More... | |
static ssize_t | write (int fd, const std::string &str) |
Writes a string to a file descriptor. More... | |
static ssize_t | writev (int fd, const struct iovec *vector, int count) |
Atomically writes data from multiple buffers to a file descriptor. More... | |
static ssize_t | sendto (int fd, const void *vptr, size_t n, const sockaddr *to, socklen_t tolen) |
Sends data through a network connection; if the socket is TCP then the address is ignored. More... | |
static ssize_t | send (int fd, const void *vptr, size_t n) |
Sends data through a network connection. More... | |
static ssize_t | recvfrom (int fd, void *vptr, size_t n, struct sockaddr *from, socklen_t *fromlen) |
Receives data from a network connection and returns the sender's address. More... | |
static ssize_t | recv (int fd, void *vptr, size_t n) |
Receives data from a network connection. More... | |
static bool | set_flags (int fd, int flags) |
Sets fcntl flags of a socket. More... | |
static char * | file_to_buffer (const String &fname, off_t *lenp) |
Reads a full file into a new buffer; the buffer is allocated with operator new[], and the caller has to delete[] it. More... | |
static String | file_to_string (const String &fname) |
Reads a full file into a String. More... | |
static void * | mmap (const String &fname, off_t *lenp) |
Maps a full file into memory using mmap; the mapping will be released when the application terminates (there's currently no munmap) More... | |
static bool | mkdirs (const String &dirname) |
Creates a directory (with all parent directories, if required) More... | |
static bool | exists (const String &fname) |
Checks if a file or directory exists. More... | |
static bool | unlink (const String &fname) |
Unlinks (deletes) a file or directory. More... | |
static bool | rename (const String &oldpath, const String &newpath) |
Renames a file or directory. More... | |
static uint64_t | size (const String &fname) |
Returns the size of a file (0 on error) More... | |
static off_t | length (const String &fname) |
Returns the size of a file (-1 on error) More... | |
static void | add_trailing_slash (String &path) |
Adds a trailing slash to a path. More... | |
static bool | expand_tilde (String &fname) |
Expands a leading tilde character in a filename. More... | |
static void | readdir (const String &dirname, const String &fname_regex, std::vector< struct dirent > &listing) |
Reads all directory entries, applies a regular expression and returns those which match. More... | |
Static Public Attributes | |
static std::mutex | ms_mutex |
Mutex for protecting thread-unsafe glibc library function calls. More... | |
The FileUtils class provides static functions to easily access and handle files and the file system.
Definition at line 51 of file FileUtils.h.
|
static |
Adds a trailing slash to a path.
Definition at line 466 of file FileUtils.cc.
|
static |
Checks if a file or directory exists.
Definition at line 420 of file FileUtils.cc.
|
static |
Expands a leading tilde character in a filename.
Examples:
~chris/foo -> /home/chris/foo ~/foo -> /home/$USER/foo
Definition at line 472 of file FileUtils.cc.
|
static |
Reads a full file into a new buffer; the buffer is allocated with operator new[], and the caller has to delete[] it.
fname | The file name |
lenp | Receives the length of the buffer, in bytes |
Definition at line 282 of file FileUtils.cc.
Reads a full file into a String.
fname | The file name |
Definition at line 333 of file FileUtils.cc.
|
static |
Returns the size of a file (-1 on error)
fname | The path of the file |
Definition at line 458 of file FileUtils.cc.
|
static |
Creates a directory (with all parent directories, if required)
dirname | The directory name to create |
Definition at line 366 of file FileUtils.cc.
|
static |
Maps a full file into memory using mmap; the mapping will be released when the application terminates (there's currently no munmap)
fname | The file name |
lenp | Receives the length of the buffer, in bytes |
Definition at line 343 of file FileUtils.cc.
|
static |
Reads positional data from a file descriptor into a buffer.
fd | The open file descriptor |
vptr | Pointer to the memory buffer |
n | Maximum size to read, in bytes |
offset | The start offset in the file |
Definition at line 97 of file FileUtils.cc.
Reads a whole file into a String.
fname | The file name |
contents | A reference to a String which will receive the data |
Definition at line 59 of file FileUtils.cc.
|
static |
Reads data from a file descriptor into a buffer.
fd | The open file descriptor |
vptr | Pointer to the memory buffer |
n | Maximum size to read, in bytes |
Definition at line 71 of file FileUtils.cc.
|
static |
Reads all directory entries, applies a regular expression and returns those which match.
This function will call HT_FATAL on error!
dirname | The directory name |
fname_regex | The regular expression; can be empty |
listing | Vector with the results |
Definition at line 511 of file FileUtils.cc.
|
static |
Receives data from a network connection.
fd | Open file handle/socket descriptor |
vptr | Pointer to the memory buffer which receives the data |
n | Capacity of the memory buffer, in bytes |
Definition at line 242 of file FileUtils.cc.
|
static |
Receives data from a network connection and returns the sender's address.
fd | Open file handle/socket descriptor |
vptr | Pointer to the memory buffer which receives the data |
n | Capacity of the memory buffer, in bytes |
from | The sender's address |
fromlen | Length of the sockaddr structure |
Definition at line 227 of file FileUtils.cc.
Renames a file or directory.
oldpath | The path of the file (or directory) to rename |
newpath | The new filename |
Definition at line 438 of file FileUtils.cc.
|
static |
Sends data through a network connection.
fd | Open file handle/socket descriptor |
vptr | Pointer to the memory buffer which is sent |
n | Size of the memory buffer, in bytes |
Definition at line 203 of file FileUtils.cc.
|
static |
Sends data through a network connection; if the socket is TCP then the address is ignored.
For UDP sockets the address structure specifies the recipient.
fd | Open file handle/socket descriptor |
vptr | Pointer to the memory buffer which is sent |
n | Size of the memory buffer, in bytes |
to | The recipient's address (only UDP; ignored for TCP sockets) |
tolen | Length of the sockaddr structure |
Definition at line 178 of file FileUtils.cc.
|
static |
Sets fcntl flags of a socket.
fd | Open file handle/socket descriptor |
flags | The fcntl flags; will be ORed with the existing flags |
Definition at line 256 of file FileUtils.cc.
|
static |
Returns the size of a file (0 on error)
fname | The path of the file |
Definition at line 450 of file FileUtils.cc.
|
static |
Unlinks (deletes) a file or directory.
Definition at line 427 of file FileUtils.cc.
|
static |
Writes a String buffer to a file; the file is overwritten if it already exists.
fname | Path of the file that is (over)written |
contents | The string contents that are written to the file |
Definition at line 124 of file FileUtils.cc.
|
static |
Writes a memory buffer to a file descriptor.
fd | Open file handle |
vptr | Pointer to the memory buffer |
n | Size of the memory buffer, in bytes |
Definition at line 139 of file FileUtils.cc.
|
inlinestatic |
Writes a string to a file descriptor.
fd | Open file handle |
str | String to write to file |
Definition at line 104 of file FileUtils.h.
|
static |
Atomically writes data from multiple buffers to a file descriptor.
struct iovec { void *iov_base; // Starting address size_t iov_len; // Number of bytes to transfer };
fd | Open file handle |
vector | An iovec array holding pointers to the data |
count | Number of iovec structures in vector |
Definition at line 162 of file FileUtils.cc.
|
static |
Mutex for protecting thread-unsafe glibc library function calls.
Definition at line 269 of file FileUtils.h.