The template represents a single file on mumufs. More...
#include <mumulib.h>
Public Member Functions | |
| Entry (const std::string &path, RWMode openMode=ReadWrite, bool isBlocking=true) | |
| Constructs a single entry. Creates the corresponding file if it does not exist. | |
| ~Entry () | |
| Closes the file. | |
| Entry (const Entry &rhs) | |
| Constructs a copy of entry. | |
| bool | Select (struct timeval *inTimeout) |
| select(...) for the file on mumufs | |
| void | Write (const unsigned char *Buffer, size_t BufferSize) |
| Write a raw buffer to the entry. | |
| void | Write (const Type &T) |
| Write the given object to the entry. Be carefull, this is the generic implementation which supposes that the data occupies a consequent memory address space. | |
| int | Read (unsigned char *Buffer, size_t BufferSize) |
| Reads raw data into the given buffer. | |
| Type | Read (void) |
| reading of entry. It suits for blocking mode. | |
| int | Read (Type &T) |
| reading of entry. It suits for non-blocking mode. | |
| template<> | |
| void | Write (const std::string &T) |
| template<> | |
| int | Read (std::string &S) |
| template<> | |
| std::string | Read (void) |
Protected Member Functions | |
| struct timeval | AddTimeval (const struct timeval &Left, const struct timeval &Right) const |
| Utility function to add two timeval values. | |
| struct timeval | SubTimeval (const struct timeval &Left, const struct timeval &Right) const |
| Utility function to subtract two timeval values. | |
| bool | LessTimeval (const struct timeval &Left, const struct timeval &Right) const |
| Utility function to compare two timeval values. | |
| off_t | GetFileSize (void) const |
| Retrieves file size in bytes. | |
The template represents a single file on mumufs.
Definition at line 49 of file mumulib.h.
| mumu::Entry< Type >::Entry | ( | const std::string & | path, | |
| RWMode | openMode = ReadWrite, |
|||
| bool | isBlocking = true | |||
| ) | [inline, explicit] |
Constructs a single entry. Creates the corresponding file if it does not exist.
| path | Path to a file on mumufs. | |
| openMode | How to open a file. Default is ReadWrite. | |
| isBlocking | file opening mode. Default is blocking. |
| std::runtime_error | in case of problems |
The blocking mode matters for read(...) calls only
Definition at line 66 of file mumulib.h.
00068 : fd( -1 ), mode( openMode ) 00069 { 00070 int flags( openMode | O_CREAT ); 00071 if ( !isBlocking ) flags |= O_NONBLOCK; 00072 00073 fd = open( path.c_str(), flags, S_IRUSR | S_IWUSR | 00074 S_IRGRP | S_IWGRP | 00075 S_IROTH | S_IWOTH ); 00076 if ( fd < 0 ) throw std::runtime_error( "Cannot open the given entry." ); 00077 }
| mumu::Entry< Type >::Entry | ( | const Entry< Type > & | rhs | ) | [inline] |
| struct timeval mumu::Entry< Type >::AddTimeval | ( | const struct timeval & | Left, | |
| const struct timeval & | Right | |||
| ) | const [inline, read, protected] |
Utility function to add two timeval values.
| Left | First timeval | |
| Right | Second timeval |
Definition at line 109 of file mumulib.h.
Referenced by mumu::Entry< Type >::Select().
| off_t mumu::Entry< Type >::GetFileSize | ( | void | ) | const [inline, protected] |
Retrieves file size in bytes.
| Throws | std::runtime_error in case of fstat() call errors |
Definition at line 165 of file mumulib.h.
Referenced by mumu::Entry< Type >::Read().
| bool mumu::Entry< Type >::LessTimeval | ( | const struct timeval & | Left, | |
| const struct timeval & | Right | |||
| ) | const [inline, protected] |
Utility function to compare two timeval values.
| Left | First timeval | |
| Right | Second timeval |
Definition at line 151 of file mumulib.h.
Referenced by mumu::Entry< Type >::Select().
| std::string mumu::Entry< std::string >::Read | ( | void | ) | [inline] |
Definition at line 400 of file mumulib.h.
References mumu::Entry< Type >::Read().
00401 { 00402 std::string S; 00403 Read( S ); 00404 return S; 00405 }
| int mumu::Entry< std::string >::Read | ( | std::string & | S | ) | [inline] |
| S | A reference to the object to be filled |
| std::runtime_error | in case of errors |
Definition at line 359 of file mumulib.h.
References mumu::Entry< Type >::GetFileSize().
00360 { 00361 if ( mode == WriteOnly ) throw std::runtime_error( "Cannot read from WriteOnly entry" ); 00362 00363 off_t BufferSize( GetFileSize() + 1 ); 00364 char * Buffer( new char[ BufferSize ] ); 00365 00366 for ( ; ; ) 00367 { 00368 int Bytes( read( fd, Buffer, BufferSize ) ); 00369 00370 if ( Bytes > 0 && Bytes < static_cast<int>( BufferSize ) ) 00371 { 00372 if ( Buffer[ Bytes - 1 ] == '\0' ) S = std::string( Buffer, Bytes - 1 ); 00373 else S = std::string( Buffer, Bytes ); 00374 delete [] Buffer; 00375 return Bytes; 00376 } 00377 00378 if ( Bytes == -1 && errno == EAGAIN ) { delete [] Buffer; return 0; } // Blocking IO 00379 if ( Bytes == -1 && errno == EINTR ) continue; 00380 if ( Bytes == -1 && errno == EINVAL ) 00381 { 00382 delete [] Buffer; 00383 BufferSize = GetFileSize() + 1; 00384 Buffer = new char[ BufferSize ]; 00385 continue; 00386 } 00387 00388 delete [] Buffer; 00389 throw std::runtime_error( "Error reading from file descriptor" ); 00390 } 00391 }
| int mumu::Entry< Type >::Read | ( | Type & | T | ) | [inline] |
reading of entry. It suits for non-blocking mode.
| T | A reference to the object to be filled |
| std::runtime_error | in case of errors |
Definition at line 313 of file mumulib.h.
00314 { 00315 if ( mode == WriteOnly ) throw std::runtime_error( "Cannot read from WriteOnly entry" ); 00316 for ( ; ; ) 00317 { 00318 int Bytes( read( fd, &T, sizeof( T ) ) ); 00319 if ( Bytes == sizeof( T ) ) return Bytes; 00320 00321 if ( Bytes == -1 && errno == EINTR ) continue; 00322 if ( Bytes == -1 && errno == EAGAIN ) return 0; 00323 00324 throw std::runtime_error( "Error reading from file descriptor" ); 00325 } 00326 }
| Type mumu::Entry< Type >::Read | ( | void | ) | [inline] |
reading of entry. It suits for blocking mode.
Definition at line 300 of file mumulib.h.
Referenced by mumu::Entry< Type >::Read().
00301 { 00302 Type T; 00303 Read( T ); 00304 return T; 00305 }
| int mumu::Entry< Type >::Read | ( | unsigned char * | Buffer, | |
| size_t | BufferSize | |||
| ) | [inline] |
Reads raw data into the given buffer.
| Buffer | Pointer to the buffer | |
| BufferSize | Size of the provided buffer |
| std::runtime_error | in case of errors |
Definition at line 280 of file mumulib.h.
00281 { 00282 if ( mode == WriteOnly ) throw std::runtime_error( "Cannot read from WriteOnly entry" ); 00283 00284 for ( ; ; ) 00285 { 00286 int Bytes( read( fd, Buffer, BufferSize ) ); 00287 if ( Bytes > 0 ) return Bytes; 00288 if ( Bytes == -1 && errno == EINTR ) continue; 00289 if ( Bytes == -1 && errno == EAGAIN ) return 0; 00290 if ( Bytes == -1 && errno == EINVAL ) return -1; 00291 00292 throw std::runtime_error( "Error reading from file descriptor." ); 00293 } 00294 }
| bool mumu::Entry< Type >::Select | ( | struct timeval * | inTimeout | ) | [inline] |
select(...) for the file on mumufs
| inTimeout | select timeout. It could be NULL. If NULL then waits forever. |
| if | the open mode is WriteOnly |
Definition at line 184 of file mumulib.h.
References mumu::Entry< Type >::AddTimeval(), mumu::Entry< Type >::LessTimeval(), and mumu::Entry< Type >::SubTimeval().
00185 { 00186 if ( mode == WriteOnly ) throw std::runtime_error( "Select for WriteOnly mode is not supported" ); 00187 00188 fd_set Set; 00189 struct timeval AbsoluteEnd; 00190 struct timeval Timeout; 00191 struct timeval * Argument( NULL ); 00192 00193 if ( inTimeout != NULL ) 00194 { 00195 Timeout = *inTimeout; 00196 Argument = &Timeout; 00197 gettimeofday( &AbsoluteEnd, NULL ); 00198 AbsoluteEnd = AddTimeval( AbsoluteEnd, Timeout ); 00199 } 00200 00201 00202 FD_ZERO( &Set ); 00203 FD_SET( fd, &Set ); 00204 00205 int RetVal; 00206 00207 for ( ; ; ) 00208 { 00209 RetVal = select( fd + 1, &Set, NULL, NULL, Argument ); 00210 00211 // We have exactly 1 fd so this check is enough for this specific case 00212 if ( RetVal >= 1 ) return true; 00213 if ( RetVal == 0 ) return false; 00214 00215 // The last case is RetVal < 0 => error 00216 if ( errno == EINTR ) 00217 { 00218 // It is the timeout exceeded, we need to call select again 00219 // We can reach this point only if we had !NULL timeout 00220 FD_ZERO( &Set ); 00221 FD_SET( fd, &Set ); 00222 00223 struct timeval Current; 00224 gettimeofday( &Current, NULL ); 00225 00226 if ( LessTimeval( AbsoluteEnd, Current ) ) return false; 00227 00228 Timeout = SubTimeval( AbsoluteEnd, Current ); 00229 continue; 00230 } 00231 00232 throw std::runtime_error( "Select call error." ); 00233 } 00234 }
| struct timeval mumu::Entry< Type >::SubTimeval | ( | const struct timeval & | Left, | |
| const struct timeval & | Right | |||
| ) | const [inline, read, protected] |
Utility function to subtract two timeval values.
| Left | First timeval | |
| Right | Second timeval |
Definition at line 130 of file mumulib.h.
Referenced by mumu::Entry< Type >::Select().
| void mumu::Entry< std::string >::Write | ( | const std::string & | T | ) | [inline] |
| T | Object to be written |
| std::runtime_error | in case of problems |
Definition at line 336 of file mumulib.h.
00337 { 00338 if ( mode == ReadOnly ) throw std::runtime_error( "Cannot write into ReadOnly entry" ); 00339 00340 size_t size( T.size() + 1 ); // +1 is for trailing \0 00341 for ( ; ; ) 00342 { 00343 int Bytes( write( fd, T.c_str(), size ) ); 00344 if ( Bytes == static_cast< int >( size ) ) return; 00345 00346 if ( Bytes == -1 && errno == EINTR ) continue; 00347 00348 throw std::runtime_error( "Error writing to file descriptor" ); 00349 } 00350 }
| void mumu::Entry< Type >::Write | ( | const Type & | T | ) | [inline] |
Write the given object to the entry. Be carefull, this is the generic implementation which supposes that the data occupies a consequent memory address space.
| T | Object to be written |
| std::runtime_error | in case of problems |
Definition at line 265 of file mumulib.h.
References mumu::Entry< Type >::Write().
00266 { 00267 this->Write( reinterpret_cast<const unsigned char *>(&T), sizeof( T ) ); 00268 }
| void mumu::Entry< Type >::Write | ( | const unsigned char * | Buffer, | |
| size_t | BufferSize | |||
| ) | [inline] |
Write a raw buffer to the entry.
| Buffer | Pointer to the buffer | |
| BufferSize | Size of the buffer |
| std::runtime_error | in case of problems |
Definition at line 242 of file mumulib.h.
Referenced by mumu::Entry< Type >::Write().
00243 { 00244 if ( mode == ReadOnly ) throw std::runtime_error( "Cannot write into ReadOnly entry" ); 00245 00246 for ( ; ; ) 00247 { 00248 int Bytes( write( fd, Buffer, BufferSize ) ); 00249 if ( Bytes == static_cast<int>(BufferSize) ) return; 00250 00251 if ( Bytes == -1 && errno == EINTR ) continue; 00252 00253 throw std::runtime_error( "Error writing to file descriptor." ); 00254 } 00255 }