7 #include <packager/file/file_util.h>
18 #include <absl/strings/str_format.h>
23 std::string TempFileName() {
25 const uint32_t process_id =
static_cast<uint32_t
>(GetCurrentProcessId());
27 const uint32_t process_id =
static_cast<uint32_t
>(getpid());
29 const size_t thread_id =
30 std::hash<std::thread::id>{}(std::this_thread::get_id());
35 static thread_local uint32_t instance_id = 0;
38 return absl::StrFormat(
"packager-tempfile-%x-%zx-%x", process_id, thread_id,
43 bool TempFilePath(
const std::string& temp_dir, std::string* temp_file_path) {
44 std::filesystem::path temp_dir_path;
46 if (temp_dir.empty()) {
47 temp_dir_path = std::filesystem::temp_directory_path();
49 temp_dir_path = std::filesystem::u8path(temp_dir);
52 *temp_file_path = (temp_dir_path / TempFileName()).
string();
56 std::string MakePathRelative(
const std::filesystem::path& media_path,
57 const std::filesystem::path& parent_path) {
58 auto relative_path = std::filesystem::relative(media_path, parent_path);
59 if (relative_path.empty() || *relative_path.begin() ==
"..") {
61 relative_path = media_path;
64 return relative_path.lexically_normal().generic_string();
All the methods that are virtual are virtual for mocking.
bool TempFilePath(const std::string &temp_dir, std::string *temp_file_path)