DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
file_util.cc
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/file/file_util.h"
8 
9 #include <inttypes.h>
10 
11 #include "packager/base/files/file_path.h"
12 #include "packager/base/files/file_util.h"
13 #include "packager/base/strings/stringprintf.h"
14 #include "packager/base/threading/platform_thread.h"
15 #include "packager/base/time/time.h"
16 
17 namespace shaka {
18 namespace {
19 // Create a temp file name using process/thread id and current time.
20 std::string TempFileName() {
21  const int32_t tid = static_cast<int32_t>(base::PlatformThread::CurrentId());
22  const int64_t current_time = base::Time::Now().ToInternalValue();
23  return base::StringPrintf("packager-tempfile-%x-%" PRIx64, tid, current_time);
24 }
25 } // namespace
26 
27 bool TempFilePath(const std::string& temp_dir, std::string* temp_file_path) {
28  if (temp_dir.empty()) {
29  base::FilePath file_path;
30  if (!base::CreateTemporaryFile(&file_path)) {
31  LOG(ERROR) << "Failed to create temporary file.";
32  return false;
33  }
34  *temp_file_path = file_path.AsUTF8Unsafe();
35  } else {
36  *temp_file_path =
37  base::FilePath::FromUTF8Unsafe(temp_dir)
38  .Append(base::FilePath::FromUTF8Unsafe(TempFileName()))
39  .AsUTF8Unsafe();
40  }
41  return true;
42 }
43 
44 } // namespace shaka
bool TempFilePath(const std::string &temp_dir, std::string *temp_file_path)
Definition: file_util.cc:27