diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2020-05-25 22:04:28 +0300 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2020-05-27 14:36:19 +0200 |
commit | e6470e37093084251b7ee60a904a78e54d13e31b (patch) | |
tree | 02845c95ff3c9a16808d2f5b602f8ae83ab3dc43 /libbuild2/script | |
parent | f5fba11159da64e53829a151a9d19d022ac63662 (diff) |
Use dir_name_view for script environment working and sandbox directories
Diffstat (limited to 'libbuild2/script')
-rw-r--r-- | libbuild2/script/run.cxx | 86 | ||||
-rw-r--r-- | libbuild2/script/run.hxx | 13 | ||||
-rw-r--r-- | libbuild2/script/script.cxx | 2 | ||||
-rw-r--r-- | libbuild2/script/script.hxx | 24 |
4 files changed, 81 insertions, 44 deletions
diff --git a/libbuild2/script/run.cxx b/libbuild2/script/run.cxx index 870a70f..a48421c 100644 --- a/libbuild2/script/run.cxx +++ b/libbuild2/script/run.cxx @@ -24,6 +24,35 @@ namespace build2 { namespace script { + string + diag_path (const path& d) + { + string r ("'"); + + r += stream_verb_map ().path < 1 + ? diag_relative (d) + : d.representation (); + + r += '\''; + return r; + } + + string + diag_path (const dir_name_view& dn) + { + string r; + if (dn.name != nullptr && *dn.name) + { + r += **dn.name; + r += ' '; + } + + assert (dn.path != nullptr); + + r += diag_path (*dn.path); + return r; + } + // Normalize a path. Also make the relative path absolute using the // specified directory unless it is already absolute. // @@ -260,7 +289,7 @@ namespace build2 path eop; if (rd.type == redirect_type::file) - eop = normalize (rd.file.path, env.work_dir, ll); + eop = normalize (rd.file.path, *env.work_dir.path, ll); else { eop = path (op + ".orig"); @@ -894,19 +923,19 @@ namespace build2 for (const auto& cl: c.cleanups) { const path& p (cl.path); - path np (normalize (p, env.work_dir, ll)); + path np (normalize (p, *env.work_dir.path, ll)); const string& ls (np.leaf ().string ()); bool wc (ls == "*" || ls == "**" || ls == "***"); const path& cp (wc ? np.directory () : np); - const dir_path& sd (env.sandbox_dir); + const dir_path* sd (env.sandbox_dir.path); - if (!sd.empty () && !cp.sub (sd)) + if (sd != nullptr && !cp.sub (*sd)) fail (ll) << (wc ? "wildcard" : p.to_directory () ? "directory" : "file") << " cleanup " << p << " is out of " - << env.sandbox_dir_name << " " << sd; + << diag_path (env.sandbox_dir); env.clean ({cl.type, move (np)}, false); } @@ -993,6 +1022,8 @@ namespace build2 // auto std_path = [&env, &ci, &li, &ll] (const char* n) -> path { + using std::to_string; + path p (n); // 0 if belongs to a single-line script, otherwise is the command line @@ -1078,7 +1109,7 @@ namespace build2 } case redirect_type::file: { - isp = normalize (in.file.path, env.work_dir, ll); + isp = normalize (in.file.path, *env.work_dir.path, ll); open_stdin (); break; @@ -1193,7 +1224,7 @@ namespace build2 // p = r.file.mode == redirect_fmode::compare ? std_path (what) - : normalize (r.file.path, env.work_dir, ll); + : normalize (r.file.path, *env.work_dir.path, ll); m |= r.file.mode == redirect_fmode::append ? fdopen_mode::at_end @@ -1378,21 +1409,20 @@ namespace build2 if (pre) { - const dir_path& wd (env.work_dir); - const dir_path& sd (env.sandbox_dir); + const dir_path& wd (*env.work_dir.path); + const dir_path* sd (env.sandbox_dir.path); auto fail = [] (const string& d) {throw runtime_error (d);}; - if (!sd.empty () && !from.sub (sd) && !force) - fail ("'" + from.representation () + - "' is out of " + env.sandbox_dir_name + " '" + - sd.string () + "'"); + if (sd != nullptr && !from.sub (*sd) && !force) + fail (diag_path (from) + " is out of " + + diag_path (env.sandbox_dir)); auto check_wd = [&wd, &env, fail] (const path& p) { if (wd.sub (path_cast<dir_path> (p))) - fail ("'" + p.string () + "' contains " + - env.work_dir_name + " '" + wd.string () + "'"); + fail (diag_path (p) + " contains " + + diag_path (env.work_dir)); }; check_wd (from); @@ -1404,7 +1434,7 @@ namespace build2 // if (cln->enabled) cln->move = !butl::entry_exists (to) && - (sd.empty () || to.sub (sd)); + (sd == nullptr || to.sub (*sd)); } else if (cln->enabled) { @@ -1469,20 +1499,18 @@ namespace build2 { if (pre) { - const dir_path& wd (env.work_dir); - const dir_path& sd (env.sandbox_dir); + const dir_path& wd (*env.work_dir.path); + const dir_path* sd (env.sandbox_dir.path); auto fail = [] (const string& d) {throw runtime_error (d);}; - if (!sd.empty () && !p.sub (sd) && !force) - fail ("'" + p.representation () + - "' is out of " + env.sandbox_dir_name + " '" + - sd.string () + "'"); + if (sd != nullptr && !p.sub (*sd) && !force) + fail (diag_path (p) + " is out of " + + diag_path (env.sandbox_dir)); if (wd.sub (path_cast<dir_path> (p))) - fail ("'" + p.string () + - "' contains " + env.work_dir_name + - " '" + wd.string () + "'"); + fail (diag_path (p) + " contains " + + diag_path (env.work_dir)); } }, @@ -1520,7 +1548,7 @@ namespace build2 builtin b (bf (r, c.arguments, move (ifd), move (ofd.out), move (efd), - env.work_dir, + *env.work_dir.path, bcs)); success = run_pipe (env, @@ -1573,7 +1601,7 @@ namespace build2 program (path (s, 1, s.size () - 1)); } else - program (env.work_dir / p); + program (*env.work_dir.path / p); } } catch (const invalid_path& e) @@ -1594,7 +1622,7 @@ namespace build2 pp, args.data (), {ifd.get (), -1}, process::pipe (ofd), {-1, efd.get ()}, - env.work_dir.string ().c_str ()); + env.work_dir.path->string ().c_str ()); ifd.reset (); ofd.out.reset (); @@ -1783,7 +1811,7 @@ namespace build2 clean (environment& env, const location& ll) { context& ctx (env.context); - const dir_path& wdir (env.work_dir); + const dir_path& wdir (*env.work_dir.path); // Note that we operate with normalized paths here. // diff --git a/libbuild2/script/run.hxx b/libbuild2/script/run.hxx index 3f73eed..477dd88 100644 --- a/libbuild2/script/run.hxx +++ b/libbuild2/script/run.hxx @@ -56,6 +56,19 @@ namespace build2 // void print_dir (diag_record&, const dir_path&, const location&); + + // Return the quoted path representation with the preserved trailing + // directory separator. The path is relative if the verbosity level is + // less than 3. + // + string + diag_path (const path&); + + // Same as above, but prepends the path with a name, if present. The path + // must be not NULL. + // + string + diag_path (const dir_name_view&); } } diff --git a/libbuild2/script/script.cxx b/libbuild2/script/script.cxx index 9e8780e..c85bfd3 100644 --- a/libbuild2/script/script.cxx +++ b/libbuild2/script/script.cxx @@ -633,7 +633,7 @@ namespace build2 const path& p (c.path); - if (!sandbox_dir.empty () && !p.sub (sandbox_dir)) + if (sandbox_dir.path != nullptr && !p.sub (*sandbox_dir.path)) { if (implicit) return; diff --git a/libbuild2/script/script.hxx b/libbuild2/script/script.hxx index 359bb36..b887df6 100644 --- a/libbuild2/script/script.hxx +++ b/libbuild2/script/script.hxx @@ -363,16 +363,14 @@ namespace build2 // using the rm or mv builtins will fail the script execution. Must be // an absolute path. // - const dir_path& work_dir; // @@ dir_path_name - const string& work_dir_name; // Directory name for diagnostics. + const dir_name_view work_dir; - // If non-empty, then any attempt to remove or move a filesystem entry - // outside this directory using an explicit cleanup or the rm/mv + // If path is not NULL, then any attempt to remove or move a filesystem + // entry outside this directory using an explicit cleanup or the rm/mv // builtins will fail the script execution, unless the --force option is - // specified for the builtin. Must be an absolute path, unless is empty. + // specified for the builtin. Must be an absolute path, unless is NULL. // - const dir_path& sandbox_dir; // @@ dir_path_name - const string& sandbox_dir_name; // Directory name for diagnostics. + const dir_name_view sandbox_dir; // Used by the script running machinery to create special files in it. // Must be an absolute path. @@ -396,8 +394,8 @@ namespace build2 environment (build2::context& ctx, const target_triplet& pt, - const dir_path& wd, const string& wn, - const dir_path& sd, const string& sn, + const dir_name_view& wd, + const dir_name_view& sd, const dir_path& td, bool tk, redirect&& i = redirect (redirect_type::pass), redirect&& o = redirect (redirect_type::pass), @@ -405,9 +403,7 @@ namespace build2 : context (ctx), platform (pt), work_dir (wd), - work_dir_name (wn), sandbox_dir (sd), - sandbox_dir_name (sn), temp_dir (td), temp_dir_keep (tk), in (move (i)), @@ -420,15 +416,15 @@ namespace build2 // environment (build2::context& ctx, const target_triplet& pt, - const dir_path& wd, const string& wn, + const dir_name_view& wd, const dir_path& td, bool tk, redirect&& i = redirect (redirect_type::pass), redirect&& o = redirect (redirect_type::pass), redirect&& e = redirect (redirect_type::pass)) : environment (ctx, pt, - wd, wn, - empty_dir_path, empty_string, + wd, + dir_name_view (), td, tk, move (i), move (o), move (e)) { |