1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
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
9 #include "base/base_paths.h"
18 #include "base/environment.h"
19 #include "base/files/file_path.h"
20 #include "base/files/file_util.h"
21 #include "base/logging.h"
22 #include "base/nix/xdg_util.h"
23 #include "base/notreached.h"
24 #include "base/path_service.h"
25 #include "base/process/process_metrics.h"
26 #include "build/build_config.h"
28 #if defined(OS_FREEBSD)
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
31 #elif defined(OS_SOLARIS) || defined(OS_AIX)
37 bool PathProviderPosix(int key, FilePath* result) {
40 case FILE_MODULE: { // TODO(evanm): is this correct?
41 #if defined(OS_LINUX) || defined(OS_CHROMEOS)
43 if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
44 NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
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);
53 // Upon return, |length| is the number of bytes written to |bin_dir|
54 // including the string terminator.
55 int error = sysctl(name, 4, bin_dir, &length, NULL, 0);
56 if (error < 0 || length <= 1) {
57 NOTREACHED() << "Unable to resolve path.";
60 *result = FilePath(FilePath::StringType(bin_dir, length - 1));
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() << ".";
68 *result = FilePath(bin_dir);
70 #elif defined(OS_OPENBSD) || defined(OS_AIX)
71 // There is currently no way to get the executable path on OpenBSD
73 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
74 *result = FilePath(cpath);
76 *result = FilePath("/usr/local/chrome/chrome");
80 case DIR_SOURCE_ROOT: {
81 // Allow passing this in the environment, for more flexibility in build
82 // tree configurations (sub-project builds, gyp --output_dir, etc.)
83 std::unique_ptr<Environment> env(Environment::Create());
84 std::string cr_source_root;
86 if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
87 path = FilePath(cr_source_root);
88 if (PathExists(path)) {
92 DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
93 << "point to a directory.";
95 // On POSIX, unit tests execute two levels deep from the source root.
96 // For example: out/{Debug|Release}/net_unittest
97 if (PathService::Get(DIR_EXE, &path)) {
98 *result = path.DirName().DirName();
102 DLOG(ERROR) << "Couldn't find your source root. "
103 << "Try running from your chromium/src directory.";
106 case DIR_USER_DESKTOP:
107 *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop");
110 std::unique_ptr<Environment> env(Environment::Create());
112 nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache"));