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