aboutsummaryrefslogtreecommitdiff
path: root/bpkg/package.cxx
blob: 02cb49d9bc1ae2f8872eed998df4d5776202db23 (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
// file      : bpkg/package.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

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

#include <stdexcept> // invalid_argument

#include <bpkg/database>

using namespace std;

namespace bpkg
{
  // available_package_id
  //
  bool
  operator< (const available_package_id& x, const available_package_id& y)
  {
    int r (x.name.compare (y.name));

    if (r != 0)
      return r < 0;

    const auto& xv (x.version);
    const auto& yv (y.version);

    if (xv.epoch != yv.epoch)
      return xv.epoch < yv.epoch;

    r = xv.canonical_upstream.compare (yv.canonical_upstream);

    if (r != 0)
      return r < 0;

    return xv.revision < yv.revision;
  }

  // available_package
  //

  // Check if the package is available from the specified repository or
  // one of its 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)
  {
    const auto& cs (r->complements);

    for (const package_location& pl: ap->locations)
    {
      // First check the repository itself.
      //
      if (pl.repository.object_id () == r->name)
        return r;

      // Then check all the complements without loading them.
      //
      if (cs.find (pl.repository) != cs.end ())
        return pl.repository.load ();

      // Finally, load the complements and check them recursively.
      //
      for (const lazy_shared_ptr<repository>& cr: cs)
      {
        if (shared_ptr<repository> r = find (cr.load (), ap))
          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;
  }

  shared_ptr<available_package>
  filter_one (const shared_ptr<repository>& r, result<available_package>&& apr)
  {
    for (shared_ptr<available_package> ap: pointer_result (apr))
    {
      if (find (r, ap) != nullptr)
        return ap;
    }

    return nullptr;
  }

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

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

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