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