-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch.h
150 lines (118 loc) · 3.02 KB
/
patch.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#pragma once
#include "common.h"
#include <cstdint>
#include <vector>
#include <filesystem>
#include <fstream>
#include <sstream>
class File
{
public:
File()
{
File::g_files.push_back(*this);
}
virtual bool validate(const std::filesystem::path& root) = 0;
virtual bool perform(const std::filesystem::path& root) = 0;
static const std::vector<std::reference_wrapper<File>>& getFiles()
{
return g_files;
}
private:
static std::vector<std::reference_wrapper<File>> g_files;
};
class AddedFile : public File
{
public:
AddedFile(const std::filesystem::path& path, const std::vector<std::uint8_t>& buffer)
: m_path(path)
, m_data(buffer)
{
}
bool validate(const std::filesystem::path& root)
{
return !std::filesystem::exists(root / m_path);
}
bool perform(const std::filesystem::path& root)
{
std::cout << "Creating: " << m_path.make_preferred().string() << std::endl;
auto path = root / m_path;
std::filesystem::create_directories(path.parent_path());
std::ofstream file(path, std::ios::binary);
if (!file.is_open())
return false;
unzip(m_data, file);
return true;
}
private:
std::filesystem::path m_path;
const std::vector<std::uint8_t>& m_data;
};
class RemovedFile : public File
{
public:
RemovedFile(const std::filesystem::path& path, const std::vector<std::uint8_t>& buffer)
: m_path(path)
, m_checkSum(*reinterpret_cast<const std::uint32_t*>(buffer.data()))
{
}
bool validate(const std::filesystem::path& root)
{
auto path = root / m_path;
if (std::filesystem::is_regular_file(path))
{
std::ifstream file(path, std::ios::binary);
return crc32(file) == m_checkSum;
}
return false;
}
bool perform(const std::filesystem::path& root)
{
std::cout << "Deleting: " << m_path.make_preferred().string() << std::endl;
return std::filesystem::remove(root / m_path);
}
private:
std::filesystem::path m_path;
std::uint32_t m_checkSum;
};
class ModifiedFile : public File
{
public:
ModifiedFile(const std::filesystem::path& path, const std::vector<std::uint8_t>& crc, const std::vector<std::uint8_t>& buffer)
: m_path(path)
, m_checkSum(*reinterpret_cast<const std::uint32_t*>(crc.data()))
, m_patch(buffer)
{
}
bool validate(const std::filesystem::path& root)
{
auto path = root / m_path;
if (std::filesystem::is_regular_file(path))
{
std::ifstream file(path, std::ios::binary);
return crc32(file) == m_checkSum;
}
return false;
}
bool perform(const std::filesystem::path& root)
{
std::cout << "Updating: " << m_path.make_preferred().string() << std::endl;
auto path = root / m_path;
auto tempPath = root / (m_path.string() + ".tmp");
{
std::ifstream inFile(path, std::ios::binary);
if (!inFile.is_open())
return false;
std::ofstream tempFile(tempPath, std::ios::binary);
if (!tempFile.is_open())
return false;
patch(m_patch, inFile, tempFile);
}
std::filesystem::rename(tempPath, path);
return true;
}
private:
std::filesystem::path m_path;
std::uint32_t m_checkSum;
const std::vector<std::uint8_t>& m_patch;
};