aboutsummaryrefslogtreecommitdiff
path: root/bpkg/rep-info.cxx
blob: 086a1420eb1d8b9a8e204126e762f66c9b581327 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// file      : bpkg/rep-info.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/rep-info.hxx>

#include <iostream>  // cout
#include <algorithm> // find_if()

#include <libbutl/sha256.mxx>              // sha256_to_fingerprint()
#include <libbutl/manifest-serializer.mxx>

#include <libbpkg/manifest.hxx>

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

#include <bpkg/rep-fetch.hxx>

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";

    if ((o.repositories_file_specified () || o.packages_file_specified ()) &&
        !o.manifest ())
      fail << (o.repositories_file_specified ()
               ? "--repositories-file"
               : "--packages-file")
           << " specified without --manifest" <<
        info << "run 'bpkg help rep-info' for more information";

    repository_location rl (
      parse_location (args.next (),
                      o.type_specified ()
                      ? optional<repository_type> (o.type ())
                      : nullopt));

    // Fetch everything we will need before printing anything. Ignore
    // unknown manifest entries unless we are dumping them.
    //
    dir_path d (o.directory ());
    rep_fetch_data rfd (
      rep_fetch (o,
                 o.directory_specified () && d.empty () ? nullptr : &d,
                 rl,
                 !o.manifest () /* ignore_unknow */,
                 o.deep () /* expand_values */));

    // Now print.
    //
    bool cert_info (o.cert_fingerprint ()  ||
                    o.cert_name ()         ||
                    o.cert_organization () ||
                    o.cert_email ());

    bool all (!o.name ()         &&
              !o.repositories () &&
              !o.packages ()     &&
              !cert_info);

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

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

      // Certificate.
      //
      if (all || cert_info)
      {
        shared_ptr<const certificate>& cert (rfd.certificate);
        const optional<string>& cert_pem (rfd.certificate_pem);

        if (cert_pem)
        {
          // Repository is signed. If we got the repository certificate as the
          // result of authentication then use it for printing as well.
          // Otherwise parse it's PEM representation.
          //
          if (cert == nullptr)
            cert = parse_certificate (o, *cert_pem, rl);
          else
            assert (!cert->dummy ());
        }
        else if (cert != nullptr)
        {
          // Reset the dummy certificate pointer that we got as a result of
          // the unsigned repository authentication.
          //
          assert (cert->dummy ());
          cert = nullptr;
        }

        if (all)
        {
          // Print in the human-friendly format (nothing for an unsigned
          // repository).
          //
          if (cert != nullptr)
            cout << "CN=" << cert->name << "/O=" << cert->organization <<
              "/" << cert->email << endl
                 << cert->fingerprint << endl;
        }
        else
        {
          // Print in the structured format if any of --cert-* options are
          // specified. Print empty lines for an unsigned repository.
          //
          if (o.cert_fingerprint ())
          {
            if (cert != nullptr)
              cout << cert->fingerprint;
            cout << endl;
          }

          if (o.cert_name ())
          {
            if (cert != nullptr)
              cout << "name:" << cert->name;
            cout << endl;
          }

          if (o.cert_organization ())
          {
            if (cert != nullptr)
              cout << cert->organization;
            cout << endl;
          }

          if (o.cert_email ())
          {
            if (cert != nullptr)
              cout << cert->email;
            cout << endl;
          }
        }
      }

      // Repositories.
      //
      if (all || o.repositories ())
      {
        if (o.manifest ())
        {
          // Merge repository manifest lists, adding the fragment value to
          // prerequisite/complement repository manifests, and picking the
          // latest base repository manifest.
          //
          vector<repository_manifest> rms;

          for (rep_fetch_data::fragment& fr: rfd.fragments)
          {
            for (repository_manifest& rm: fr.repositories)
            {
              if (rm.effective_role () != repository_role::base)
              {
                if (!fr.id.empty ())
                  rm.fragment = fr.id;

                rms.push_back (move (rm));
              }
            }
          }

          // Append the latest base repository manifest or an empty base if
          // there are no fragments.
          //
          rms.push_back (
            !rfd.fragments.empty ()
            ? find_base_repository (rfd.fragments.back ().repositories)
            : repository_manifest ());

          auto serialize = [&rms] (ostream& os, const string& name)
          {
            // Note: serializing without any extra repository_manifests info.
            //
            manifest_serializer s (os, name);

            for (const repository_manifest& rm: rms)
              rm.serialize (s);

            s.next ("", ""); // End of stream.
          };

          if (o.repositories_file_specified ())
          {
            const path& p (o.repositories_file ());

            try
            {
              // Let's set the binary mode not to litter the manifest file
              // with the carriage return characters on Windows.
              //
              ofdstream ofs (p, fdopen_mode::binary);
              serialize (ofs, p.string ());
              ofs.close ();
            }
            catch (const io_error& e)
            {
              fail << "unable to write to " << p << ": " << e;
            }
          }
          else
            serialize (cout, "stdout");
        }
        else
        {
          // Merge complements/prerequisites from all fragments "upgrading"
          // prerequisites to complements and preferring locations from the
          // latest fragments.
          //
          vector<repository_manifest> rms;

          for (rep_fetch_data::fragment& fr: rfd.fragments)
          {
            for (repository_manifest& rm: fr.repositories)
            {
              if (rm.effective_role () == repository_role::base)
                continue;

              repository_location l (rm.location, rl); // Complete.

              auto i (find_if (rms.begin (), rms.end (),
                               [&l] (const repository_manifest& i)
                               {
                                 return i.location.canonical_name () ==
                                   l.canonical_name ();
                               }));

              if (i == rms.end ())
              {
                rm.location = move (l);
                rms.push_back (move (rm));
              }
              else
              {
                if (rm.effective_role () == repository_role::complement)
                  i->role = rm.effective_role ();

                i->location = move (l);
              }
            }
          }

          for (const repository_manifest& rm: rms)
          {
            repository_role rr (rm.effective_role ());

            const repository_location& l (rm.location);
            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 ())
        {
          // Merge package manifest lists, adding the fragment.
          //
          vector<package_manifest> pms;

          for (rep_fetch_data::fragment& fr: rfd.fragments)
          {
            for (package_manifest& pm: fr.packages)
            {
              if (!fr.id.empty ())
                pm.fragment = fr.id;

              pms.push_back (move (pm));
            }
          }

          auto serialize = [&pms] (ostream& os, const string& name)
          {
            // Note: serializing without any extra package_manifests info.
            //
            manifest_serializer s (os, name);

            for (const package_manifest& pm: pms)
              pm.serialize (s);

            s.next ("", ""); // End of stream.
          };

          if (o.packages_file_specified ())
          {
            const path& p (o.packages_file ());

            try
            {
              // Let's set the binary mode not to litter the manifest file
              // with the carriage return characters on Windows.
              //
              ofdstream ofs (p, fdopen_mode::binary);
              serialize (ofs, p.string ());
              ofs.close ();
            }
            catch (const io_error& e)
            {
              fail << "unable to write to " << p << ": " << e;
            }
          }
          else
            serialize (cout, "stdout");

        }
        else
        {
          // Merge packages from all the fragments.
          //
          vector<package_manifest> pms;

          for (rep_fetch_data::fragment& fr: rfd.fragments)
          {
            for (package_manifest& pm: fr.packages)
            {
              auto i (find_if (pms.begin (), pms.end (),
                               [&pm] (const package_manifest& i)
                               {
                                 return i.name == pm.name &&
                                   i.version == pm.version;
                               }));

              if (i == pms.end ())
                pms.push_back (move (pm));
            }
          }

          // Separate package list from the general repository info.
          //
          cout << endl;

          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 io_error&)
    {
      fail << "unable to write to stdout";
    }

    return 0;
  }

  default_options_files
  options_files (const char*, const rep_info_options& o, const strings&)
  {
    // bpkg.options
    // bpkg-rep-info.options

    // If bpkg-rep-info operates in the configuration directory, then use it
    // as a search start directory.
    //
    optional<dir_path> start;

    // Let rep_info() complain later for invalid configuration directory.
    //
    try
    {
      dir_path d;

      if (o.directory_specified ())
        d = dir_path (o.directory ());
      else if (exists (bpkg_dir))
        d = current_dir;

      if (!d.empty ())
        start = move (normalize (d, "configuration"));
    }
    catch (const invalid_path&) {}

    return default_options_files {
      {path ("bpkg.options"), path ("bpkg-rep-info.options")},
      move (start)};
  }

  rep_info_options
  merge_options (const default_options<rep_info_options>& defs,
                 const rep_info_options& cmd)
  {
    return merge_default_options (
      defs,
      cmd,
      [] (const default_options_entry<rep_info_options>& e,
          const rep_info_options&)
      {
        if (e.options.directory_specified ())
          fail (e.file) << "--directory|-d in default options file";
      });
  }
}