2828#include " Resolver.h"
2929#include " Session.h"
3030
31+ #include < algorithm>
3132#include < cerrno>
3233#include < climits>
3334#include < string_view>
3435
3536#include < unistd.h>
3637
37- #pragma GCC diagnostic push
38- #pragma GCC diagnostic ignored "-Wvla"
39-
4038namespace {
4139
4240 constexpr char DIR_SEPARATOR = ' /' ;
@@ -112,37 +110,6 @@ namespace {
112110 char * const * const argv;
113111 };
114112
115- // Util class to concatenate directory and file.
116- //
117- // Use this class to allocate buffer and assemble the content of it.
118- class PathBuilder {
119- public:
120- constexpr PathBuilder (const std::string_view &prefix, const std::string_view &file)
121- : prefix(prefix)
122- , file(file)
123- {
124- }
125-
126- [[nodiscard]] constexpr size_t length () const noexcept
127- {
128- return prefix.length () + file.length () + 2 ;
129- }
130-
131- constexpr void assemble (char * it) const noexcept
132- {
133- char * end = it + length ();
134-
135- it = el::array::copy (prefix.begin (), prefix.end (), it, end);
136- *it++ = DIR_SEPARATOR;
137- it = el::array::copy (file.begin (), file.end (), it, end);
138- *it = 0 ;
139- }
140-
141- private:
142- const std::string_view prefix;
143- const std::string_view file;
144- };
145-
146113 class PathResolver {
147114 public:
148115 struct Result {
@@ -157,9 +124,9 @@ namespace {
157124 public:
158125 explicit PathResolver (el::Resolver const &resolver);
159126
160- Result from_current_directory (const char * file);
161- Result from_path (const char * file, char * const * envp);
162- Result from_search_path (const char * file, const char *search_path);
127+ Result from_current_directory (std::string_view const & file);
128+ Result from_path (std::string_view const & file, char * const * envp);
129+ Result from_search_path (std::string_view const & file, const char *search_path);
163130
164131 PathResolver (PathResolver const &) = delete ;
165132 PathResolver (PathResolver &&) noexcept = delete ;
@@ -168,7 +135,7 @@ namespace {
168135 PathResolver &&operator =(PathResolver &&) noexcept = delete ;
169136
170137 private:
171- static bool contains_dir_separator (const char * candidate);
138+ static bool contains_dir_separator (std::string_view const & candidate);
172139
173140 private:
174141 el::Resolver const &resolver_;
@@ -180,9 +147,9 @@ namespace {
180147 , result_()
181148 { }
182149
183- PathResolver::Result PathResolver::from_current_directory (const char * file) {
150+ PathResolver::Result PathResolver::from_current_directory (std::string_view const & file) {
184151 // create absolute path to the given file.
185- if (nullptr == resolver_.realpath (file, result_)) {
152+ if (nullptr == resolver_.realpath (file. begin () , result_)) {
186153 return PathResolver::Result {nullptr , ENOENT };
187154 }
188155 // check if it's okay to execute.
@@ -196,7 +163,7 @@ namespace {
196163 return PathResolver::Result {nullptr , ENOENT };
197164 }
198165
199- PathResolver::Result PathResolver::from_path (const char * file, char * const * envp) {
166+ PathResolver::Result PathResolver::from_path (std::string_view const & file, char * const * envp) {
200167 if (contains_dir_separator (file)) {
201168 // the file contains a dir separator, it is treated as path.
202169 return from_current_directory (file);
@@ -218,7 +185,7 @@ namespace {
218185 }
219186 }
220187
221- PathResolver::Result PathResolver::from_search_path (const char * file, const char *search_path) {
188+ PathResolver::Result PathResolver::from_search_path (std::string_view const & file, const char *search_path) {
222189 if (contains_dir_separator (file)) {
223190 // the file contains a dir separator, it is treated as path.
224191 return from_current_directory (file);
@@ -230,11 +197,15 @@ namespace {
230197 continue ;
231198 }
232199 // create a path
233- const PathBuilder path_builder (path, std::string_view (file));
234- char buffer[path_builder.length ()];
235- path_builder.assemble (buffer);
200+ char candidate[PATH_MAX];
201+ {
202+ auto it = el::array::copy (path.begin (), path.end (), candidate, candidate + PATH_MAX);
203+ *it++ = DIR_SEPARATOR;
204+ it = el::array::copy (file.begin (), file.end (), it, candidate + PATH_MAX);
205+ *it = 0 ;
206+ }
236207 // check if it's okay to execute.
237- if (auto result = from_current_directory (buffer ); result) {
208+ if (auto result = from_current_directory (candidate ); result) {
238209 return result;
239210 }
240211 }
@@ -243,16 +214,14 @@ namespace {
243214 }
244215 }
245216
246- bool PathResolver::contains_dir_separator (const char *const candidate) {
247- for (auto it = candidate; *it != 0 ; ++it) {
248- if (*it == DIR_SEPARATOR) {
249- return true ;
250- }
251- }
252- return false ;
217+ bool PathResolver::contains_dir_separator (std::string_view const &candidate) {
218+ return std::find (candidate.begin (), candidate.end (), DIR_SEPARATOR) != candidate.end ();
253219 }
254220}
255221
222+ #pragma GCC diagnostic push
223+ #pragma GCC diagnostic ignored "-Wvla"
224+
256225namespace el {
257226
258227 Executor::Executor (el::Resolver const & resolver, el::Session const & session) noexcept
0 commit comments