aboutsummaryrefslogtreecommitdiff
path: root/src/FATStorage.h
blob: 29bcbc4743a33834f9ea4560ce3c194a8ec7d5ea (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
    Copyright 2016-2022 melonDS team

    This file is part of melonDS.

    melonDS is free software: you can redistribute it and/or modify it under
    the terms of the GNU General Public License as published by the Free
    Software Foundation, either version 3 of the License, or (at your option)
    any later version.

    melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with melonDS. If not, see http://www.gnu.org/licenses/.
*/

#ifndef FATSTORAGE_H
#define FATSTORAGE_H

#include <stdio.h>
#include <string>
#include <map>
#include <filesystem>

#include "types.h"
#include "fatfs/ff.h"


class FATStorage
{
public:
    FATStorage(std::string filename, u64 size, bool readonly, std::string sourcedir);
    ~FATStorage();

    bool Open();
    void Close();

    bool InjectFile(std::string path, u8* data, u32 len);

    u32 ReadSectors(u32 start, u32 num, u8* data);
    u32 WriteSectors(u32 start, u32 num, u8* data);

private:
    std::string FilePath;
    std::string IndexPath;
    std::string SourceDir;
    bool ReadOnly;

    FILE* File;
    u64 FileSize;

    static FILE* FF_File;
    static u64 FF_FileSize;
    static UINT FF_ReadStorage(BYTE* buf, LBA_t sector, UINT num);
    static UINT FF_WriteStorage(BYTE* buf, LBA_t sector, UINT num);

    static u32 ReadSectorsInternal(FILE* file, u64 filelen, u32 start, u32 num, u8* data);
    static u32 WriteSectorsInternal(FILE* file, u64 filelen, u32 start, u32 num, u8* data);

    void LoadIndex();
    void SaveIndex();

    bool ExportFile(std::string path, std::filesystem::path out);
    void ExportDirectory(std::string path, std::string outbase, int level);
    bool DeleteHostDirectory(std::string path, std::string outbase, int level);
    void ExportChanges(std::string outbase);

    bool CanFitFile(u32 len);
    bool DeleteDirectory(std::string path, int level);
    void CleanupDirectory(std::string sourcedir, std::string path, int level);
    bool ImportFile(std::string path, std::filesystem::path in);
    bool ImportDirectory(std::string sourcedir);
    u64 GetDirectorySize(std::filesystem::path sourcedir);

    bool Load(std::string filename, u64 size, std::string sourcedir);
    bool Save();

    typedef struct
    {
        std::string Path;
        bool IsReadOnly;

    } DirIndexEntry;

    typedef struct
    {
        std::string Path;
        bool IsReadOnly;
        u64 Size;
        s64 LastModified;
        u32 LastModifiedInternal;

    } FileIndexEntry;

    std::map<std::string, DirIndexEntry> DirIndex;
    std::map<std::string, FileIndexEntry> FileIndex;
};

#endif // FATSTORAGE_H