include/File.h
changeset 0 31a43d94cc26
child 2 851de24ecb61
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/include/File.h	Sun Sep 20 15:14:12 2009 -0700
     1.3 @@ -0,0 +1,107 @@
     1.4 +/*
     1.5 + *  File.h
     1.6 + *  Ottoman
     1.7 + *
     1.8 + *  Created by Jens Alfke on 8/20/09.
     1.9 + *  Copyright 2009 Jens Alfke. All rights reserved.
    1.10 + *  BSD-Licensed: See the file "LICENSE.txt" for details.
    1.11 + */
    1.12 +
    1.13 +#ifndef _MOOSEYARD_FILE_
    1.14 +#define _MOOSEYARD_FILE_
    1.15 +#include "Base.h"
    1.16 +
    1.17 +namespace Mooseyard {
    1.18 +
    1.19 +struct Blob;
    1.20 +class MemoryMap;
    1.21 +
    1.22 +/** An open file on disk; a thin wrapper around a Unix file descriptor. */
    1.23 +class File {
    1.24 +public:
    1.25 +    
    1.26 +    struct Error {
    1.27 +        const int code;
    1.28 +        const char* const message;
    1.29 +        
    1.30 +        Error(int c, const char *m)                                 :code(c), message(m) { }
    1.31 +        Error(const char *m);
    1.32 +    };
    1.33 +    static const int kEOF = 10001;
    1.34 +    
    1.35 +    
    1.36 +    File (const char *filename, bool writeable =false, bool create =false)  throw(Error);
    1.37 +    File (const char *filename, int oflag)  throw(Error);
    1.38 +    ~File();
    1.39 +    
    1.40 +    off_t length() const  throw(Error);
    1.41 +    void setLength (off_t)  throw(Error);
    1.42 +
    1.43 +    off_t position() const  throw(Error);
    1.44 +    void setPosition (off_t)  throw(Error);
    1.45 +    off_t setPositionToEnd (off_t bytesBefore =0)  throw(Error);
    1.46 +    
    1.47 +    void read (void *dst, size_t)  throw(Error);
    1.48 +    size_t write (const void *src, size_t)  throw(Error);
    1.49 +
    1.50 +    void readFrom (off_t where, void *dst, size_t)  throw(Error);
    1.51 +    void writeTo (off_t where, const void *src, size_t)  throw(Error);
    1.52 +    
    1.53 +    size_t writeMultiple (Blob blobs[], int count)  throw(Error);
    1.54 +    size_t writePadding (int alignment);  // alignment should be a power of 2
    1.55 +    
    1.56 +    MemoryMap* map();
    1.57 +    const MemoryMap* map() const                            {return const_cast<File*>(this)->map();}
    1.58 +    void mapRegion (off_t position, size_t length);
    1.59 +    const void* mappedPosition (off_t position) const;
    1.60 +    
    1.61 +    void flush()  throw(Error);             // Regular fsync call
    1.62 +    void flushDisk()  throw(Error);         // Expensive F_FULLFSYNC call
    1.63 +    
    1.64 +    template <typename T>
    1.65 +    void read (T& t)  throw(Error)                              {read(&t,sizeof(t));}
    1.66 +    template <typename T>
    1.67 +    size_t write (const T& t)  throw(Error)                     {return write(&t,sizeof(t));}
    1.68 +    template <typename T>
    1.69 +    void readFrom (off_t where, T &t)  throw(Error)             {readFrom(where,&t,sizeof(t));}
    1.70 +    template <typename T>
    1.71 +    void writeTo (off_t where, const T &t)  throw(Error)        {writeTo(where,&t,sizeof(t));}
    1.72 +    
    1.73 +    bool hasPath (const char *path) const  throw(Error);
    1.74 +
    1.75 +    static void unlink (const char *filename)  throw(Error);
    1.76 +    static void rename (const char *srcFilename, const char *dstFilename)  throw(Error);
    1.77 +    
    1.78 +    
    1.79 +    class Lock {
    1.80 +    public:
    1.81 +        Lock (File*, bool block =true)  throw(Error);
    1.82 +        ~Lock();
    1.83 +        bool isLocked() const                   {return _locked;}
    1.84 +        operator bool() const                   {return _locked;}
    1.85 +        bool retry();
    1.86 +    private:
    1.87 +        UNCOPYABLE(Lock);
    1.88 +        File *_file;
    1.89 +        int _mode;
    1.90 +        bool _locked;
    1.91 +    };
    1.92 +    
    1.93 +private:
    1.94 +    static int _open (const char *filename, int posixMode)  throw(Error);
    1.95 +    static int _check (int result)  throw(Error);
    1.96 +    static void _checkRead (ssize_t result, size_t expectedSize)  throw(Error);
    1.97 +    bool _lock (bool block);
    1.98 +    void _unlock();
    1.99 +    UNCOPYABLE(File);
   1.100 +    
   1.101 +    const int _fd;
   1.102 +    mutable MemoryMap *_memoryMap;
   1.103 +    int _locked;
   1.104 +    friend class Lock;
   1.105 +    friend class MemoryMap;
   1.106 +};
   1.107 +
   1.108 +}
   1.109 +
   1.110 +#endif _MOOSEYARD_FILE_