The Pedigree Project  0.1
Directory.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef DIRECTORY_H
21 #define DIRECTORY_H
22 
23 #include "File.h"
24 #include "pedigree/kernel/Log.h"
25 #include "pedigree/kernel/compiler.h"
26 #include "pedigree/kernel/processor/types.h"
27 #include "pedigree/kernel/time/Time.h"
28 #include "pedigree/kernel/utilities/HashTable.h"
29 #include "pedigree/kernel/utilities/LazyEvaluate.h"
30 #include "pedigree/kernel/utilities/Pointers.h"
31 #include "pedigree/kernel/utilities/String.h"
32 #include "pedigree/kernel/utilities/utility.h"
33 
34 class StringView;
35 class HashedStringView;
36 
45 {
46  friend class Filesystem;
47 
48  public:
50  static Directory *fromFile(File *pF)
51  {
52  if (!pF->isDirectory())
53  FATAL("Casting non-directory File to Directory!");
54  return reinterpret_cast<Directory *>(pF);
55  }
56 
58  Directory();
59 
61  private:
62  Directory(const Directory &file);
63  Directory &operator=(const Directory &);
64 
65  public:
67  Directory(
68  const String &name, Time::Timestamp accessedTime,
69  Time::Timestamp modifiedTime, Time::Timestamp creationTime,
70  uintptr_t inode, class Filesystem *pFs, size_t size, File *pParent);
72  virtual ~Directory();
73 
75  virtual bool isDirectory()
76  {
77  return true;
78  }
79 
81  File *getChild(size_t n);
82 
84  size_t getNumChildren();
85 
87  virtual void cacheDirectoryContents();
88 
90  virtual bool isCachePopulated() const
91  {
92  return m_bCachePopulated;
93  }
94 
96  File *lookup(const HashedStringView &s) const;
97 
99  void remove(const HashedStringView &s);
100 
107  Directory *getReparsePoint() const;
108 
110  void setReparsePoint(Directory *pTarget);
111 
119  bool addEphemeralFile(File *pFile);
120 
126  bool empty();
127 
128  protected:
130  {
133 
135 
136  // No copy construction (UniqueArray)
137  NOT_COPYABLE_OR_ASSIGNABLE(DirectoryEntryMetadata);
138 
139  // These two should always be known at metadata creation time.
140  Directory *pDirectory;
141  String filename;
142 
143  // Space for anything else to be stored by the filesystem.
144  UniqueArray<char> opaque;
145  };
146 
147  private:
148  static File *evaluateEntry(const DirectoryEntryMetadata &meta);
149  static void destroyEntry(File *file);
150 
151  virtual bool isBytewise() const
152  {
153  // This will cause read()/write() to fail as readBytewise/writeBytewise
154  // are not overridden and remain as their default implementation, which
155  // errors out.
156  return true;
157  }
158 
159  protected:
160  typedef LazyEvaluate<
161  File, DirectoryEntryMetadata, evaluateEntry, destroyEntry>
163 
164  private:
166 
168  DirectoryEntryCache m_Cache;
169 
175 
177  Directory *m_ReparseTarget = nullptr;
178 
179  protected:
181  virtual const DirectoryEntryCache &getCache()
182  {
183  return m_Cache;
184  }
185 
188  {
189  m_bCachePopulated = true;
190  }
191 
193  void addDirectoryEntry(const String &name, File *pTarget);
194 
196  void addDirectoryEntry(const String &name, DirectoryEntryMetadata &&meta);
197 
199  void preallocateDirectoryEntries(size_t count);
200 
202  virtual File *convertToFile(const DirectoryEntryMetadata &meta);
203 };
204 
206 
207 #endif
virtual bool isBytewise() const
Definition: Directory.h:151
void operator=(const Filesystem &)
static Directory * fromFile(File *pF)
Definition: Directory.h:50
Definition: String.h:49
virtual const DirectoryEntryCache & getCache()
Definition: Directory.h:181
bool m_bCachePopulated
Definition: Directory.h:174
virtual bool isDirectory()
Definition: Directory.h:75
virtual bool isDirectory()
Definition: File.cc:436
#define FATAL(text)
Definition: Log.h:89
Definition: File.h:66
DirectoryEntryCache m_Cache
Definition: Directory.h:168
void markCachePopulated()
Definition: Directory.h:187
virtual bool isCachePopulated() const
Definition: Directory.h:90