blob: 0eac8d5e0f97ccfa3f6e52a66924a8a651ebe49a [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
Qingsi Wang2039ee72018-11-02 16:30:10 +000067namespace {
68
Patrik Höglund8434aeb2018-10-05 14:52:11 +020069#if defined(WEBRTC_WIN)
henrike@webrtc.org34773d92013-07-08 14:55:23 +000070const char* kPathDelimiter = "\\";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000071#else
henrike@webrtc.org34773d92013-07-08 14:55:23 +000072const char* kPathDelimiter = "/";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000073#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000074
Qingsi Wang2039ee72018-11-02 16:30:10 +000075} // namespace
76
Patrik Höglund8434aeb2018-10-05 14:52:11 +020077std::string DirName(const std::string& path) {
78 if (path.empty())
79 return "";
80 if (path == kPathDelimiter)
81 return path;
82
83 std::string result = path;
84 if (result.back() == *kPathDelimiter)
85 result.pop_back(); // Remove trailing separator.
86
87 return result.substr(0, result.find_last_of(kPathDelimiter));
88}
89
ehmaldonado88df0bc2017-02-10 09:27:14 -080090bool FileExists(const std::string& file_name) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000091 struct stat file_info = {0};
92 return stat(file_name.c_str(), &file_info) == 0;
93}
94
alessiobe49fede2017-03-15 06:04:59 -070095bool DirExists(const std::string& directory_name) {
96 struct stat directory_info = {0};
Yves Gerey665174f2018-06-19 15:03:05 +020097 return stat(directory_name.c_str(), &directory_info) == 0 &&
98 S_ISDIR(directory_info.st_mode);
alessiobe49fede2017-03-15 06:04:59 -070099}
100
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000101std::string OutputPath() {
Artem Titov8f726be2018-10-23 15:50:10 +0200102 return webrtc::test::internal::OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000103}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000104
105std::string WorkingDir() {
Artem Titov8f726be2018-10-23 15:50:10 +0200106 return webrtc::test::internal::WorkingDir();
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000107}
108
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000109// Generate a temporary filename in a safe way.
110// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
Yves Gerey665174f2018-06-19 15:03:05 +0200111std::string TempFilename(const std::string& dir, const std::string& prefix) {
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000112#ifdef WIN32
113 wchar_t filename[MAX_PATH];
Yves Gerey665174f2018-06-19 15:03:05 +0200114 if (::GetTempFileName(rtc::ToUtf16(dir).c_str(), rtc::ToUtf16(prefix).c_str(),
115 0, filename) != 0)
nisse07b83882017-03-14 01:32:50 -0700116 return rtc::ToUtf8(filename);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000117 assert(false);
118 return "";
119#else
120 int len = dir.size() + prefix.size() + 2 + 6;
kwibergbfefb032016-05-01 14:53:46 -0700121 std::unique_ptr<char[]> tempname(new char[len]);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000122
Yves Gerey665174f2018-06-19 15:03:05 +0200123 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(), prefix.c_str());
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000124 int fd = ::mkstemp(tempname.get());
125 if (fd == -1) {
126 assert(false);
127 return "";
128 } else {
129 ::close(fd);
130 }
131 std::string ret(tempname.get());
132 return ret;
133#endif
134}
135
Artem Titove62f6002018-03-19 15:40:00 +0100136std::string GenerateTempFilename(const std::string& dir,
137 const std::string& prefix) {
138 std::string filename = TempFilename(dir, prefix);
139 RemoveFile(filename);
140 return filename;
141}
142
Danil Chapovalov431abd92018-06-18 12:54:17 +0200143absl::optional<std::vector<std::string>> ReadDirectory(std::string path) {
alessiob00b16f42017-06-01 03:29:40 -0700144 if (path.length() == 0)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200145 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700146
147#if defined(WEBRTC_WIN)
148 // Append separator character if needed.
149 if (path.back() != '\\')
150 path += '\\';
151
152 // Init.
153 WIN32_FIND_DATA data;
154 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
155 if (handle == INVALID_HANDLE_VALUE)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200156 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700157
158 // Populate output.
159 std::vector<std::string> found_entries;
160 do {
161 const std::string name = rtc::ToUtf8(data.cFileName);
162 if (name != "." && name != "..")
163 found_entries.emplace_back(path + name);
164 } while (::FindNextFile(handle, &data) == TRUE);
165
166 // Release resources.
167 if (handle != INVALID_HANDLE_VALUE)
168 ::FindClose(handle);
169#else
170 // Append separator character if needed.
171 if (path.back() != '/')
172 path += '/';
173
174 // Init.
175 DIR* dir = ::opendir(path.c_str());
176 if (dir == nullptr)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200177 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700178
179 // Populate output.
180 std::vector<std::string> found_entries;
181 while (dirent* dirent = readdir(dir)) {
182 const std::string& name = dirent->d_name;
183 if (name != "." && name != "..")
184 found_entries.emplace_back(path + name);
185 }
186
187 // Release resources.
188 closedir(dir);
189#endif
190
Danil Chapovalov431abd92018-06-18 12:54:17 +0200191 return absl::optional<std::vector<std::string>>(std::move(found_entries));
alessiob00b16f42017-06-01 03:29:40 -0700192}
193
ehmaldonado88df0bc2017-02-10 09:27:14 -0800194bool CreateDir(const std::string& directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000195 struct stat path_info = {0};
196 // Check if the path exists already:
197 if (stat(directory_name.c_str(), &path_info) == 0) {
198 if (!S_ISDIR(path_info.st_mode)) {
Yves Gerey665174f2018-06-19 15:03:05 +0200199 fprintf(stderr,
200 "Path %s exists but is not a directory! Remove this "
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000201 "file and re-run to create the directory.\n",
202 directory_name.c_str());
203 return false;
204 }
205 } else {
206#ifdef WIN32
207 return _mkdir(directory_name.c_str()) == 0;
208#else
Yves Gerey665174f2018-06-19 15:03:05 +0200209 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000210#endif
211 }
212 return true;
213}
214
nisse57efb032017-05-18 03:55:59 -0700215bool RemoveDir(const std::string& directory_name) {
216#ifdef WIN32
Yves Gerey665174f2018-06-19 15:03:05 +0200217 return RemoveDirectoryA(directory_name.c_str()) != FALSE;
nisse57efb032017-05-18 03:55:59 -0700218#else
Yves Gerey665174f2018-06-19 15:03:05 +0200219 return rmdir(directory_name.c_str()) == 0;
nisse57efb032017-05-18 03:55:59 -0700220#endif
221}
222
223bool RemoveFile(const std::string& file_name) {
224#ifdef WIN32
225 return DeleteFileA(file_name.c_str()) != FALSE;
226#else
227 return unlink(file_name.c_str()) == 0;
228#endif
229}
230
ehmaldonado88df0bc2017-02-10 09:27:14 -0800231std::string ResourcePath(const std::string& name,
232 const std::string& extension) {
Artem Titov8f726be2018-10-23 15:50:10 +0200233 return webrtc::test::internal::ResourcePath(name, extension);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000234}
235
Niels Möller392f8d02018-06-01 10:14:44 +0200236std::string JoinFilename(const std::string& dir, const std::string& name) {
237 RTC_CHECK(!dir.empty()) << "Special cases not implemented.";
238 return dir + kPathDelimiter + name;
239}
240
ehmaldonado88df0bc2017-02-10 09:27:14 -0800241size_t GetFileSize(const std::string& filename) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000242 FILE* f = fopen(filename.c_str(), "rb");
243 size_t size = 0;
244 if (f != NULL) {
245 if (fseek(f, 0, SEEK_END) == 0) {
246 size = ftell(f);
247 }
248 fclose(f);
249 }
250 return size;
251}
252
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000253} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000254} // namespace webrtc