include/File.h
author Jens Alfke <jens@mooseyard.com>
Sun Sep 20 15:14:12 2009 -0700 (2009-09-20)
changeset 0 31a43d94cc26
child 2 851de24ecb61
permissions -rw-r--r--
First official checkin.
     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, bool writeable =false, bool create =false)  throw(Error);
    34     File (const char *filename, int oflag)  throw(Error);
    35     ~File();
    36     
    37     off_t length() const  throw(Error);
    38     void setLength (off_t)  throw(Error);
    39 
    40     off_t position() const  throw(Error);
    41     void setPosition (off_t)  throw(Error);
    42     off_t setPositionToEnd (off_t bytesBefore =0)  throw(Error);
    43     
    44     void read (void *dst, size_t)  throw(Error);
    45     size_t write (const void *src, size_t)  throw(Error);
    46 
    47     void readFrom (off_t where, void *dst, size_t)  throw(Error);
    48     void writeTo (off_t where, const void *src, size_t)  throw(Error);
    49     
    50     size_t writeMultiple (Blob blobs[], int count)  throw(Error);
    51     size_t writePadding (int alignment);  // alignment should be a power of 2
    52     
    53     MemoryMap* map();
    54     const MemoryMap* map() const                            {return const_cast<File*>(this)->map();}
    55     void mapRegion (off_t position, size_t length);
    56     const void* mappedPosition (off_t position) const;
    57     
    58     void flush()  throw(Error);             // Regular fsync call
    59     void flushDisk()  throw(Error);         // Expensive F_FULLFSYNC call
    60     
    61     template <typename T>
    62     void read (T& t)  throw(Error)                              {read(&t,sizeof(t));}
    63     template <typename T>
    64     size_t write (const T& t)  throw(Error)                     {return write(&t,sizeof(t));}
    65     template <typename T>
    66     void readFrom (off_t where, T &t)  throw(Error)             {readFrom(where,&t,sizeof(t));}
    67     template <typename T>
    68     void writeTo (off_t where, const T &t)  throw(Error)        {writeTo(where,&t,sizeof(t));}
    69     
    70     bool hasPath (const char *path) const  throw(Error);
    71 
    72     static void unlink (const char *filename)  throw(Error);
    73     static void rename (const char *srcFilename, const char *dstFilename)  throw(Error);
    74     
    75     
    76     class Lock {
    77     public:
    78         Lock (File*, bool block =true)  throw(Error);
    79         ~Lock();
    80         bool isLocked() const                   {return _locked;}
    81         operator bool() const                   {return _locked;}
    82         bool retry();
    83     private:
    84         UNCOPYABLE(Lock);
    85         File *_file;
    86         int _mode;
    87         bool _locked;
    88     };
    89     
    90 private:
    91     static int _open (const char *filename, int posixMode)  throw(Error);
    92     static int _check (int result)  throw(Error);
    93     static void _checkRead (ssize_t result, size_t expectedSize)  throw(Error);
    94     bool _lock (bool block);
    95     void _unlock();
    96     UNCOPYABLE(File);
    97     
    98     const int _fd;
    99     mutable MemoryMap *_memoryMap;
   100     int _locked;
   101     friend class Lock;
   102     friend class MemoryMap;
   103 };
   104 
   105 }
   106 
   107 #endif _MOOSEYARD_FILE_