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