1818 */
1919
2020#include " collect/SessionWrapper.h"
21-
2221#include " collect/Application.h"
22+ #include " report/libexec/Resolver.h"
23+ #include " report/libexec/Environment.h"
2324#include " report/wrapper/Environment.h"
25+ #include " libsys/Errors.h"
2426#include " libsys/Os.h"
2527#include " libsys/Path.h"
2628
@@ -85,31 +87,6 @@ namespace {
8587 return os;
8688 }
8789
88- rust::Result<fs::path> is_executable (const fs::path& path)
89- {
90- // Check if we can get the relpath of this file
91- std::error_code error_code;
92- auto result = fs::canonical (path, error_code);
93- if (error_code) {
94- return rust::Err (std::runtime_error (error_code.message ()));
95- }
96- // Check if the file is executable.
97- return (0 == access (result.c_str (), X_OK))
98- ? rust::Result<fs::path>(rust::Ok (result))
99- : rust::Result<fs::path>(rust::Err (std::runtime_error (" Not executable" )));
100- }
101-
102- rust::Result<fs::path> find_from_path (const std::list<fs::path>& paths, const fs::path& file)
103- {
104- for (const auto & path : paths) {
105- auto executable = is_executable (path / file);
106- if (executable.is_ok ()) {
107- return executable;
108- }
109- }
110- return rust::Err (std::runtime_error (" Not found" ));
111- }
112-
11390 rust::Result<std::list<fs::path>> list_dir (const fs::path& path)
11491 {
11592 std::list<fs::path> result;
@@ -129,42 +106,40 @@ namespace {
129106
130107namespace ic {
131108
132- rust::Result<Session::SharedPtr> WrapperSession::from (const flags::Arguments& args, sys::env::Vars&& environment )
109+ rust::Result<Session::SharedPtr> WrapperSession::from (const flags::Arguments& args, const char **envp )
133110 {
134111 const bool verbose = args.as_bool (ic::Application::VERBOSE).unwrap_or (false );
135- auto path = sys::os::get_path (environment);
136112 auto wrapper_dir = args.as_string (ic::Application::WRAPPER);
137113 auto wrappers = wrapper_dir
138114 .and_then <std::list<fs::path>>([](auto wrapper_dir) {
139115 return list_dir (wrapper_dir);
140116 });
141117
142- auto mapping_and_override = rust::merge (path, wrappers)
143- .map <std::map<std::string, std::string>>([](auto tuple) {
144- const auto & [paths, wrappers] = tuple;
118+ auto mapping_and_override = wrappers
119+ .map <std::map<std::string, std::string>>([&envp](auto wrappers) {
145120 // Find the executables with the same name from the path.
146- std::map<std::string, std::string> result = {};
121+ std::map<std::string, std::string> result;
122+ el::Resolver resolver;
147123 for (const auto & wrapper : wrappers) {
148124 auto basename = wrapper.filename ();
149- auto candidate = find_from_path (paths, basename );
125+ auto candidate = resolver. from_path (basename. c_str (), const_cast < char * const *>(envp) );
150126 candidate.on_success ([&result, &basename](auto candidate) {
151- result[basename] = candidate. string () ;
127+ result[basename] = candidate;
152128 });
153129 }
154130 return result;
155131 })
156- .map <std::tuple<std::map<std::string, std::string>, std::map<std::string, std::string>>>([&environment ](auto mapping) {
132+ .map <std::tuple<std::map<std::string, std::string>, std::map<std::string, std::string>>>([&envp ](auto mapping) {
157133 std::map<std::string, std::string> override ;
134+ el::Resolver resolver;
158135 // check if any environment variable is naming the real compiler
159136 for (auto implicit : IMPLICITS) {
160137 // find any of the implicit defined in environment.
161- if (auto env_it = environment. find ( implicit.env ); env_it != environment. end () ) {
138+ if (auto env_it = el::env::get_env_value (envp, implicit.env ); env_it != nullptr ) {
162139 // FIXME: it would be more correct if we shell-split the `env_it->second`
163140 // and use only the program name, but not the argument. But then how
164141 // to deal with the errors?
165- auto program = sys::Process::Builder (env_it->second )
166- .set_environment (environment)
167- .resolve_executable ();
142+ auto program = resolver.from_path (std::string_view (env_it), const_cast <char * const *>(envp));
168143
169144 // find the current mapping for the program the user wants to run.
170145 // and replace the program what the wrapper will call.
@@ -185,13 +160,13 @@ namespace ic {
185160 });
186161
187162 return rust::merge (wrapper_dir, mapping_and_override)
188- .map <Session::SharedPtr>([&verbose, &environment ](const auto & tuple) {
163+ .map <Session::SharedPtr>([&verbose, &envp ](const auto & tuple) {
189164 const auto & [const_wrapper_dir, const_mapping_and_override] = tuple;
190165 const auto & [const_mapping, const_override] = const_mapping_and_override;
191166 std::string wrapper_dir (const_wrapper_dir);
192167 std::map<std::string, std::string> mapping (const_mapping);
193168 std::map<std::string, std::string> override (const_override);
194- return std::make_shared<WrapperSession>(verbose, std::move (wrapper_dir), std::move (mapping), std::move (override ), environment );
169+ return std::make_shared<WrapperSession>(verbose, std::move (wrapper_dir), std::move (mapping), std::move (override ), sys::env::from (envp) );
195170 });
196171 }
197172
@@ -200,7 +175,7 @@ namespace ic {
200175 std::string&& wrapper_dir,
201176 std::map<std::string, std::string>&& mapping,
202177 std::map<std::string, std::string>&& override ,
203- const sys::env::Vars& environment)
178+ sys::env::Vars& & environment)
204179 : Session()
205180 , verbose_(verbose)
206181 , wrapper_dir_(wrapper_dir)
@@ -250,10 +225,11 @@ namespace ic {
250225
251226 rust::Result<sys::Process::Builder> WrapperSession::supervise (const std::vector<std::string_view>& command) const
252227 {
253- return rust::Ok (
254- sys::Process::Builder (command.front ())
228+ auto result = sys::Process::Builder (command.front ())
255229 .add_arguments (command.begin (), command.end ())
256- .set_environment (set_up_environment ()));
230+ .set_environment (set_up_environment ());
231+
232+ return rust::Ok (result);
257233 }
258234
259235 std::string WrapperSession::get_session_type () const
0 commit comments