blob: 93dfaf67b5d3a06565bfbfc83069ba782ea9c05b [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.org0db7dc62011-11-13 01:34:05 +000039static const char* kOutputFallbackPath = "./";
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000040const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
41
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000042std::string ProjectRootPath() {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000043 char path_buffer[FILENAME_MAX];
44 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
45 fprintf(stderr, "Cannot get current directory!\n");
46 return kCannotFindProjectRootDir;
47 }
48
49 // Check for our file that verifies the root dir.
50 std::string current_path(path_buffer);
51 FILE* file = NULL;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000052 int path_delimiter_index = current_path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000053 while (path_delimiter_index > -1) {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000054 std::string root_filename = current_path + kPathDelimiter +
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000055 kProjectRootFileName;
56 file = fopen(root_filename.c_str(), "r");
57 if (file != NULL) {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000058 return current_path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000059 }
60
61 // Move up one directory in the directory tree.
62 current_path = current_path.substr(0, path_delimiter_index);
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000063 path_delimiter_index = current_path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000064 }
65
66 // 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) {
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000074 return kOutputFallbackPath;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000075 }
76 path += kOutputDirName;
77 struct stat path_info = {0};
78 // Check if the path exists already:
79 if (stat(path.c_str(), &path_info) == 0) {
80 if (!S_ISDIR(path_info.st_mode)) {
81 fprintf(stderr, "Path %s exists but is not a directory! Remove this file "
82 "and re-run to create the output folder.\n", path.c_str());
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000083 return kOutputFallbackPath;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000084 }
85 } else {
86#ifdef WIN32
87 _mkdir(path.c_str());
88#else
89 mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
90#endif
91 }
92 return path + kPathDelimiter;
93}
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000094} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000095} // namespace webrtc