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

#include <bpkg/pkg-disfigure>

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

using namespace std;
using namespace butl;

namespace bpkg
{
  void
  pkg_disfigure (const dir_path& c,
                 transaction& t,
                 const shared_ptr<package>& p)
  {
    tracer trace ("pkg_disfigure");

    database& db (t.database ());

    // Calculate package's src_root and out_root.
    //
    assert (p->src_root); // Must be set since unpacked.
    assert (p->out_root); // Must be set since configured.

    dir_path src_root (p->src_root->absolute ()
                       ? *p->src_root
                       : c / *p->src_root);
    dir_path out_root (c / *p->out_root); // Always relative.

    level4 ([&]{trace << "src_root: " << src_root << ", "
                      << "out_root: " << out_root;});

    // Form the buildspec.
    //
    // Why do we need to specify src_root? While it shouldn't be
    // necessary for a completely configured package, we might
    // also be called to disfigure a partially configured one.
    //
    string bspec;

    if (src_root == out_root)
      bspec = "disfigure(" + out_root.string () + "/)";
    else
      bspec = "disfigure(" +
        src_root.string () + "/@" +
        out_root.string () + "/)";

    level4 ([&]{trace << "buildspec: " << bspec;});

    // Disfigure.
    //
    try
    {
      if (exists (out_root))
        run_b (bspec);

      // Make sure the out directory is gone unless it is the same as src.
      //
      if (out_root != src_root && exists (out_root))
        fail << "package output directory " << out_root << " still exists";
    }
    catch (const failed&)
    {
      // If we failed to disfigure the package, set it to the broken
      // state. The user can then try to clean things up with pkg-purge.
      //
      p->state = state::broken;
      db.update (p);
      t.commit ();

      info << "package " << p->name << " is now broken; "
           << "use 'pkg-purge' to remove";
      throw;
    }

    p->out_root = optional<dir_path> ();
    p->state = state::unpacked;

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

  void
  pkg_disfigure (const pkg_disfigure_options& o, cli::scanner& args)
  {
    tracer trace ("pkg_disfigure");

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

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

    string n (args.next ());

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

    shared_ptr<package> p (db.find<package> (n));

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

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

    level4 ([&]{trace << p->name << " " << p->version;});

    pkg_disfigure (c, t, p); // Commits the transaction.

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