FileSystem.h revision affc150dc44fab1911775a49636d0ce85333b634
1//===- FileSystem.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// This file declares the mcld::sys::fs:: namespace. It follows TR2/boost
10// filesystem (v3), but modified to remove exception handling and the
11// path class.
12//===----------------------------------------------------------------------===//
13
14#ifndef MCLD_FILE_SYSTEM_H
15#define MCLD_FILE_SYSTEM_H
16#ifdef ENABLE_UNITTEST
17#include <gtest.h>
18#endif
19
20#include "mcld/Support/PathCache.h"
21#include <mcld/Config/Config.h>
22#include <string>
23#include <iosfwd>
24#include <locale>
25
26namespace mcld {
27namespace sys {
28
29namespace fs {
30
31enum FileType
32{
33  StatusError,
34  StatusUnknown = StatusError,
35  FileNotFound,
36  RegularFile,
37  DirectoryFile,
38  SymlinkFile,
39  BlockFile,
40  CharacterFile,
41  FifoFile,
42  SocketFile,
43  ReparseFile,
44  TypeUnknown,
45  StatusKnown,
46  IsSymLink
47};
48
49/** \class FileStatus
50 *  \brief FileStatus
51 */
52class FileStatus
53{
54public:
55  FileStatus()
56    : m_Value(StatusError) {}
57
58  explicit FileStatus(FileType v)
59    : m_Value(v) {}
60
61  void setType(FileType v)   { m_Value = v; }
62  FileType type() const   { return m_Value; }
63
64private:
65  FileType m_Value;
66};
67
68inline bool operator==(const FileStatus& rhs, const FileStatus& lhs) {
69  return rhs.type() == lhs.type();
70}
71
72inline bool operator!=(const FileStatus& rhs, const FileStatus& lhs ) {
73  return !(rhs == lhs);
74}
75
76class Path;
77class DirIterator;
78class Directory;
79
80bool exists(const Path &pPath);
81bool is_directory(const Path &pPath);
82
83inline static bool exists(FileStatus f) {
84  return (f.type() != StatusError)&&(f.type() != FileNotFound);
85}
86
87inline static bool is_directory(FileStatus f) {
88  return f.type() == mcld::sys::fs::DirectoryFile;
89}
90
91namespace detail
92{
93
94extern std::string static_library_extension;
95extern std::string shared_library_extension;
96extern std::string executable_extension;
97extern std::string relocatable_extension;
98extern std::string assembly_extension;
99extern std::string bitcode_extension;
100
101size_t canonicalize(std::string& pPathName);
102bool not_found_error(int perrno);
103void status(const Path& p, FileStatus& pFileStatus);
104void symlink_status(const Path& p, FileStatus& pFileStatus);
105mcld::sys::fs::PathCache::entry_type* bring_one_into_cache(DirIterator& pIter);
106void open_dir(Directory& pDir);
107void close_dir(Directory& pDir);
108void get_pwd(std::string& pPWD);
109
110int open(const Path& pPath, int pOFlag);
111int open(const Path& pPath, int pOFlag, int pPermission);
112ssize_t pread(int pFD, void* pBuf, size_t pCount, size_t pOffset);
113ssize_t pwrite(int pFD, const void* pBuf, size_t pCount, size_t pOffset);
114int ftruncate(int pFD, size_t pLength);
115
116} // namespace of detail
117} // namespace of fs
118} // namespace of sys
119} // namespace of mcld
120
121#endif
122
123