27 #ifndef Common_FileUtils_h
28 #define Common_FileUtils_h
37 #include <sys/socket.h>
38 #include <sys/types.h>
68 static ssize_t
read(
int fd,
void *vptr,
size_t n);
78 static ssize_t
pread(
int fd,
void *vptr,
size_t n, off_t offset);
87 static ssize_t
write(
const String &fname,
const std::string &contents);
96 static ssize_t
write(
int fd,
const void *vptr,
size_t n);
104 static ssize_t
write(
int fd,
const std::string &
str) {
105 return write(fd, str.c_str(), str.length());
120 static ssize_t
writev(
int fd,
const struct iovec *vector,
int count);
133 static ssize_t
sendto(
int fd,
const void *vptr,
size_t n,
134 const sockaddr *to, socklen_t tolen);
143 static ssize_t
send(
int fd,
const void *vptr,
size_t n);
155 static ssize_t
recvfrom(
int fd,
void *vptr,
size_t n,
156 struct sockaddr *from, socklen_t *fromlen);
165 static ssize_t
recv(
int fd,
void *vptr,
size_t n);
173 static bool set_flags(
int fd,
int flags);
199 static void *
mmap(
const String &fname, off_t *lenp);
266 std::vector<struct dirent> &listing);
276 #endif // Common_FileUtils_h
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 ...
static std::mutex ms_mutex
Mutex for protecting thread-unsafe glibc library function calls.
static bool read(const String &fname, String &contents)
Reads a whole file into a String.
std::string String
A String is simply a typedef to std::string.
static bool unlink(const String &fname)
Unlinks (deletes) a file or directory.
static off_t length(const String &fname)
Returns the size of a file (-1 on error)
The FileUtils class provides static functions to easily access and handle files and the file system...
Po::typed_value< String > * str(String *v=0)
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.
static void add_trailing_slash(String &path)
Adds a trailing slash to a path.
static bool exists(const String &fname)
Checks if a file or directory exists.
static bool mkdirs(const String &dirname)
Creates a directory (with all parent directories, if required)
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.
static ssize_t pread(int fd, void *vptr, size_t n, off_t offset)
Reads positional data from a file descriptor into a buffer.
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...
static ssize_t send(int fd, const void *vptr, size_t n)
Sends data through a network connection.
static uint64_t size(const String &fname)
Returns the size of a file (0 on error)
static ssize_t write(int fd, const std::string &str)
Writes a string to a file descriptor.
static bool expand_tilde(String &fname)
Expands a leading tilde character in a filename.
static bool set_flags(int fd, int flags)
Sets fcntl flags of a socket.
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...
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...
static ssize_t writev(int fd, const struct iovec *vector, int count)
Atomically writes data from multiple buffers to a file descriptor.
A String class based on std::string.
static bool rename(const String &oldpath, const String &newpath)
Renames a file or directory.
static ssize_t recv(int fd, void *vptr, size_t n)
Receives data from a network connection.
static String file_to_string(const String &fname)
Reads a full file into a String.