webrtc_m130/test/testsupport/file_utils.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

247 lines
6.2 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* 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.
*/
#include "test/testsupport/file_utils.h"
#if defined(WEBRTC_POSIX)
#include <unistd.h>
#endif
#if defined(WEBRTC_WIN)
#include <direct.h>
#include <tchar.h>
#include <windows.h>
#include <algorithm>
#include <codecvt>
#include <locale>
#include "Shlwapi.h"
#include "WinDef.h"
#include "rtc_base/win32.h"
#define GET_CURRENT_DIR _getcwd
#else
#include <dirent.h>
#define GET_CURRENT_DIR getcwd
#endif
#include <sys/stat.h> // To check for directory existence.
#ifndef S_ISDIR // Not defined in stat.h on Windows.
#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include <type_traits>
#include <utility>
#if defined(WEBRTC_IOS)
#include "test/testsupport/ios_file_utils.h"
#elif defined(WEBRTC_MAC)
#include "test/testsupport/mac_file_utils.h"
#endif
#include "rtc_base/checks.h"
#include "rtc_base/string_utils.h"
#include "test/testsupport/file_utils_override.h"
namespace webrtc {
namespace test {
#if defined(WEBRTC_WIN)
const char* kPathDelimiter = "\\";
#else
const char* kPathDelimiter = "/";
#endif
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));
}
bool FileExists(const std::string& file_name) {
Improving the way fileutil.h finds test resources. Adding the argv[0] path to the working directory to make it possible find the project root even when the test is executed from outside the project dir (like it is on some buildbots). Removed tests that moves into directories since they no longer work with this approach. The new functionality have been verified by manual tests of the following cases, example with a checkout root dir called webrtc/: Working dir: Command line: webrtc trunk/out/Debug/test webrtc/trunk out/Debug/test webrtc/trunk/out Debug/test webrtc/trunk/out ./Debug/test webrtc/trunk/out/Debug ./test webrtc/trunk/out/Debug/subdir ../test webrtc/trunk/out/Debug/subdir ./../test I also made another program with its own main method (only links with 'test_support', not 'test_support_main') and made sure that it was still possible to use as before (i.e. works within the project tree but not above it): #include "testsupport/fileutils.h" int main(int argc, char** argv) { printf("Working dir: %s\n", webrtc::test::WorkingDir().c_str()); printf("Project root: %s\n", webrtc::test::ProjectRootPath().c_str()); printf("Output path: %s\n", webrtc::test::OutputPath().c_str()); } BUG=Existing implementation cannot handle when the working directory is outside the project checkout. TEST=test_support_unittests and manual tests with video_codecs_test_framework_integration_tests + passing all trybots + memcheck tool Review URL: https://webrtc-codereview.appspot.com/858014 git-svn-id: http://webrtc.googlecode.com/svn/trunk@2927 4adac7df-926f-26a2-2b94-8c16560cd09d
2012-10-15 18:14:12 +00:00
struct stat file_info = {0};
return stat(file_name.c_str(), &file_info) == 0;
}
bool DirExists(const std::string& directory_name) {
struct stat directory_info = {0};
return stat(directory_name.c_str(), &directory_info) == 0 &&
S_ISDIR(directory_info.st_mode);
}
std::string OutputPath() {
return webrtc::test::internal::OutputPath();
}
std::string WorkingDir() {
return webrtc::test::internal::WorkingDir();
}
// Generate a temporary filename in a safe way.
// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
std::string TempFilename(const std::string& dir, const std::string& prefix) {
#ifdef WIN32
wchar_t filename[MAX_PATH];
if (::GetTempFileNameW(rtc::ToUtf16(dir).c_str(),
rtc::ToUtf16(prefix).c_str(), 0, filename) != 0)
return rtc::ToUtf8(filename);
RTC_DCHECK_NOTREACHED();
return "";
#else
int len = dir.size() + prefix.size() + 2 + 6;
std::unique_ptr<char[]> tempname(new char[len]);
snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(), prefix.c_str());
int fd = ::mkstemp(tempname.get());
if (fd == -1) {
RTC_DCHECK_NOTREACHED();
return "";
} else {
::close(fd);
}
std::string ret(tempname.get());
return ret;
#endif
}
std::string GenerateTempFilename(const std::string& dir,
const std::string& prefix) {
std::string filename = TempFilename(dir, prefix);
RemoveFile(filename);
return filename;
}
absl::optional<std::vector<std::string>> ReadDirectory(std::string path) {
if (path.length() == 0)
return absl::optional<std::vector<std::string>>();
#if defined(WEBRTC_WIN)
// Append separator character if needed.
if (path.back() != '\\')
path += '\\';
// Init.
WIN32_FIND_DATAW data;
HANDLE handle = ::FindFirstFileW(rtc::ToUtf16(path + '*').c_str(), &data);
if (handle == INVALID_HANDLE_VALUE)
return absl::optional<std::vector<std::string>>();
// 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);
} while (::FindNextFileW(handle, &data) == TRUE);
// 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)
return absl::optional<std::vector<std::string>>();
// 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
return absl::optional<std::vector<std::string>>(std::move(found_entries));
}
bool CreateDir(const std::string& directory_name) {
struct stat path_info = {0};
// Check if the path exists already:
if (stat(directory_name.c_str(), &path_info) == 0) {
if (!S_ISDIR(path_info.st_mode)) {
fprintf(stderr,
"Path %s exists but is not a directory! Remove this "
"file and re-run to create the directory.\n",
directory_name.c_str());
return false;
}
} else {
#ifdef WIN32
return _mkdir(directory_name.c_str()) == 0;
#else
return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
#endif
}
return true;
}
Reland of reduce dependencies on rtc::FileSystem in FileRotatingStream tests... (patchset #1 id:1 of https://codereview.webrtc.org/2885393002/ ) Reason for revert: Downstream project now fixed. Original issue's description: > Revert of Reduce dependencies on rtc::FileSystem in FileRotatingStream tests, adding helpers in webrtc::test:: (patchset #7 id:120001 of https://codereview.webrtc.org/2872283002/ ) > > Reason for revert: > Fails to compile successfully. > > > Original issue's description: > > Reduce dependencies on rtc::FileSystem in FileRotatingStream tests. > > > > Use webrtc::test::OutputPath instead of Filesystem::GetAppTempFolder. > > Added functions RemoveFile and RemoveDir in the webrtc::test namespace, > > to replace use of Filesystem::DeleteFolderAndContents. > > > > This makes Filesystem::DeleteFolderAndContents unused, to be deleted > > together with related code in a followup cl. > > > > BUG=webrtc:7345 > > > > Review-Url: https://codereview.webrtc.org/2872283002 > > Cr-Commit-Position: refs/heads/master@{#18173} > > Committed: https://chromium.googlesource.com/external/webrtc/+/dd7b5f32b59d9def668b9e9487589a572f20f6e0 > > TBR=pthatcher@webrtc.org,kjellander@webrtc.org,tommi@webrtc.org,nisse@webrtc.org > # Skipping CQ checks because original CL landed less than 1 days ago. > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > BUG=webrtc:7345 > > Review-Url: https://codereview.webrtc.org/2885393002 > Cr-Commit-Position: refs/heads/master@{#18180} > Committed: https://chromium.googlesource.com/external/webrtc/+/deaa33d2f59783e849ddc01428be807d92431983 TBR=pthatcher@webrtc.org,kjellander@webrtc.org,tommi@webrtc.org,ehmaldonado@webrtc.org # Not skipping CQ checks because original CL landed more than 1 days ago. BUG=webrtc:7345 Review-Url: https://codereview.webrtc.org/2885413002 Cr-Commit-Position: refs/heads/master@{#18193}
2017-05-18 03:55:59 -07:00
bool RemoveDir(const std::string& directory_name) {
#ifdef WIN32
return RemoveDirectoryA(directory_name.c_str()) != FALSE;
#else
return rmdir(directory_name.c_str()) == 0;
#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
}
std::string ResourcePath(const std::string& name,
const std::string& extension) {
return webrtc::test::internal::ResourcePath(name, extension);
}
std::string JoinFilename(const std::string& dir, const std::string& name) {
RTC_CHECK(!dir.empty()) << "Special cases not implemented.";
return dir + kPathDelimiter + name;
}
size_t GetFileSize(const std::string& filename) {
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;
}
} // namespace test
} // namespace webrtc