include/File.h
author Jens Alfke <jens@mooseyard.com>
Tue Sep 29 15:46:42 2009 -0700 (2009-09-29)
changeset 9 629f61203db1
parent 8 21a6c17f4e3e
permissions -rw-r--r--
* Merged in jbm's changes for Linux compatibility, fixing Mac compatibility in the process :)
* Fixed two regressions having to do with the _previousTrailerPosition in VersionDictionary.cpp.
* Made sure RTTI is enabled in the OttomanTest target because gtest requires it.
     1 /*
     2  *  File.h
     3  *  Ottoman
     4  *
     5  *  Created by Jens Alfke on 8/20/09.
     6  *  Copyright 2009 Jens Alfke. All rights reserved.
     7  *  BSD-Licensed: See the file "LICENSE.txt" for details.
     8  */
     9 
    10 #ifndef _MOOSEYARD_FILE_
    11 #define _MOOSEYARD_FILE_
    12 #include "Base.h"
    13 
    14 namespace Mooseyard {
    15 
    16 struct Blob;
    17 class MemoryMap;
    18 
    19 /** An open file on disk; a thin wrapper around a Unix file descriptor. */
    20 class File {
    21 public:
    22     
    23     struct Error {
    24         const int code;
    25         const char* const message;
    26         
    27         Error(int c, const char *m)                                 :code(c), message(m) { }
    28         Error(const char *m);
    29     };
    30     static const int kEOF = 10001;
    31     
    32     
    33     File (const char *filename, int oflag, bool locked =false)  throw(Error);
    34     ~File();
    35     
    36     off_t length() const  throw(Error);
    37     void setLength (off_t)  throw(Error);
    38 
    39     off_t position() const  throw(Error);
    40     void setPosition (off_t)  throw(Error);
    41     off_t setPositionToEnd (off_t bytesBefore =0)  throw(Error);
    42     
    43     void read (void *dst, size_t)  throw(Error);
    44     size_t write (const void *src, size_t)  throw(Error);
    45 
    46     void readFrom (off_t where, void *dst, size_t)  throw(Error);
    47     void writeTo (off_t where, const void *src, size_t)  throw(Error);
    48     
    49     size_t writeMultiple (Blob blobs[], int count)  throw(Error);
    50     size_t writePadding (int alignment);  // alignment should be a power of 2
    51     
    52     MemoryMap* map();
    53     const MemoryMap* map() const                            {return const_cast<File*>(this)->map();}
    54     void mapRegion (off_t position, size_t length);
    55     const void* mappedPosition (off_t position) const;
    56     
    57     void flush()  throw(Error);             // Regular fsync call
    58     void flushDisk()  throw(Error);         // Expensive F_FULLFSYNC call
    59     
    60     template <typename T>
    61     void read (T& t)  throw(Error)                              {read(&t,sizeof(t));}
    62     template <typename T>
    63     size_t write (const T& t)  throw(Error)                     {return write(&t,sizeof(t));}
    64     template <typename T>
    65     void readFrom (off_t where, T &t)  throw(Error)             {readFrom(where,&t,sizeof(t));}
    66     template <typename T>
    67     void writeTo (off_t where, const T &t)  throw(Error)        {writeTo(where,&t,sizeof(t));}
    68     
    69     bool hasPath (const char *path) const  throw(Error);
    70 
    71     static void unlink (const char *filename)  throw(Error);
    72     static void rename (const char *srcFilename, const char *dstFilename)  throw(Error);
    73     
    74     
    75     class Lock {
    76     public:
    77         Lock (File*, bool block =true)  throw(Error);
    78         ~Lock();
    79         bool isLocked() const                   {return _locked;}
    80         operator bool() const                   {return _locked;}
    81         bool retry();
    82     private:
    83         UNCOPYABLE(Lock);
    84         File *_file;
    85         int _mode;
    86         bool _locked;
    87     };
    88     
    89 private:
    90     static int _open (const char *filename, int posixMode)  throw(Error);
    91     static int _check (int result)  throw(Error);
    92     static void _checkRead (ssize_t result, size_t expectedSize)  throw(Error);
    93     bool _lock (bool block);
    94     void _unlock();
    95     UNCOPYABLE(File);
    96     
    97     const int _fd;
    98     mutable MemoryMap *_memoryMap;
    99     int _locked;
   100     friend class Lock;
   101     friend class MemoryMap;
   102 };
   103 
   104 }
   105 
   106 #endif /* _MOOSEYARD_FILE_ */