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

#include <bpkg/rep-create>

#include <map>
#include <utility>  // pair, move()
#include <cassert>
#include <fstream>
#include <iostream>
#include <system_error>

#include <butl/process>
#include <butl/fdstream>
#include <butl/filesystem>

#include <bpkg/manifest>
#include <bpkg/manifest-parser>
#include <bpkg/manifest-serializer>

#include <bpkg/types>
#include <bpkg/utility>
#include <bpkg/diagnostics>

using namespace std;
using namespace butl;

namespace bpkg
{
  using package_key = pair<string, version>;
  using package_data = pair<path, package_manifest>;
  using package_map = map<package_key, package_data>;

  static void
  collect (package_map& map, const dir_path& d, const dir_path& root)
  try
  {
    tracer trace ("collect");

    for (const dir_entry& de: dir_iterator (d)) // system_error
    {
      path p (de.path ());

      // Ignore entries that start with a dot (think .git/).
      //
      if (p.string ().front () == '.')
      {
        level3 ([&]{trace << "skipping '" << p << "' in " << d;});
        continue;
      }

      switch (de.type ()) // Follow symlinks, system_error.
      {
      case entry_type::directory:
        {
          collect (map, path_cast<dir_path> (d / p), root);
          continue;
        }
      case entry_type::regular:
        break;
      default:
        fail << "unexpected entry '" << p << "' in directory " << d;
      }

      // Ignore well-known top-level files.
      //
      if (d == root)
      {
        if (p == path ("repositories") ||
            p == path ("packages"))
          continue;
      }

      path fp (d / p);

      // Figure out the package directory. Strip the top-level extension
      // and, as a special case, if the second-level extension is .tar,
      // strip that as well (e.g., .tar.bz2).
      //
      p = p.base ();
      if (const char* e = p.extension ())
      {
        if (e == string ("tar"))
          p = p.base ();
      }

      level4 ([&]{trace << "found package " << p << " in " << fp;});

      // Extract the manifest.
      //
      path mf (p / path ("manifest"));

      const char* args[] {
        "tar",
        "-xOf",                // -O/--to-stdout -- extract to STDOUT.
        fp.string ().c_str (),
        mf.string ().c_str (),
        nullptr};

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

      try
      {
        process pr (args, 0, -1); // Open pipe to stdout.

        try
        {
          ifdstream is (pr.in_ofd);
          is.exceptions (ifdstream::badbit | ifdstream::failbit);

          manifest_parser mp (is, mf.string ());
          package_manifest m (mp);

          // Verify package archive/directory is <name>-<version>.
          //
          {
            path ep (m.name + "-" + m.version.string ());

            if (p != ep)
              fail << "package archive/directory name mismatch in " << fp <<
                info << "extracted from archive '" << p << "'" <<
                info << "expected from manifest '" << ep << "'";
          }

          // Add package archive location relative to the repository root.
          //
          m.location = fp.leaf (root);

          package_key k (m.name, m.version); // Argument evaluation order.
          auto r (map.emplace (move (k), package_data (fp, move (m))));

          // Diagnose duplicates.
          //
          if (!r.second)
          {
            const package_manifest& m (r.first->second.second);

            fail << "duplicate package " << m.name << " " << m.version <<
              info << "first package archive is " << r.first->second.first <<
              info << "second package archive is " << fp;
          }
        }
        // Ignore these exceptions if the child process exited with
        // an error status since that's the source of the failure.
        //
        catch (const manifest_parsing& e)
        {
          if (pr.wait ())
            fail (e.name, e.line, e.column) << e.description <<
              info << "package archive " << fp;
        }
        catch (const ifdstream::failure&)
        {
          if (pr.wait ())
            fail << "unable to extract " << mf << " from " << fp;
        }

        if (!pr.wait ())
        {
          // While it is reasonable to assuming the child process issued
          // diagnostics, tar, specifically, doesn't mention the archive
          // name.
          //
          fail << fp << " does not appear to be a bpkg package";
        }
      }
      catch (const process_error& e)
      {
        error << "unable to execute " << args[0] << ": " << e.what ();

        if (e.child ())
          exit (1);

        throw failed ();
      }
    }
  }
  catch (const system_error& e)
  {
    error << "unable to scan directory " << d << ": " << e.what ();
    throw failed ();
  }

  void
  rep_create (const rep_create_options&, cli::scanner& args)
  try
  {
    tracer trace ("rep_create");

    dir_path d (args.more () ? args.next () : ".");
    if (d.empty ())
      throw invalid_path (d.string ());

    level4 ([&]{trace << "creating repository in " << d;});

    // Load the 'repositories' file to make sure it is there and
    // is valid.
    //
    path rf (d / path ("repositories"));

    if (!exists (rf))
      fail << "file " << rf << " does not exist";

    try
    {
      ifstream ifs;
      ifs.exceptions (ofstream::badbit | ofstream::failbit);
      ifs.open (rf.string ());

      manifest_parser mp (ifs, rf.string ());
      repository_manifests ms (mp);
      level3 ([&]{trace << ms.size () - 1 << " prerequisite repository(s)";});
    }
    catch (const manifest_parsing& e)
    {
      fail (e.name, e.line, e.column) << e.description;
    }
    catch (const ifstream::failure&)
    {
      fail << "unable to read from " << rf;
    }

    // While we could have serialized as we go along, the order of
    // packages will be pretty much random and not reproducible. By
    // collecting all the manifests in a map we get a sorted list.
    //
    package_map pm;
    collect (pm, d, d);

    // Serialize.
    //
    path p (d / path ("packages"));

    try
    {
      ofstream ofs;
      ofs.exceptions (ofstream::badbit | ofstream::failbit);
      ofs.open (p.string ());

      manifest_serializer s (ofs, p.string ());

      for (const auto& p: pm)
      {
        const package_manifest& m (p.second.second);

        if (verb)
          text << "adding " << m.name << " " << m.version;

        m.serialize (s);
      }

      s.next ("", ""); // The end.
    }
    catch (const manifest_serialization& e)
    {
      fail << "unable to save manifest: " << e.description;
    }
    catch (const ofstream::failure&)
    {
      fail << "unable to write to " << p;
    }

    if (verb)
    {
      d.complete ();
      d.normalize ();
      text << pm.size () << " package(s) in " << d;
    }
  }
  catch (const invalid_path& e)
  {
    fail << "invalid path: '" << e.path () << "'";
  }
}