sbuild  1.7.2
run-parts.h
1 /* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_RUN_PARTS_H
20 #define SBUILD_RUN_PARTS_H
21 
22 #include <sbuild/custom-error.h>
23 #include <sbuild/environment.h>
24 #include <sbuild/types.h>
25 
26 #include <set>
27 #include <string>
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 
32 namespace sbuild
33 {
34 
38  class run_parts
39  {
40  public:
43  {
46  EXEC,
47  PIPE,
48  DUP,
49  POLL,
51  };
52 
55 
70  run_parts (const std::string& directory,
71  bool lsb_mode = true,
72  bool abort_on_error = true,
73  mode_t umask = 022);
74 
76  ~run_parts ();
77 
83  bool
84  get_verbose () const;
85 
91  void
92  set_verbose (bool verbose);
93 
99  bool
100  get_reverse () const;
101 
107  void
108  set_reverse (bool reverse);
109 
119  int
120  run(const string_list& command,
121  const environment& env);
122 
130  template <class charT, class traits>
131  friend
132  std::basic_ostream<charT,traits>&
133  operator << (std::basic_ostream<charT,traits>& stream,
134  const run_parts& rhs)
135  {
136  if (!rhs.reverse)
137  {
138  for (const auto& program : rhs.programs)
139  stream << program << '\n';
140  }
141  else
142  {
143  for (program_set::const_reverse_iterator pos = rhs.programs.rbegin();
144  pos != rhs.programs.rend();
145  ++pos)
146  stream << *pos << '\n';
147  }
148  return stream;
149  }
150 
151  private:
161  int
162  run_child(const std::string& file,
163  const string_list& command,
164  const environment& env);
165 
174  void
175  wait_for_child (pid_t pid,
176  int& child_status);
177 
179  typedef std::set<std::string> program_set;
180 
182  bool lsb_mode;
186  mode_t umask;
188  bool verbose;
190  bool reverse;
192  std::string directory;
194  program_set programs;
195  };
196 
197 }
198 
199 #endif /* SBUILD_RUN_PARTS_H */
200 
201 /*
202  * Local Variables:
203  * mode:C++
204  * End:
205  */
int run_child(const std::string &file, const string_list &command, const environment &env)
Run the command specified by file (an absolute pathname), using command and env as the argv and envir...
Definition: run-parts.cc:158
Debian source builder components.
Definition: ctty.cc:31
void set_reverse(bool reverse)
Set the script execution order.
Definition: run-parts.cc:110
void set_verbose(bool verbose)
Set the verbosity level.
Definition: run-parts.cc:98
program_set programs
The list of scripts to run.
Definition: run-parts.h:194
Wait for child failed.
Definition: run-parts.h:45
bool abort_on_error
Whether to abort on script execution error.
Definition: run-parts.h:184
Container of environment variables.
Definition: environment.h:38
int run(const string_list &command, const environment &env)
Run all scripts in the specified directory.
Definition: run-parts.cc:116
bool get_verbose() const
Get the verbosity level.
Definition: run-parts.cc:92
bool lsb_mode
The LSB mode for allowed filenames.
Definition: run-parts.h:182
run_parts(const std::string &directory, bool lsb_mode=true, bool abort_on_error=true, mode_t umask=022)
The constructor.
Definition: run-parts.cc:53
bool get_reverse() const
Get the script execution order.
Definition: run-parts.cc:104
error_code
Error codes.
Definition: run-parts.h:42
Failed to create pipe.
Definition: run-parts.h:47
Failed to poll file descriptor.
Definition: run-parts.h:49
bool verbose
Verbose logging.
Definition: run-parts.h:188
std::vector< std::string > string_list
A string vector.
Definition: types.h:38
Custom error.
Definition: custom-error.h:31
Failed to execute.
Definition: run-parts.h:46
Failed to read file descriptor.
Definition: run-parts.h:50
custom_error< error_code > error
Exception type.
Definition: run-parts.h:54
mode_t umask
The umask to run scripts with.
Definition: run-parts.h:186
Run all scripts or programs within a directory.
Definition: run-parts.h:38
bool reverse
Execute scripts in reverse order.
Definition: run-parts.h:190
Failed to duplicate file descriptor.
Definition: run-parts.h:48
~run_parts()
The destructor.
Definition: run-parts.cc:87
void wait_for_child(pid_t pid, int &child_status)
Wait for a child process to complete, and check its exit status.
Definition: run-parts.cc:347
std::string directory
The directory to run scripts from.
Definition: run-parts.h:192
std::set< std::string > program_set
A sorted set of filenames to use.
Definition: run-parts.h:179
Failed to fork child.
Definition: run-parts.h:44