ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
FileDescriptor Class Reference

Abstraction of a file descriptor. More...

#include <FileDescriptor.hpp>

Inheritance diagram for FileDescriptor:
Inheritance graph

Public Member Functions

 FileDescriptor (const std::string &name, const int flags)
 Constructor for descriptors of files.
 FileDescriptor (const std::string &name, const int flags, const mode_t mode)
 Constructor for descriptors of files.
int getLength ()
 Method to get the length of current available data.
int lseek (int offset)
 Method to reposition at a certain offset.
void sync ()
 Sync the descriptor.
- Public Member Functions inherited from PosixDescriptor
virtual ~PosixDescriptor ()
 Destructor.
void async_read (void(*handler)(Buffer *b, size_t size), Buffer *b, size_t size)
 Run asynchronous read operation.
void async_read (void(*handler)(void *b, size_t size), void *b, size_t size)
 Run asynchronous read operation.
void async_write (void(*handler)(Buffer *b, size_t size), Buffer *b, size_t size)
 Run asynchronous write operation.
void async_write (void(*handler)(void *b, size_t size), void *b, size_t size)
 Run asynchronous write operation.
int read (Buffer *b, size_t size)
 Method to read from the descriptor and fill a buffer.
int read (void *p, size_t size)
 Method to read from the descriptor.
int write (Buffer *b, size_t size)
 Method to write data in a buffer to the descriptor.
int write (const void *p, size_t size)
 Method to write to the descriptor.
int write (const std::string &s)
 Method to write a string to the descriptor.
virtual void close ()
 Method to close the descriptor.
int getDescriptorNumber () const
 Method to get descriptor number.
 PosixDescriptor (const PosixDescriptor &src)
 Copy constructor.
PosixDescriptoroperator= (const PosixDescriptor &src)
 Assignment operator.
bool flush ()
 Method to flush this specific descriptor.
int ioctl (int request)
 Ioctl on the file descriptor.
int ioctl (int request, void *argp)
 Ioctl on the file descriptor.

Additional Inherited Members

- Protected Member Functions inherited from PosixDescriptor
int do_read (void *p, size_t size)
 Low-level read.
int do_write (const void *p, size_t size)
 Low-level write.
 PosixDescriptor ()
 Constructor.
- Protected Attributes inherited from PosixDescriptor
int fd_
 Number of the file descriptor.

Detailed Description

Abstraction of a file descriptor.

This is an abstract class for the concept of file descriptor. The descriptor corresponds to an open file.

Example of usage:

FileDescriptor fd ("/tmp/myfile", O_RDONLY);
Buffer b (10);
fd.read (b, b.getSize());

Definition at line 41 of file FileDescriptor.hpp.

Constructor & Destructor Documentation

FileDescriptor ( const std::string &  name,
const int  flags 
)

Constructor for descriptors of files.

It calls open() with the given flags.

Parameters
nameof the file
flagsthat can be
  • O_RDONLY
  • O_WRONLY
  • or O_RDWR
or-ed with zero or more flags:
  • O_APPEND
  • O_ASYNC
  • O_CREAT
  • O_NONBLOCK
  • O_SYNC
Exceptions
runtime_errorif the ::open() returns an error

Definition at line 48 of file FileDescriptor.cpp.

{
fd_ = open(name.c_str(), flags);
if (fd_ <= 0) {
ERROR("Opening file " << name);
throw std::runtime_error ("Open file error");
}
}
FileDescriptor ( const std::string &  name,
const int  flags,
const mode_t  mode 
)

Constructor for descriptors of files.

It calls open() with the given flags.

Parameters
nameof the file
flagsthat can be
  • O_RDONLY
  • O_WRONLY
  • or O_RDWR
or-ed with zero or more flags:
  • O_APPEND
  • O_ASYNC
  • O_CREAT
  • O_NONBLOCK
  • O_SYNC
modecan be
  • S_IRWXU 00700 user (file owner) has read, write and execute permission
  • S_IRUSR 00400 user has read permission
  • S_IWUSR 00200 user has write permission
  • S_IXUSR 00100 user has execute permission
  • S_IRWXG 00070 group has read, write and execute permission
  • S_IRGRP 00040 group has read permission
  • S_IWGRP 00020 group has write permission
  • S_IXGRP 00010 group has execute permission
  • S_IRWXO 00007 others have read, write and execute permission
  • S_IROTH 00004 others have read permission
  • S_IWOTH 00002 others have write permission
  • S_IXOTH 00001 others have execute permission
Exceptions
runtime_errorif the ::open() returns an error

Definition at line 94 of file FileDescriptor.cpp.

{
fd_ = open(name.c_str(), flags, mode);
if (fd_ <= 0) {
ERROR("Opening file " << name);
throw std::runtime_error ("Open file error");
}
}

Member Function Documentation

int getLength ( )

Method to get the length of current available data.

Returns
length of data available on the file

Definition at line 111 of file FileDescriptor.cpp.

{
int currentPosition = ::lseek(fd_, 0, SEEK_CUR);
int end = ::lseek(fd_, 0, SEEK_END);
::lseek(fd_, currentPosition, SEEK_SET);
return end;
}

Here is the call graph for this function:

int lseek ( int  offset)
inline

Method to reposition at a certain offset.

Parameters
offsetOffset from the beginning of file
Returns
the actual offset location in case of success; -1 in case of error

Definition at line 56 of file FileDescriptor.hpp.

{
return ::lseek(fd_, offset, SEEK_SET);
}

Here is the caller graph for this function:

void sync ( )
inline

Sync the descriptor.

Method to call fsync on the descriptor. Usually after a write operation.

Definition at line 67 of file FileDescriptor.hpp.

{
::fsync(fd_);
}

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