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 <string>
22 #include <iosfwd>
23 #include <locale>
24
25 namespace mcld {
26 namespace sys {
27 namespace fs {
28
29 enum FileType
30 {
31 StatusError,
32 StatusUnknown = StatusError,
33 FileNotFound,
34 RegularFile,
35 DirectoryFile,
36 SymlinkFile,
37 BlockFile,
38 CharacterFile,
39 FifoFile,
40 SocketFile,
41 ReparseFile,
42 TypeUnknown,
43 StatusKnown,
44 IsSymLink
45 };
46
47 /** \class FileStatus
48 * \brief FileStatus
49 */
50 class FileStatus
51 {
52 public:
FileStatus()53 FileStatus()
54 : m_Value(StatusError) {}
55
FileStatus(FileType v)56 explicit FileStatus(FileType v)
57 : m_Value(v) {}
58
setType(FileType v)59 void setType(FileType v) { m_Value = v; }
type()60 FileType type() const { return m_Value; }
61
62 private:
63 FileType m_Value;
64 };
65
66 inline bool operator==(const FileStatus& rhs, const FileStatus& lhs) {
67 return rhs.type() == lhs.type();
68 }
69
70 inline bool operator!=(const FileStatus& rhs, const FileStatus& lhs ) {
71 return !(rhs == lhs);
72 }
73
74 class Path;
75 class DirIterator;
76 class Directory;
77
78 bool exists(const Path &pPath);
79 bool is_directory(const Path &pPath);
80
exists(FileStatus f)81 inline static bool exists(FileStatus f) {
82 return (f.type() != StatusError)&&(f.type() != FileNotFound);
83 }
84
is_directory(FileStatus f)85 inline static bool is_directory(FileStatus f) {
86 return f.type() == mcld::sys::fs::DirectoryFile;
87 }
88
89 namespace detail
90 {
91
92 typedef unsigned char* Address;
93 typedef off_t Offset;
94 extern std::string static_library_extension;
95 extern std::string shared_library_extension;
96 extern std::string executable_extension;
97 extern std::string relocatable_extension;
98 extern std::string assembly_extension;
99 extern std::string bitcode_extension;
100
101 size_t canonicalize(std::string& pPathName);
102 bool not_found_error(int perrno);
103 void status(const Path& p, FileStatus& pFileStatus);
104 void symlink_status(const Path& p, FileStatus& pFileStatus);
105 mcld::sys::fs::PathCache::entry_type* bring_one_into_cache(DirIterator& pIter);
106 void open_dir(Directory& pDir);
107 void close_dir(Directory& pDir);
108 void get_pwd(std::string& pPWD);
109 size_t pread(int pFD, Address pBuf, size_t pCount, off_t pOffset);
110 size_t pwrite(int pFD, const Address pBuf, size_t pCount, off_t pOffset);
111 char *strerror(int pErrnum);
112
113 } // namespace of detail
114 } // namespace of fs
115 } // namespace of sys
116 } // namespace of mcld
117
118 #endif
119
120