0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Static Public Member Functions | Static Public Attributes | List of all members
Hypertable::FileUtils Class Reference

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...
 

Detailed Description

The FileUtils class provides static functions to easily access and handle files and the file system.

Definition at line 51 of file FileUtils.h.

Member Function Documentation

void FileUtils::add_trailing_slash ( String path)
static

Adds a trailing slash to a path.

Definition at line 466 of file FileUtils.cc.

bool FileUtils::exists ( const String fname)
static

Checks if a file or directory exists.

Returns
true if the file or directory exists, otherwise false

Definition at line 420 of file FileUtils.cc.

bool FileUtils::expand_tilde ( String fname)
static

Expands a leading tilde character in a filename.

Examples:

~chris/foo -> /home/chris/foo ~/foo -> /home/$USER/foo

Returns
true on success, false on error

Definition at line 472 of file FileUtils.cc.

char * FileUtils::file_to_buffer ( const String fname,
off_t *  lenp 
)
static

Reads a full file into a new buffer; the buffer is allocated with operator new[], and the caller has to delete[] it.

Parameters
fnameThe file name
lenpReceives the length of the buffer, in bytes
Returns
A pointer allocated with new[]; needs to be delete[]d by the caller. Returns 0 on error (sets errno)

Definition at line 282 of file FileUtils.cc.

String FileUtils::file_to_string ( const String fname)
static

Reads a full file into a String.

Parameters
fnameThe file name
Returns
A string with the data, or an empty string on error (sets errno)

Definition at line 333 of file FileUtils.cc.

off_t FileUtils::length ( const String fname)
static

Returns the size of a file (-1 on error)

Parameters
fnameThe path of the file
Returns
The file size (in bytes) or -1 on error (sets errno)

Definition at line 458 of file FileUtils.cc.

bool FileUtils::mkdirs ( const String dirname)
static

Creates a directory (with all parent directories, if required)

Parameters
dirnameThe directory name to create
Returns
true on success, otherwise falls (sets errno)

Definition at line 366 of file FileUtils.cc.

void * FileUtils::mmap ( const String fname,
off_t *  lenp 
)
static

