file_util.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include <sys/stat.h>
  10. #ifndef _MSC_VER
  11. #include <unistd.h> // close()
  12. #endif
  13. #include <cstdio>
  14. #include <cstdlib>
  15. #include <cstring>
  16. #include <fstream>
  17. #include <ios>
  18. #include <string>
  19. namespace libwebm {
  20. std::string GetTempFileName() {
  21. #if !defined _MSC_VER && !defined __MINGW32__
  22. std::string temp_file_name_template_str =
  23. std::string(std::getenv("TEST_TMPDIR") ? std::getenv("TEST_TMPDIR")
  24. : ".") +
  25. "/libwebm_temp.XXXXXX";
  26. char* temp_file_name_template =
  27. new char[temp_file_name_template_str.length() + 1];
  28. memset(temp_file_name_template, 0, temp_file_name_template_str.length() + 1);
  29. temp_file_name_template_str.copy(temp_file_name_template,
  30. temp_file_name_template_str.length(), 0);
  31. int fd = mkstemp(temp_file_name_template);
  32. std::string temp_file_name =
  33. (fd != -1) ? std::string(temp_file_name_template) : std::string();
  34. delete[] temp_file_name_template;
  35. if (fd != -1) {
  36. close(fd);
  37. }
  38. return temp_file_name;
  39. #else
  40. char tmp_file_name[_MAX_PATH];
  41. #if defined _MSC_VER || defined MINGW_HAS_SECURE_API
  42. errno_t err = tmpnam_s(tmp_file_name);
  43. #else
  44. char* fname_pointer = tmpnam(tmp_file_name);
  45. int err = (fname_pointer == &tmp_file_name[0]) ? 0 : -1;
  46. #endif
  47. if (err == 0) {
  48. return std::string(tmp_file_name);
  49. }
  50. return std::string();
  51. #endif
  52. }
  53. uint64_t GetFileSize(const std::string& file_name) {
  54. uint64_t file_size = 0;
  55. #ifndef _MSC_VER
  56. struct stat st;
  57. st.st_size = 0;
  58. if (stat(file_name.c_str(), &st) == 0) {
  59. #else
  60. struct _stat st;
  61. st.st_size = 0;
  62. if (_stat(file_name.c_str(), &st) == 0) {
  63. #endif
  64. file_size = st.st_size;
  65. }
  66. return file_size;
  67. }
  68. bool GetFileContents(const std::string& file_name, std::string* contents) {
  69. std::ifstream file(file_name.c_str());
  70. *contents = std::string(static_cast<size_t>(GetFileSize(file_name)), 0);
  71. if (file.good() && contents->size()) {
  72. file.read(&(*contents)[0], contents->size());
  73. }
  74. return !file.fail();
  75. }
  76. TempFileDeleter::TempFileDeleter() { file_name_ = GetTempFileName(); }
  77. TempFileDeleter::~TempFileDeleter() {
  78. std::ifstream file(file_name_.c_str());
  79. if (file.good()) {
  80. file.close();
  81. std::remove(file_name_.c_str());
  82. }
  83. }
  84. } // namespace libwebm