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.
4
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).
8
9 #include "base/base_paths.h"
10
11 #include <limits.h>
12 #include <stddef.h>
13
14 #include <memory>
15 #include <ostream>
16 #include <string>
17
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 // Unused, and this file is not ported to libchrome.
23 // #include "base/nix/xdg_util.h"
24 #include "base/path_service.h"
25 #include "base/process/process_metrics.h"
26 #include "build/build_config.h"
27
28 #if defined(OS_FREEBSD)
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
31 #elif defined(OS_SOLARIS) || defined(OS_AIX)
32 #include <stdlib.h>
33 #endif
34
35 namespace base {
36
PathProviderPosix(int key,FilePath * result)37 bool PathProviderPosix(int key, FilePath* result) {
38 switch (key) {
39 case FILE_EXE:
40 case FILE_MODULE: { // TODO(evanm): is this correct?
41 #if defined(OS_LINUX)
42 FilePath bin_dir;
43 if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
44 NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
45 return false;
46 }
47 *result = bin_dir;
48 return true;
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.";
58 return false;
59 }
60 *result = FilePath(FilePath::StringType(bin_dir, length - 1));
61 return true;
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;
70 #elif defined(OS_OPENBSD) || defined(OS_AIX)
71 // There is currently no way to get the executable path on OpenBSD
72 char* cpath;
73 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
74 *result = FilePath(cpath);
75 else
76 *result = FilePath("/usr/local/chrome/chrome");
77 return true;
78 #endif
79 }
80 // Following paths are not supported in libchrome/libmojo.
81 #if 0
82 case DIR_SOURCE_ROOT: {
83 // Allow passing this in the environment, for more flexibility in build
84 // tree configurations (sub-project builds, gyp --output_dir, etc.)
85 std::unique_ptr<Environment> env(Environment::Create());
86 std::string cr_source_root;
87 FilePath path;
88 if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
89 path = FilePath(cr_source_root);
90 if (PathExists(path)) {
91 *result = path;
92 return true;
93 }
94 DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
95 << "point to a directory.";
96 }
97 // On POSIX, unit tests execute two levels deep from the source root.
98 // For example: out/{Debug|Release}/net_unittest
99 if (PathService::Get(DIR_EXE, &path)) {
100 *result = path.DirName().DirName();
101 return true;
102 }
103
104 DLOG(ERROR) << "Couldn't find your source root. "
105 << "Try running from your chromium/src directory.";
106 return false;
107 }
108 case DIR_USER_DESKTOP:
109 *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop");
110 return true;
111 case DIR_CACHE: {
112 std::unique_ptr<Environment> env(Environment::Create());
113 FilePath cache_dir(
114 nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache"));
115 *result = cache_dir;
116 return true;
117 }
118 #endif
119 }
120 return false;
121 }
122
123 } // namespace base
124