The Pedigree Project  0.1
File.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 FILE_H
21 #define FILE_H
22 
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/process/Mutex.h"
25 #include "pedigree/kernel/processor/types.h"
26 #include "pedigree/kernel/time/Time.h"
27 #include "pedigree/kernel/utilities/Cache.h"
28 #include "pedigree/kernel/utilities/CacheConstants.h"
29 #include "pedigree/kernel/utilities/HashTable.h"
30 #include "pedigree/kernel/utilities/List.h"
31 #include "pedigree/kernel/utilities/StaticString.h"
32 #include "pedigree/kernel/utilities/String.h"
33 #include "pedigree/kernel/utilities/new"
34 
35 class Event;
36 class Filesystem;
37 class Thread;
38 
39 // RWX for owner.
40 #define FILE_UR 0001
41 #define FILE_UW 0002
42 #define FILE_UX 0004
43 #define FILE_UMASK 0007
44 #define FILE_UBITS 0
45 // RWX for group.
46 #define FILE_GR 0010
47 #define FILE_GW 0020
48 #define FILE_GX 0040
49 #define FILE_GMASK 0070
50 #define FILE_GBITS 3
51 // RWX for others.
52 #define FILE_OR 0100
53 #define FILE_OW 0200
54 #define FILE_OX 0400
55 #define FILE_OMASK 0700
56 #define FILE_OBITS 6
57 // Ancillary file bits.
58 #define FILE_STICKY 01000
59 #define FILE_AMASK 07000
60 #define FILE_ABITS 9
61 
62 #define FILE_BAD_BLOCK static_cast<uintptr_t>(-1)
63 
67 {
68  friend class Filesystem;
69 
70  public:
72  File();
73 
75  private:
76  File(const File &file);
77  File &operator=(const File &);
78 
79  public:
81  File(
82  const String &name, Time::Timestamp accessedTime,
83  Time::Timestamp modifiedTime, Time::Timestamp creationTime,
84  uintptr_t inode, class Filesystem *pFs, size_t size, File *pParent);
86  virtual ~File();
87 
93  virtual uint64_t read(
94  uint64_t location, uint64_t size, uintptr_t buffer,
95  bool bCanBlock = true) final;
99  virtual uint64_t write(
100  uint64_t location, uint64_t size, uintptr_t buffer,
101  bool bCanBlock = true) final;
102 
106  virtual physical_uintptr_t getPhysicalPage(size_t offset);
107 
113  virtual void returnPhysicalPage(size_t offset);
114 
122  virtual void sync();
123 
127  virtual void sync(size_t offset, bool async);
128 
130  Time::Timestamp getCreationTime();
132  void setCreationTime(Time::Timestamp t);
133 
135  Time::Timestamp getAccessedTime();
137  void setAccessedTime(Time::Timestamp t);
138 
140  Time::Timestamp getModifiedTime();
142  void setModifiedTime(Time::Timestamp t);
143 
145  String getName() const;
146  void getName(String &s) const;
147  // File names cannot be changed.
148 
150  virtual String getFullPath(bool bWithLabel = true);
151 
153  virtual void truncate();
154 
155  size_t getSize();
156  void setSize(size_t sz);
157 
159  virtual bool isSymlink();
160 
162  virtual bool isDirectory();
163 
165  virtual bool isPipe() const;
166 
168  virtual bool isFifo() const;
169 
171  virtual bool isSocket() const;
172 
173  uintptr_t getInode() const;
174  virtual void setInode(uintptr_t inode);
175 
176  Filesystem *getFilesystem() const;
177  void setFilesystem(Filesystem *pFs);
178 
179  virtual void fileAttributeChanged();
180 
181  virtual void increaseRefCount(bool bIsWriter);
182  virtual void decreaseRefCount(bool bIsWriter);
183 
184  void setPermissions(uint32_t perms);
185  uint32_t getPermissions() const;
186 
187  void setUid(size_t uid);
188  size_t getUid() const;
189 
190  void setGid(size_t gid);
191  size_t getGid() const;
192 
193  File *getParent() const;
194 
200  virtual int select(bool bWriting = false, int timeout = 0);
201 
206  void monitor(Thread *pThread, Event *pEvent);
207 
209  void cullMonitorTargets(Thread *pThread);
210 
212  virtual bool supports(const size_t command) const;
213 
215  virtual int command(const size_t command, void *buffer);
216 
219  virtual size_t getBlockSize() const;
220 
222  void enableDirect();
223 
225  void disableDirect();
226 
228  virtual void preallocate(size_t expectedSize, bool zero=true);
229 
236  virtual File *open();
237 
238  protected:
244  virtual bool isBytewise() const;
245 
247  virtual uint64_t readBytewise(
248  uint64_t location, uint64_t size, uintptr_t buffer,
249  bool bCanBlock = true);
251  virtual uint64_t writeBytewise(
252  uint64_t location, uint64_t size, uintptr_t buffer,
253  bool bCanBlock = true);
254 
256  virtual uintptr_t readBlock(uint64_t location);
261  virtual void writeBlock(uint64_t location, uintptr_t addr);
262 
264  virtual void extend(size_t newSize);
265 
272  virtual void extend(size_t newSize, uint64_t location, uint64_t size);
273 
275  void dataChanged();
276 
278  void getFilesystemLabel(HugeStaticString &s);
279 
287  static void writeCallback(
288  CacheConstants::CallbackCause cause, uintptr_t loc, uintptr_t page,
289  void *meta);
290 
300  virtual void pinBlock(uint64_t location);
301 
305  virtual void unpinBlock(uint64_t location);
306 
315  void evict(uint64_t location);
316 
318  void setPermissionsOnly(uint32_t perms);
319 
321  void setUidOnly(size_t uid);
322 
324  void setGidOnly(size_t gid);
325 
326  String m_Name;
327  Time::Timestamp m_AccessedTime;
328  Time::Timestamp m_ModifiedTime;
329  Time::Timestamp m_CreationTime;
330  uintptr_t m_Inode;
331 
332  class Filesystem *m_pFilesystem;
333  size_t m_Size;
334 
335  File *m_pParent;
336 
337  size_t m_nWriters, m_nReaders;
338 
339  size_t m_Uid;
340  size_t m_Gid;
341  uint32_t m_Permissions;
342 
344  {
345  public:
346  DataCacheKey() = default;
347  DataCacheKey(size_t block) : m_Block(block)
348  {
349  }
350  ~DataCacheKey() = default;
351 
352  size_t hash() const
353  {
354  return m_Block;
355  }
356 
357  bool operator==(const DataCacheKey &other) const
358  {
359  return m_Block == other.m_Block;
360  }
361 
362  private:
363  size_t m_Block = ~static_cast<size_t>(0);
364  };
365 
367 
368  bool m_bDirect;
369 
370 #ifndef VFS_NOMMU
371 
380 #endif
381 
382 #ifdef THREADS
383  Mutex m_Lock;
384 
386  {
387  MonitorTarget(Thread *pT, Event *pE) : pThread(pT), pEvent(pE)
388  {
389  }
390  Thread *pThread;
391  Event *pEvent;
392  };
393 
394  List<MonitorTarget *> m_MonitorTargets;
395 #endif
396 
397  private:
399  uintptr_t getCachedPage(size_t block, bool locked = true);
400 
402  void setCachedPage(size_t block, uintptr_t value, bool locked = true);
403 
406  bool useFillCache() const;
407 
409  uintptr_t readIntoCache(uintptr_t block);
410 };
411 
412 #endif
Cache m_FillCache
Definition: File.h:379
void operator=(const Filesystem &)
Definition: Mutex.h:58
Definition: String.h:49
Definition: Cache.h:118
Definition: List.h:64
Definition: Thread.h:54
Definition: Event.h:48
bool operator==(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T1 > &x1, const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x2)
Definition: Iterator.h:326
Definition: File.h:66