FileHandle.h revision 22add6ff3426df1a85089fe6a6e1597ee3b6f300
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 20namespace 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 */ 29class FileHandle 30{ 31public: 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 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 = S_IRUSR, 58 WriteOwner = S_IWUSR, 59 ExeOwner = S_IXUSR, 60 ReadGroup = S_IRGRP, 61 WriteGroup = S_IWGRP, 62 ExeGroup = S_IXGRP, 63 ReadOther = S_IROTH, 64 WriteOther = S_IWOTH, 65 ExeOther = S_IXOTH, 66 System = 0xFFFFFFFF 67 }; 68 69 typedef Flags<PermissionEnum> Permission; 70 71public: 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 isReadable() const; 124 125 bool isWritable() const; 126 127 bool isReadWrite() const; 128 129 int error() const 130 { return errno; } 131 132private: 133 sys::fs::Path m_Path; 134 int m_Handler; 135 unsigned int m_Size; 136 uint16_t m_State; 137 OpenMode m_OpenMode; 138}; 139 140} // namespace of mcld 141 142#endif 143 144