blob: baba3cd83f3be503185008ecb37007fbd11d37cb [file] [log] [blame]
[email protected]569edabd2012-02-03 23:10:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b2e97292008-09-02 18:20:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]dea1d7d2012-09-20 16:24:525// 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).
[email protected]b2e97292008-09-02 18:20:348
dcheng093de9b2016-04-04 21:25:519#include "base/base_paths.h"
10
avi51ba3e692015-12-26 17:30:5011#include <limits.h>
avi9b6f42932015-12-26 22:15:1412#include <stddef.h>
avi51ba3e692015-12-26 17:30:5013
dcheng093de9b2016-04-04 21:25:5114#include <memory>
[email protected]2edc2862011-04-04 18:04:3715#include <ostream>
16#include <string>
[email protected]b2e97292008-09-02 18:20:3417
[email protected]76b90d312010-08-03 03:00:5018#include "base/environment.h"
[email protected]57999812013-02-24 05:40:5219#include "base/files/file_path.h"
[email protected]e3177dd52014-08-13 20:22:1420#include "base/files/file_util.h"
[email protected]b2e97292008-09-02 18:20:3421#include "base/logging.h"
[email protected]57999812013-02-24 05:40:5222#include "base/nix/xdg_util.h"
[email protected]b2e97292008-09-02 18:20:3423#include "base/path_service.h"
[email protected]dd4b51262013-07-25 21:38:2324#include "base/process/process_metrics.h"
[email protected]dea1d7d2012-09-20 16:24:5225#include "build/build_config.h"
[email protected]b2e97292008-09-02 18:20:3426
[email protected]2edc2862011-04-04 18:04:3727#if defined(OS_FREEBSD)
28#include <sys/param.h>
29#include <sys/sysctl.h>
[email protected]94f8c952011-06-25 04:54:4130#elif defined(OS_SOLARIS)
31#include <stdlib.h>
[email protected]2edc2862011-04-04 18:04:3732#endif
33
[email protected]b2e97292008-09-02 18:20:3434namespace base {
35
[email protected]5d1937bb2009-11-21 01:29:0036bool PathProviderPosix(int key, FilePath* result) {
[email protected]640517f2008-10-30 23:54:0437 FilePath path;
[email protected]b2e97292008-09-02 18:20:3438 switch (key) {
thestigbf8ab1b2016-11-19 04:15:4239 case FILE_EXE:
40 case FILE_MODULE: { // TODO(evanm): is this correct?
[email protected]99aae102010-05-10 16:30:2741#if defined(OS_LINUX)
[email protected]723571a2010-12-03 17:37:5442 FilePath bin_dir;
[email protected]b264eab2013-11-27 23:22:0843 if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
[email protected]094797b72012-09-15 10:51:0544 NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
[email protected]b2e97292008-09-02 18:20:3445 return false;
46 }
[email protected]723571a2010-12-03 17:37:5447 *result = bin_dir;
[email protected]b2e97292008-09-02 18:20:3448 return true;
[email protected]99aae102010-05-10 16:30:2749#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);
[email protected]72d7b962011-10-25 16:22:2753 // Upon return, |length| is the number of bytes written to |bin_dir|
54 // including the string terminator.
[email protected]99aae102010-05-10 16:30:2755 int error = sysctl(name, 4, bin_dir, &length, NULL, 0);
[email protected]72d7b962011-10-25 16:22:2756 if (error < 0 || length <= 1) {
[email protected]99aae102010-05-10 16:30:2757 NOTREACHED() << "Unable to resolve path.";
58 return false;
59 }
[email protected]72d7b962011-10-25 16:22:2760 *result = FilePath(FilePath::StringType(bin_dir, length - 1));
[email protected]99aae102010-05-10 16:30:2761 return true;
[email protected]94f8c952011-06-25 04:54:4162#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;
[email protected]817f0f142011-10-13 04:23:2270#elif defined(OS_OPENBSD)
71 // There is currently no way to get the executable path on OpenBSD
[email protected]094797b72012-09-15 10:51:0572 char* cpath;
[email protected]ea725b32011-10-25 17:43:0573 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
74 *result = FilePath(cpath);
75 else
76 *result = FilePath("/usr/local/chrome/chrome");
[email protected]817f0f142011-10-13 04:23:2277 return true;
[email protected]99aae102010-05-10 16:30:2778#endif
[email protected]b2e97292008-09-02 18:20:3479 }
thestigbf8ab1b2016-11-19 04:15:4280 case DIR_SOURCE_ROOT: {
[email protected]3766ed1c2012-07-26 20:53:5681 // Allow passing this in the environment, for more flexibility in build
82 // tree configurations (sub-project builds, gyp --output_dir, etc.)
thestigbf8ab1b2016-11-19 04:15:4283 std::unique_ptr<Environment> env(Environment::Create());
[email protected]3766ed1c2012-07-26 20:53:5684 std::string cr_source_root;
85 if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
86 path = FilePath(cr_source_root);
thestigbf8ab1b2016-11-19 04:15:4287 if (PathExists(path)) {
[email protected]3766ed1c2012-07-26 20:53:5688 *result = path;
89 return true;
[email protected]3766ed1c2012-07-26 20:53:5690 }
thestigbf8ab1b2016-11-19 04:15:4291 DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
92 << "point to a directory.";
[email protected]3766ed1c2012-07-26 20:53:5693 }
[email protected]5d1937bb2009-11-21 01:29:0094 // On POSIX, unit tests execute two levels deep from the source root.
[email protected]016498e2010-12-03 00:59:2395 // For example: out/{Debug|Release}/net_unittest
thestigbf8ab1b2016-11-19 04:15:4296 if (PathService::Get(DIR_EXE, &path)) {
[email protected]569edabd2012-02-03 23:10:0497 *result = path.DirName().DirName();
[email protected]95d050e2009-09-10 19:30:4698 return true;
99 }
[email protected]569edabd2012-02-03 23:10:04100
[email protected]a42d4632011-10-26 21:48:00101 DLOG(ERROR) << "Couldn't find your source root. "
102 << "Try running from your chromium/src directory.";
[email protected]95d050e2009-09-10 19:30:46103 return false;
[email protected]632be2f2010-04-21 23:28:43104 }
thestigbf8ab1b2016-11-19 04:15:42105 case DIR_USER_DESKTOP:
106 *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop");
[email protected]dea1d7d2012-09-20 16:24:52107 return true;
thestigbf8ab1b2016-11-19 04:15:42108 case DIR_CACHE: {
109 std::unique_ptr<Environment> env(Environment::Create());
110 FilePath cache_dir(
111 nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache"));
[email protected]9e9b6e8e2009-12-02 08:45:01112 *result = cache_dir;
113 return true;
[email protected]92e44ae0a2012-07-23 08:22:44114 }
[email protected]b2e97292008-09-02 18:20:34115 }
116 return false;
117}
118
119} // namespace base