aboutsummaryrefslogtreecommitdiff
path: root/bpkg/package.cxx
blob: 1c98f42bb8ce0f3febeb3373794a83a80f875b25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// file      : bpkg/package.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/package.hxx>
#include <bpkg/package-odb.hxx>

#include <bpkg/database.hxx>

using namespace std;

namespace bpkg
{
  const version wildcard_version (0, "0", nullopt, 0);

  // available_package_id
  //
  bool
  operator< (const available_package_id& x, const available_package_id& y)
  {
    int r (x.name.compare (y.name));
    return r != 0 ? r < 0 : x.version < y.version;
  }

  // available_package
  //
  // Check if the package is available from the specified repository,
  // its prerequisite repositories, or one of their complements,
  // recursively. Return the first repository that contains the
  // package or NULL if none are.
  //
  static shared_ptr<repository>
  find (const shared_ptr<repository>& r,
        const shared_ptr<available_package>& ap,
        bool prereq = true)
  {
    const auto& ps (r->prerequisites);
    const auto& cs (r->complements);

    for (const package_location& pl: ap->locations)
    {
      const lazy_shared_ptr<repository>& lr (pl.repository);

      // First check the repository itself.
      //
      if (lr.object_id () == r->name)
        return r;

      // Then check all the complements and prerequisites without
      // loading them.
      //
      if (cs.find (lr) != cs.end () || (prereq && ps.find (lr) != ps.end ()))
        return lr.load ();

      // Finally, load the complements and prerequisites and check them
      // recursively.
      //
      for (const lazy_shared_ptr<repository>& cr: cs)
      {
        // Should we consider prerequisites of our complements as our
        // prerequisites? I'd say not.
        //
        if (shared_ptr<repository> r = find (cr.load (), ap, false))
          return r;
      }

      if (prereq)
      {
        for (const lazy_weak_ptr<repository>& pr: ps)
        {
          if (shared_ptr<repository> r = find (pr.load (), ap, false))
            return r;
        }
      }
    }

    return nullptr;
  }

  vector<shared_ptr<available_package>>
  filter (const shared_ptr<repository>& r, result<available_package>&& apr)
  {
    vector<shared_ptr<available_package>> aps;

    for (shared_ptr<available_package> ap: pointer_result (apr))
    {
      if (find (r, ap) != nullptr)
        aps.push_back (move (ap));
    }

    return aps;
  }

  pair<shared_ptr<available_package>, shared_ptr<repository>>
  filter_one (const shared_ptr<repository>& r, result<available_package>&& apr)
  {
    using result = pair<shared_ptr<available_package>, shared_ptr<repository>>;

    for (shared_ptr<available_package> ap: pointer_result (apr))
    {
      if (shared_ptr<repository> pr = find (r, ap))
        return result (move (ap), move (pr));
    }

    return result ();
  }

  // selected_package
  //
  string selected_package::
  version_string () const
  {
    return version != wildcard_version ? version.string () : "*";
  }

  ostream&
  operator<< (ostream& os, const selected_package& p)
  {
    os << (p.system () ? "sys:" : "") << p.name << "/" << p.version_string ();
    return os;
  }

  // state
  //
  string
  to_string (package_state s)
  {
    switch (s)
    {
    case package_state::transient:  return "transient";
    case package_state::broken:     return "broken";
    case package_state::fetched:    return "fetched";
    case package_state::unpacked:   return "unpacked";
    case package_state::configured: return "configured";
    }

    return string (); // Should never reach.
  }

  package_state
  to_package_state (const string& s)
  {
         if (s == "transient")  return package_state::transient;
    else if (s == "broken")     return package_state::broken;
    else if (s == "fetched")    return package_state::fetched;
    else if (s == "unpacked")   return package_state::unpacked;
    else if (s == "configured") return package_state::configured;
    else throw invalid_argument ("invalid package state '" + s + "'");
  }

  // substate
  //
  string
  to_string (package_substate s)
  {
    switch (s)
    {
    case package_substate::none:   return "none";
    case package_substate::system: return "system";
    }

    return string (); // Should never reach.
  }

  package_substate
  to_package_substate (const string& s)
  {
         if (s == "none")   return package_substate::none;
    else if (s == "system") return package_substate::system;
    else throw invalid_argument ("invalid package substate '" + s + "'");
  }

  // certificate
  //
  ostream&
  operator<< (ostream& os, const certificate& c)
  {
    using butl::operator<<;

    if (c.dummy ())
      os << c.name << " (dummy)";
    else
      os << c.name << ", \"" << c.organization << "\" <" << c.email << ">, "
         << c.start_date << " - " << c.end_date << ", " << c.fingerprint;

    return os;
  }
}