blob: a71d8f8db57d57ee94034114db4b68e0288e68ab [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
andrew@webrtc.org7a281a52012-06-27 03:22:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "test/testsupport/fileutils.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000012
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000013#include <assert.h>
henrike@webrtc.orgf2aafe42014-04-29 17:54:17 +000014
Patrik Höglund8434aeb2018-10-05 14:52:11 +020015#if defined(WEBRTC_POSIX)
16#include <unistd.h>
17#endif
18
19#if defined(WEBRTC_WIN)
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000020#include <direct.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000021#include <tchar.h>
22#include <windows.h>
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000023#include <algorithm>
Patrik Höglund8434aeb2018-10-05 14:52:11 +020024#include <codecvt>
25#include <locale>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000026
ehmaldonado00f2ee02016-11-18 07:06:41 -080027#include "Shlwapi.h"
ehmaldonado37535bf2016-12-05 06:42:45 -080028#include "WinDef.h"
ehmaldonado00f2ee02016-11-18 07:06:41 -080029
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/win32.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000031#define GET_CURRENT_DIR _getcwd
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000032#else
alessiob00b16f42017-06-01 03:29:40 -070033#include <dirent.h>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000034#include <unistd.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000035
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000036#define GET_CURRENT_DIR getcwd
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000037#endif
38
39#include <sys/stat.h> // To check for directory existence.
Yves Gerey665174f2018-06-19 15:03:05 +020040#ifndef S_ISDIR // Not defined in stat.h on Windows.
41#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000042#endif
43
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000044#include <stdio.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000045#include <stdlib.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000046#include <string.h>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000047
kwibergbfefb032016-05-01 14:53:46 -070048#include <memory>
alessiob00b16f42017-06-01 03:29:40 -070049#include <utility>
kwibergbfefb032016-05-01 14:53:46 -070050
Patrik Höglund8434aeb2018-10-05 14:52:11 +020051#if defined(WEBRTC_IOS)
52#include "test/testsupport/iosfileutils.h"
53#endif
54
55#if defined(WEBRTC_MAC)
56#include "test/testsupport/macfileutils.h"
57#endif
58
59#include "rtc_base/arraysize.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "rtc_base/checks.h"
Niels Möllerd7b91312018-05-24 11:21:34 +020061#include "rtc_base/stringutils.h"
Artem Titov8f726be2018-10-23 15:50:10 +020062#include "test/testsupport/fileutils_override.h"
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000063
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000064namespace webrtc {
65namespace test {
66
Patrik Höglund8434aeb2018-10-05 14:52:11 +020067#if defined(WEBRTC_WIN)
henrike@webrtc.org34773d92013-07-08 14:55:23 +000068const char* kPathDelimiter = "\\";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000069#else
henrike@webrtc.org34773d92013-07-08 14:55:23 +000070const char* kPathDelimiter = "/";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000071#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000072
Patrik Höglund8434aeb2018-10-05 14:52:11 +020073std::string DirName(const std::string& path) {
74 if (path.empty())
75 return "";
76 if (path == kPathDelimiter)
77 return path;
78
79 std::string result = path;
80 if (result.back() == *kPathDelimiter)
81 result.pop_back(); // Remove trailing separator.
82
83 return result.substr(0, result.find_last_of(kPathDelimiter));
84}
85
ehmaldonado88df0bc2017-02-10 09:27:14 -080086bool FileExists(const std::string& file_name) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000087 struct stat file_info = {0};
88 return stat(file_name.c_str(), &file_info) == 0;
89}
90
alessiobe49fede2017-03-15 06:04:59 -070091bool DirExists(const std::string& directory_name) {
92 struct stat directory_info = {0};
Yves Gerey665174f2018-06-19 15:03:05 +020093 return stat(directory_name.c_str(), &directory_info) == 0 &&
94 S_ISDIR(directory_info.st_mode);
alessiobe49fede2017-03-15 06:04:59 -070095}
96
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000097std::string OutputPath() {
Artem Titov8f726be2018-10-23 15:50:10 +020098 return webrtc::test::internal::OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000099}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000100
101std::string WorkingDir() {
Artem Titov8f726be2018-10-23 15:50:10 +0200102 return webrtc::test::internal::WorkingDir();
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000103}
104
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000105// Generate a temporary filename in a safe way.
106// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
Yves Gerey665174f2018-06-19 15:03:05 +0200107std::string TempFilename(const std::string& dir, const std::string& prefix) {
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000108#ifdef WIN32
109 wchar_t filename[MAX_PATH];
Yves Gerey665174f2018-06-19 15:03:05 +0200110 if (::GetTempFileName(rtc::ToUtf16(dir).c_str(), rtc::ToUtf16(prefix).c_str(),
111 0, filename) != 0)
nisse07b83882017-03-14 01:32:50 -0700112 return rtc::ToUtf8(filename);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000113 assert(false);
114 return "";
115#else
116 int len = dir.size() + prefix.size() + 2 + 6;
kwibergbfefb032016-05-01 14:53:46 -0700117 std::unique_ptr<char[]> tempname(new char[len]);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000118
Yves Gerey665174f2018-06-19 15:03:05 +0200119 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(), prefix.c_str());
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000120 int fd = ::mkstemp(tempname.get());
121 if (fd == -1) {
122 assert(false);
123 return "";
124 } else {
125 ::close(fd);
126 }
127 std::string ret(tempname.get());
128 return ret;
129#endif
130}
131
Artem Titove62f6002018-03-19 15:40:00 +0100132std::string GenerateTempFilename(const std::string& dir,
133 const std::string& prefix) {
134 std::string filename = TempFilename(dir, prefix);
135 RemoveFile(filename);
136 return filename;
137}
138
Danil Chapovalov431abd92018-06-18 12:54:17 +0200139absl::optional<std::vector<std::string>> ReadDirectory(std::string path) {
alessiob00b16f42017-06-01 03:29:40 -0700140 if (path.length() == 0)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200141 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700142
143#if defined(WEBRTC_WIN)
144 // Append separator character if needed.
145 if (path.back() != '\\')
146 path += '\\';
147
148 // Init.
149 WIN32_FIND_DATA data;
150 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
151 if (handle == INVALID_HANDLE_VALUE)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200152 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700153
154 // Populate output.
155 std::vector<std::string> found_entries;
156 do {
157 const std::string name = rtc::ToUtf8(data.cFileName);
158 if (name != "." && name != "..")
159 found_entries.emplace_back(path + name);
160 } while (::FindNextFile(handle, &data) == TRUE);
161
162 // Release resources.
163 if (handle != INVALID_HANDLE_VALUE)
164 ::FindClose(handle);
165#else
166 // Append separator character if needed.
167 if (path.back() != '/')
168 path += '/';
169
170 // Init.
171 DIR* dir = ::opendir(path.c_str());
172 if (dir == nullptr)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200173 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700174
175 // Populate output.
176 std::vector<std::string> found_entries;
177 while (dirent* dirent = readdir(dir)) {
178 const std::string& name = dirent->d_name;
179 if (name != "." && name != "..")
180 found_entries.emplace_back(path + name);
181 }
182
183 // Release resources.
184 closedir(dir);
185#endif
186
Danil Chapovalov431abd92018-06-18 12:54:17 +0200187 return absl::optional<std::vector<std::string>>(std::move(found_entries));
alessiob00b16f42017-06-01 03:29:40 -0700188}
189
ehmaldonado88df0bc2017-02-10 09:27:14 -0800190bool CreateDir(const std::string& directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000191 struct stat path_info = {0};
192 // Check if the path exists already:
193 if (stat(directory_name.c_str(), &path_info) == 0) {
194 if (!S_ISDIR(path_info.st_mode)) {
Yves Gerey665174f2018-06-19 15:03:05 +0200195 fprintf(stderr,
196 "Path %s exists but is not a directory! Remove this "
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000197 "file and re-run to create the directory.\n",
198 directory_name.c_str());
199 return false;
200 }
201 } else {
202#ifdef WIN32
203 return _mkdir(directory_name.c_str()) == 0;
204#else
Yves Gerey665174f2018-06-19 15:03:05 +0200205 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000206#endif
207 }
208 return true;
209}
210
nisse57efb032017-05-18 03:55:59 -0700211bool RemoveDir(const std::string& directory_name) {
212#ifdef WIN32
Yves Gerey665174f2018-06-19 15:03:05 +0200213 return RemoveDirectoryA(directory_name.c_str()) != FALSE;
nisse57efb032017-05-18 03:55:59 -0700214#else
Yves Gerey665174f2018-06-19 15:03:05 +0200215 return rmdir(directory_name.c_str()) == 0;
nisse57efb032017-05-18 03:55:59 -0700216#endif
217}
218
219bool RemoveFile(const std::string& file_name) {
220#ifdef WIN32
221 return DeleteFileA(file_name.c_str()) != FALSE;
222#else
223 return unlink(file_name.c_str()) == 0;
224#endif
225}
226
ehmaldonado88df0bc2017-02-10 09:27:14 -0800227std::string ResourcePath(const std::string& name,
228 const std::string& extension) {
Artem Titov8f726be2018-10-23 15:50:10 +0200229 return webrtc::test::internal::ResourcePath(name, extension);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000230}
231
Niels Möller392f8d02018-06-01 10:14:44 +0200232std::string JoinFilename(const std::string& dir, const std::string& name) {
233 RTC_CHECK(!dir.empty()) << "Special cases not implemented.";
234 return dir + kPathDelimiter + name;
235}
236
ehmaldonado88df0bc2017-02-10 09:27:14 -0800237size_t GetFileSize(const std::string& filename) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000238 FILE* f = fopen(filename.c_str(), "rb");
239 size_t size = 0;
240 if (f != NULL) {
241 if (fseek(f, 0, SEEK_END) == 0) {
242 size = ftell(f);
243 }
244 fclose(f);
245 }
246 return size;
247}
248
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000249} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000250} // namespace webrtc