Directory.h revision 87f34658dec9097d987d254a990ea7f311bfc95f
15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- Directory.h --------------------------------------------------------===//
25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//                     The MCLinker Project
45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// This file is distributed under the University of Illinois Open Source
65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// License. See LICENSE.TXT for details.
75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#ifndef MCLD_SUPPORT_DIRECTORY_H
1087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#define MCLD_SUPPORT_DIRECTORY_H
115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#ifdef ENABLE_UNITTEST
125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <gtest.h>
135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif
145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
15f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines#include <mcld/ADT/TypeTraits.h>
16f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines#include <mcld/Support/FileSystem.h>
17f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines#include <mcld/Support/Path.h>
185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <llvm/Support/Allocator.h>
19affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <cstddef>
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
21f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines#include "PathCache.h"
225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace mcld {
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace sys {
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace fs {
265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass DirIterator;
285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class Directory
305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  \brief A Directory object stores a Path object, a FileStatus object for
315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *   non-symbolic link status, and a FileStatus object for symbolic link
325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *   status. The FileStatus objects act as value caches.
335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */
345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass Directory
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaofriend mcld::sys::fs::PathCache::entry_type* detail::bring_one_into_cache(DirIterator& pIter);
375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaofriend void detail::open_dir(Directory& pDir);
385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaofriend void detail::close_dir(Directory& pDir);
395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  friend class DirIterator;
415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef DirIterator iterator;
445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// default constructor
475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Directory();
485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// constructor - a directory whose path is pPath
505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  explicit Directory(const Path& pPath,
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                     FileStatus st = FileStatus(),
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                     FileStatus symlink_st = FileStatus());
535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// copy constructor
555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// when a copying construction happens, the cache is not copied.
565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Directory(const Directory& pCopy);
575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// assignment
595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// When an assignment occurs, the cache is clear.
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Directory& operator=(const Directory& pCopy);
615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// destructor, inheritable.
635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  virtual ~Directory();
645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// Since we have default construtor, we must provide assign.
665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void assign(const Path& pPath,
675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao              FileStatus st = FileStatus(),
685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao              FileStatus symlink_st = FileStatus());
695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// clear - clear the cache and close the directory handler
715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void clear();
725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool isGood() const;
745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// path - the path of the directory
765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  const Path& path() const
775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  { return m_Path; }
785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  FileStatus status() const;
805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  FileStatus symlinkStatus() const;
815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // -----  iterators  ----- //
835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // While the iterators move, the direcotry is modified.
845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // Thus, we only provide non-constant iterator.
855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  iterator begin();
865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  iterator end();
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprotected:
895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  mcld::sys::fs::Path m_Path;
905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  mutable FileStatus m_FileStatus;
915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  mutable FileStatus m_SymLinkStatus;
925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  intptr_t m_Handler;
935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // the cache of directory
945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  mcld::sys::fs::PathCache m_Cache;
955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool m_CacheFull;
965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class DirIterator
995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  \brief A DirIterator object can traverse all entries in a Directory
1005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
1015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  DirIterator will open the directory and add entry into Directory::m_Cache
1025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  DirIterator() is the end of a directory.
1035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  If the end of the directory elements is reached, the iterator becomes
1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  equal to the end iterator value - DirIterator().
1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  @see Directory
1075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */
1085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass DirIterator
1095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
1105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaofriend mcld::sys::fs::PathCache::entry_type* detail::bring_one_into_cache(DirIterator& pIter);
1115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaofriend class Directory;
1125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
1135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef mcld::sys::fs::PathCache            DirCache;
1145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
1165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef Directory                       value_type;
1175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef ConstTraits<Directory>          const_traits;
1185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef NonConstTraits<Directory>       non_const_traits;
1195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef std::input_iterator_tag         iterator_category;
1205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef size_t                          size_type;
1215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef ptrdiff_t                       difference_type;
1225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
1245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  explicit DirIterator(Directory* pParent,
1255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                       const DirCache::iterator& pIter);
1265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
1285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // Since StringMapIterator has no default constructor, we also have none.
1295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  DirIterator(const DirIterator &X);
1305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ~DirIterator();
1315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  DirIterator& operator=(const DirIterator& pCopy);
1325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  DirIterator& operator++();
1345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  DirIterator operator++(int);
1355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path* generic_path();
1375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path* path();
1395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  const Path* path() const;
1405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool operator==(const DirIterator& y) const;
1425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool operator!=(const DirIterator& y) const;
1435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
1455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Directory* m_pParent; // get handler
1465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  DirCache::iterator m_Iter; // for full situation
1475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  DirCache::entry_type* m_pEntry;
1485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
1495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of fs
1515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of sys
1525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of mcld
1535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif
155