wreport::sys::FileDescriptor Class Reference

Common operations on file descriptors. More...

`#include <sys.h>`

Inheritance diagram for wreport::sys::FileDescriptor:

## Public Member Functions

| FileDescriptor (FileDescriptor &&o)
| FileDescriptor (int fd)
| FileDescriptor (const FileDescriptor &o)=default
FileDescriptor & | operator= (const FileDescriptor &o)=default
virtual void | throw_error (const char *desc)
| Throw an exception based on errno and the given message.
virtual void | throw_runtime_error (const char *desc)
| Throw a runtime_error unrelated from errno.
bool | is_open () const
| Check if the file descriptor is open (that is, if it is not -1).
void | close ()
| Close the file descriptor, setting its value to -1.
void | fstat (struct stat &st)
void | fchmod (mode_t mode)
void | futimens (const struct ::timespec ts[2])
void | fsync ()
void | fdatasync ()
int | dup ()
size_t | read (void *buf, size_t count)
bool | read_all_or_retry (void *buf, size_t count)
| Read count bytes into bufr, retrying partial reads, stopping at EOF.
void | read_all_or_throw (void *buf, size_t count)
| Read all the data into buf, throwing runtime_error in case of a partial read.
size_t | write (const void *buf, size_t count)
template<typename Container>
size_t | write (const Container &c)
void | write_all_or_retry (const void *buf, size_t count)
| Write all the data in buf, retrying partial writes.
template<typename Container>
void | write_all_or_retry (const Container &c)
void | write_all_or_throw (const void *buf, size_t count)
| Write all the data in buf, throwing runtime_error in case of a partial write.
template<typename Container>
void | write_all_or_throw (const Container &c)
off_t | lseek (off_t offset, int whence=SEEK_SET)
size_t | pread (void *buf, size_t count, off_t offset)
size_t | pwrite (const void *buf, size_t count, off_t offset)
template<typename Container>
size_t | pwrite (const Container &c, off_t offset)
void | ftruncate (off_t length)
MMap | mmap (size_t length, int prot, int flags, off_t offset=0)
bool | ofd_setlk (struct ::flock &)
| Open file description locks F_OFD_SETLK operation.
bool | ofd_setlkw (struct ::flock &, bool retry_on_signal=true)
| Open file description locks F_OFD_SETLKW operation.
bool | ofd_getlk (struct ::flock &)
| Open file description locks F_OFD_GETLK operation.
int | getfl ()
| Get open flags for the file.
void | setfl (int flags)
| Set open flags for the file.
| operator int () const

## Protected Attributes

int | fd = -1

## Detailed Description

Common operations on file descriptors.

Except when documented otherwise, methods of this class are just thin wrappers around the libc functions with the same name, that check error results and throw exceptions if the functions failed.

Implementing what to do on construction and destruction is left to the subclassers: at the FileDescriptor level, the destructor does nothing and leaves the file descriptor open.

## Member Function Documentation

## close()

void wreport::sys::FileDescriptor::close | ( |  | ) |

Close the file descriptor, setting its value to -1.

Does nothing if the file descriptor is already closed

## ofd_getlk()

bool wreport::sys::FileDescriptor::ofd_getlk | ( | struct ::flock & |  | ) |

Open file description locks F_OFD_GETLK operation.

Returns true if the lock would have been obtainable, false if not.

## ofd_setlk()

bool wreport::sys::FileDescriptor::ofd_setlk | ( | struct ::flock & |  | ) |

Open file description locks F_OFD_SETLK operation.

Returns true if the lock was obtained, false if acquiring the lock failed.

## ofd_setlkw()

bool wreport::sys::FileDescriptor::ofd_setlkw | ( | struct ::flock & | ,
|  | bool | retry_on_signal = true )

Open file description locks F_OFD_SETLKW operation.

Returns true if the lock was obtained, false if a signal was received while waiting for the lock.

If retry_on_signal is true, acquiring the lock is automatically retried in case of signals, and the function always returns true.

## read_all_or_retry()

bool wreport::sys::FileDescriptor::read_all_or_retry | ( | void * | buf,
|  | size_t | count )

Read count bytes into bufr, retrying partial reads, stopping at EOF.

Return true if count bytes have been read, false in case of eof, and raise an exception in case EOF was found after reading between 0 and count-1 bytes.

## throw_error()

virtual void wreport::sys::FileDescriptor::throw_error | ( | const char * | desc | ) |

virtual

Throw an exception based on errno and the given message.

This can be overridden by subclasses that may have more information about the file descriptor, so that they can generate more descriptive messages.

Reimplemented in wreport::sys::NamedFileDescriptor.

## throw_runtime_error()

virtual void wreport::sys::FileDescriptor::throw_runtime_error | ( | const char * | desc | ) |

virtual

Throw a runtime_error unrelated from errno.

This can be overridden by subclasses that may have more information about the file descriptor, so that they can generate more descriptive messages.

Reimplemented in wreport::sys::NamedFileDescriptor.

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

- sys.h