aboutsummaryrefslogtreecommitdiff
path: root/bpkg/utility.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-09-07 15:59:37 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-09-07 15:59:37 +0200
commitb1078fdb9fac747c19dbdacd24c2838aae7d9f6b (patch)
tree3452bab97d91b063ac3cac2cb3d84ae184c21858 /bpkg/utility.cxx
parent8f94aaa067426a259f5396abdaf4945671799b5c (diff)
Implement cfg-create command
Diffstat (limited to 'bpkg/utility.cxx')
-rw-r--r--bpkg/utility.cxx80
1 files changed, 80 insertions, 0 deletions
diff --git a/bpkg/utility.cxx b/bpkg/utility.cxx
index c274563..1cd518b 100644
--- a/bpkg/utility.cxx
+++ b/bpkg/utility.cxx
@@ -6,6 +6,7 @@
#include <system_error>
+#include <butl/process>
#include <butl/filesystem>
#include <bpkg/types>
@@ -43,4 +44,83 @@ namespace bpkg
throw failed ();
}
}
+
+ bool
+ empty (const dir_path& d)
+ {
+ try
+ {
+ dir_iterator i (d);
+ return i == end (i);
+ }
+ catch (const system_error& e)
+ {
+ error << "unable to scan directory " << d << ": " << e.what ();
+ throw failed ();
+ }
+ }
+
+ void
+ mk (const dir_path& d)
+ {
+ try
+ {
+ try_mkdir (d);
+ }
+ catch (const system_error& e)
+ {
+ fail << "unable to create directory " << d << ": " << e.what ();
+ }
+ }
+
+ void
+ mk_p (const dir_path& d)
+ {
+ try
+ {
+ try_mkdir_p (d);
+ }
+ catch (const system_error& e)
+ {
+ fail << "unable to create directory " << d << ": " << e.what ();
+ }
+ }
+
+ void
+ rm_r (const dir_path& d, bool dir)
+ {
+ try
+ {
+ rmdir_r (d, dir);
+ }
+ catch (const system_error& e)
+ {
+ fail << "unable to remove " << (dir ? "" : "contents of ")
+ << "directory " << d << ": " << e.what ();
+ }
+ }
+
+ void
+ run (const char* const args[])
+ {
+ if (verb >= 2)
+ print_process (args);
+
+ try
+ {
+ process pr (args);
+
+ if (!pr.wait ())
+ throw failed (); // Assume the child issued diagnostics.
+ }
+ catch (const process_error& e)
+ {
+ error << "unable to execute " << args[0] << ": " << e.what ();
+
+ if (e.child ())
+ exit (1);
+
+ throw failed ();
+ }
+ }
}