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

#include <bpkg/rep-info>

#include <iostream>  // cout

#include <bpkg/manifest>
#include <bpkg/manifest-serializer>

#include <bpkg/auth>
#include <bpkg/fetch>
#include <bpkg/package>
#include <bpkg/diagnostics>
#include <bpkg/manifest-utility>

using namespace std;
using namespace butl;

namespace bpkg
{
  int
  rep_info (const rep_info_options& o, cli::scanner& args)
  {
    tracer trace ("rep_info");

    if (!args.more ())
      fail << "repository location argument expected" <<
        info << "run 'bpkg help rep-info' for more information";

    repository_location rl (parse_location (args.next ()));

    // Fetch everything we will need before printing anything. Ignore
    // unknown manifest entries unless we are dumping them. First fetch
    // the repositories list and authenticate the base's certificate.
    //
    pair<repository_manifests, string/*checksum*/> rmc (
      fetch_repositories (o, rl, !o.manifest ()));

    repository_manifests& rms (rmc.first);

    bool a (o.auth () != auth::none &&
            (o.auth () == auth::all || rl.remote ()));

    const optional<string> cert_pem (rms.back ().certificate);
    shared_ptr<const certificate> cert;

    if (a)
    {
      dir_path d (o.directory ());
      cert = authenticate_certificate (
        o,
        o.directory_specified () && d.empty () ? nullptr : &d,
        cert_pem,
        rl);

      a = !cert->dummy ();
    }

    // Now fetch the packages list and make sure it matches the repositories
    // we just fetched.
    //
    pair<package_manifests, string/*checksum*/> pmc (
      fetch_packages (o, rl, !o.manifest ()));

    package_manifests& pms (pmc.first);

    if (rmc.second != pms.sha256sum)
      fail << "repositories manifest file checksum mismatch for "
           << rl.canonical_name () <<
        info << "try again";

    if (a)
    {
      signature_manifest sm (fetch_signature (o, rl, true));

      if (sm.sha256sum != pmc.second)
        fail << "packages manifest file checksum mismatch for "
             << rl.canonical_name () <<
          info << "try again";

      dir_path d (o.directory ());
      assert (cert != nullptr);

      authenticate_repository (
        o,
        o.directory_specified () && d.empty () ? nullptr : &d,
        cert_pem,
        *cert,
        sm,
        rl);
    }

    // Now print.
    //
    bool all (!o.name () && !o.repositories () && !o.packages ());

    try
    {
      cout.exceptions (ostream::badbit | ostream::failbit);

      if (all || o.name ())
        cout << rl.canonical_name () << " " << rl << endl;

      // Repositories.
      //
      if (all || o.repositories ())
      {
        if (o.manifest ())
        {
          manifest_serializer s (cout, "STDOUT");
          rms.serialize (s);
        }
        else
        {
          for (const repository_manifest& rm: rms)
          {
            repository_role rr (rm.effective_role ());

            if (rr == repository_role::base)
              continue; // Entry for this repository.

            repository_location l (rm.location, rl); // Complete.
            const string& n (l.canonical_name ());

            switch (rr)
            {
            case repository_role::complement:
              cout << "complement " << n << " " << l << endl;
              break;
            case repository_role::prerequisite:
              cout << "prerequisite " << n << " " << l << endl;
              break;
            case repository_role::base:
              assert (false);
            }
          }
        }
      }

      // Packages.
      //
      if (all || o.packages ())
      {
        if (o.manifest ())
        {
          manifest_serializer s (cout, "STDOUT");
          pms.serialize (s);
        }
        else
        {
          for (const package_manifest& pm: pms)
            cout << pm.name << " " << pm.version << endl;
        }
      }
    }
    catch (const manifest_serialization& e)
    {
      fail << "unable to serialize manifest: " << e.description;
    }
    catch (const ostream::failure&)
    {
      fail << "unable to write to STDOUT";
    }

    return 0;
  }
}