Upload upstream chromium 94.0.4606.31
[platform/framework/web/chromium-efl.git] / base / base_paths_posix.cc
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 #include "base/nix/xdg_util.h"
23 #include "base/notreached.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
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) || defined(OS_CHROMEOS)
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     case DIR_SOURCE_ROOT: {
81       // Allow passing this in the environment, for more flexibility in build
82       // tree configurations (sub-project builds, gyp --output_dir, etc.)
83       std::unique_ptr<Environment> env(Environment::Create());
84       std::string cr_source_root;
85       FilePath path;
86       if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
87         path = FilePath(cr_source_root);
88         if (PathExists(path)) {
89           *result = path;
90           return true;
91         }
92         DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
93                       << "point to a directory.";
94       }
95       // On POSIX, unit tests execute two levels deep from the source root.
96       // For example:  out/{Debug|Release}/net_unittest
97       if (PathService::Get(DIR_EXE, &path)) {
98         *result = path.DirName().DirName();
99         return true;
100       }
101
102       DLOG(ERROR) << "Couldn't find your source root.  "
103                   << "Try running from your chromium/src directory.";
104       return false;
105     }
106     case DIR_USER_DESKTOP:
107       *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop");
108       return true;
109     case DIR_CACHE: {
110       std::unique_ptr<Environment> env(Environment::Create());
111       FilePath cache_dir(
112           nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache"));
113       *result = cache_dir;
114       return true;
115     }
116   }
117   return false;
118 }
119
120 }  // namespace base