blob: 6d7b5e13c59b62b5f09065c3f49e4df189e53e98 [file] [log] [blame]
jochen@chromium.org48995032012-02-04 08:10:04 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
mmentovai@google.comaa13be62008-09-03 03:20:34 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
gab@chromium.org97fc1e62012-09-21 01:24:52 +09005// 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).
mmentovai@google.comaa13be62008-09-03 03:20:34 +09008
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +09009#include <ostream>
10#include <string>
mmentovai@google.comaa13be62008-09-03 03:20:34 +090011
gab@chromium.org97fc1e62012-09-21 01:24:52 +090012#include "base/base_paths.h"
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +090013#include "base/environment.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090014#include "base/file_util.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090015#include "base/files/file_path.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090016#include "base/logging.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090017#include "base/memory/scoped_ptr.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090018#include "base/nix/xdg_util.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090019#include "base/path_service.h"
thestig@chromium.org0c1da822012-09-15 19:51:05 +090020#include "base/process_util.h"
gab@chromium.org97fc1e62012-09-21 01:24:52 +090021#include "build/build_config.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090022
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090023#if defined(OS_FREEBSD)
24#include <sys/param.h>
25#include <sys/sysctl.h>
chromium@hybridsource.org8f85a6a2011-06-25 13:54:41 +090026#elif defined(OS_SOLARIS)
27#include <stdlib.h>
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090028#endif
29
mmentovai@google.comaa13be62008-09-03 03:20:34 +090030namespace base {
31
evan@chromium.orgc1365092009-11-21 10:29:00 +090032bool PathProviderPosix(int key, FilePath* result) {
evanm@google.com874d1672008-10-31 08:54:04 +090033 FilePath path;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090034 switch (key) {
35 case base::FILE_EXE:
wtc@chromium.org9b07a8f2009-09-01 07:25:00 +090036 case base::FILE_MODULE: { // TODO(evanm): is this correct?
pvalchev@google.comcba6a202010-05-11 01:30:27 +090037#if defined(OS_LINUX)
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +090038 FilePath bin_dir;
thestig@chromium.org0c1da822012-09-15 19:51:05 +090039 if (!file_util::ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
40 NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
mmentovai@google.comaa13be62008-09-03 03:20:34 +090041 return false;
42 }
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +090043 *result = bin_dir;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090044 return true;
pvalchev@google.comcba6a202010-05-11 01:30:27 +090045#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);
grt@chromium.orgb3b798c2011-10-26 01:22:27 +090049 // Upon return, |length| is the number of bytes written to |bin_dir|
50 // including the string terminator.
pvalchev@google.comcba6a202010-05-11 01:30:27 +090051 int error = sysctl(name, 4, bin_dir, &length, NULL, 0);
grt@chromium.orgb3b798c2011-10-26 01:22:27 +090052 if (error < 0 || length <= 1) {
pvalchev@google.comcba6a202010-05-11 01:30:27 +090053 NOTREACHED() << "Unable to resolve path.";
54 return false;
55 }
grt@chromium.orgb3b798c2011-10-26 01:22:27 +090056 *result = FilePath(FilePath::StringType(bin_dir, length - 1));
pvalchev@google.comcba6a202010-05-11 01:30:27 +090057 return true;
chromium@hybridsource.org8f85a6a2011-06-25 13:54:41 +090058#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;
mark@chromium.org2e9e81c2011-10-13 13:23:22 +090066#elif defined(OS_OPENBSD)
67 // There is currently no way to get the executable path on OpenBSD
thestig@chromium.org0c1da822012-09-15 19:51:05 +090068 char* cpath;
robert.nagy@gmail.comdd706e92011-10-26 02:43:05 +090069 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
70 *result = FilePath(cpath);
71 else
72 *result = FilePath("/usr/local/chrome/chrome");
mark@chromium.org2e9e81c2011-10-13 13:23:22 +090073 return true;
pvalchev@google.comcba6a202010-05-11 01:30:27 +090074#endif
mmentovai@google.comaa13be62008-09-03 03:20:34 +090075 }
mmoss@google.com9d288752010-04-22 08:28:43 +090076 case base::DIR_SOURCE_ROOT: {
maruel@chromium.org89f74e22012-07-27 05:53:56 +090077 // 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);
brettw@chromium.org10b64122013-07-12 02:36:07 +090083 if (base::PathExists(path)) {
maruel@chromium.org89f74e22012-07-27 05:53:56 +090084 *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 }
evan@chromium.orgc1365092009-11-21 10:29:00 +090091 // On POSIX, unit tests execute two levels deep from the source root.
evan@chromium.orgb33d2542010-12-03 09:59:23 +090092 // For example: out/{Debug|Release}/net_unittest
mattm@chromium.org1ed95d52009-09-11 04:30:46 +090093 if (PathService::Get(base::DIR_EXE, &path)) {
jochen@chromium.org48995032012-02-04 08:10:04 +090094 *result = path.DirName().DirName();
mattm@chromium.org1ed95d52009-09-11 04:30:46 +090095 return true;
96 }
jochen@chromium.org48995032012-02-04 08:10:04 +090097
brettw@chromium.org5faed3c2011-10-27 06:48:00 +090098 DLOG(ERROR) << "Couldn't find your source root. "
99 << "Try running from your chromium/src directory.";
mattm@chromium.org1ed95d52009-09-11 04:30:46 +0900100 return false;
mmoss@google.com9d288752010-04-22 08:28:43 +0900101 }
gab@chromium.org97fc1e62012-09-21 01:24:52 +0900102 case base::DIR_USER_DESKTOP:
103 *result = base::nix::GetXDGUserDirectory("DESKTOP", "Desktop");
104 return true;
thorogood@chromium.orge77009d2012-07-23 17:22:44 +0900105 case base::DIR_CACHE: {
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +0900106 scoped_ptr<base::Environment> env(base::Environment::Create());
brettw@chromium.orge47345a2010-10-16 13:56:06 +0900107 FilePath cache_dir(base::nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME",
108 ".cache"));
thestig@chromium.orgeea83062009-12-02 17:45:01 +0900109 *result = cache_dir;
110 return true;
thorogood@chromium.orge77009d2012-07-23 17:22:44 +0900111 }
gab@chromium.org97fc1e62012-09-21 01:24:52 +0900112 case base::DIR_HOME:
thorogood@chromium.orge77009d2012-07-23 17:22:44 +0900113 *result = file_util::GetHomeDir();
114 return true;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900115 }
116 return false;
117}
118
119} // namespace base