• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include "common/file_util.h"
9 
10 #include <sys/stat.h>
11 #ifndef _MSC_VER
12 #include <unistd.h>  // close()
13 #endif
14 
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cstring>
18 #include <fstream>
19 #include <ios>
20 #include <string>
21 
22 namespace libwebm {
23 
GetTempFileName()24 std::string GetTempFileName() {
25 #if !defined _MSC_VER && !defined __MINGW32__
26   std::string temp_file_name_template_str =
27       std::string(std::getenv("TEST_TMPDIR") ? std::getenv("TEST_TMPDIR")
28                                              : ".") +
29       "/libwebm_temp.XXXXXX";
30   char* temp_file_name_template =
31       new char[temp_file_name_template_str.length() + 1];
32   memset(temp_file_name_template, 0, temp_file_name_template_str.length() + 1);
33   temp_file_name_template_str.copy(temp_file_name_template,
34                                    temp_file_name_template_str.length(), 0);
35   int fd = mkstemp(temp_file_name_template);
36   std::string temp_file_name =
37       (fd != -1) ? std::string(temp_file_name_template) : std::string();
38   delete[] temp_file_name_template;
39   if (fd != -1) {
40     close(fd);
41   }
42   return temp_file_name;
43 #else
44   char tmp_file_name[_MAX_PATH];
45 #if defined _MSC_VER || defined MINGW_HAS_SECURE_API
46   errno_t err = tmpnam_s(tmp_file_name);
47 #else
48   char* fname_pointer = tmpnam(tmp_file_name);
49   int err = (fname_pointer == &tmp_file_name[0]) ? 0 : -1;
50 #endif
51   if (err == 0) {
52     return std::string(tmp_file_name);
53   }
54   return std::string();
55 #endif
56 }
57 
GetFileSize(const std::string & file_name)58 uint64_t GetFileSize(const std::string& file_name) {
59   uint64_t file_size = 0;
60 #ifndef _MSC_VER
61   struct stat st;
62   st.st_size = 0;
63   if (stat(file_name.c_str(), &st) == 0) {
64 #else
65   struct _stat st;
66   st.st_size = 0;
67   if (_stat(file_name.c_str(), &st) == 0) {
68 #endif
69     file_size = st.st_size;
70   }
71   return file_size;
72 }
73 
74 bool GetFileContents(const std::string& file_name, std::string* contents) {
75   std::ifstream file(file_name.c_str());
76   *contents = std::string(static_cast<size_t>(GetFileSize(file_name)), 0);
77   if (file.good() && contents->size()) {
78     file.read(&(*contents)[0], contents->size());
79   }
80   return !file.fail();
81 }
82 
83 TempFileDeleter::TempFileDeleter() { file_name_ = GetTempFileName(); }
84 
85 TempFileDeleter::~TempFileDeleter() {
86   std::ifstream file(file_name_.c_str());
87   if (file.good()) {
88     file.close();
89     std::remove(file_name_.c_str());
90   }
91 }
92 
93 }  // namespace libwebm
94