mirror of
https://github.com/shaka-project/shaka-packager.git
synced 2026-04-02 11:20:08 +00:00
This work was done over ~80 individual commits in the `cmake` branch, which are now being merged back into `main`. As a roll-up commit, it is too big to be reviewable, but each change was reviewed individually in context of the `cmake` branch. After this, the `cmake` branch will be renamed `cmake-porting-history` and preserved. --------- Co-authored-by: Geoff Jukes <geoffjukes@users.noreply.github.com> Co-authored-by: Bartek Zdanowski <bartek.zdanowski@gmail.com> Co-authored-by: Carlos Bentzen <cadubentzen@gmail.com> Co-authored-by: Dennis E. Mungai <2356871+Brainiarc7@users.noreply.github.com> Co-authored-by: Cosmin Stejerean <cstejerean@gmail.com> Co-authored-by: Carlos Bentzen <carlos.bentzen@bitmovin.com> Co-authored-by: Cosmin Stejerean <cstejerean@meta.com> Co-authored-by: Cosmin Stejerean <cosmin@offbytwo.com>
50 lines
1.6 KiB
C++
50 lines
1.6 KiB
C++
// Copyright 2022 Google LLC. All rights reserved.
|
|
//
|
|
// 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
|
|
|
|
#include <packager/file/file_test_util.h>
|
|
|
|
#include <filesystem>
|
|
|
|
namespace shaka {
|
|
|
|
std::string generate_unique_temp_path() {
|
|
// Generate a unique name for a temporary file, using standard library
|
|
// routines, to avoid a circular dependency on any of our own code for
|
|
// generating temporary files. The template must end in 6 X's.
|
|
auto temp_path_template =
|
|
std::filesystem::temp_directory_path() / "packager-test.XXXXXX";
|
|
std::string temp_path_template_string = temp_path_template.string();
|
|
#if defined(OS_WIN)
|
|
// _mktemp will modify the string passed to it to reflect the generated name
|
|
// (replacing the X characters with something else).
|
|
_mktemp(temp_path_template_string.data());
|
|
#else
|
|
// mkstemp will create and open the file, modify the character points to
|
|
// reflect the generated name (replacing the X characters with something
|
|
// else), and return an open file descriptor. Then we close it and use the
|
|
// generated name.
|
|
int fd = mkstemp(temp_path_template_string.data());
|
|
close(fd);
|
|
#endif
|
|
return temp_path_template_string;
|
|
}
|
|
|
|
void delete_file(const std::string& path) {
|
|
std::error_code ec;
|
|
std::filesystem::remove(std::filesystem::u8path(path), ec);
|
|
// Ignore errors.
|
|
}
|
|
|
|
TempFile::TempFile() : path_(generate_unique_temp_path()) {}
|
|
|
|
TempFile::~TempFile() {
|
|
std::error_code ec;
|
|
std::filesystem::remove(std::filesystem::u8path(path_), ec);
|
|
// Ignore errors.
|
|
}
|
|
|
|
} // namespace shaka
|