-
Notifications
You must be signed in to change notification settings - Fork 0
/
fat_map.h
74 lines (61 loc) · 2.08 KB
/
fat_map.h
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
#pragma once
#include <cassert>
#include <cstdint>
#include <iostream>
#define ASSERT(x) assert(x)
namespace cs5250 {
class FATMap {
private:
uint8_t fat_num_;
uint32_t size_;
std::vector<uint32_t *> cluster_starts_;
public:
FATMap(uint8_t fat_num, uint32_t size,
std::vector<uint32_t *> &&cluster_starts)
: fat_num_(fat_num), size_(size), cluster_starts_(cluster_starts) {}
uint32_t Lookup(uint32_t cluster_number) {
if (cluster_number < 0 || cluster_number >= size_) {
std::cerr << "cluster number out of range" << std::endl;
return 0;
}
return cluster_starts_[0][cluster_number] & 0x0FFFFFFF;
}
void SetFree(uint32_t cluster_number) {
if (cluster_number < 0 || cluster_number >= size_) {
std::cerr << "cluster number out of range" << std::endl;
return;
}
for (auto &cluster_start : cluster_starts_)
cluster_start[cluster_number] = 0;
}
template <bool free_first = true>
void Set(uint32_t cluster_number, uint32_t next_cluster) {
if (cluster_number < 0 || cluster_number >= size_) {
std::cerr << "cluster number out of range" << std::endl;
return;
}
for (auto &cluster_start : cluster_starts_) {
if constexpr (free_first)
ASSERT(cluster_start[cluster_number] == 0);
else {
ASSERT(IsEndOfFile(cluster_start[cluster_number]));
}
cluster_start[cluster_number] = next_cluster;
}
}
inline bool IsEndOfFile(uint32_t fat_entry_value) const {
return fat_entry_value >= 0x0FFFFFF8;
}
std::optional<std::vector<uint32_t>> FindFree(uint32_t num) {
std::vector<uint32_t> free_clusters;
for (uint32_t i = 0; i < size_; i++) {
if (Lookup(i) == 0) {
free_clusters.push_back(i);
if (free_clusters.size() == num)
return free_clusters;
}
}
return std::nullopt;
}
};
} // namespace cs5250