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

#include <bpkg/fetch>

#include <fstream>

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

#include <bpkg/manifest-parser>

#include <bpkg/diagnostics>

using namespace std;
using namespace butl;

namespace bpkg
{
  template <typename M>
  static M
  fetch_file (const path& f)
  {
    if (!exists (f))
      fail << "file " << f << " does not exist";

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

      manifest_parser mp (ifs, f.string ());
      return M (mp);
    }
    catch (const manifest_parsing& e)
    {
      error (e.name, e.line, e.column) << e.description;
    }
    catch (const ifstream::failure&)
    {
      error << "unable to read from " << f;
    }

    throw failed ();
  }

  static const path repositories ("repositories");

  repository_manifests
  fetch_repositories (const dir_path& d)
  {
    return fetch_file<repository_manifests> (d / repositories);
  }

  repository_manifests
  fetch_repositories (const repository_location& rl)
  {
    assert (/*rl.remote () ||*/ rl.absolute ());

    return rl.remote ()
      ? repository_manifests ()
      : fetch_file<repository_manifests> (rl.path () / repositories);
  }

  static const path packages ("packages");

  package_manifests
  fetch_packages (const dir_path& d)
  {
    return fetch_file<package_manifests> (d / packages);
  }

  package_manifests
  fetch_packages (const repository_location& rl)
  {
    assert (/*rl.remote () ||*/ rl.absolute ());

    return rl.remote ()
      ? package_manifests ()
      : fetch_file<package_manifests> (rl.path () / packages);
  }
}