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