The Pedigree Project 0.1
FatFile-resize.cc
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted.
6 */
7
8#include "pedigree/kernel/syscallError.h"
9
10#include "FatFile.h"
11#include "FatFilesystem.h"
12
14 resize(0);
15}
16
17void FatFile::extend(size_t newSize) {
19 if (!m_pFilesystem->tryAcquireOperation(operation)) {
20 return;
21 }
22 FatFilesystem* filesystem = static_cast<FatFilesystem*>(m_pFilesystem);
23 const size_t oldSize = getSize();
24 filesystem->extend(this, newSize);
25 const size_t size = getSize();
26 const size_t pageSize = getBlockSize();
27 const size_t within = oldSize % pageSize;
28 if (size > oldSize && within) {
29 const size_t pageOffset = oldSize - within;
30 const uintptr_t page = cacheState().fill.lookup(pageOffset);
31 if (page) {
32 // Shared mappings may have dirtied padding beyond the previous EOF.
33 const size_t amount = size - oldSize < pageSize - within ? size - oldSize : pageSize - within;
34 ByteSet(reinterpret_cast<void*>(page + within), 0, amount);
35 cacheState().fill.markDirty(pageOffset);
36 cacheState().fill.release(pageOffset);
37 }
38 }
39}
40
41void FatFile::extend(size_t newSize, uint64_t, uint64_t) {
43 if (!m_pFilesystem->tryAcquireOperation(operation)) {
44 return;
45 }
46 extend(newSize);
47}
48
49bool FatFile::prepareShrink(const ShrinkContext& context, UniquePointer<PreparedShrink>& prepared) {
50 return static_cast<FatFilesystem*>(m_pFilesystem)
51 ->prepareFileShrink(this, context.newSize, prepared);
52}
53
54bool FatFile::resizeFile(size_t size) {
55 if (size > UINT32_MAX) {
56 SYSCALL_ERROR(FileTooLarge);
57 return false;
58 }
59 if (size == getSize())
60 return true;
61 if (size < getSize()) {
62 SYSCALL_ERROR(InvalidArgument);
63 return false;
64 }
65 static_cast<FatFilesystem*>(m_pFilesystem)->extend(this, size);
66 return getSize() == size;
67}
void release(uintptr_t key)
Definition Cache.cc:1570
void markDirty(uintptr_t key)
Definition Cache.cc:2135
uintptr_t lookup(uintptr_t key)
Definition Cache.cc:737
void truncate() override
void extend(size_t newSize) override
bool resize(size_t size)
Definition File.cc:1213
virtual size_t getBlockSize() const
Definition File.cc:1077