kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 16 | #else |
| 17 | #include <unistd.h> |
| 18 | #define GET_CURRENT_DIR getcwd |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 19 | #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.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | #include <cstdio> |
| 27 | |
| 28 | namespace webrtc { |
| 29 | namespace test { |
| 30 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 31 | #ifdef WIN32 |
| 32 | static const char* kPathDelimiter = "\\"; |
| 33 | #else |
| 34 | static const char* kPathDelimiter = "/"; |
| 35 | #endif |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 +0000 | [diff] [blame] | 36 | // The file we're looking for to identify the project root dir. |
| 37 | static const char* kProjectRootFileName = "DEPS"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 38 | static const char* kOutputDirName = "out"; |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 39 | static const char* kOutputFallbackPath = "./"; |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 +0000 | [diff] [blame] | 40 | const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR"; |
| 41 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 42 | std::string ProjectRootPath() { |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 43 | 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.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 52 | int path_delimiter_index = current_path.find_last_of(kPathDelimiter); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 53 | while (path_delimiter_index > -1) { |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 54 | std::string root_filename = current_path + kPathDelimiter + |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 55 | kProjectRootFileName; |
| 56 | file = fopen(root_filename.c_str(), "r"); |
| 57 | if (file != NULL) { |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 58 | return current_path + kPathDelimiter; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Move up one directory in the directory tree. |
| 62 | current_path = current_path.substr(0, path_delimiter_index); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 63 | path_delimiter_index = current_path.find_last_of(kPathDelimiter); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Reached the root directory. |
| 67 | fprintf(stderr, "Cannot find project root directory!\n"); |
| 68 | return kCannotFindProjectRootDir; |
| 69 | } |
| 70 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 71 | std::string OutputPath() { |
| 72 | std::string path = ProjectRootPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 73 | if (path == kCannotFindProjectRootDir) { |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 74 | return kOutputFallbackPath; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 75 | } |
| 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.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 83 | return kOutputFallbackPath; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 84 | } |
| 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.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 94 | } // namespace test |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 95 | } // namespace webrtc |