blob: 129651fd9f343f2fc9285352b105f23e46cdff6d [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
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
11#include "fileutils.h"
12
13#ifdef WIN32
14#include <direct.h>
15#define GET_CURRENT_DIR _getcwd
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000016#else
17#include <unistd.h>
18#define GET_CURRENT_DIR getcwd
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000019#endif
20
21#include <sys/stat.h> // To check for directory existence.
22#ifndef S_ISDIR // Not defined in stat.h on Windows.
23#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000024#endif
25
26#include <cstdio>
27
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000028#include "typedefs.h" // For architecture defines
29
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000030namespace webrtc {
31namespace test {
32
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000033#ifdef WIN32
34static const char* kPathDelimiter = "\\";
35#else
36static const char* kPathDelimiter = "/";
37#endif
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000038// The file we're looking for to identify the project root dir.
39static const char* kProjectRootFileName = "DEPS";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000040static const char* kOutputDirName = "out";
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000041static const char* kFallbackPath = "./";
42static const char* kResourcesDirName = "resources";
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000043const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
44
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000045std::string ProjectRootPath() {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000046 std::string working_dir = WorkingDir();
47 if (working_dir == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000048 return kCannotFindProjectRootDir;
49 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000050 // Check for our file that verifies the root dir.
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000051 std::string current_path(working_dir);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000052 FILE* file = NULL;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000053 int path_delimiter_index = current_path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000054 while (path_delimiter_index > -1) {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000055 std::string root_filename = current_path + kPathDelimiter +
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000056 kProjectRootFileName;
57 file = fopen(root_filename.c_str(), "r");
58 if (file != NULL) {
kjellander@webrtc.org54832102011-11-25 09:33:41 +000059 fclose(file);
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000060 return current_path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000061 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000062 // Move up one directory in the directory tree.
63 current_path = current_path.substr(0, path_delimiter_index);
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000064 path_delimiter_index = current_path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000065 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000066 // Reached the root directory.
67 fprintf(stderr, "Cannot find project root directory!\n");
68 return kCannotFindProjectRootDir;
69}
70
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000071std::string OutputPath() {
72 std::string path = ProjectRootPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000073 if (path == kCannotFindProjectRootDir) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000074 return kFallbackPath;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000075 }
76 path += kOutputDirName;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000077 if (!CreateDirectory(path)) {
78 return kFallbackPath;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000079 }
80 return path + kPathDelimiter;
81}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000082
83std::string WorkingDir() {
84 char path_buffer[FILENAME_MAX];
85 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
86 fprintf(stderr, "Cannot get current directory!\n");
87 return kFallbackPath;
88 }
89 else {
90 return std::string(path_buffer);
91 }
92}
93
94bool CreateDirectory(std::string directory_name) {
95 struct stat path_info = {0};
96 // Check if the path exists already:
97 if (stat(directory_name.c_str(), &path_info) == 0) {
98 if (!S_ISDIR(path_info.st_mode)) {
99 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
100 "file and re-run to create the directory.\n",
101 directory_name.c_str());
102 return false;
103 }
104 } else {
105#ifdef WIN32
106 return _mkdir(directory_name.c_str()) == 0;
107#else
108 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
109#endif
110 }
111 return true;
112}
113
114bool FileExists(std::string file_name) {
115 struct stat file_info = {0};
116 return stat(file_name.c_str(), &file_info) == 0;
117}
118
119std::string ResourcePath(std::string name, std::string extension) {
120 std::string platform = "win";
121#ifdef WEBRTC_LINUX
122 platform = "linux";
123#endif // WEBRTC_LINUX
124#ifdef WEBRTC_MAC
125 platform = "mac";
126#endif // WEBRTC_MAC
127
128#ifdef WEBRTC_ARCH_64_BITS
129 std::string architecture = "64";
130#else
131 std::string architecture = "32";
132#endif // WEBRTC_ARCH_64_BITS
133
134 std::string resources_path = ProjectRootPath() + kResourcesDirName + kPathDelimiter;
135 std::string resource_file = resources_path + name + "_" + platform + "_" +
136 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000137 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000138 return resource_file;
139 }
140 // Try without architecture.
141 resource_file = resources_path + name + "_" + platform + "." + extension;
142 if (FileExists(resource_file)) {
143 return resource_file;
144 }
145 // Try without platform.
146 resource_file = resources_path + name + "_" + architecture + "." + extension;
147 if (FileExists(resource_file)) {
148 return resource_file;
149 }
150 // Fall back on name without architecture or platform.
151 return resources_path + name + "." + extension;
152}
153
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000154} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000155} // namespace webrtc