-// Copyright (C) 2020-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2023 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
#include <core/wincompat.hpp>
#include <util/TimePoint.hpp>
+#include <util/file.hpp>
#include <limits.h> // NOLINT: PATH_MAX is defined in limits.h
mtr_shutdown();
if (!m_args_info.output_obj.empty()) {
- Util::copy_file(m_tmp_trace_file, m_args_info.output_obj + ".ccache-trace");
+ util::copy_file(m_tmp_trace_file, m_args_info.output_obj + ".ccache-trace");
}
Util::unlink_tmp(m_tmp_trace_file);
}
return i;
}
-void
-copy_fd(int fd_in, int fd_out)
-{
- util::read_fd(fd_in, [=](nonstd::span<const uint8_t> data) {
- util::write_fd(fd_out, data.data(), data.size());
- });
-}
-
-void
-copy_file(const std::string& src, const std::string& dest, bool via_tmp_file)
-{
- Fd src_fd(open(src.c_str(), O_RDONLY | O_BINARY));
- if (!src_fd) {
- throw core::Error(
- FMT("Failed to open {} for reading: {}", src, strerror(errno)));
- }
-
- unlink(dest.c_str());
-
- Fd dest_fd;
- std::string tmp_file;
- if (via_tmp_file) {
- TemporaryFile temp_file(dest);
- dest_fd = std::move(temp_file.fd);
- tmp_file = temp_file.path;
- } else {
- dest_fd =
- Fd(open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
- if (!dest_fd) {
- throw core::Error(
- FMT("Failed to open {} for writing: {}", dest, strerror(errno)));
- }
- }
-
- copy_fd(*src_fd, *dest_fd);
- dest_fd.close();
- src_fd.close();
-
- if (via_tmp_file) {
- Util::rename(tmp_file, dest);
- }
-}
-
bool
create_dir(std::string_view dir)
{
// `dir` (a directory) and `path` (any path).
size_t common_dir_prefix_length(std::string_view dir, std::string_view path);
-// Copy all data from `fd_in` to `fd_out`. Throws `core::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 `core::Error` on error.
-void copy_file(const std::string& src,
- const std::string& dest,
- bool via_tmp_file = false);
-
// Create a directory if needed, including its parents if needed.
//
// Returns true if the directory exists or could be created, otherwise false.
}
LOG("Copying {} to {}", source, dest);
- Util::copy_file(source, dest, via_tmp_file);
+ util::copy_file(
+ source, dest, via_tmp_file ? util::ViaTmpFile::yes : util::ViaTmpFile::no);
}
void
#include <Fd.hpp>
#include <Logging.hpp>
#include <Stat.hpp>
+#include <TemporaryFile.hpp>
+#include <Util.hpp>
#include <Win32Util.hpp>
#include <fmtmacros.hpp>
#include <util/Bytes.hpp>
+#include <util/expected.hpp>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
namespace util {
+nonstd::expected<void, std::string>
+copy_file(const std::string& src,
+ const std::string& dest,
+ ViaTmpFile via_tmp_file)
+{
+ Fd src_fd(open(src.c_str(), O_RDONLY | O_BINARY));
+ if (!src_fd) {
+ return nonstd::make_unexpected(
+ FMT("Failed to open {} for reading: {}", src, strerror(errno)));
+ }
+
+ unlink(dest.c_str());
+
+ Fd dest_fd;
+ std::string tmp_file;
+ if (via_tmp_file == ViaTmpFile::yes) {
+ TemporaryFile temp_file(dest);
+ dest_fd = std::move(temp_file.fd);
+ tmp_file = temp_file.path;
+ } else {
+ dest_fd =
+ Fd(open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
+ if (!dest_fd) {
+ return nonstd::make_unexpected(
+ FMT("Failed to open {} for writing: {}", dest, strerror(errno)));
+ }
+ }
+ TRY(util::read_fd(*src_fd, [&](nonstd::span<const uint8_t> data) {
+ util::write_fd(*dest_fd, data.data(), data.size());
+ }));
+
+ dest_fd.close();
+ src_fd.close();
+
+ if (via_tmp_file == ViaTmpFile::yes) {
+ Util::rename(tmp_file, dest);
+ }
+
+ return {};
+}
+
void
create_cachedir_tag(const std::string& dir)
{
// --- Interface ---
enum class InPlace { yes, no };
+enum class ViaTmpFile { yes, no };
+
+// Copy a file from `src` to `dest`. If `via_tmp_file` is yes, `src` is copied
+// to a temporary file and then renamed to dest.
+nonstd::expected<void, std::string>
+copy_file(const std::string& src,
+ const std::string& dest,
+ ViaTmpFile via_tmp_file = ViaTmpFile::no);
void create_cachedir_tag(const std::string& dir);