blob: 09d7baa34952a1bbe18f4ff1400bede23d94cffa [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.org54832102011-11-25 09:33:41 +000058 fclose(file);
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000059 return current_path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000060 }
61
62 // 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 }
66
67 // Reached the root directory.
68 fprintf(stderr, "Cannot find project root directory!\n");
69 return kCannotFindProjectRootDir;
70}
71
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000072std::string OutputPath() {
73 std::string path = ProjectRootPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000074 if (path == kCannotFindProjectRootDir) {
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000075 return kOutputFallbackPath;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000076 }
77 path += kOutputDirName;
78 struct stat path_info = {0};
79 // Check if the path exists already:
80 if (stat(path.c_str(), &path_info) == 0) {
81 if (!S_ISDIR(path_info.st_mode)) {
82 fprintf(stderr, "Path %s exists but is not a directory! Remove this file "
83 "and re-run to create the output folder.\n", path.c_str());
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000084 return kOutputFallbackPath;
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000085 }
86 } else {
87#ifdef WIN32
88 _mkdir(path.c_str());
89#else
90 mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
91#endif
92 }
93 return path + kPathDelimiter;
94}
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000095} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000096} // namespace webrtc