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/path_service.h"
24 #include "base/process/process_metrics.h"
25 #include "build/build_config.h"
27 #if defined(OS_FREEBSD)
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30 #elif defined(OS_SOLARIS) || defined(OS_AIX)
36 bool PathProviderPosix(int key, FilePath* result) {
39 case FILE_MODULE: { // TODO(evanm): is this correct?
42 if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
43 NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
48 #elif defined(OS_FREEBSD)
49 int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
50 char bin_dir[PATH_MAX + 1];
51 size_t length = sizeof(bin_dir);
52 // Upon return, |length| is the number of bytes written to |bin_dir|
53 // including the string terminator.
54 int error = sysctl(name, 4, bin_dir, &length, NULL, 0);
55 if (error < 0 || length <= 1) {
56 NOTREACHED() << "Unable to resolve path.";
59 *result = FilePath(FilePath::StringType(bin_dir, length - 1));
61 #elif defined(OS_SOLARIS)
62 char bin_dir[PATH_MAX + 1];
63 if (realpath(getexecname(), bin_dir) == NULL) {
64 NOTREACHED() << "Unable to resolve " << getexecname() << ".";
67 *result = FilePath(bin_dir);
69 #elif defined(OS_OPENBSD) || defined(OS_AIX)
70 // There is currently no way to get the executable path on OpenBSD
72 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
73 *result = FilePath(cpath);
75 *result = FilePath("/usr/local/chrome/chrome");
79 case DIR_SOURCE_ROOT: {
80 // Allow passing this in the environment, for more flexibility in build
81 // tree configurations (sub-project builds, gyp --output_dir, etc.)
82 std::unique_ptr<Environment> env(Environment::Create());
83 std::string cr_source_root;
85 if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
86 path = FilePath(cr_source_root);
87 if (PathExists(path)) {
91 DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
92 << "point to a directory.";
94 // On POSIX, unit tests execute two levels deep from the source root.
95 // For example: out/{Debug|Release}/net_unittest
96 if (PathService::Get(DIR_EXE, &path)) {
97 *result = path.DirName().DirName();
101 DLOG(ERROR) << "Couldn't find your source root. "
102 << "Try running from your chromium/src directory.";
105 case DIR_USER_DESKTOP:
106 *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop");
109 std::unique_ptr<Environment> env(Environment::Create());
111 nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache"));