Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
ostree_ref.cc
1 #include "ostree_ref.h"
2 
3 #include <algorithm>
4 #include <fstream>
5 #include <iomanip>
6 #include <iostream>
7 #include <iterator>
8 #include <utility>
9 
10 #include "logging/logging.h"
11 #include "utilities/utils.h"
12 
13 using std::string;
14 
15 OSTreeRef::OSTreeRef(const OSTreeRepo &repo, const string &ref_name) : ref_name_(ref_name) {
16  if (boost::filesystem::is_regular_file(repo.root() / "/refs/heads/" / ref_name)) {
17  std::ifstream f((repo.root() / "/refs/heads/" / ref_name).string(), std::ios::in | std::ios::binary);
18 
19  std::istream_iterator<char> start(f);
20  std::istream_iterator<char> end;
21  string res(start, end);
22 
23  // Strip trailing \n
24  while (!res.empty() && res[res.size() - 1] == '\n') {
25  res.resize(res.size() - 1);
26  }
27  ref_content_ = res;
28  is_valid = true;
29  } else {
30  is_valid = false;
31  }
32 }
33 
34 OSTreeRef::OSTreeRef(const TreehubServer &serve_repo, string ref_name)
35  : is_valid(true), ref_name_(std::move(ref_name)) {
36  CurlEasyWrapper curl_handle;
37  serve_repo.InjectIntoCurl(Url(), curl_handle.get());
38  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEFUNCTION, &OSTreeRef::curl_handle_write);
39  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEDATA, this);
40  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
41  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_FAILONERROR, true);
42  CURLcode rc = curl_easy_perform(curl_handle.get());
43  if (rc != CURLE_OK) {
44  is_valid = false;
45  }
46  ref_content_ = http_response_.str();
47 }
48 
49 void OSTreeRef::PushRef(const TreehubServer &push_target, CURL *curl_handle) const {
50  assert(IsValid());
51 
52  push_target.InjectIntoCurl(Url(), curl_handle);
53  curlEasySetoptWrapper(curl_handle, CURLOPT_WRITEFUNCTION, &OSTreeRef::curl_handle_write);
54  curlEasySetoptWrapper(curl_handle, CURLOPT_WRITEDATA, this);
55  curlEasySetoptWrapper(curl_handle, CURLOPT_PRIVATE, this); // Used by ostree_ref_from_curl
56 
57  curlEasySetoptWrapper(curl_handle, CURLOPT_POST, 1);
58  curlEasySetoptWrapper(curl_handle, CURLOPT_POSTFIELDSIZE, ref_content_.size());
59  curlEasySetoptWrapper(curl_handle, CURLOPT_COPYPOSTFIELDS, ref_content_.c_str());
60  curlEasySetoptWrapper(curl_handle, CURLOPT_VERBOSE, get_curlopt_verbose());
61 }
62 
63 bool OSTreeRef::IsValid() const { return is_valid; }
64 
65 string OSTreeRef::Url() const { return "refs/heads/" + ref_name_; }
66 
67 OSTreeHash OSTreeRef::GetHash() const { return OSTreeHash::Parse(ref_content_); }
68 
69 size_t OSTreeRef::curl_handle_write(void *buffer, size_t size, size_t nmemb, void *userp) {
70  auto *that = static_cast<OSTreeRef *>(userp);
71  assert(that);
72  that->http_response_.write(static_cast<const char *>(buffer), static_cast<std::streamsize>(size * nmemb));
73  return size * nmemb;
74 }
75 
76 // vim: set tabstop=2 shiftwidth=2 expandtab:
CurlEasyWrapper
Definition: utils.h:146
OSTreeHash
Definition: ostree_hash.h:10
TreehubServer
Definition: treehub_server.h:11
OSTreeRepo
A source repository to read OSTree objects from.
Definition: ostree_repo.h:19
OSTreeHash::Parse
static OSTreeHash Parse(const std::string &hash)
Parse an OSTree hash from a string.
Definition: ostree_hash.cc:7
OSTreeRef
Definition: ostree_ref.h:13