Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 206650f

Browse files
authored
chore: update model's size on import (#1630)
* chore: update model's size on import * refactor: consolidate get file size using filesystem API * chore: remove auto declare * chore: correct file size retrieval error handling * fix: missing import gguf_parser * chore: change back to auto type declare
1 parent 55bbe0d commit 206650f

File tree

4 files changed

+20
-48
lines changed

4 files changed

+20
-48
lines changed

engine/config/gguf_parser.cc

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdexcept>
1010
#include <string>
1111
#include <vector>
12+
#include <filesystem>
1213

1314
#ifdef _WIN32
1415
#include <io.h>
@@ -71,24 +72,9 @@ void GGUFHandler::OpenFile(const std::string& file_path) {
7172
CloseHandle(file_handle_);
7273

7374
#else
74-
FILE* fd = fopen(file_path.c_str(), "rb");
75-
if (!fd) {
76-
perror("Error opening file");
77-
throw std::runtime_error("Failed to open file");
78-
}
79-
80-
// Get file size
81-
// file_size_ = lseek(fd, 0, SEEK_END);
82-
fseek(fd, 0, SEEK_END); // move file pointer to end of file
83-
file_size_ = ftell(fd); // get the file size, in bytes
84-
fclose(fd);
85-
if (file_size_ == -1) {
86-
perror("Error getting file size");
87-
// close(fd);
88-
throw std::runtime_error("Failed to get file size");
89-
}
75+
file_size_ = std::filesystem::file_size(file_path);
76+
9077
int file_descriptor = open(file_path.c_str(), O_RDONLY);
91-
;
9278
// Memory-map the file
9379
data_ = static_cast<uint8_t*>(
9480
mmap(nullptr, file_size_, PROT_READ, MAP_PRIVATE, file_descriptor, 0));

engine/controllers/models.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,12 @@ void Models::ImportModel(
351351
modelPath, file_path,
352352
std::filesystem::copy_options::update_existing);
353353
model_config.files.push_back(file_path.string());
354+
auto size = std::filesystem::file_size(file_path);
355+
model_config.size = size;
354356
} else {
355357
model_config.files.push_back(modelPath);
358+
auto size = std::filesystem::file_size(modelPath);
359+
model_config.size = size;
356360
}
357361
model_config.model = modelHandle;
358362
model_config.name = modelName.empty() ? model_config.name : modelName;

engine/services/download_service.cc

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <optional>
88
#include <ostream>
99
#include <utility>
10-
#include "download_service.h"
1110
#include "utils/format_utils.h"
1211
#include "utils/huggingface_utils.h"
1312
#include "utils/logging_utils.h"
@@ -148,11 +147,10 @@ cpp::result<bool, std::string> DownloadService::Download(
148147
std::string mode = "wb";
149148
if (std::filesystem::exists(download_item.localPath) &&
150149
download_item.bytes.has_value()) {
151-
curl_off_t existing_file_size = GetLocalFileSize(download_item.localPath);
152-
if (existing_file_size == -1) {
153-
CLI_LOG("Cannot get file size: " << download_item.localPath.string()
154-
<< " . Start download over!");
155-
} else {
150+
try {
151+
curl_off_t existing_file_size =
152+
std::filesystem::file_size(download_item.localPath);
153+
156154
CTL_INF("Existing file size: " << download_item.downloadUrl << " - "
157155
<< download_item.localPath.string()
158156
<< " - " << existing_file_size);
@@ -186,6 +184,9 @@ cpp::result<bool, std::string> DownloadService::Download(
186184
return false;
187185
}
188186
}
187+
} catch (const std::filesystem::filesystem_error& e) {
188+
CLI_LOG("Cannot get file size: "
189+
<< e.what() << download_item.localPath.string() << "\n");
189190
}
190191
}
191192

@@ -205,12 +206,12 @@ cpp::result<bool, std::string> DownloadService::Download(
205206
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
206207

207208
if (mode == "ab") {
208-
auto local_file_size = GetLocalFileSize(download_item.localPath);
209-
if (local_file_size != -1) {
210-
curl_easy_setopt(curl, CURLOPT_RESUME_FROM_LARGE,
211-
GetLocalFileSize(download_item.localPath));
212-
} else {
213-
CTL_ERR("Cannot get file size: " << download_item.localPath.string());
209+
try {
210+
curl_off_t local_file_size =
211+
std::filesystem::file_size(download_item.localPath);
212+
curl_easy_setopt(curl, CURLOPT_RESUME_FROM_LARGE, local_file_size);
213+
} catch (const std::filesystem::filesystem_error& e) {
214+
CTL_ERR("Cannot get file size: " << e.what() << '\n');
214215
}
215216
}
216217

@@ -225,23 +226,6 @@ cpp::result<bool, std::string> DownloadService::Download(
225226
curl_easy_cleanup(curl);
226227
return true;
227228
}
228-
229-
curl_off_t DownloadService::GetLocalFileSize(
230-
const std::filesystem::path& path) const {
231-
auto file = fopen(path.string().c_str(), "r");
232-
if (!file) {
233-
return -1;
234-
}
235-
236-
if (fseek64(file, 0, SEEK_END) != 0) {
237-
return -1;
238-
}
239-
240-
auto file_size = ftell64(file);
241-
fclose(file);
242-
return file_size;
243-
}
244-
245229
void DownloadService::WorkerThread() {
246230
while (!stop_flag_) {
247231
DownloadTask task;

engine/services/download_service.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ class DownloadService {
9090
const std::string& download_id,
9191
const DownloadItem& download_item) noexcept;
9292

93-
curl_off_t GetLocalFileSize(const std::filesystem::path& path) const;
94-
9593
std::shared_ptr<EventQueue> event_queue_;
9694

9795
CURLM* multi_handle_;

0 commit comments

Comments
 (0)