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() {
|
2018-08-06 23:11:57 +00:00
|
|
|
const int32_t process_id = static_cast<int32_t>(base::GetCurrentProcId());
|
|
|
|
const int32_t thread_id =
|
|
|
|
static_cast<int32_t>(base::PlatformThread::CurrentId());
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
static thread_local int32_t instance_id = 0;
|
|
|
|
++instance_id;
|
|
|
|
|
2016-10-04 22:46:02 +00:00
|
|
|
const int64_t current_time = base::Time::Now().ToInternalValue();
|
2018-08-06 23:11:57 +00:00
|
|
|
return base::StringPrintf("packager-tempfile-%x-%x-%x-%" PRIx64, process_id,
|
|
|
|
thread_id, instance_id, 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
|