-// Copyright (C) 2020 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2021 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
std::string add_exe_suffix(const std::string& program);
// Recreate a Windows command line string based on `argv`. If `prefix` is
-// non-empty, add it as the first argument.
-// If escape_backslashes is true, emit additional backslash for each backslash
-// which is not preceding '"' and is not at the end of argv[i] either.
+// non-empty, add it as the first argument. If `escape_backslashes` is true,
+// emit an additional backslash for each backslash that is not preceding '"' and
+// is not at the end of `argv[i]` either.
std::string argv_to_string(const char* const* argv,
const std::string& prefix,
bool escape_backslashes = false);
saved_orig_args = std::move(ctx.orig_args);
auto execv_argv = saved_orig_args.to_argv();
LOG("Executing {}", Util::format_argv_for_logging(execv_argv.data()));
- // Run execute_noreturn below after ctx and finalizer have been
+ // Execute the original command below after ctx and finalizer have been
// destructed.
}
}
umask(*original_umask);
}
auto execv_argv = saved_orig_args.to_argv();
- execute_noreturn(const_cast<char* const*>(execv_argv.data()),
- saved_temp_dir);
+ execute_noreturn(execv_argv.data(), saved_temp_dir);
throw Fatal(
"execute_noreturn of {} failed: {}", execv_argv[0], strerror(errno));
}
// Copyright (C) 2002 Andrew Tridgell
-// Copyright (C) 2011-2020 Joel Rosdahl and other contributors
+// Copyright (C) 2011-2021 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
#include "fmtmacros.hpp"
#ifdef _WIN32
+# include "Finalizer.hpp"
# include "Win32Util.hpp"
#endif
std::string args = Win32Util::argv_to_string(argv, sh);
std::string full_path = Win32Util::add_exe_suffix(path);
std::string tmp_file_path;
+
+ Finalizer tmp_file_remover([&tmp_file_path] {
+ if (!tmp_file_path.empty()) {
+ Util::unlink_tmp(tmp_file_path);
+ }
+ });
+
if (args.length() > 8192) {
TemporaryFile tmp_file(FMT("{}/cmd_args", temp_dir));
args = Win32Util::argv_to_string(argv + 1, sh, true);
Util::write_fd(*tmp_file.fd, args.data(), args.length());
args = FMT("{} @{}", full_path, tmp_file.path);
tmp_file_path = tmp_file.path;
- LOG("args from file {}", tmp_file.path);
+ LOG("Arguments from {}", tmp_file.path);
}
BOOL ret = CreateProcess(full_path.c_str(),
const_cast<char*>(args.c_str()),
full_path,
Win32Util::error_message(error),
error);
- if (!tmp_file_path.empty()) {
- Util::unlink_tmp(tmp_file_path);
- }
return -1;
}
WaitForSingleObject(pi.hProcess, INFINITE);
- if (!tmp_file_path.empty()) {
- Util::unlink_tmp(tmp_file_path);
- }
-
DWORD exitcode;
GetExitCodeProcess(pi.hProcess, &exitcode);
CloseHandle(pi.hProcess);
}
void
-execute_noreturn(const char* const* argv, const std::string& /* unused */)
+execute_noreturn(const char* const* argv, const std::string& /*temp_dir*/)
{
execv(argv[0], const_cast<char* const*>(argv));
}