2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2016 Google LLC. All rights reserved.
|
2016-10-04 22:46:02 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/file/file_util.h>
|
2016-10-04 22:46:02 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2016-10-04 22:46:02 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <filesystem>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <absl/strings/str_format.h>
|
2016-10-04 22:46:02 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace {
|
2017-08-01 22:34:20 +00:00
|
|
|
// Create a temp file name using process id, thread id and current time.
|
2016-10-04 22:46:02 +00:00
|
|
|
std::string TempFileName() {
|
2023-12-01 17:32:19 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
const uint32_t process_id = static_cast<uint32_t>(GetCurrentProcessId());
|
|
|
|
#else
|
|
|
|
const uint32_t process_id = static_cast<uint32_t>(getpid());
|
|
|
|
#endif
|
|
|
|
const size_t thread_id =
|
|
|
|
std::hash<std::thread::id>{}(std::this_thread::get_id());
|
2018-08-06 23:11:57 +00:00
|
|
|
|
|
|
|
// We may need two or more temporary files in the same thread. There might be
|
|
|
|
// name collision if they are requested around the same time, e.g. called
|
|
|
|
// consecutively. Use a thread_local instance to avoid that.
|
2023-12-01 17:32:19 +00:00
|
|
|
static thread_local uint32_t instance_id = 0;
|
2018-08-06 23:11:57 +00:00
|
|
|
++instance_id;
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
return absl::StrFormat("packager-tempfile-%x-%zx-%x", process_id, thread_id,
|
|
|
|
instance_id);
|
2016-10-04 22:46:02 +00:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool TempFilePath(const std::string& temp_dir, std::string* temp_file_path) {
|
2023-12-01 17:32:19 +00:00
|
|
|
std::filesystem::path temp_dir_path;
|
|
|
|
|
2016-10-04 22:46:02 +00:00
|
|
|
if (temp_dir.empty()) {
|
2023-12-01 17:32:19 +00:00
|
|
|
temp_dir_path = std::filesystem::temp_directory_path();
|
2016-10-04 22:46:02 +00:00
|
|
|
} else {
|
2023-12-01 17:32:19 +00:00
|
|
|
temp_dir_path = std::filesystem::u8path(temp_dir);
|
2016-10-04 22:46:02 +00:00
|
|
|
}
|
2023-12-01 17:32:19 +00:00
|
|
|
|
|
|
|
*temp_file_path = (temp_dir_path / TempFileName()).string();
|
2016-10-04 22:46:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
std::string MakePathRelative(const std::filesystem::path& media_path,
|
|
|
|
const std::filesystem::path& parent_path) {
|
|
|
|
auto relative_path = std::filesystem::relative(media_path, parent_path);
|
|
|
|
if (relative_path.empty() || *relative_path.begin() == "..") {
|
|
|
|
// Not related.
|
|
|
|
relative_path = media_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return relative_path.lexically_normal().generic_string();
|
|
|
|
}
|
|
|
|
|
2016-10-04 22:46:02 +00:00
|
|
|
} // namespace shaka
|