FileHandle.h revision affc150dc44fab1911775a49636d0ce85333b634
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#include <errno.h>
17
18namespace mcld
19{
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 */
28class FileHandle
29{
30public:
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    IOStateEnd = 1L << 16
38  };
39
40  enum OpenModeEnum
41  {
42    NotOpen   = 0x00,
43    ReadOnly  = 0x01,
44    WriteOnly = 0x02,
45    ReadWrite = ReadOnly | WriteOnly,
46    Append    = 0x04,
47    Create    = 0x08,
48    Truncate  = 0x10,
49    Unknown   = 0xFF
50  };
51
52  typedef Flags<OpenModeEnum> OpenMode;
53
54  enum PermissionEnum
55  {
56    ReadOwner   = 0x0400,
57    WriteOwner  = 0x0200,
58    ExeOwner    = 0x0100,
59    ReadGroup   = 0x0040,
60    WriteGroup  = 0x0020,
61    ExeGroup    = 0x0010,
62    ReadOther   = 0x0004,
63    WriteOther  = 0x0002,
64    ExeOther    = 0x0001
65  };
66
67  typedef Flags<PermissionEnum> Permission;
68
69public:
70  FileHandle();
71
72  ~FileHandle();
73
74  bool open(const sys::fs::Path& pPath,
75            OpenMode pMode);
76
77  bool open(const sys::fs::Path& pPath,
78            OpenMode pMode,
79            Permission pPerm);
80
81  bool delegate(int pFD, OpenMode pMode = Unknown);
82
83  bool close();
84
85  void setState(IOState pState);
86
87  void cleanState(IOState pState = GoodBit);
88
89  // truncate - truncate the file up to the pSize.
90  bool truncate(size_t pSize);
91
92  bool read(void* pMemBuffer, size_t pStartOffset, size_t pLength);
93
94  bool write(const void* pMemBuffer, size_t pStartOffset, size_t pLength);
95
96  bool mmap(void*& pMemBuffer, size_t pStartOffset, size_t pLength);
97
98  bool munmap(void* pMemBuffer, size_t pLength);
99
100  // -----  observers  ----- //
101  const sys::fs::Path& path() const
102  { return m_Path; }
103
104  size_t size() const
105  { return m_Size; }
106
107  int handler() const
108  { return m_Handler; }
109
110  uint16_t rdstate() const
111  { return m_State; }
112
113  bool isOpened() const;
114
115  bool isGood() const;
116
117  bool isBad() const;
118
119  bool isFailed() const;
120
121  bool isReadable() const;
122
123  bool isWritable() const;
124
125  bool isReadWrite() const;
126
127  int error() const
128  { return errno; }
129
130private:
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