1 #include "ostree_http_repo.h"
7 #include <boost/property_tree/ini_parser.hpp>
9 #include "logging/logging.h"
10 #include "utilities/utils.h"
12 namespace pt = boost::property_tree;
14 bool OSTreeHttpRepo::LooksValid()
const {
15 if (FetchObject(
"config")) {
18 pt::read_ini((root_ /
"config").
string(), config);
19 if (config.get<std::string>(
"core.mode") !=
"archive-z2") {
20 LOG_WARNING <<
"OSTree repo is not in archive-z2 format";
25 }
catch (
const pt::ini_parser_error &error) {
26 LOG_WARNING <<
"Couldn't parse OSTree config file: " << (root_ /
"config").
string();
28 }
catch (
const pt::ptree_error &error) {
29 LOG_WARNING <<
"Could not find core.mode in OSTree config file";
37 OSTreeRef OSTreeHttpRepo::GetRef(
const std::string &refname)
const {
return OSTreeRef(*server_, refname); }
39 bool OSTreeHttpRepo::FetchObject(
const boost::filesystem::path &path)
const {
40 CURLcode err = CURLE_OK;
42 curlEasySetoptWrapper(easy_handle.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
43 server_->InjectIntoCurl(path.string(), easy_handle.get());
44 curlEasySetoptWrapper(easy_handle.get(), CURLOPT_WRITEFUNCTION, &OSTreeHttpRepo::curl_handle_write);
45 boost::filesystem::create_directories((root_ / path).parent_path());
46 std::string filename = (root_ / path).
string();
47 int fp = open(filename.c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
49 LOG_ERROR <<
"Failed to open file: " << filename;
52 curlEasySetoptWrapper(easy_handle.get(), CURLOPT_WRITEDATA, &fp);
53 curlEasySetoptWrapper(easy_handle.get(), CURLOPT_FAILONERROR,
true);
54 err = curl_easy_perform(easy_handle.get());
57 if (err == CURLE_HTTP_RETURNED_ERROR) {
60 remove((root_ / path).c_str());
62 }
else if (err != CURLE_OK) {
64 char *last_url =
nullptr;
65 curl_easy_getinfo(easy_handle.get(), CURLINFO_EFFECTIVE_URL, &last_url);
66 LOG_ERROR <<
"Failed to get object:" << curl_easy_strerror(err);
67 if (last_url !=
nullptr) {
68 LOG_ERROR <<
"Url: " << last_url;
70 remove((root_ / path).c_str());
77 size_t OSTreeHttpRepo::curl_handle_write(
void *buffer,
size_t size,
size_t nmemb,
void *userp) {
78 return static_cast<size_t>(write(*static_cast<int *>(userp), buffer, nmemb * size));