FileSystem.h revision 5460a1f25d9ddecb5c70667267d66d51af177a99
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 25namespace mcld { 26namespace sys { 27namespace fs { 28 29enum 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 */ 50class FileStatus 51{ 52public: 53 FileStatus() 54 : m_Value(StatusError) {} 55 56 explicit FileStatus(FileType v) 57 : m_Value(v) {} 58 59 void setType(FileType v) { m_Value = v; } 60 FileType type() const { return m_Value; } 61 62private: 63 FileType m_Value; 64}; 65 66inline bool operator==(const FileStatus& rhs, const FileStatus& lhs) { 67 return rhs.type() == lhs.type(); 68} 69 70inline bool operator!=(const FileStatus& rhs, const FileStatus& lhs ) { 71 return !(rhs == lhs); 72} 73 74class Path; 75class DirIterator; 76class Directory; 77 78bool exists(const Path &pPath); 79bool is_directory(const Path &pPath); 80 81inline static bool exists(FileStatus f) { 82 return (f.type() != StatusError)&&(f.type() != FileNotFound); 83} 84 85inline static bool is_directory(FileStatus f) { 86 return f.type() == mcld::sys::fs::DirectoryFile; 87} 88 89namespace detail 90{ 91 92typedef unsigned char* Address; 93typedef off_t Offset; 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); 109size_t pread(int pFD, Address pBuf, size_t pCount, off_t pOffset); 110size_t pwrite(int pFD, const Address pBuf, size_t pCount, off_t pOffset); 111char *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