|
| 1 | +#include "imagemanager.hpp" |
| 2 | + |
| 3 | +namespace NImageManager { |
| 4 | + |
| 5 | + TStatus TImageManager::Install(Images image) { |
| 6 | + TStatus status; |
| 7 | + |
| 8 | + status = DownloadImage(image); |
| 9 | + if (status.Code == TStatus::Failed) { |
| 10 | + status.Error = "TImageManager::Install: " + status.Error; |
| 11 | + return status; |
| 12 | + } |
| 13 | + |
| 14 | + status = DearchiveImage(); |
| 15 | + return { TStatus::Success }; |
| 16 | + } |
| 17 | + |
| 18 | + // GOD FORBID US - curl requires C-styled function with this signature |
| 19 | + // TBD - move it to os.hpp - VoidZeroNull0 |
| 20 | + size_t WriteArchive(void* ptr, size_t size, size_t nmemb, FILE* stream) { |
| 21 | + return fwrite(ptr, size, nmemb, stream); |
| 22 | + } |
| 23 | + |
| 24 | + TStatus TImageManager::DownloadImage(Images image) { |
| 25 | + TStatus status; |
| 26 | + |
| 27 | + CURL* curl = curl_easy_init(); |
| 28 | + if (!curl) { |
| 29 | + status.Code = TStatus::Failed; |
| 30 | + status.Error = "TImageManager::DownloadImage:Could not init curl"; |
| 31 | + return status; |
| 32 | + } |
| 33 | + |
| 34 | + std::string url; |
| 35 | + CurrentImageName = ""; |
| 36 | + if (image == Images::Alpine) { |
| 37 | + url = "https://dl-cdn.alpinelinux.org/alpine/v3.21/releases/x86_64/alpine-minirootfs-3.21.3-x86_64.tar.gz"; |
| 38 | + CurrentImageName = "alpine"; |
| 39 | + } |
| 40 | + else { |
| 41 | + status.Code = TStatus::Failed; |
| 42 | + status.Error = "TImageManager::DownloadImage: Non-implemented image"; |
| 43 | + return status; |
| 44 | + } |
| 45 | + |
| 46 | + std::string path = MakeString() << ImagesLocation << CurrentImageName << ".tar.gz"; |
| 47 | + |
| 48 | + FILE* file = fopen(path.c_str(), "wb"); |
| 49 | + if (!file) { |
| 50 | + status.Code = TStatus::Failed; |
| 51 | + status.Error = "TImageManager::DownloadImage: Could not create file"; |
| 52 | + return status; |
| 53 | + } |
| 54 | + |
| 55 | + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 56 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteArchive); |
| 57 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); |
| 58 | + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 59 | + |
| 60 | + CURLcode result = curl_easy_perform(curl); |
| 61 | + if (result != CURLE_OK) { |
| 62 | + status.Code = TStatus::Failed; |
| 63 | + status.Error = MakeString() << "TImageManager::DownloadImage: Curl result error" << result; |
| 64 | + fclose(file); |
| 65 | + curl_easy_cleanup(curl); |
| 66 | + return status; |
| 67 | + } |
| 68 | + |
| 69 | + fclose(file); |
| 70 | + curl_easy_cleanup(curl); |
| 71 | + |
| 72 | + return { TStatus::Success }; |
| 73 | + } |
| 74 | + |
| 75 | + TStatus TImageManager::DearchiveImage() { |
| 76 | + TStatus status; |
| 77 | + |
| 78 | + struct archive* a = archive_read_new(); |
| 79 | + struct archive* ext = archive_write_disk_new(); |
| 80 | + struct archive_entry* entry; |
| 81 | + |
| 82 | + archive_read_support_format_tar(a); |
| 83 | + archive_read_support_filter_gzip(a); // для .gz |
| 84 | + |
| 85 | + std::string path = MakeString() << ImagesLocation << CurrentImageName << ".tar.gz"; |
| 86 | + if (archive_read_open_filename(a, path.c_str(), 10240)) { |
| 87 | + status.Code = TStatus::Failed; |
| 88 | + status.Error = "TImageManager::DownloadImage: Could not open archive"; |
| 89 | + return status; |
| 90 | + } |
| 91 | + |
| 92 | + path = MakeString() << ImagesLocation << "/" << CurrentImageName; |
| 93 | + while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { |
| 94 | + const char* currentFile = archive_entry_pathname(entry); |
| 95 | + std::string fullOutputPath = path + "/" + currentFile; |
| 96 | + archive_entry_set_pathname(entry, fullOutputPath.c_str()); |
| 97 | + |
| 98 | + archive_write_header(ext, entry); |
| 99 | + const void* buff; |
| 100 | + size_t size; |
| 101 | + la_int64_t offset; |
| 102 | + |
| 103 | + while (archive_read_data_block(a, &buff, &size, &offset) == ARCHIVE_OK) { |
| 104 | + archive_write_data_block(ext, buff, size, offset); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + archive_read_close(a); |
| 109 | + archive_read_free(a); |
| 110 | + archive_write_close(ext); |
| 111 | + archive_write_free(ext); |
| 112 | + |
| 113 | + return { TStatus::Success }; |
| 114 | + } |
| 115 | +} // namespace NImageManager |
0 commit comments