blob: 9b54989b0c0392dbf4ea7129b61d8d8326d5bec6 [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
28namespace webrtc {
29namespace test {
30
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000031#ifdef WIN32
32static const char* kPathDelimiter = "\\";
33#else
34static const char* kPathDelimiter = "/";
35#endif
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000036// The file we're looking for to identify the project root dir.
37static const char* kProjectRootFileName = "DEPS";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000038static const char* kOutputDirName = "out";
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000039const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
40
41std::string GetProjectRootPath() {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000042 char path_buffer[FILENAME_MAX];
43 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
44 fprintf(stderr, "Cannot get current directory!\n");
45 return kCannotFindProjectRootDir;
46 }
47
48 // Check for our file that verifies the root dir.
49 std::string current_path(path_buffer);
50 FILE* file = NULL;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000051 int path_delimiter_index = current_path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000052 while (path_delimiter_index > -1) {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000053 std::string root_filename = current_path + kPathDelimiter +
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000054 kProjectRootFileName;
55 file = fopen(root_filename.c_str(), "r");
56 if (file != NULL) {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000057 return current_path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000058 }
59
60 // Move up one directory in the directory tree.
61 current_path = current_path.substr(0, path_delimiter_index);
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000062 path_delimiter_index = current_path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000063 }
64
65 // Reached the root directory.
66 fprintf(stderr, "Cannot find project root directory!\n");
67 return kCannotFindProjectRootDir;
68}
69
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000070std::string GetOutputDir() {
71 std::string path = GetProjectRootPath();
72 if (path == kCannotFindProjectRootDir) {
73 return kCannotFindProjectRootDir;
74 }
75 path += kOutputDirName;
76 struct stat path_info = {0};
77 // Check if the path exists already:
78 if (stat(path.c_str(), &path_info) == 0) {
79 if (!S_ISDIR(path_info.st_mode)) {
80 fprintf(stderr, "Path %s exists but is not a directory! Remove this file "
81 "and re-run to create the output folder.\n", path.c_str());
82 return kCannotFindProjectRootDir;
83 }
84 } else {
85#ifdef WIN32
86 _mkdir(path.c_str());
87#else
88 mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
89#endif
90 }
91 return path + kPathDelimiter;
92}
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000093} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000094} // namespace webrtc