2011-10-13 14:24:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 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.
|
|
|
|
*/
|
|
|
|
|
2011-12-08 08:42:18 +01:00
|
|
|
#include "testsupport/fileutils.h"
|
2011-10-13 14:24:41 +02:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <direct.h>
|
|
|
|
#define GET_CURRENT_DIR _getcwd
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#define GET_CURRENT_DIR getcwd
|
2011-11-09 12:24:14 +01:00
|
|
|
#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)
|
2011-10-13 14:24:41 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
2011-12-05 17:31:12 +01:00
|
|
|
#include "typedefs.h" // For architecture defines
|
|
|
|
|
2011-10-13 14:24:41 +02:00
|
|
|
namespace webrtc {
|
|
|
|
namespace test {
|
|
|
|
|
2011-11-09 12:24:14 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
static const char* kPathDelimiter = "\\";
|
|
|
|
#else
|
|
|
|
static const char* kPathDelimiter = "/";
|
|
|
|
#endif
|
2011-10-31 21:22:02 +01:00
|
|
|
// The file we're looking for to identify the project root dir.
|
|
|
|
static const char* kProjectRootFileName = "DEPS";
|
2011-11-09 12:24:14 +01:00
|
|
|
static const char* kOutputDirName = "out";
|
2011-12-05 17:31:12 +01:00
|
|
|
static const char* kFallbackPath = "./";
|
|
|
|
static const char* kResourcesDirName = "resources";
|
2011-10-31 21:22:02 +01:00
|
|
|
const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
|
|
|
|
|
2011-11-13 02:34:05 +01:00
|
|
|
std::string ProjectRootPath() {
|
2011-12-05 17:31:12 +01:00
|
|
|
std::string working_dir = WorkingDir();
|
|
|
|
if (working_dir == kFallbackPath) {
|
2011-10-13 14:24:41 +02:00
|
|
|
return kCannotFindProjectRootDir;
|
|
|
|
}
|
|
|
|
// Check for our file that verifies the root dir.
|
2011-12-05 17:31:12 +01:00
|
|
|
std::string current_path(working_dir);
|
2011-10-13 14:24:41 +02:00
|
|
|
FILE* file = NULL;
|
2011-11-09 12:24:14 +01:00
|
|
|
int path_delimiter_index = current_path.find_last_of(kPathDelimiter);
|
2011-10-13 14:24:41 +02:00
|
|
|
while (path_delimiter_index > -1) {
|
2011-11-09 12:24:14 +01:00
|
|
|
std::string root_filename = current_path + kPathDelimiter +
|
2011-10-13 14:24:41 +02:00
|
|
|
kProjectRootFileName;
|
|
|
|
file = fopen(root_filename.c_str(), "r");
|
|
|
|
if (file != NULL) {
|
2011-11-25 10:33:41 +01:00
|
|
|
fclose(file);
|
2011-11-09 12:24:14 +01:00
|
|
|
return current_path + kPathDelimiter;
|
2011-10-13 14:24:41 +02:00
|
|
|
}
|
|
|
|
// Move up one directory in the directory tree.
|
|
|
|
current_path = current_path.substr(0, path_delimiter_index);
|
2011-11-09 12:24:14 +01:00
|
|
|
path_delimiter_index = current_path.find_last_of(kPathDelimiter);
|
2011-10-13 14:24:41 +02:00
|
|
|
}
|
|
|
|
// Reached the root directory.
|
|
|
|
fprintf(stderr, "Cannot find project root directory!\n");
|
|
|
|
return kCannotFindProjectRootDir;
|
|
|
|
}
|
|
|
|
|
2012-03-22 13:56:54 +01:00
|
|
|
#ifdef WEBRTC_ANDROID
|
|
|
|
|
|
|
|
std::string OutputPath() {
|
|
|
|
// We need to touch this variable so it doesn't get flagged as unused.
|
|
|
|
(void)kOutputDirName;
|
|
|
|
return "/sdcard/";
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // WEBRTC_ANDROID
|
|
|
|
|
2011-11-13 02:34:05 +01:00
|
|
|
std::string OutputPath() {
|
|
|
|
std::string path = ProjectRootPath();
|
2011-11-09 12:24:14 +01:00
|
|
|
if (path == kCannotFindProjectRootDir) {
|
2011-12-05 17:31:12 +01:00
|
|
|
return kFallbackPath;
|
2011-11-09 12:24:14 +01:00
|
|
|
}
|
|
|
|
path += kOutputDirName;
|
2011-12-05 17:31:12 +01:00
|
|
|
if (!CreateDirectory(path)) {
|
|
|
|
return kFallbackPath;
|
|
|
|
}
|
|
|
|
return path + kPathDelimiter;
|
|
|
|
}
|
|
|
|
|
2012-03-22 13:56:54 +01:00
|
|
|
#endif // !WEBRTC_ANDROID
|
|
|
|
|
2011-12-05 17:31:12 +01:00
|
|
|
std::string WorkingDir() {
|
|
|
|
char path_buffer[FILENAME_MAX];
|
|
|
|
if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
|
|
|
|
fprintf(stderr, "Cannot get current directory!\n");
|
|
|
|
return kFallbackPath;
|
2011-12-08 08:42:18 +01:00
|
|
|
} else {
|
2011-12-05 17:31:12 +01:00
|
|
|
return std::string(path_buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CreateDirectory(std::string directory_name) {
|
2011-11-09 12:24:14 +01:00
|
|
|
struct stat path_info = {0};
|
|
|
|
// Check if the path exists already:
|
2011-12-05 17:31:12 +01:00
|
|
|
if (stat(directory_name.c_str(), &path_info) == 0) {
|
2011-11-09 12:24:14 +01:00
|
|
|
if (!S_ISDIR(path_info.st_mode)) {
|
2011-12-05 17:31:12 +01:00
|
|
|
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;
|
2011-11-09 12:24:14 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#ifdef WIN32
|
2011-12-05 17:31:12 +01:00
|
|
|
return _mkdir(directory_name.c_str()) == 0;
|
2011-11-09 12:24:14 +01:00
|
|
|
#else
|
2011-12-05 17:31:12 +01:00
|
|
|
return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
|
2011-11-09 12:24:14 +01:00
|
|
|
#endif
|
|
|
|
}
|
2011-12-05 17:31:12 +01:00
|
|
|
return true;
|
2011-11-09 12:24:14 +01:00
|
|
|
}
|
2011-12-05 17:31:12 +01:00
|
|
|
|
|
|
|
bool FileExists(std::string file_name) {
|
|
|
|
struct stat file_info = {0};
|
|
|
|
return stat(file_name.c_str(), &file_info) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ResourcePath(std::string name, std::string extension) {
|
|
|
|
std::string platform = "win";
|
|
|
|
#ifdef WEBRTC_LINUX
|
|
|
|
platform = "linux";
|
|
|
|
#endif // WEBRTC_LINUX
|
|
|
|
#ifdef WEBRTC_MAC
|
|
|
|
platform = "mac";
|
|
|
|
#endif // WEBRTC_MAC
|
|
|
|
|
|
|
|
#ifdef WEBRTC_ARCH_64_BITS
|
|
|
|
std::string architecture = "64";
|
|
|
|
#else
|
|
|
|
std::string architecture = "32";
|
|
|
|
#endif // WEBRTC_ARCH_64_BITS
|
|
|
|
|
2011-12-08 08:42:18 +01:00
|
|
|
std::string resources_path = ProjectRootPath() + kResourcesDirName +
|
|
|
|
kPathDelimiter;
|
2011-12-05 17:31:12 +01:00
|
|
|
std::string resource_file = resources_path + name + "_" + platform + "_" +
|
|
|
|
architecture + "." + extension;
|
2011-12-07 19:50:17 +01:00
|
|
|
if (FileExists(resource_file)) {
|
2011-12-05 17:31:12 +01:00
|
|
|
return resource_file;
|
|
|
|
}
|
|
|
|
// Try without architecture.
|
|
|
|
resource_file = resources_path + name + "_" + platform + "." + extension;
|
|
|
|
if (FileExists(resource_file)) {
|
|
|
|
return resource_file;
|
|
|
|
}
|
|
|
|
// Try without platform.
|
|
|
|
resource_file = resources_path + name + "_" + architecture + "." + extension;
|
|
|
|
if (FileExists(resource_file)) {
|
|
|
|
return resource_file;
|
|
|
|
}
|
|
|
|
// Fall back on name without architecture or platform.
|
|
|
|
return resources_path + name + "." + extension;
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:42:18 +01:00
|
|
|
size_t GetFileSize(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;
|
|
|
|
}
|
|
|
|
|
2011-10-13 14:24:41 +02:00
|
|
|
} // namespace test
|
2011-11-09 12:24:14 +01:00
|
|
|
} // namespace webrtc
|