aboutsummaryrefslogtreecommitdiff
path: root/bdep/utility.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'bdep/utility.cxx')
-rw-r--r--bdep/utility.cxx73
1 files changed, 73 insertions, 0 deletions
diff --git a/bdep/utility.cxx b/bdep/utility.cxx
index 2600de9..45f924a 100644
--- a/bdep/utility.cxx
+++ b/bdep/utility.cxx
@@ -29,6 +29,57 @@ namespace bdep
const char* argv0;
dir_path exec_dir;
+ dir_path temp_dir;
+
+ auto_rmfile
+ tmp_file (const string& p)
+ {
+ assert (!temp_dir.empty ());
+ return auto_rmfile (temp_dir / path::traits::temp_name (p));
+ }
+
+ auto_rmdir
+ tmp_dir (const string& p)
+ {
+ assert (!temp_dir.empty ());
+ return auto_rmdir (temp_dir / dir_path (path::traits::temp_name (p)));
+ }
+
+ void
+ init_tmp (const dir_path& prj)
+ {
+ // Whether the project is required or optional depends on the command so
+ // if the project directory does not exist or it is not a bdep project
+ // directory, we simply create tmp in a system one and let the command
+ // complain if necessary.
+ //
+ dir_path d (prj.empty () ||
+ !exists (prj / bdep_dir, true /* ignore_error */)
+ ? dir_path::temp_path ("bdep")
+ : prj / bdep_dir / dir_path ("tmp"));
+
+ if (exists (d))
+ rm_r (d, true /* dir_itself */, 2);
+
+ mk (d); // We shouldn't need mk_p().
+
+ temp_dir = move (d);
+ }
+
+ void
+ clean_tmp (bool ignore_error)
+ {
+ if (!temp_dir.empty () && exists (temp_dir))
+ {
+ rm_r (temp_dir,
+ true /* dir_itself */,
+ 3,
+ ignore_error ? rm_error_mode::ignore : rm_error_mode::fail);
+
+ temp_dir.clear ();
+ }
+ }
+
bool
exists (const path& f, bool ignore_error)
{
@@ -101,6 +152,28 @@ namespace bdep
}
}
+ void
+ rm_r (const dir_path& d, bool dir, uint16_t v, rm_error_mode m)
+ {
+ if (verb >= v)
+ text << (dir ? "rmdir -r " : "rm -r ") << (dir ? d : d / dir_path ("*"));
+
+ try
+ {
+ rmdir_r (d, dir, m == rm_error_mode::ignore);
+ }
+ catch (const system_error& e)
+ {
+ bool w (m == rm_error_mode::warn);
+
+ (w ? warn : error) << "unable to remove " << (dir ? "" : "contents of ")
+ << "directory " << d << ": " << e;
+
+ if (!w)
+ throw failed ();
+ }
+ }
+
const char*
name_bpkg (const common_options& co)
{