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

#include <bpkg/pkg-purge>

#include <memory> // shared_ptr

#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_purge (const pkg_purge_options& o, cli::scanner& args)
  {
    tracer trace ("pkg_purge");

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

    if (!args.more ())
      fail << "package name argument expected" <<
        info << "run 'bpkg help pkg-purge' 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;

    // Make sure the package is in a state from which it can be purged.
    //
    switch (p->state)
    {
    case state::fetched:
      {
        // If we have --keep, then this is a no-op. We could have
        // detected this and returned but we still want the normal
        // diagnostics. So instead the logic below takes care of
        // this situation.
        //
        break;
      }
    case state::unpacked:
      {
        if (o.keep () && !p->archive)
          fail << "package " << n << " has no archive to keep";

        break;
      }
    case state::broken:
      {
        if (!o.force ())
          fail << "broken package " << n << " can only be purged with --force";

        if (o.keep ())
          fail << "cannot keep broken package " << n;

        break;
      }
    default:
      {
        fail << p->state << " package " << n << " cannot be purged";
      }
    }

    // First clean up the package source directory.
    //
    if (p->purge_src)
    {
      dir_path d (p->src_root->absolute () ? *p->src_root : c / *p->src_root);

      if (p->state != state::broken)
      {
        try
        {
          if (exists (d)) // Don't complain if someone did our job for us.
            rm_r (d);

          p->src_root = optional<dir_path> ();
          p->purge_src = false;
        }
        catch (const failed&)
        {
          p->state = state::broken;
          db.update (p);
          t.commit ();

          info << "package " << n << " is now broken; "
               << "use 'pkg-purge --force' to remove";
          throw;
        }
      }
      else
      {
        // If we are broken, simply make sure the user cleaned things up
        // manually.
        //
        if (exists (d))
          fail << "broken package " << n << " source directory still exists" <<
            info << "remove " << d << " manually then re-run pkg-purge";
      }
    }

    // Also check the output directory of broken packages.
    //
    if (p->out_root)
    {
      assert (p->state == state::broken); // Can only be present if broken.

      dir_path d (c / *p->out_root); // Always relative.

      if (exists (d))
        fail << "broken package " << n << " output directory still exists" <<
          info << "remove " << d << " manually then re-run pkg-purge";
    }

    // Now the archive. Pretty much the same code as above but for a file.
    //
    if (p->purge_archive && !o.keep ())
    {
      path a (p->archive->absolute () ? *p->archive : c / *p->archive);

      if (p->state != state::broken)
      {
        try
        {
          if (exists (a))
            rm (a);

          p->archive = optional<path> ();
          p->purge_archive = false;
        }
        catch (const failed&)
        {
          p->state = state::broken;
          db.update (p);
          t.commit ();

          info << "package " << n << " is now broken; "
               << "use 'pkg-purge --force' to remove";
          throw;
        }
      }
      else
      {
        if (exists (a))
          fail << "broken package " << n << " archive still exists" <<
            info << "remove " << a << " manually then re-run pkg-purge";
      }
    }

    if (o.keep ())
    {
      if (p->state != state::fetched) // That no-op we were talking about.
      {
        p->state = state::fetched;
        db.update (p);
      }
    }
    else
      db.erase (p);

    t.commit ();

    if (verb)
        text << (o.keep () ? "keeping archive " : "purged ")
             << p->name << " " << p->version;
  }
}