|
8 | 8 | #include <cloysterhpc/functions.h> |
9 | 9 | #include <cloysterhpc/models/os.h> |
10 | 10 | #include <cloysterhpc/services/log.h> |
11 | | -#include <cstddef> |
12 | | -#include <fstream> |
13 | | -#include <ios> |
14 | | -#include <istream> |
| 11 | +#include <cloysterhpc/services/files.h> |
15 | 12 | #include <unordered_map> |
16 | | -#include <vector> |
17 | 13 |
|
18 | | -#include <glibmm.h> |
19 | 14 |
|
20 | 15 | // @FIXME: This file need some work |
21 | 16 | // |
@@ -109,42 +104,16 @@ bool DiskImage::hasVerifiedChecksum(const std::filesystem::path& path) |
109 | 104 | "e" } |
110 | 105 | }; |
111 | 106 |
|
112 | | - Glib::Checksum checksum(Glib::Checksum::ChecksumType::CHECKSUM_SHA256); |
| 107 | + auto checksum = cloyster::services::files::checksum(path); |
| 108 | + LOG_INFO("SHA256 checksum of file {} is: {}", path.string(), checksum); |
113 | 109 |
|
114 | | - std::ifstream file(path, std::ios::in | std::ios::binary); |
115 | | - if (!file.is_open()) { |
116 | | - throw std::filesystem::filesystem_error( |
117 | | - "Failed to open file", path, std::error_code()); |
118 | | - } |
119 | | - |
120 | | - // Read the file in chunks of 16834 bytes |
121 | | - constexpr std::size_t chunk_size = 16384; |
122 | | - std::vector<std::byte> buffer(chunk_size); |
123 | | - |
124 | | - while (file.read(reinterpret_cast<std::istream::char_type*>(buffer.data()), |
125 | | - static_cast<std::streamsize>(buffer.size()))) { |
126 | | - auto bytesRead = static_cast<gsize>(file.gcount()); |
127 | | - |
128 | | - checksum.update( |
129 | | - reinterpret_cast<const unsigned char*>(buffer.data()), bytesRead); |
130 | | - } |
131 | | - |
132 | | - // Handle any leftover bytes after the while loop ends |
133 | | - auto bytesRead = static_cast<gsize>(file.gcount()); |
134 | | - if (bytesRead > 0) { |
135 | | - checksum.update( |
136 | | - reinterpret_cast<const unsigned char*>(buffer.data()), bytesRead); |
137 | | - } |
138 | | - |
139 | | - LOG_INFO(fmt::format("SHA256 checksum of file {} is: {}", path.string(), |
140 | | - checksum.get_string())); |
141 | | - |
142 | | - if (checksum.get_string() |
| 110 | + if (checksum |
143 | 111 | == hash_map.find(path.filename().string())->second) { |
144 | 112 | LOG_TRACE("Checksum - The disk image is valid") |
145 | 113 | return true; |
146 | 114 | } |
147 | 115 |
|
| 116 | + |
148 | 117 | LOG_TRACE("Checksum - The disk image is invalid. Maybe you're using a " |
149 | 118 | "custom image?"); |
150 | 119 | return false; |
|
0 commit comments