summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys/directory.h
blob: 12bb90ec8a458935498dafb7392a1c5d90c2a5ce (plain) (blame)
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
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <cstddef>
#include <iterator>
#include <string_view>
#include "common/common_funcs.h"
#include "common/common_types.h"

////////////////////////////////////////////////////////////////////////////////////////////////////
// FileSys namespace

namespace FileSys {

enum EntryType : u8 {
    Directory = 0,
    File = 1,
};

// Structure of a directory entry, from
// http://switchbrew.org/index.php?title=Filesystem_services#DirectoryEntry
struct Entry {
    Entry(std::string_view view, EntryType entry_type, u64 entry_size)
        : type{entry_type}, file_size{entry_size} {
        const std::size_t copy_size = view.copy(filename, std::size(filename) - 1);
        filename[copy_size] = '\0';
    }

    char filename[0x300];
    INSERT_PADDING_BYTES(4);
    EntryType type;
    INSERT_PADDING_BYTES(3);
    u64 file_size;
};
static_assert(sizeof(Entry) == 0x310, "Directory Entry struct isn't exactly 0x310 bytes long!");
static_assert(offsetof(Entry, type) == 0x304, "Wrong offset for type in Entry.");
static_assert(offsetof(Entry, file_size) == 0x308, "Wrong offset for file_size in Entry.");

class DirectoryBackend : NonCopyable {
public:
    DirectoryBackend() {}
    virtual ~DirectoryBackend() {}

    /**
     * List files contained in the directory
     * @param count Number of entries to return at once in entries
     * @param entries Buffer to read data into
     * @return Number of entries listed
     */
    virtual u64 Read(const u64 count, Entry* entries) = 0;

    /// Returns the number of entries still left to read.
    virtual u64 GetEntryCount() const = 0;

    /**
     * Close the directory
     * @return true if the directory closed correctly
     */
    virtual bool Close() const = 0;
};

} // namespace FileSys