Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
ostree_http_repo.cc
1 #include "ostree_http_repo.h"
2 
3 #include <fcntl.h>
4 
5 #include <string>
6 
7 #include <boost/property_tree/ini_parser.hpp>
8 
9 namespace pt = boost::property_tree;
10 
11 bool OSTreeHttpRepo::LooksValid() const {
12  if (FetchObject("config")) {
13  pt::ptree config;
14  try {
15  pt::read_ini((root_ / "config").string(), config);
16  if (config.get<std::string>("core.mode") != "archive-z2") {
17  LOG_WARNING << "OSTree repo is not in archive-z2 format";
18  return false;
19  }
20  return true;
21 
22  } catch (const pt::ini_parser_error &error) {
23  LOG_WARNING << "Couldn't parse OSTree config file: " << (root_ / "config").string();
24  return false;
25  } catch (const pt::ptree_error &error) {
26  LOG_WARNING << "Could not find core.mode in OSTree config file";
27  return false;
28  }
29  } else {
30  return false;
31  }
32 }
33 
34 OSTreeRef OSTreeHttpRepo::GetRef(const std::string &refname) const { return OSTreeRef(*server_, refname); }
35 
36 bool OSTreeHttpRepo::FetchObject(const boost::filesystem::path &path) const {
37  CURLcode err = CURLE_OK;
38  server_->InjectIntoCurl(path.string(), easy_handle_.get());
39  boost::filesystem::create_directories((root_ / path).parent_path());
40  std::string filename = (root_ / path).string();
41  int fp = open(filename.c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
42  if (fp == -1) {
43  LOG_ERROR << "Failed to open file: " << filename;
44  return false;
45  }
46  curlEasySetoptWrapper(easy_handle_.get(), CURLOPT_WRITEDATA, &fp);
47  err = curl_easy_perform(easy_handle_.get());
48  close(fp);
49 
50  if (err == CURLE_HTTP_RETURNED_ERROR) {
51  // http error (error code >= 400)
52  // verbose mode will display the details
53  remove((root_ / path).c_str());
54  return false;
55  } else if (err != CURLE_OK) {
56  // other unexpected error
57  char *last_url = nullptr;
58  curl_easy_getinfo(easy_handle_.get(), CURLINFO_EFFECTIVE_URL, &last_url);
59  LOG_ERROR << "Failed to get object:" << curl_easy_strerror(err);
60  if (last_url != nullptr) {
61  LOG_ERROR << "Url: " << last_url;
62  }
63  remove((root_ / path).c_str());
64  return false;
65  }
66 
67  return true;
68 }
69 
70 size_t OSTreeHttpRepo::curl_handle_write(void *buffer, size_t size, size_t nmemb, void *userp) {
71  return static_cast<size_t>(write(*static_cast<int *>(userp), buffer, nmemb * size));
72 }
73 
74 // vim: set tabstop=2 shiftwidth=2 expandtab:
OSTreeRef
Definition: ostree_ref.h:13