Home | History | Annotate | Download | only in Support
      1 //===- FileHandle.h -------------------------------------------------------===//
      2 //
      3 //                     The MCLinker Project
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 #ifndef MCLD_FILE_HANDLE_H
     10 #define MCLD_FILE_HANDLE_H
     11 #ifdef ENABLE_UNITTEST
     12 #include <gtest.h>
     13 #endif
     14 #include <mcld/Support/Path.h>
     15 #include <mcld/ADT/Flags.h>
     16 
     17 #include <errno.h>
     18 
     19 namespace mcld {
     20 
     21 /** \class FileHandle
     22  *  \brief FileHandle class provides an interface for reading from and writing
     23  *  to files.
     24  *
     25  *  Operators of FileHandle should neither throw exceptions nor call expressive
     26  *  diagnostic output.
     27  */
     28 class FileHandle
     29 {
     30 public:
     31   enum IOState
     32   {
     33     GoodBit    = 0,       // no error
     34     BadBit     = 1L << 0, // error due to the inappropriate operation
     35     EOFBit     = 1L << 1, // reached End-Of-File
     36     FailBit    = 1L << 2, // internal logic fail
     37     DeputedBit = 1L << 3, // the file descriptor is delegated
     38     IOStateEnd = 1L << 16
     39   };
     40 
     41   enum OpenModeEnum
     42   {
     43     NotOpen   = 0x00,
     44     ReadOnly  = 0x01,
     45     WriteOnly = 0x02,
     46     ReadWrite = ReadOnly | WriteOnly,
     47     Append    = 0x04,
     48     Create    = 0x08,
     49     Truncate  = 0x10,
     50     Unknown   = 0xFF
     51   };
     52 
     53   typedef Flags<OpenModeEnum> OpenMode;
     54 
     55   enum PermissionEnum
     56   {
     57     ReadOwner   = 0x0400,
     58     WriteOwner  = 0x0200,
     59     ExeOwner    = 0x0100,
     60     ReadGroup   = 0x0040,
     61     WriteGroup  = 0x0020,
     62     ExeGroup    = 0x0010,
     63     ReadOther   = 0x0004,
     64     WriteOther  = 0x0002,
     65     ExeOther    = 0x0001,
     66     System      = 0xFFFF
     67   };
     68 
     69   typedef Flags<PermissionEnum> Permission;
     70 
     71 public:
     72   FileHandle();
     73 
     74   ~FileHandle();
     75 
     76   /// open - open the file.
     77   /// @return if we meet any trouble during opening the file, return false.
     78   ///         use rdstate() to see what happens.
     79   bool open(const sys::fs::Path& pPath,
     80             OpenMode pMode,
     81             Permission pPerm = System);
     82 
     83   bool delegate(int pFD, OpenMode pMode = Unknown);
     84 
     85   bool close();
     86 
     87   void setState(IOState pState);
     88 
     89   void cleanState(IOState pState = GoodBit);
     90 
     91   // truncate - truncate the file up to the pSize.
     92   bool truncate(size_t pSize);
     93 
     94   bool read(void* pMemBuffer, size_t pStartOffset, size_t pLength);
     95 
     96   bool write(const void* pMemBuffer, size_t pStartOffset, size_t pLength);
     97 
     98   bool mmap(void*& pMemBuffer, size_t pStartOffset, size_t pLength);
     99 
    100   bool munmap(void* pMemBuffer, size_t pLength);
    101 
    102   // -----  observers  ----- //
    103   const sys::fs::Path& path() const
    104   { return m_Path; }
    105 
    106   size_t size() const
    107   { return m_Size; }
    108 
    109   int handler() const
    110   { return m_Handler; }
    111 
    112   uint16_t rdstate() const
    113   { return m_State; }
    114 
    115   bool isOpened() const;
    116 
    117   bool isGood() const;
    118 
    119   bool isBad() const;
    120 
    121   bool isFailed() const;
    122 
    123   bool isOwned() const;
    124 
    125   bool isReadable() const;
    126 
    127   bool isWritable() const;
    128 
    129   bool isReadWrite() const;
    130 
    131   int error() const { return errno; }
    132 
    133 private:
    134   sys::fs::Path m_Path;
    135   int m_Handler;
    136   unsigned int m_Size;
    137   uint16_t m_State;
    138   OpenMode m_OpenMode;
    139 };
    140 
    141 } // namespace of mcld
    142 
    143 #endif
    144 
    145