2011-10-13 12:24:41 +00:00
|
|
|
/*
|
2012-06-27 03:22:37 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-10-13 12:24:41 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "test/testsupport/file_utils.h"
|
2011-10-13 12:24:41 +00:00
|
|
|
|
2014-04-16 08:04:26 +00:00
|
|
|
#include <assert.h>
|
2014-04-29 17:54:17 +00:00
|
|
|
|
2018-10-05 14:52:11 +02:00
|
|
|
#if defined(WEBRTC_POSIX)
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(WEBRTC_WIN)
|
2011-10-13 12:24:41 +00:00
|
|
|
#include <direct.h>
|
2014-04-16 08:04:26 +00:00
|
|
|
#include <tchar.h>
|
|
|
|
|
#include <windows.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2013-08-29 11:26:41 +00:00
|
|
|
#include <algorithm>
|
2018-10-05 14:52:11 +02:00
|
|
|
#include <codecvt>
|
|
|
|
|
#include <locale>
|
2014-04-16 08:04:26 +00:00
|
|
|
|
2016-11-18 07:06:41 -08:00
|
|
|
#include "Shlwapi.h"
|
2016-12-05 06:42:45 -08:00
|
|
|
#include "WinDef.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/win32.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
|
2011-10-13 12:24:41 +00:00
|
|
|
#define GET_CURRENT_DIR _getcwd
|
|
|
|
|
#else
|
2017-06-01 03:29:40 -07:00
|
|
|
#include <dirent.h>
|
2014-04-16 08:04:26 +00:00
|
|
|
|
2011-10-13 12:24:41 +00:00
|
|
|
#define GET_CURRENT_DIR getcwd
|
2011-11-09 11:24:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <sys/stat.h> // To check for directory existence.
|
2018-06-19 15:03:05 +02:00
|
|
|
#ifndef S_ISDIR // Not defined in stat.h on Windows.
|
|
|
|
|
#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
|
2011-10-13 12:24:41 +00:00
|
|
|
#endif
|
|
|
|
|
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <stdio.h>
|
2014-04-16 08:04:26 +00:00
|
|
|
#include <stdlib.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2016-05-01 14:53:46 -07:00
|
|
|
#include <memory>
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <type_traits>
|
2017-06-01 03:29:40 -07:00
|
|
|
#include <utility>
|
2016-05-01 14:53:46 -07:00
|
|
|
|
2018-10-05 14:52:11 +02:00
|
|
|
#if defined(WEBRTC_IOS)
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "test/testsupport/ios_file_utils.h"
|
2019-07-08 09:47:41 +02:00
|
|
|
#elif defined(WEBRTC_MAC)
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "test/testsupport/mac_file_utils.h"
|
2018-10-05 14:52:11 +02:00
|
|
|
#endif
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/string_utils.h"
|
|
|
|
|
#include "test/testsupport/file_utils_override.h"
|
2011-12-05 16:31:12 +00:00
|
|
|
|
2011-10-13 12:24:41 +00:00
|
|
|
namespace webrtc {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
2018-10-05 14:52:11 +02:00
|
|
|
#if defined(WEBRTC_WIN)
|
2013-07-08 14:55:23 +00:00
|
|
|
const char* kPathDelimiter = "\\";
|
2011-11-09 11:24:14 +00:00
|
|
|
#else
|
2013-07-08 14:55:23 +00:00
|
|
|
const char* kPathDelimiter = "/";
|
2011-11-09 11:24:14 +00:00
|
|
|
#endif
|
2012-10-16 04:31:20 +00:00
|
|
|
|
2018-10-05 14:52:11 +02:00
|
|
|
std::string DirName(const std::string& path) {
|
|
|
|
|
if (path.empty())
|
|
|
|
|
return "";
|
|
|
|
|
if (path == kPathDelimiter)
|
|
|
|
|
return path;
|
|
|
|
|
|
|
|
|
|
std::string result = path;
|
|
|
|
|
if (result.back() == *kPathDelimiter)
|
|
|
|
|
result.pop_back(); // Remove trailing separator.
|
|
|
|
|
|
|
|
|
|
return result.substr(0, result.find_last_of(kPathDelimiter));
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 09:27:14 -08:00
|
|
|
bool FileExists(const std::string& file_name) {
|
2012-10-15 18:14:12 +00:00
|
|
|
struct stat file_info = {0};
|
|
|
|
|
return stat(file_name.c_str(), &file_info) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-15 06:04:59 -07:00
|
|
|
bool DirExists(const std::string& directory_name) {
|
|
|
|
|
struct stat directory_info = {0};
|
2018-06-19 15:03:05 +02:00
|
|
|
return stat(directory_name.c_str(), &directory_info) == 0 &&
|
|
|
|
|
S_ISDIR(directory_info.st_mode);
|
2017-03-15 06:04:59 -07:00
|
|
|
}
|
|
|
|
|
|
2011-11-13 01:34:05 +00:00
|
|
|
std::string OutputPath() {
|
2018-10-23 15:50:10 +02:00
|
|
|
return webrtc::test::internal::OutputPath();
|
2011-12-05 16:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string WorkingDir() {
|
2018-10-23 15:50:10 +02:00
|
|
|
return webrtc::test::internal::WorkingDir();
|
2011-12-05 16:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-16 08:04:26 +00:00
|
|
|
// Generate a temporary filename in a safe way.
|
|
|
|
|
// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
|
2018-06-19 15:03:05 +02:00
|
|
|
std::string TempFilename(const std::string& dir, const std::string& prefix) {
|
2014-04-16 08:04:26 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
|
wchar_t filename[MAX_PATH];
|
2019-03-25 09:01:02 +01:00
|
|
|
if (::GetTempFileNameW(rtc::ToUtf16(dir).c_str(),
|
|
|
|
|
rtc::ToUtf16(prefix).c_str(), 0, filename) != 0)
|
2017-03-14 01:32:50 -07:00
|
|
|
return rtc::ToUtf8(filename);
|
2014-04-16 08:04:26 +00:00
|
|
|
assert(false);
|
|
|
|
|
return "";
|
|
|
|
|
#else
|
|
|
|
|
int len = dir.size() + prefix.size() + 2 + 6;
|
2016-05-01 14:53:46 -07:00
|
|
|
std::unique_ptr<char[]> tempname(new char[len]);
|
2014-04-16 08:04:26 +00:00
|
|
|
|
2018-06-19 15:03:05 +02:00
|
|
|
snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(), prefix.c_str());
|
2014-04-16 08:04:26 +00:00
|
|
|
int fd = ::mkstemp(tempname.get());
|
|
|
|
|
if (fd == -1) {
|
|
|
|
|
assert(false);
|
|
|
|
|
return "";
|
|
|
|
|
} else {
|
|
|
|
|
::close(fd);
|
|
|
|
|
}
|
|
|
|
|
std::string ret(tempname.get());
|
|
|
|
|
return ret;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-19 15:40:00 +01:00
|
|
|
std::string GenerateTempFilename(const std::string& dir,
|
|
|
|
|
const std::string& prefix) {
|
|
|
|
|
std::string filename = TempFilename(dir, prefix);
|
|
|
|
|
RemoveFile(filename);
|
|
|
|
|
return filename;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-18 12:54:17 +02:00
|
|
|
absl::optional<std::vector<std::string>> ReadDirectory(std::string path) {
|
2017-06-01 03:29:40 -07:00
|
|
|
if (path.length() == 0)
|
2018-06-18 12:54:17 +02:00
|
|
|
return absl::optional<std::vector<std::string>>();
|
2017-06-01 03:29:40 -07:00
|
|
|
|
|
|
|
|
#if defined(WEBRTC_WIN)
|
|
|
|
|
// Append separator character if needed.
|
|
|
|
|
if (path.back() != '\\')
|
|
|
|
|
path += '\\';
|
|
|
|
|
|
|
|
|
|
// Init.
|
2019-03-25 09:01:02 +01:00
|
|
|
WIN32_FIND_DATAW data;
|
|
|
|
|
HANDLE handle = ::FindFirstFileW(rtc::ToUtf16(path + '*').c_str(), &data);
|
2017-06-01 03:29:40 -07:00
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
2018-06-18 12:54:17 +02:00
|
|
|
return absl::optional<std::vector<std::string>>();
|
2017-06-01 03:29:40 -07:00
|
|
|
|
|
|
|
|
// Populate output.
|
|
|
|
|
std::vector<std::string> found_entries;
|
|
|
|
|
do {
|
|
|
|
|
const std::string name = rtc::ToUtf8(data.cFileName);
|
|
|
|
|
if (name != "." && name != "..")
|
|
|
|
|
found_entries.emplace_back(path + name);
|
2019-03-25 09:01:02 +01:00
|
|
|
} while (::FindNextFileW(handle, &data) == TRUE);
|
2017-06-01 03:29:40 -07:00
|
|
|
|
|
|
|
|
// Release resources.
|
|
|
|
|
if (handle != INVALID_HANDLE_VALUE)
|
|
|
|
|
::FindClose(handle);
|
|
|
|
|
#else
|
|
|
|
|
// Append separator character if needed.
|
|
|
|
|
if (path.back() != '/')
|
|
|
|
|
path += '/';
|
|
|
|
|
|
|
|
|
|
// Init.
|
|
|
|
|
DIR* dir = ::opendir(path.c_str());
|
|
|
|
|
if (dir == nullptr)
|
2018-06-18 12:54:17 +02:00
|
|
|
return absl::optional<std::vector<std::string>>();
|
2017-06-01 03:29:40 -07:00
|
|
|
|
|
|
|
|
// Populate output.
|
|
|
|
|
std::vector<std::string> found_entries;
|
|
|
|
|
while (dirent* dirent = readdir(dir)) {
|
|
|
|
|
const std::string& name = dirent->d_name;
|
|
|
|
|
if (name != "." && name != "..")
|
|
|
|
|
found_entries.emplace_back(path + name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Release resources.
|
|
|
|
|
closedir(dir);
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-06-18 12:54:17 +02:00
|
|
|
return absl::optional<std::vector<std::string>>(std::move(found_entries));
|
2017-06-01 03:29:40 -07:00
|
|
|
}
|
|
|
|
|
|
2017-02-10 09:27:14 -08:00
|
|
|
bool CreateDir(const std::string& directory_name) {
|
2011-11-09 11:24:14 +00:00
|
|
|
struct stat path_info = {0};
|
|
|
|
|
// Check if the path exists already:
|
2011-12-05 16:31:12 +00:00
|
|
|
if (stat(directory_name.c_str(), &path_info) == 0) {
|
2011-11-09 11:24:14 +00:00
|
|
|
if (!S_ISDIR(path_info.st_mode)) {
|
2018-06-19 15:03:05 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
|
"Path %s exists but is not a directory! Remove this "
|
2011-12-05 16:31:12 +00:00
|
|
|
"file and re-run to create the directory.\n",
|
|
|
|
|
directory_name.c_str());
|
|
|
|
|
return false;
|
2011-11-09 11:24:14 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
#ifdef WIN32
|
2011-12-05 16:31:12 +00:00
|
|
|
return _mkdir(directory_name.c_str()) == 0;
|
2011-11-09 11:24:14 +00:00
|
|
|
#else
|
2018-06-19 15:03:05 +02:00
|
|
|
return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
|
2011-11-09 11:24:14 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
2011-12-05 16:31:12 +00:00
|
|
|
return true;
|
2011-11-09 11:24:14 +00:00
|
|
|
}
|
2011-12-05 16:31:12 +00:00
|
|
|
|
2017-05-18 03:55:59 -07:00
|
|
|
bool RemoveDir(const std::string& directory_name) {
|
|
|
|
|
#ifdef WIN32
|
2018-06-19 15:03:05 +02:00
|
|
|
return RemoveDirectoryA(directory_name.c_str()) != FALSE;
|
2017-05-18 03:55:59 -07:00
|
|
|
#else
|
2018-06-19 15:03:05 +02:00
|
|
|
return rmdir(directory_name.c_str()) == 0;
|
2017-05-18 03:55:59 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RemoveFile(const std::string& file_name) {
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
return DeleteFileA(file_name.c_str()) != FALSE;
|
|
|
|
|
#else
|
|
|
|
|
return unlink(file_name.c_str()) == 0;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 09:27:14 -08:00
|
|
|
std::string ResourcePath(const std::string& name,
|
|
|
|
|
const std::string& extension) {
|
2018-10-23 15:50:10 +02:00
|
|
|
return webrtc::test::internal::ResourcePath(name, extension);
|
2011-12-05 16:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-01 10:14:44 +02:00
|
|
|
std::string JoinFilename(const std::string& dir, const std::string& name) {
|
|
|
|
|
RTC_CHECK(!dir.empty()) << "Special cases not implemented.";
|
|
|
|
|
return dir + kPathDelimiter + name;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 09:27:14 -08:00
|
|
|
size_t GetFileSize(const std::string& filename) {
|
2011-12-08 07:42:18 +00:00
|
|
|
FILE* f = fopen(filename.c_str(), "rb");
|
|
|
|
|
size_t size = 0;
|
|
|
|
|
if (f != NULL) {
|
|
|
|
|
if (fseek(f, 0, SEEK_END) == 0) {
|
|
|
|
|
size = ftell(f);
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-13 12:24:41 +00:00
|
|
|
} // namespace test
|
2011-11-09 11:24:14 +00:00
|
|
|
} // namespace webrtc
|