[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 5 | // Defines base::PathProviderPosix, default path provider on POSIX OSes that |
| 6 | // don't have their own base_paths_OS.cc implementation (i.e. all but Mac and |
| 7 | // Android). |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 8 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 9 | #include "base/base_paths.h" |
| 10 | |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 11 | #include <limits.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 12 | #include <stddef.h> |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 13 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 14 | #include <memory> |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 15 | #include <ostream> |
| 16 | #include <string> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 17 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 18 | #include "base/environment.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 19 | #include "base/files/file_path.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 21 | #include "base/logging.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 22 | #include "base/nix/xdg_util.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 23 | #include "base/path_service.h" |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 24 | #include "base/process/process_metrics.h" |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 25 | #include "build/build_config.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 26 | |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 27 | #if defined(OS_FREEBSD) |
| 28 | #include <sys/param.h> |
| 29 | #include <sys/sysctl.h> |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 30 | #elif defined(OS_SOLARIS) |
| 31 | #include <stdlib.h> |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 32 | #endif |
| 33 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 34 | namespace base { |
| 35 | |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 36 | bool PathProviderPosix(int key, FilePath* result) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 37 | FilePath path; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 38 | switch (key) { |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 39 | case FILE_EXE: |
| 40 | case FILE_MODULE: { // TODO(evanm): is this correct? |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 41 | #if defined(OS_LINUX) |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 42 | FilePath bin_dir; |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 43 | if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) { |
[email protected] | 094797b7 | 2012-09-15 10:51:05 | [diff] [blame] | 44 | NOTREACHED() << "Unable to resolve " << kProcSelfExe << "."; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 45 | return false; |
| 46 | } |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 47 | *result = bin_dir; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 48 | return true; |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 49 | #elif defined(OS_FREEBSD) |
| 50 | int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; |
| 51 | char bin_dir[PATH_MAX + 1]; |
| 52 | size_t length = sizeof(bin_dir); |
[email protected] | 72d7b96 | 2011-10-25 16:22:27 | [diff] [blame] | 53 | // Upon return, |length| is the number of bytes written to |bin_dir| |
| 54 | // including the string terminator. |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 55 | int error = sysctl(name, 4, bin_dir, &length, NULL, 0); |
[email protected] | 72d7b96 | 2011-10-25 16:22:27 | [diff] [blame] | 56 | if (error < 0 || length <= 1) { |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 57 | NOTREACHED() << "Unable to resolve path."; |
| 58 | return false; |
| 59 | } |
[email protected] | 72d7b96 | 2011-10-25 16:22:27 | [diff] [blame] | 60 | *result = FilePath(FilePath::StringType(bin_dir, length - 1)); |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 61 | return true; |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 62 | #elif defined(OS_SOLARIS) |
| 63 | char bin_dir[PATH_MAX + 1]; |
| 64 | if (realpath(getexecname(), bin_dir) == NULL) { |
| 65 | NOTREACHED() << "Unable to resolve " << getexecname() << "."; |
| 66 | return false; |
| 67 | } |
| 68 | *result = FilePath(bin_dir); |
| 69 | return true; |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 70 | #elif defined(OS_OPENBSD) |
| 71 | // There is currently no way to get the executable path on OpenBSD |
[email protected] | 094797b7 | 2012-09-15 10:51:05 | [diff] [blame] | 72 | char* cpath; |
[email protected] | ea725b3 | 2011-10-25 17:43:05 | [diff] [blame] | 73 | if ((cpath = getenv("CHROME_EXE_PATH")) != NULL) |
| 74 | *result = FilePath(cpath); |
| 75 | else |
| 76 | *result = FilePath("/usr/local/chrome/chrome"); |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 77 | return true; |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 78 | #endif |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 79 | } |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 80 | case DIR_SOURCE_ROOT: { |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 81 | // Allow passing this in the environment, for more flexibility in build |
| 82 | // tree configurations (sub-project builds, gyp --output_dir, etc.) |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 83 | std::unique_ptr<Environment> env(Environment::Create()); |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 84 | std::string cr_source_root; |
| 85 | if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) { |
| 86 | path = FilePath(cr_source_root); |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 87 | if (PathExists(path)) { |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 88 | *result = path; |
| 89 | return true; |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 90 | } |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 91 | DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not " |
| 92 | << "point to a directory."; |
[email protected] | 3766ed1c | 2012-07-26 20:53:56 | [diff] [blame] | 93 | } |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 94 | // On POSIX, unit tests execute two levels deep from the source root. |
[email protected] | 016498e | 2010-12-03 00:59:23 | [diff] [blame] | 95 | // For example: out/{Debug|Release}/net_unittest |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 96 | if (PathService::Get(DIR_EXE, &path)) { |
[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 97 | *result = path.DirName().DirName(); |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 98 | return true; |
| 99 | } |
[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 100 | |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 101 | DLOG(ERROR) << "Couldn't find your source root. " |
| 102 | << "Try running from your chromium/src directory."; |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 103 | return false; |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 104 | } |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 105 | case DIR_USER_DESKTOP: |
| 106 | *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop"); |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 107 | return true; |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 108 | case DIR_CACHE: { |
| 109 | std::unique_ptr<Environment> env(Environment::Create()); |
| 110 | FilePath cache_dir( |
| 111 | nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache")); |
[email protected] | 9e9b6e8e | 2009-12-02 08:45:01 | [diff] [blame] | 112 | *result = cache_dir; |
| 113 | return true; |
[email protected] | 92e44ae0a | 2012-07-23 08:22:44 | [diff] [blame] | 114 | } |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | } // namespace base |