summaryrefslogtreecommitdiff
path: root/VirtualFileSystem/FileBackedDiskDevice.h
blob: 10c4af692293127a5fd45dbc22ac0c342fcdf854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#pragma once

#include "DiskDevice.h"
#include <AK/RetainPtr.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <stdio.h>

class FileBackedDiskDevice final : public DiskDevice {
public:
    static RetainPtr<FileBackedDiskDevice> create(String&& imagePath, unsigned blockSize);
    virtual ~FileBackedDiskDevice() override;

    bool isValid() const { return m_file; }

    virtual unsigned blockSize() const override;
    virtual bool readBlock(unsigned index, byte* out) const override;
    virtual bool writeBlock(unsigned index, const byte*) override;

private:
    virtual const char* className() const override;

    bool readInternal(qword offset, unsigned length, byte* out) const;
    bool writeInternal(qword offset, unsigned length, const byte* data);

    FileBackedDiskDevice(String&& imagePath, unsigned blockSize);

    String m_imagePath;
    FILE* m_file { nullptr };
    qword m_fileLength { 0 };
    unsigned m_blockSize { 0 };
};