Maps a full file into memory using mmap; the mapping will be released when the application terminates (there's currently no munmap)

Parameters
fnameThe file name
lenpReceives the length of the buffer, in bytes
Returns
A pointer to the mapped data

Definition at line 343 of file FileUtils.cc.

ssize_t FileUtils::pread ( int  fd,
void *  vptr,
size_t  n,
off_t  offset 
)
static

Reads positional data from a file descriptor into a buffer.

Parameters
fdThe open file descriptor
vptrPointer to the memory buffer
nMaximum size to read, in bytes
offsetThe start offset in the file
Returns
The number of bytes read, or -1 on error

Definition at line 97 of file FileUtils.cc.

bool FileUtils::read ( const String fname,
String contents 
)
static

Reads a whole file into a String.

Parameters
fnameThe file name
contentsA reference to a String which will receive the data
Returns
true on success, false on error

Definition at line 59 of file FileUtils.cc.

ssize_t FileUtils::read ( int  fd,
void *  vptr,
size_t  n 
)
static

Reads data from a file descriptor into a buffer.

Parameters
fdThe open file descriptor
vptrPointer to the memory buffer
nMaximum size to read, in bytes
Returns
The number of bytes read, or -1 on error

Definition at line 71 of file FileUtils.cc.

void FileUtils::readdir ( const String dirname,
const String fname_regex,
std::vector< struct dirent > &  listing 
)
static

Reads all directory entries, applies a regular expression and returns those which match.

This function will call HT_FATAL on error!

Parameters
dirnameThe directory name
fname_regexThe regular expression; can be empty
listingVector with the results

Definition at line 511 of file FileUtils.cc.

ssize_t FileUtils::recv ( int  fd,
void *  vptr,
size_t  n 
)
static

Receives data from a network connection.

Parameters
fdOpen file handle/socket descriptor
vptrPointer to the memory buffer which receives the data
nCapacity of the memory buffer, in bytes
Returns
Total number of bytes received, or -1 on error, 0 on eof

Definition at line 242 of file FileUtils.cc.

ssize_t FileUtils::recvfrom ( int  fd,
void *  vptr,
size_t  n,
struct sockaddr *  from,
socklen_t *  fromlen 
)
static

Receives data from a network connection and returns the sender's address.

Parameters
fdOpen file handle/socket descriptor
vptrPointer to the memory buffer which receives the data
nCapacity of the memory buffer, in bytes
fromThe sender's address
fromlenLength of the sockaddr structure
Returns
Total number of bytes received, or -1 on error, 0 on eof

Definition at line 227 of file FileUtils.cc.

bool FileUtils::rename ( const String oldpath,
const String newpath 
)
static

Renames a file or directory.

Parameters
oldpathThe path of the file (or directory) to rename
newpathThe new filename
Returns
true on success, otherwise false (sets errno)

Definition at line 438 of file FileUtils.cc.

ssize_t FileUtils::send ( int  fd,
const void *  vptr,
size_t  n 
)
static

Sends data through a network connection.

Parameters
fdOpen file handle/socket descriptor
vptrPointer to the memory buffer which is sent
nSize of the memory buffer, in bytes
Returns
Total number of bytes sent, or -1 on error

Definition at line 203 of file FileUtils.cc.

ssize_t FileUtils::sendto ( int  fd,
const void *  vptr,
size_t  n,
const sockaddr *  to,
socklen_t  tolen 
)
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.

Parameters
fdOpen file handle/socket descriptor
vptrPointer to the memory buffer which is sent
nSize of the memory buffer, in bytes
toThe recipient's address (only UDP; ignored for TCP sockets)
tolenLength of the sockaddr structure
Returns
Total number of bytes sent, or -1 on error

Definition at line 178 of file FileUtils.cc.

bool FileUtils::set_flags ( int  fd,
int  flags 
)
static

Sets fcntl flags of a socket.

Parameters
fdOpen file handle/socket descriptor
flagsThe fcntl flags; will be ORed with the existing flags
Returns
true on success, otherwise false (sets errno)

Definition at line 256 of file FileUtils.cc.

uint64_t FileUtils::size ( const String fname)
static

Returns the size of a file (0 on error)

Parameters
fnameThe path of the file
Returns
The file size (in bytes) or 0 on error (sets errno)

Definition at line 450 of file FileUtils.cc.

bool FileUtils::unlink ( const String fname)
static

Unlinks (deletes) a file or directory.

Returns
true on success, otherwise false (sets errno)

Definition at line 427 of file FileUtils.cc.

ssize_t FileUtils::write ( const String fname,
const std::string &  contents 
)
static

Writes a String buffer to a file; the file is overwritten if it already exists.

Parameters
fnamePath of the file that is (over)written
contentsThe string contents that are written to the file
Returns
Number of bytes written, or -1 on error

Definition at line 124 of file FileUtils.cc.

ssize_t FileUtils::write ( int  fd,
const void *  vptr,
size_t  n 
)
static

Writes a memory buffer to a file descriptor.

Parameters
fdOpen file handle
vptrPointer to the memory buffer
nSize of the memory buffer, in bytes
Returns
Number of bytes written, or -1 on error

Definition at line 139 of file FileUtils.cc.

static ssize_t Hypertable::FileUtils::write ( int  fd,
const std::string &  str 
)
inlinestatic

Writes a string to a file descriptor.

Parameters
fdOpen file handle
strString to write to file
Returns
Number of bytes written, or -1 on error

Definition at line 104 of file FileUtils.h.

ssize_t FileUtils::writev ( int  fd,
const struct iovec *  vector,
int  count 
)
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 };

Parameters
fdOpen file handle
vectorAn iovec array holding pointers to the data
countNumber of iovec structures in vector
Returns
Total number of bytes written, or -1 on error

Definition at line 162 of file FileUtils.cc.

Member Data Documentation

std::mutex FileUtils::ms_mutex
static

Mutex for protecting thread-unsafe glibc library function calls.

Definition at line 269 of file FileUtils.h.


The documentation for this class was generated from the following files: