2016-10-04 22:46:02 +00:00
|
|
|
// Copyright 2016 Google Inc. 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
|
|
|
|
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/file_util.h"
|
2016-10-04 22:46:02 +00:00
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "packager/base/files/file_path.h"
|
|
|
|
#include "packager/base/files/file_util.h"
|
2017-08-01 22:34:20 +00:00
|
|
|
#include "packager/base/process/process_handle.h"
|
2016-10-04 22:46:02 +00:00
|
|
|
#include "packager/base/strings/stringprintf.h"
|
|
|
|
#include "packager/base/threading/platform_thread.h"
|
|
|
|
#include "packager/base/time/time.h"
|
|
|
|
|
|
|
|
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() {
|
2017-08-01 22:34:20 +00:00
|
|
|
const int32_t pid = static_cast<int32_t>(base::GetCurrentProcId());
|
2016-10-04 22:46:02 +00:00
|
|
|
const int32_t tid = static_cast<int32_t>(base::PlatformThread::CurrentId());
|
|
|
|
const int64_t current_time = base::Time::Now().ToInternalValue();
|
2017-08-01 22:34:20 +00:00
|
|
|
return base::StringPrintf("packager-tempfile-%x-%x-%" PRIx64, pid, tid,
|
|
|
|
current_time);
|
2016-10-04 22:46:02 +00:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool TempFilePath(const std::string& temp_dir, std::string* temp_file_path) {
|
|
|
|
if (temp_dir.empty()) {
|
|
|
|
base::FilePath file_path;
|
|
|
|
if (!base::CreateTemporaryFile(&file_path)) {
|
|
|
|
LOG(ERROR) << "Failed to create temporary file.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*temp_file_path = file_path.AsUTF8Unsafe();
|
|
|
|
} else {
|
|
|
|
*temp_file_path =
|
|
|
|
base::FilePath::FromUTF8Unsafe(temp_dir)
|
|
|
|
.Append(base::FilePath::FromUTF8Unsafe(TempFileName()))
|
|
|
|
.AsUTF8Unsafe();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace shaka
|