aboutsummaryrefslogtreecommitdiff
path: root/bbot/utility.txx
blob: 383db741d4c89f6fc2de9bd1bc3440193c459007 (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
// file      : bbot/utility.txx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <butl/fdstream>

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

#include <bbot/diagnostics>

namespace bbot
{
  template <typename I, typename O, typename E, typename P, typename... A>
  process
  run_io_start (tracer& t,
                I&& in,
                O&& out,
                E&& err,
                const dir_path& cwd,
                P&& p,
                A&&... args)
  {
    try
    {
      return butl::process_start (
        [&t] (const char* c[], size_t n) {run_trace (t, c, n);},
        forward<I> (in),
        forward<O> (out),
        forward<E> (err),
        cwd,
        p,
        forward<A> (args)...);
    }
    catch (const process_error& e)
    {
      fail << "unable to execute " << p << ": " << e << endf;
    }
  }

  template <typename P>
  process_exit::code_type
  run_io_finish_exit (tracer&, process& pr, P&& p)
  {
    try
    {
      pr.wait ();

      if (!pr.exit->normal ())
        fail << "process " << p << " terminated abnormally: "
             << pr.exit->description ();

      return pr.exit->code ();
    }
    catch (const process_error& e)
    {
      fail << "unable to execute " << p << ": " << e << endf;
    }
  }

  template <typename P>
  inline void
  run_io_finish (tracer& t, process& pr, P&& p)
  {
    if (run_io_finish_exit (t, pr, p) != 0)
      fail << "process " << p << " terminated with non-zero exit code";
  }

  template <typename I, typename O, typename E, typename P, typename... A>
  inline process_exit::code_type
  run_io_exit (tracer& t, I&& in, O&& out, E&& err, const P& p, A&&... args)
  {
    process pr (run_io_start (t,
                              forward<I> (in),
                              forward<O> (out),
                              forward<E> (err),
                              dir_path (),
                              p,
                              forward<A> (args)...));

    return run_io_finish_exit (t, pr, p);
  }

  template <typename I, typename O, typename E, typename P, typename... A>
  inline void
  run_io (tracer& t, I&& in, O&& out, E&& err, const P& p, A&&... args)
  {
    process pr (run_io_start (t,
                              forward<I> (in),
                              forward<O> (out),
                              forward<E> (err),
                              dir_path (),
                              p,
                              forward<A> (args)...));
    run_io_finish (t, pr, p);
  }

  template <typename T>
  T
  parse_manifest (const path& f, const char* what, bool ignore_unknown)
  {
    using namespace butl;

    try
    {
      if (!file_exists (f))
        fail << what << " manifest file " << f << " does not exist";

      ifdstream ifs (f);
      manifest_parser p (ifs, f.string ());
      return T (p, ignore_unknown);
    }
    catch (const manifest_parsing& e)
    {
      fail << "invalid " << what << " manifest: "
           << f << ':' << e.line << ':' << e.column << ": " << e.description
           << endf;
    }
    catch (const io_error& e)
    {
      fail << "unable to read " << what << " manifest " << f << ": " << e
           << endf;
    }
    catch (const system_error& e) // EACCES, etc.
    {
      fail << "unable to access " << what << " manifest " << f << ": " << e
           << endf;
    }
  }

  template <typename T>
  void
  serialize_manifest (const T& m, const path& f, const char* what)
  {
    using namespace std;
    using namespace butl;

    try
    {
      ofdstream ofs (f, ios::binary);
      auto_rmfile arm (f); // Try to remove on failure ignoring errors.

      serialize_manifest (m, ofs, f.string (), what);

      ofs.close ();
      arm.cancel ();
    }
    catch (const system_error& e) // EACCES, etc.
    {
      fail << "unable to access " << what << " manifest " << f << ": " << e;
    }
  }

  template <typename T>
  void
  serialize_manifest (const T& m,
                      ostream& os,
                      const string& name,
                      const char* what)
  {
    using namespace std;
    using namespace butl;

    try
    {
      manifest_serializer s (os, name);
      m.serialize (s);
    }
    catch (const manifest_serialization& e)
    {
      fail << "invalid " << what << " manifest: " << e.description;
    }
    catch (const io_error& e)
    {
      fail << "unable to write " << what << " manifest " << name << ": " << e;
    }
  }
}