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