| 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 | #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" |
| 26 |
| 27 | #if defined(OS_FREEBSD) |
| 28 | #include <sys/param.h> |
| 29 | #include <sys/sysctl.h> |
| 30 | #elif defined(OS_SOLARIS) || defined(OS_AIX) |
| 31 | #include <stdlib.h> |
| 32 | #endif |
| 33 |
| 34 | namespace base { |
| 35 |
| 36 | bool PathProviderPosix(int key, FilePath* result) { |
| 37 | switch (key) { |
| 38 | case FILE_EXE: |
| 39 | case FILE_MODULE: { // TODO(evanm): is this correct? |
| 40 | #if defined(OS_LINUX) |
| 41 | FilePath bin_dir; |
| 42 | if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) { |
| 43 | NOTREACHED() << "Unable to resolve " << kProcSelfExe << "."; |
| 44 | return false; |
| 45 | } |
| 46 | *result = bin_dir; |
| 47 | return true; |
| 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."; |
| 57 | return false; |
| 58 | } |
| 59 | *result = FilePath(FilePath::StringType(bin_dir, length - 1)); |
| 60 | return true; |
| 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() << "."; |
| 65 | return false; |
| 66 | } |
| 67 | *result = FilePath(bin_dir); |
| 68 | return true; |
| 69 | #elif defined(OS_OPENBSD) || defined(OS_AIX) |
| 70 | // There is currently no way to get the executable path on OpenBSD |
| 71 | char* cpath; |
| 72 | if ((cpath = getenv("CHROME_EXE_PATH")) != NULL) |
| 73 | *result = FilePath(cpath); |
| 74 | else |
| 75 | *result = FilePath("/usr/local/chrome/chrome"); |
| 76 | return true; |
| 77 | #endif |
| 78 | } |
| 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; |
| 84 | FilePath path; |
| 85 | if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) { |
| 86 | path = FilePath(cr_source_root); |
| 87 | if (PathExists(path)) { |
| 88 | *result = path; |
| 89 | return true; |
| 90 | } |
| 91 | DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not " |
| 92 | << "point to a directory."; |
| 93 | } |
| 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(); |
| 98 | return true; |
| 99 | } |
| 100 |
| 101 | DLOG(ERROR) << "Couldn't find your source root. " |
| 102 | << "Try running from your chromium/src directory."; |
| 103 | return false; |
| 104 | } |
| 105 | case DIR_USER_DESKTOP: |
| 106 | *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop"); |
| 107 | return true; |
| 108 | case DIR_CACHE: { |
| 109 | std::unique_ptr<Environment> env(Environment::Create()); |
| 110 | FilePath cache_dir( |
| 111 | nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache")); |
| 112 | *result = cache_dir; |
| 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 |
| 119 | } // namespace base |
| 120 |