#include "ResultExtractor.hpp"
-#include "Context.hpp"
-#include "logging.hpp"
+#include "Util.hpp"
#include "third_party/nonstd/string_view.hpp"
{
assert(m_dest_fd);
- if (!write_fd(*m_dest_fd, data, size)) {
- throw Error(fmt::format("Failed to write to {}", m_dest_path));
+ try {
+ Util::write_fd(*m_dest_fd, data, size);
+ } catch (Error& e) {
+ throw Error(
+ fmt::format("Failed to write to {}: {}", m_dest_path, e.what()));
}
}
if (m_dest_file_type == FileType::stderr_output
|| (m_dest_file_type == FileType::dependency && !m_dest_path.empty())) {
m_dest_data.append(reinterpret_cast<const char*>(data), size);
- } else if (!write_fd(*m_dest_fd, data, size)) {
- throw Error(fmt::format("Failed to write to {}", m_dest_path));
+ } else {
+ try {
+ Util::write_fd(*m_dest_fd, data, size);
+ } catch (Error& e) {
+ throw Error(
+ fmt::format("Failed to write to {}: {}", m_dest_path, e.what()));
+ }
}
}
{
size_t start_pos = 0;
- if (m_rewrite_dependency_target) {
- size_t colon_pos = m_dest_data.find(':');
- if (colon_pos != std::string::npos) {
- if (!write_fd(*m_dest_fd,
- m_ctx.args_info.output_obj.data(),
- m_ctx.args_info.output_obj.length())) {
- throw Error(fmt::format("Failed to write to {}", m_dest_path));
+ try {
+ if (m_rewrite_dependency_target) {
+ size_t colon_pos = m_dest_data.find(':');
+ if (colon_pos != std::string::npos) {
+ Util::write_fd(*m_dest_fd,
+ m_ctx.args_info.output_obj.data(),
+ m_ctx.args_info.output_obj.length());
+ start_pos = colon_pos;
}
-
- start_pos = colon_pos;
}
- }
- if (!write_fd(*m_dest_fd,
- m_dest_data.data() + start_pos,
- m_dest_data.length() - start_pos)) {
- throw Error(fmt::format("Failed to write to {}", m_dest_path));
+ Util::write_fd(*m_dest_fd,
+ m_dest_data.data() + start_pos,
+ m_dest_data.length() - start_pos);
+ } catch (Error& e) {
+ throw Error(
+ fmt::format("Failed to write to {}: {}", m_dest_path, e.what()));
}
}
return i;
}
+void
+copy_fd(int fd_in, int fd_out)
+{
+ ssize_t n;
+ char buf[READ_BUFFER_SIZE];
+ while ((n = read(fd_in, buf, sizeof(buf))) != 0) {
+ if (n == -1 && errno != EINTR) {
+ break;
+ }
+ if (n > 0) {
+ write_fd(fd_out, buf, n);
+ }
+ }
+}
+
void
copy_file(const std::string& src, const std::string& dest, bool via_tmp_file)
{
}
}
- if (!copy_fd(*src_fd, *dest_fd)) {
- throw Error(strerror(errno));
- }
-
+ copy_fd(*src_fd, *dest_fd);
dest_fd.close();
src_fd.close();
return ENOMEM;
}
int err = 0;
- if (!write_fd(fd, buf, bytes_to_write)) {
+ try {
+ write_fd(fd, buf, bytes_to_write);
+ } catch (Error& e) {
err = errno;
}
lseek(fd, saved_pos, SEEK_SET);
}
}
- if (!write_fd(STDERR_FILENO, text_to_send->data(), text_to_send->length())) {
- throw Error("Failed to write to stderr");
+ try {
+ write_fd(STDERR_FILENO, text_to_send->data(), text_to_send->length());
+ } catch (Error& e) {
+ throw Error(fmt::format("Failed to write to stderr: {}", e.what()));
}
}
});
}
+void
+write_fd(int fd, const void* data, size_t size)
+{
+ ssize_t written = 0;
+ do {
+ ssize_t count =
+ write(fd, static_cast<const uint8_t*>(data) + written, size - written);
+ if (count == -1) {
+ if (errno != EAGAIN && errno != EINTR) {
+ throw Error(strerror(errno));
+ }
+ } else {
+ written += count;
+ }
+ } while (static_cast<size_t>(written) < size);
+}
+
void
write_file(const std::string& path,
const std::string& data,
size_t common_dir_prefix_length(nonstd::string_view dir,
nonstd::string_view path);
+// Copy all data from `fd_in` to `fd_out`. Throws `Error` on error.
+void copy_fd(int fd_in, int fd_out);
+
// Copy a file from `src` to `dest`. If via_tmp_file is true, `src` is copied to
// a temporary file and then renamed to dest. Throws `Error` on error.
void copy_file(const std::string& src,
// Throws Error on error.
void wipe_path(const std::string& path);
+// Write `size` bytes from `data` to `fd`. Throws `Error` on error.
+void write_fd(int fd, const void* data, size_t size);
+
// Write `data` to `path`. The file will be opened according to `open_mode`,
// which always will include `std::ios::out` even if not specified at the call
// site.
BOOL ret = FALSE;
if (length > 8192) {
TemporaryFile tmp_file(path);
- if (!write_fd(*tmp_file.fd, args, length)) {
- cc_log("Error writing @file; this command will probably fail: %s", args);
- }
+ Util::write_fd(*tmp_file.fd, args, length);
std::string atfile = fmt::format("\"@{}\"", tmp_file.path);
ret = CreateProcess(nullptr,
const_cast<char*>(atfile.c_str()),
throw FatalError(msg);
}
-bool
-write_fd(int fd, const void* buf, size_t size)
-{
- ssize_t written = 0;
- do {
- ssize_t count =
- write(fd, static_cast<const uint8_t*>(buf) + written, size - written);
- if (count == -1) {
- if (errno != EAGAIN && errno != EINTR) {
- return false;
- }
- } else {
- written += count;
- }
- } while (static_cast<size_t>(written) < size);
-
- return true;
-}
-
-// Copy all data from fd_in to fd_out.
-bool
-copy_fd(int fd_in, int fd_out)
-{
- ssize_t n;
- char buf[READ_BUFFER_SIZE];
- while ((n = read(fd_in, buf, sizeof(buf))) != 0) {
- if (n == -1 && errno != EINTR) {
- break;
- }
- if (n > 0 && !write_fd(fd_out, buf, n)) {
- return false;
- }
- }
-
- return true;
-}
-
// Return a static string with the current hostname.
const char*
get_hostname()
void fatal(const char* format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
-bool write_fd(int fd, const void* buf, size_t size);
-bool copy_fd(int fd_in, int fd_out);
const char* get_hostname();
void x_setenv(const char* name, const char* value);
void x_unsetenv(const char* name);