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