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