The Pedigree Project  0.1
FatFile.cc
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 #include "FatFile.h"
21 #include "FatFilesystem.h"
22 
24  String name, Time::Timestamp accessedTime, Time::Timestamp modifiedTime,
25  Time::Timestamp creationTime, uintptr_t inode, class Filesystem *pFs,
26  size_t size, uint32_t dirClus, uint32_t dirOffset, File *pParent)
27  : File(
28  name, accessedTime, modifiedTime, creationTime, inode, pFs, size,
29  pParent),
30  m_DirClus(dirClus), m_DirOffset(dirOffset), m_FileBlockCache()
31 {
32  m_FileBlockCache.setCallback(writeCallback, static_cast<File *>(this));
33 
34  // No permissions on FAT - set all to RWX.
35  setPermissions(
36  FILE_UR | FILE_UW | FILE_UX | FILE_GR | FILE_GW | FILE_GX | FILE_OR |
37  FILE_OW | FILE_OX);
38 }
39 
41 {
42 }
43 
44 uintptr_t FatFile::readBlock(uint64_t location)
45 {
46  FatFilesystem *pFs = static_cast<FatFilesystem *>(m_pFilesystem);
47 
48  uintptr_t buffer = m_FileBlockCache.insert(location);
49  pFs->read(this, location, getBlockSize(), buffer);
50  m_FileBlockCache.markNoLongerEditing(location);
51 
52  return buffer;
53 }
54 
55 void FatFile::writeBlock(uint64_t location, uintptr_t addr)
56 {
57  FatFilesystem *pFs = static_cast<FatFilesystem *>(m_pFilesystem);
58 
59  // Don't accidentally extend the file when writing the block.
60  size_t sz = getBlockSize();
61  uint64_t end = location + sz;
62  if (end > getSize())
63  sz = getSize() - location;
64  pFs->write(this, location, sz, addr);
65 }
66 
67 void FatFile::sync(size_t offset, bool async)
68 {
69  m_FileBlockCache.sync(offset, async);
70 }
71 
72 void FatFile::pinBlock(uint64_t location)
73 {
74  m_FileBlockCache.pin(location);
75 }
76 
77 void FatFile::unpinBlock(uint64_t location)
78 {
79  m_FileBlockCache.release(location);
80 }
81 
82 void FatFile::extend(size_t newSize)
83 {
84  FatFilesystem *pFs = static_cast<FatFilesystem *>(m_pFilesystem);
85 
86  if (m_Size < newSize)
87  {
88  pFs->extend(this, newSize);
89  m_Size = newSize;
90  }
91 }
92 
93 void FatFile::extend(size_t newSize, uint64_t location, uint64_t size)
94 {
95  // not using the hints at all
96  extend(newSize);
97 }
static void writeCallback(CacheConstants::CallbackCause cause, uintptr_t loc, uintptr_t page, void *meta)
Definition: File.cc:37
uintptr_t insert(uintptr_t key, bool *alreadyExisted=nullptr)
Definition: Cache.cc:258
virtual void extend(size_t newSize)
Definition: FatFile.cc:82
void markNoLongerEditing(uintptr_t key, size_t length=0)
Definition: Cache.cc:919
void sync(uintptr_t key, bool async)
Definition: Cache.cc:625
virtual size_t getBlockSize() const
Definition: File.cc:549
Definition: String.h:49
virtual void pinBlock(uint64_t location)
Definition: FatFile.cc:72
FatFile(const File &file)
void release(uintptr_t key)
Definition: Cache.cc:578
bool pin(uintptr_t key)
Definition: Cache.cc:557
uintptr_t readBlock(uint64_t location)
Definition: FatFile.cc:44
virtual void sync()
Definition: File.cc:351
virtual void unpinBlock(uint64_t location)
Definition: FatFile.cc:77
Definition: File.h:66
void setCallback(writeback_t newCallback, void *meta)
Definition: Cache.cc:759
virtual ~FatFile()
Definition: FatFile.cc:40
void writeBlock(uint64_t location, uintptr_t addr)
Definition: FatFile.cc:55