You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "test/testsupport/file_utils.h"
  11. #include <assert.h>
  12. #if defined(WEBRTC_POSIX)
  13. #include <unistd.h>
  14. #endif
  15. #if defined(WEBRTC_WIN)
  16. #include <direct.h>
  17. #include <tchar.h>
  18. #include <windows.h>
  19. #include <algorithm>
  20. #include <codecvt>
  21. #include <locale>
  22. #include "Shlwapi.h"
  23. #include "WinDef.h"
  24. #include "rtc_base/win32.h"
  25. #define GET_CURRENT_DIR _getcwd
  26. #else
  27. #include <dirent.h>
  28. #define GET_CURRENT_DIR getcwd
  29. #endif
  30. #include <sys/stat.h> // To check for directory existence.
  31. #ifndef S_ISDIR // Not defined in stat.h on Windows.
  32. #define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
  33. #endif
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <memory>
  37. #include <type_traits>
  38. #include <utility>
  39. #if defined(WEBRTC_IOS)
  40. #include "test/testsupport/ios_file_utils.h"
  41. #elif defined(WEBRTC_MAC)
  42. #include "test/testsupport/mac_file_utils.h"
  43. #endif
  44. #include "rtc_base/checks.h"
  45. #include "rtc_base/string_utils.h"
  46. #include "test/testsupport/file_utils_override.h"
  47. namespace webrtc {
  48. namespace test {
  49. #if defined(WEBRTC_WIN)
  50. const char* kPathDelimiter = "\\";
  51. #else
  52. const char* kPathDelimiter = "/";
  53. #endif
  54. std::string DirName(const std::string& path) {
  55. if (path.empty())
  56. return "";
  57. if (path == kPathDelimiter)
  58. return path;
  59. std::string result = path;
  60. if (result.back() == *kPathDelimiter)
  61. result.pop_back(); // Remove trailing separator.
  62. return result.substr(0, result.find_last_of(kPathDelimiter));
  63. }
  64. bool FileExists(const std::string& file_name) {
  65. struct stat file_info = {0};
  66. return stat(file_name.c_str(), &file_info) == 0;
  67. }
  68. bool DirExists(const std::string& directory_name) {
  69. struct stat directory_info = {0};
  70. return stat(directory_name.c_str(), &directory_info) == 0 &&
  71. S_ISDIR(directory_info.st_mode);
  72. }
  73. std::string OutputPath() {
  74. return webrtc::test::internal::OutputPath();
  75. }
  76. std::string WorkingDir() {
  77. return webrtc::test::internal::WorkingDir();
  78. }
  79. // Generate a temporary filename in a safe way.
  80. // Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
  81. std::string TempFilename(const std::string& dir, const std::string& prefix) {
  82. #ifdef WIN32
  83. wchar_t filename[MAX_PATH];
  84. if (::GetTempFileNameW(rtc::ToUtf16(dir).c_str(),
  85. rtc::ToUtf16(prefix).c_str(), 0, filename) != 0)
  86. return rtc::ToUtf8(filename);
  87. assert(false);
  88. return "";
  89. #else
  90. int len = dir.size() + prefix.size() + 2 + 6;
  91. std::unique_ptr<char[]> tempname(new char[len]);
  92. snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(), prefix.c_str());
  93. int fd = ::mkstemp(tempname.get());
  94. if (fd == -1) {
  95. assert(false);
  96. return "";
  97. } else {
  98. ::close(fd);
  99. }
  100. std::string ret(tempname.get());
  101. return ret;
  102. #endif
  103. }
  104. std::string GenerateTempFilename(const std::string& dir,
  105. const std::string& prefix) {
  106. std::string filename = TempFilename(dir, prefix);
  107. RemoveFile(filename);
  108. return filename;
  109. }
  110. absl::optional<std::vector<std::string>> ReadDirectory(std::string path) {
  111. if (path.length() == 0)
  112. return absl::optional<std::vector<std::string>>();
  113. #if defined(WEBRTC_WIN)
  114. // Append separator character if needed.
  115. if (path.back() != '\\')
  116. path += '\\';
  117. // Init.
  118. WIN32_FIND_DATAW data;
  119. HANDLE handle = ::FindFirstFileW(rtc::ToUtf16(path + '*').c_str(), &data);
  120. if (handle == INVALID_HANDLE_VALUE)
  121. return absl::optional<std::vector<std::string>>();
  122. // Populate output.
  123. std::vector<std::string> found_entries;
  124. do {
  125. const std::string name = rtc::ToUtf8(data.cFileName);
  126. if (name != "." && name != "..")
  127. found_entries.emplace_back(path + name);
  128. } while (::FindNextFileW(handle, &data) == TRUE);
  129. // Release resources.
  130. if (handle != INVALID_HANDLE_VALUE)
  131. ::FindClose(handle);
  132. #else
  133. // Append separator character if needed.
  134. if (path.back() != '/')
  135. path += '/';
  136. // Init.
  137. DIR* dir = ::opendir(path.c_str());
  138. if (dir == nullptr)
  139. return absl::optional<std::vector<std::string>>();
  140. // Populate output.
  141. std::vector<std::string> found_entries;
  142. while (dirent* dirent = readdir(dir)) {
  143. const std::string& name = dirent->d_name;
  144. if (name != "." && name != "..")
  145. found_entries.emplace_back(path + name);
  146. }
  147. // Release resources.
  148. closedir(dir);
  149. #endif
  150. return absl::optional<std::vector<std::string>>(std::move(found_entries));
  151. }
  152. bool CreateDir(const std::string& directory_name) {
  153. struct stat path_info = {0};
  154. // Check if the path exists already:
  155. if (stat(directory_name.c_str(), &path_info) == 0) {
  156. if (!S_ISDIR(path_info.st_mode)) {
  157. fprintf(stderr,
  158. "Path %s exists but is not a directory! Remove this "
  159. "file and re-run to create the directory.\n",
  160. directory_name.c_str());
  161. return false;
  162. }
  163. } else {
  164. #ifdef WIN32
  165. return _mkdir(directory_name.c_str()) == 0;
  166. #else
  167. return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
  168. #endif
  169. }
  170. return true;
  171. }
  172. bool RemoveDir(const std::string& directory_name) {
  173. #ifdef WIN32
  174. return RemoveDirectoryA(directory_name.c_str()) != FALSE;
  175. #else
  176. return rmdir(directory_name.c_str()) == 0;
  177. #endif
  178. }
  179. bool RemoveFile(const std::string& file_name) {
  180. #ifdef WIN32
  181. return DeleteFileA(file_name.c_str()) != FALSE;
  182. #else
  183. return unlink(file_name.c_str()) == 0;
  184. #endif
  185. }
  186. std::string ResourcePath(const std::string& name,
  187. const std::string& extension) {
  188. return webrtc::test::internal::ResourcePath(name, extension);
  189. }
  190. std::string JoinFilename(const std::string& dir, const std::string& name) {
  191. RTC_CHECK(!dir.empty()) << "Special cases not implemented.";
  192. return dir + kPathDelimiter + name;
  193. }
  194. size_t GetFileSize(const std::string& filename) {
  195. FILE* f = fopen(filename.c_str(), "rb");
  196. size_t size = 0;
  197. if (f != NULL) {
  198. if (fseek(f, 0, SEEK_END) == 0) {
  199. size = ftell(f);
  200. }
  201. fclose(f);
  202. }
  203. return size;
  204. }
  205. } // namespace test
  206. } // namespace webrtc