Skip to content
This repository was archived by the owner on Apr 16, 2026. It is now read-only.

Commit 7825697

Browse files
committed
Extract glibmm from diskImage, move it to files.cpp
1 parent 696a6e6 commit 7825697

6 files changed

Lines changed: 56 additions & 44 deletions

File tree

conanfile.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
from conan import ConanFile
22
from conan.tools.cmake import cmake_layout, CMakeToolchain, CMake
3-
from conan.tools.gnu import PkgConfigDeps
43

54
class MyProjectConan(ConanFile):
65
name = "CloysterHPC"
76
version = "0.1.1"
87
settings = "os", "arch", "compiler", "build_type"
98

109
def requirements(self):
11-
self.tool_requires("pkgconf/1.9.3") # Ensure pkg-config is available
1210
self.requires("cli11/[>=2.4.0 <2.5.0]")
1311
self.requires("spdlog/[>=1.14.0 <1.15.0]")
1412
self.requires("fmt/[>=10.0.0 <12.0.0]")
@@ -17,6 +15,8 @@ def requirements(self):
1715
self.requires("gsl-lite/[>=0.41.0 <0.42.0]")
1816
self.requires("doctest/[>=2.4.0 <2.5.0]")
1917
self.requires("sdbus-cpp/[>=2.0.0 <2.1.0]")
18+
19+
# We're using host's glibmm
2020
# self.requires("glibmm/[>=2.78.1 <2.79.0]")
2121

2222
# Override libmount to unify on 2.39.2.
@@ -31,8 +31,6 @@ def layout(self):
3131
def generate(self):
3232
tc = CMakeToolchain(self)
3333
tc.generate()
34-
pkg = PkgConfigDeps(self)
35-
pkg.generate()
3634

3735
def build(self):
3836
cmake = CMake(self)

include/cloysterhpc/functions.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <boost/process/pipe.hpp>
77
#include <cloysterhpc/services/repos.h>
88
#include <filesystem>
9-
// #include <glibmm/ustring.h>
109
#include <list>
1110
#include <optional>
1211
#include <string>

include/cloysterhpc/services/files.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ static_assert(cloyster::concepts::IsSaveable<KeyFile>);
9191
static_assert(concepts::IsMoveable<KeyFile>);
9292
static_assert(!concepts::IsCopyable<KeyFile>);
9393

94+
95+
std::string checksum(const std::string& data);
96+
std::string checksum(const std::filesystem::path& path, const std::size_t chunkSize = 16384);
97+
9498
};
9599

96100
#endif

setupDevEnvironment.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,12 @@ case "$os_version" in
123123
;;
124124
9)
125125
dnf -y install python pip libasan libubsan gcc-toolset-14 \
126-
gcc-toolset-14-libubsan-devel gcc-toolset-14-libasan-devel cppcheck
126+
gcc-toolset-14-libubsan-devel gcc-toolset-14-libasan-devel cppcheck \
127+
glibmm24 glibmm24-devel
127128
;;
128129
10)
129-
dnf -y install python pip libubsan libasan liblsan libtsan libhwasan
130+
dnf -y install python pip libubsan libasan liblsan libtsan libhwasan \
131+
glibmm-2.68 glibmm-2.68-devel
130132
;;
131133
esac
132134

src/diskImage.cpp

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88
#include <cloysterhpc/functions.h>
99
#include <cloysterhpc/models/os.h>
1010
#include <cloysterhpc/services/log.h>
11-
#include <cstddef>
12-
#include <fstream>
13-
#include <ios>
14-
#include <istream>
11+
#include <cloysterhpc/services/files.h>
1512
#include <unordered_map>
16-
#include <vector>
1713

18-
#include <glibmm.h>
1914

2015
// @FIXME: This file need some work
2116
//
@@ -109,42 +104,16 @@ bool DiskImage::hasVerifiedChecksum(const std::filesystem::path& path)
109104
"e" }
110105
};
111106

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);
113109

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
143111
== hash_map.find(path.filename().string())->second) {
144112
LOG_TRACE("Checksum - The disk image is valid")
145113
return true;
146114
}
147115

116+
148117
LOG_TRACE("Checksum - The disk image is invalid. Maybe you're using a "
149118
"custom image?");
150119
return false;

src/services/files.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
#include <concepts>
21
#include <ranges>
32
#include <stdexcept>
43
#include <utility>
4+
#include <cstddef>
5+
#include <fstream>
6+
#include <ios>
7+
#include <istream>
58

9+
#include <glibmm/checksum.h>
610
#include <glibmm/fileutils.h>
711
#include <glibmm/keyfile.h>
812

@@ -127,4 +131,40 @@ void KeyFile::save() { m_impl->safeToFile(m_impl->m_path); }
127131

128132
void KeyFile::load() { m_impl->loadFromFile(m_impl->m_path); }
129133

134+
std::string checksum(const std::string& data)
135+
{
136+
Glib::Checksum checksum(Glib::Checksum::ChecksumType::CHECKSUM_SHA256);
137+
checksum.update(data);
138+
return checksum.get_string();
139+
}
140+
141+
std::string checksum(const std::filesystem::path& path, const std::size_t chunkSize)
142+
{
143+
Glib::Checksum checksum(Glib::Checksum::ChecksumType::CHECKSUM_SHA256);
144+
std::ifstream file(path, std::ios::in | std::ios::binary);
145+
if (!file.is_open()) {
146+
throw std::filesystem::filesystem_error(
147+
"Failed to open file", path, std::error_code());
148+
}
149+
150+
std::vector<std::byte> buffer(chunkSize);
151+
152+
while (file.read(reinterpret_cast<std::istream::char_type*>(buffer.data()),
153+
static_cast<std::streamsize>(buffer.size()))) {
154+
auto bytesRead = static_cast<gsize>(file.gcount());
155+
156+
checksum.update(
157+
reinterpret_cast<const unsigned char*>(buffer.data()), bytesRead);
158+
}
159+
160+
// Handle any leftover bytes after the while loop ends
161+
auto bytesRead = static_cast<gsize>(file.gcount());
162+
if (bytesRead > 0) {
163+
checksum.update(
164+
reinterpret_cast<const unsigned char*>(buffer.data()), bytesRead);
165+
}
166+
167+
return checksum.get_string();
168+
}
169+
130170
} // namespace cloyster::services::files

0 commit comments

Comments
 (0)