aboutsummaryrefslogtreecommitdiff
path: root/build2/filesystem.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2016-07-08 16:13:58 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2016-07-08 16:13:58 +0200
commit86576cdd3c8d959ffe93f49b7744a99164f172ee (patch)
tree2430017b085cf7b59e335de8efb571d2bc46bfcf /build2/filesystem.cxx
parent6205a2d9eb7db0a25959ae34dc5406f228da92a5 (diff)
Move filesystem-related functions from context to new filesystem files
Diffstat (limited to 'build2/filesystem.cxx')
-rw-r--r--build2/filesystem.cxx99
1 files changed, 99 insertions, 0 deletions
diff --git a/build2/filesystem.cxx b/build2/filesystem.cxx
new file mode 100644
index 0000000..44b0ba4
--- /dev/null
+++ b/build2/filesystem.cxx
@@ -0,0 +1,99 @@
+// file : build2/filesystem.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <build2/filesystem>
+
+#include <build2/diagnostics>
+
+using namespace std;
+using namespace butl;
+
+namespace build2
+{
+ fs_status<mkdir_status>
+ mkdir (const dir_path& d, uint16_t v)
+ {
+ // We don't want to print the command if the directory already exists.
+ // This makes the below code a bit ugly.
+ //
+ mkdir_status ms;
+
+ try
+ {
+ ms = try_mkdir (d);
+ }
+ catch (const system_error& e)
+ {
+ if (verb >= v)
+ text << "mkdir " << d;
+
+ error << "unable to create directory " << d << ": " << e.what ();
+ throw failed ();
+ }
+
+ if (ms == mkdir_status::success)
+ {
+ if (verb >= v)
+ text << "mkdir " << d;
+ }
+
+ return ms;
+ }
+
+ fs_status<mkdir_status>
+ mkdir_p (const dir_path& d, uint16_t v)
+ {
+ // We don't want to print the command if the directory already exists.
+ // This makes the below code a bit ugly.
+ //
+ mkdir_status ms;
+
+ try
+ {
+ ms = try_mkdir_p (d);
+ }
+ catch (const system_error& e)
+ {
+ if (verb >= v)
+ text << "mkdir -p " << d;
+
+ error << "unable to create directory " << d << ": " << e.what ();
+ throw failed ();
+ }
+
+ if (ms == mkdir_status::success)
+ {
+ if (verb >= v)
+ text << "mkdir -p " << d;
+ }
+
+ return ms;
+ }
+
+ fs_status<butl::rmdir_status>
+ rmdir_r (const dir_path& d, bool dir, uint16_t v)
+ {
+ using namespace butl;
+
+ if (work.sub (d)) // Don't try to remove working directory.
+ return rmdir_status::not_empty;
+
+ if (!dir_exists (d))
+ return rmdir_status::not_exist;
+
+ if (verb >= v)
+ text << "rmdir -r " << d;
+
+ try
+ {
+ butl::rmdir_r (d, dir);
+ }
+ catch (const system_error& e)
+ {
+ fail << "unable to remove directory " << d << ": " << e.what ();
+ }
+
+ return rmdir_status::success;
+ }
+}