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

#include <bpkg/pkg-unpack>

#include <butl/process>

#include <bpkg/manifest>

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

#include <bpkg/pkg-purge>
#include <bpkg/pkg-verify>

using namespace std;
using namespace butl;

namespace bpkg
{
  shared_ptr<selected_package>
  pkg_unpack (const dir_path& c,
              transaction& t,
              const dir_path& d,
              bool replace,
              bool purge)
  {
    tracer trace ("pkg_unpack");

    database& db (t.database ());
    tracer_guard tg (db, trace);

    if (!exists (d))
      fail << "package directory " << d << " does not exist";

    // Verify the directory is a package and get its manifest.
    //
    package_manifest m (pkg_verify (d, true));
    l4 ([&]{trace << d << ": " << m.name << " " << m.version;});

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

    if (ad.sub (ac))
      ad = ad.leaf (ac);

    // See if this package already exists in this configuration.
    //
    const string& n (m.name);
    shared_ptr<selected_package> p (db.find<selected_package> (n));

    if (p != nullptr)
    {
      bool s (p->state == package_state::fetched ||
              p->state == package_state::unpacked);

      if (!replace || !s)
      {
        diag_record dr (fail);

        dr << "package " << n << " already exists in configuration " << c <<
          info << "version: " << p->version_string ()
           << ", state: " << p->state
           << ", substate: " << p->substate;

        if (s) // Suitable state for replace?
          dr << info << "use 'pkg-unpack --replace|-r' to replace";
      }

      // Clean up the source directory and archive of the package we are
      // replacing. Once this is done, there is no going back. If things
      // go badly, we can't simply abort the transaction.
      //
      pkg_purge_fs (c, t, p);

      // Use the special root repository as the repository of this package.
      //
      p->version = move (m.version);
      p->state = package_state::unpacked;
      p->repository = repository_location ();
      p->src_root = move (ad);
      p->purge_src = purge;

      db.update (p);
    }
    else
    {
      p.reset (new selected_package {
        move (m.name),
        move (m.version),
        package_state::unpacked,
        package_substate::none,
        false,   // hold package
        false,   // hold version
        repository_location (), // Root repository.
        nullopt,    // No archive
        false,      // Don't purge archive.
        move (ad),
        purge,
        nullopt,    // No output directory yet.
        {}});       // No prerequisites captured yet.

      db.persist (p);
    }

    t.commit ();
    return p;
  }

  shared_ptr<selected_package>
  pkg_unpack (const common_options& co,
              const dir_path& c,
              transaction& t,
              const string& name)
  {
    tracer trace ("pkg_unpack");

    database& db (t.database ());
    tracer_guard tg (db, trace);

    shared_ptr<selected_package> p (db.find<selected_package> (name));

    if (p == nullptr)
      fail << "package " << name << " does not exist in configuration " << c;

    if (p->state != package_state::fetched)
      fail << "package " << name << " is " << p->state <<
        info << "expected it to be fetched";

    l4 ([&]{trace << *p;});

    assert (p->archive); // Should have archive in the fetched state.

    // If the archive path is not absolute, then it must be relative
    // to the configuration.
    //
    path a (p->archive->absolute () ? *p->archive : c / *p->archive);

    l4 ([&]{trace << "archive: " << a;});

    // Extract the package directory. Currently we always extract it
    // into the configuration directory. But once we support package
    // cache, this will need to change.
    //
    // Also, since we must have verified the archive during fetch,
    // here we can just assume what the resulting directory will be.
    //
    dir_path d (c / dir_path (p->name + '-' + p->version.string ()));

    if (exists (d))
      fail << "package directory " << d << " already exists";

    // What should we do if tar or something after it fails? Cleaning
    // up the package directory sounds like the right thing to do.
    //
    auto_rm_r arm (d);

    cstrings args;

    // See if we need to decompress.
    //
    {
      string e (a.extension ());

      if      (e == "gz")    args.push_back ("gzip");
      else if (e == "bzip2") args.push_back ("bzip2");
      else if (e == "xz")    args.push_back ("xz");
      else if (e != "tar")
        fail << "unknown compression method in package " << a;
    }

    size_t i (0); // The tar command line start.
    if (!args.empty ())
    {
      args.push_back ("-dc");
      args.push_back (a.string ().c_str ());
      args.push_back (nullptr);
      i = args.size ();
    }

    args.push_back (co.tar ().string ().c_str ());

    // Add extra options.
    //
    for (const string& o: co.tar_option ())
      args.push_back (o.c_str ());

    // -C/--directory -- change to directory.
    //
    args.push_back ("-C");
    args.push_back (c.string ().c_str ());

    // An archive name that has a colon in it specifies a file or device on a
    // remote machine. That makes it impossible to use absolute Windows paths
    // unless we add the --force-local option. Note that BSD tar doesn't
    // support this option.
    //
#ifdef _WIN32
    args.push_back ("--force-local");
#endif

    args.push_back ("-xf");
    args.push_back (i == 0 ? a.string ().c_str () : "-");
    args.push_back (nullptr);
    args.push_back (nullptr); // Pipe end.

    size_t what;
    try
    {
      process_path dpp;
      process_path tpp;

      process dpr;
      process tpr;

      if (i != 0)
        dpp = process::path_search (args[what = 0]);

      tpp = process::path_search (args[what = i]);

      if (verb >= 2)
        print_process (args);

      if (i != 0)
      {
        dpr = process (dpp, &args[what = 0], 0, -1);
        tpr = process (tpp, &args[what = i], dpr);
      }
      else
        tpr = process (tpp, &args[what = 0]);

      // While it is reasonable to assuming the child process issued
      // diagnostics, tar, specifically, doesn't mention the archive name.
      //
      if (!(what = i, tpr.wait ()) ||
          !(what = 0, dpr.wait ()))
        fail << "unable to extract package archive " << a;
    }
    catch (const process_error& e)
    {
      error << "unable to execute " << args[what] << ": " << e;

      if (e.child)
        exit (1);

      throw failed ();
    }

    p->src_root = d.leaf (); // For now assuming to be in configuration.
    p->purge_src = true;

    p->state = package_state::unpacked;

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

    arm.cancel ();

    return p;
  }

  int
  pkg_unpack (const pkg_unpack_options& o, cli::scanner& args)
  {
    tracer trace ("pkg_unpack");

    if (o.replace () && !o.existing ())
      fail << "--replace|-r can only be specified with --existing|-e";

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

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

    shared_ptr<selected_package> p;

    if (o.existing ())
    {
      // The package directory case.
      //
      if (!args.more ())
        fail << "package directory argument expected" <<
          info << "run 'bpkg help pkg-unpack' for more information";

      p = pkg_unpack (
        c, t, dir_path (args.next ()), o.replace (), o.purge ());
    }
    else
    {
      // The package name case.
      //
      if (!args.more ())
        fail << "package name argument expected" <<
          info << "run 'bpkg help pkg-unpack' for more information";

      p = pkg_unpack (o, c, t, args.next ());
    }

    if (verb)
      text << "unpacked " << *p;

    return 0;
  }
}