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

#include <bpkg/pkg-fetch>

#include <bpkg/manifest>

#include <bpkg/fetch>
#include <bpkg/types>
#include <bpkg/package>
#include <bpkg/package-odb>
#include <bpkg/utility>
#include <bpkg/database>
#include <bpkg/diagnostics>

#include <bpkg/pkg-verify>

using namespace std;
using namespace butl;

namespace bpkg
{
  void
  pkg_fetch (const pkg_fetch_options& o, cli::scanner& args)
  {
    tracer trace ("pkg_fetch");

    dir_path c (o.directory ());
    level4 ([&]{trace << "configuration: " << c;});

    database db (open (c, trace));
    transaction t (db.begin ());
    session s;

    path a;
    bool purge;

    if (o.existing ())
    {
      if (!args.more ())
        fail << "archive path argument expected" <<
          info << "run 'bpkg help pkg-fetch' for more information";

      a = path (args.next ());

      if (!exists (a))
        fail << "archive file '" << a << "' does not exist";

      purge = o.purge ();
    }
    else
    {
      if (!args.more ())
        fail << "package name argument expected" <<
          info << "run 'bpkg help pkg-fetch' for more information";

      string n (args.next ());

      if (!args.more ())
        fail << "package version argument expected" <<
          info << "run 'bpkg help pkg-fetch' for more information";

      //@@ Same code as in pkg-status. Similar problem to repo_location;
      //   need a place for such utilities.
      //
      version v;
      {
        const char* s (args.next ());
        try
        {
          v = version (s);
        }
        catch (const invalid_argument& e)
        {
          fail << "invalid package version '" << s << "': " << e.what ();
        }
      }

      if (db.query_value<repository_count> () == 0)
        fail << "configuration " << c << " has no repositories" <<
          info << "use 'bpkg rep-add' to add a repository";

      if (db.query_value<available_package_count> () == 0)
        fail << "configuration " << c << " has no available packages" <<
          info << "use 'bpkg rep-fetch' to fetch available packages list";

      shared_ptr<available_package> p (
        db.find<available_package> (package_version_id (n, v)));

      if (p == nullptr)
        fail << "package " << n << " " << v << " is not available";

      // Pick a repository. Prefer local ones over the remote.
      //
      const package_location* pl (&p->locations.front ());

      for (const package_location& l: p->locations)
      {
        if (!l.repository.load ()->location.remote ())
        {
          pl = &l;
          break;
        }
      }

      if (verb > 1)
        text << "fetching " << pl->location.leaf () << " "
             << "from " << pl->repository->name ();

      a = fetch_archive (o, pl->repository->location, pl->location, c);
      purge = true;
    }

    level4 ([&]{trace << "package archive: " << a << ", purge: " << purge;});

    // Verify archive is a package and get its manifest.
    //
    package_manifest m (pkg_verify (a));
    level4 ([&]{trace << m.name << " " << m.version;});

    const auto& n (m.name);

    // See if this package already exists in this configuration.
    //
    if (shared_ptr<package> p = db.find<package> (n))
      fail << "package " << n << " already exists in configuration " << c <<
        info << "version: " << p->version << ", state: " << p->state;

    // Make the archive and configuration paths absolute and normalized.
    // If the archive is inside the configuration, use the relative path.
    // This way we can move the configuration around.
    //
    c.complete ().normalize ();
    a.complete ().normalize ();

    if (a.sub (c))
      a = a.leaf (c);

    // Add the package to the configuration.
    //
    shared_ptr<package> p (new package {
        move (m.name),
        move (m.version),
        state::fetched,
        move (a),
        purge,
        nullopt, // No source directory yet.
        false,
        nullopt  // No output directory yet.
     });

    db.persist (p);
    t.commit ();

    if (verb)
      text << "fetched " << p->name << " " << p->version;
  }
}