#include <core/exceptions.hpp>
#include <fmtmacros.hpp>
#include <util/file.hpp>
+#include <util/filesystem.hpp>
#include <util/process.hpp>
#include <cstdlib>
# include "third_party/win32/mktemp.h"
#endif
-TemporaryFile::TemporaryFile(std::string_view path_prefix,
+namespace fs = util::filesystem;
+
+TemporaryFile::TemporaryFile(const fs::path& path_prefix,
std::string_view suffix)
- : path(FMT("{}{}XXXXXX{}", path_prefix, tmp_file_infix, suffix))
{
- core::ensure_dir_exists(Util::dir_name(path));
+ if (path_prefix.has_parent_path()) {
+ core::ensure_dir_exists(path_prefix.parent_path());
+ }
+ std::string path_template =
+ FMT("{}{}XXXXXX{}", path_prefix, tmp_file_infix, suffix);
#ifdef _WIN32
// MSVC lacks mkstemps() and Mingw-w64's implementation[1] is problematic, as
// it can reuse the names of recently-deleted files unless the caller
// [1]: <https://github.com/Alexpux/mingw-w64/blob/
// d0d7f784833bbb0b2d279310ddc6afb52fe47a46/mingw-w64-crt/misc/mkstemp.c>
- fd = Fd(bsd_mkstemps(&path[0], suffix.length()));
+ fd = Fd(bsd_mkstemps(&path_template[0], suffix.length()));
#else
- fd = Fd(mkstemps(&path[0], suffix.length()));
+ fd = Fd(mkstemps(&path_template[0], suffix.length()));
#endif
if (!fd) {
throw core::Fatal(
FMT("Failed to create temporary file for {}: {}", path, strerror(errno)));
}
+ path = path_template;
util::set_cloexec_flag(*fd);
#ifndef _WIN32
}
bool
-TemporaryFile::is_tmp_file(std::string_view path)
+TemporaryFile::is_tmp_file(const fs::path& path)
{
- return Util::base_name(path).find(tmp_file_infix) != std::string::npos;
+ return path.filename().string().find(tmp_file_infix) != std::string::npos;
}
-// 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.
//
#pragma once
-#include "Fd.hpp"
+#include <Fd.hpp>
+#include <filesystem>
#include <string>
#include <string_view>
// `path_prefix` is the base path. The resulting filename will be this path
// plus a unique string plus `suffix`. If `path_prefix` refers to a
// nonexistent directory the directory will be created if possible.
- TemporaryFile(std::string_view path_prefix, std::string_view suffix = ".tmp");
+ TemporaryFile(const std::filesystem::path& path_prefix,
+ std::string_view suffix = ".tmp");
TemporaryFile(TemporaryFile&& other) noexcept = default;
TemporaryFile& operator=(TemporaryFile&& other) noexcept = default;
- static bool is_tmp_file(std::string_view path);
+ static bool is_tmp_file(const std::filesystem::path& path);
// The resulting open file descriptor in read/write mode. Unset on error.
Fd fd;
// The actual filename. Empty on error.
- std::string path;
+ std::filesystem::path path;
};
// Delete any tmp files older than 1 hour right away.
if (file.mtime() + util::Duration(3600) < current_time
- && TemporaryFile::is_tmp_file(file.path().string())) {
+ && TemporaryFile::is_tmp_file(file.path())) {
util::remove(file.path().string());
continue;
}
incompressible_size += file.size_on_disk();
}
});
- } else if (!TemporaryFile::is_tmp_file(file.path().string())) {
+ } else if (!TemporaryFile::is_tmp_file(file.path())) {
incompressible_size += file.size_on_disk();
}