1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/path_service.h"
7 #include <unordered_map>
9 #include "base/check_op.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/memory/raw_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "build/build_config.h"
25 bool PathProvider(int key, FilePath* result);
28 bool PathProviderWin(int key, FilePath* result);
29 #elif BUILDFLAG(IS_APPLE)
30 bool PathProviderMac(int key, FilePath* result);
31 #elif BUILDFLAG(IS_ANDROID)
32 bool PathProviderAndroid(int key, FilePath* result);
33 #elif BUILDFLAG(IS_FUCHSIA)
34 bool PathProviderFuchsia(int key, FilePath* result);
35 #elif BUILDFLAG(IS_POSIX)
36 // PathProviderPosix is the default path provider on POSIX OSes other than
38 bool PathProviderPosix(int key, FilePath* result);
43 typedef std::unordered_map<int, FilePath> PathMap;
45 // We keep a linked list of providers. In a debug build we ensure that no two
46 // providers claim overlapping keys.
48 PathService::ProviderFunc func;
49 struct Provider* next;
57 Provider base_provider = {PathProvider, nullptr,
64 Provider base_provider_win = {
75 #if BUILDFLAG(IS_APPLE)
76 Provider base_provider_mac = {
87 #if BUILDFLAG(IS_ANDROID)
88 Provider base_provider_android = {
99 #if BUILDFLAG(IS_FUCHSIA)
100 Provider base_provider_fuchsia = {PathProviderFuchsia, &base_provider,
107 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID)
108 Provider base_provider_posix = {
122 PathMap cache; // Cache mappings from path key to path value.
123 PathMap overrides; // Track path overrides.
124 raw_ptr<Provider> providers; // Linked list of path service providers.
125 bool cache_disabled; // Don't use cache if true;
127 PathData() : cache_disabled(false) {
128 #if BUILDFLAG(IS_WIN)
129 providers = &base_provider_win;
130 #elif BUILDFLAG(IS_APPLE)
131 providers = &base_provider_mac;
132 #elif BUILDFLAG(IS_ANDROID)
133 providers = &base_provider_android;
134 #elif BUILDFLAG(IS_FUCHSIA)
135 providers = &base_provider_fuchsia;
136 #elif BUILDFLAG(IS_POSIX)
137 providers = &base_provider_posix;
142 static PathData* GetPathData() {
143 static auto* path_data = new PathData();
147 // Tries to find |key| in the cache.
148 bool LockedGetFromCache(int key, const PathData* path_data, FilePath* result)
149 EXCLUSIVE_LOCKS_REQUIRED(path_data->lock) {
150 if (path_data->cache_disabled)
152 // check for a cached version
153 auto it = path_data->cache.find(key);
154 if (it != path_data->cache.end()) {
155 *result = it->second;
161 // Tries to find |key| in the overrides map.
162 bool LockedGetFromOverrides(int key, PathData* path_data, FilePath* result)
163 EXCLUSIVE_LOCKS_REQUIRED(path_data->lock) {
164 // check for an overridden version.
165 PathMap::const_iterator it = path_data->overrides.find(key);
166 if (it != path_data->overrides.end()) {
167 if (!path_data->cache_disabled)
168 path_data->cache[key] = it->second;
169 *result = it->second;
177 // TODO(brettw): this function does not handle long paths (filename > MAX_PATH)
178 // characters). This isn't supported very well by Windows right now, so it is
179 // moot, but we should keep this in mind for the future.
181 bool PathService::Get(int key, FilePath* result) {
182 PathData* path_data = GetPathData();
185 DCHECK_GT(key, PATH_START);
187 // Special case the current directory because it can never be cached.
188 if (key == DIR_CURRENT)
189 return GetCurrentDirectory(result);
191 Provider* provider = nullptr;
193 AutoLock scoped_lock(path_data->lock);
194 if (LockedGetFromCache(key, path_data, result))
197 if (LockedGetFromOverrides(key, path_data, result))
200 // Get the beginning of the list while it is still locked.
201 provider = path_data->providers;
206 // Iterating does not need the lock because only the list head might be
207 // modified on another thread.
209 if (provider->func(key, &path))
211 DCHECK(path.empty()) << "provider should not have modified path";
212 provider = provider->next;
218 if (path.ReferencesParent()) {
219 // Make sure path service never returns a path with ".." in it.
220 path = MakeAbsoluteFilePath(path);
226 AutoLock scoped_lock(path_data->lock);
227 if (!path_data->cache_disabled)
228 path_data->cache[key] = path;
233 FilePath PathService::CheckedGet(int key) {
235 LOG_IF(FATAL, !Get(key, &path)) << "Failed to get the path for " << key;
240 bool PathService::Override(int key, const FilePath& path) {
241 // Just call the full function with true for the value of |create|, and
242 // assume that |path| may not be absolute yet.
243 return OverrideAndCreateIfNeeded(key, path, false, true);
247 bool PathService::OverrideAndCreateIfNeeded(int key,
248 const FilePath& path,
251 PathData* path_data = GetPathData();
253 DCHECK_GT(key, PATH_START) << "invalid path key";
255 FilePath file_path = path;
257 // For some locations this will fail if called from inside the sandbox there-
258 // fore we protect this call with a flag.
260 // Make sure the directory exists. We need to do this before we translate
261 // this to the absolute path because on POSIX, MakeAbsoluteFilePath fails
262 // if called on a non-existent path.
263 if (!PathExists(file_path) && !CreateDirectory(file_path))
267 // We need to have an absolute path.
269 file_path = MakeAbsoluteFilePath(file_path);
270 if (file_path.empty())
273 DCHECK(file_path.IsAbsolute());
275 AutoLock scoped_lock(path_data->lock);
277 // Clear the cache now. Some of its entries could have depended
278 // on the value we are overriding, and are now out of sync with reality.
279 path_data->cache.clear();
281 path_data->overrides[key] = file_path;
287 bool PathService::RemoveOverrideForTests(int key) {
288 PathData* path_data = GetPathData();
291 AutoLock scoped_lock(path_data->lock);
293 if (path_data->overrides.find(key) == path_data->overrides.end())
296 // Clear the cache now. Some of its entries could have depended on the value
297 // we are going to remove, and are now out of sync.
298 path_data->cache.clear();
300 path_data->overrides.erase(key);
306 bool PathService::IsOverriddenForTests(int key) {
307 PathData* path_data = GetPathData();
310 AutoLock scoped_lock(path_data->lock);
312 return path_data->overrides.find(key) != path_data->overrides.end();
316 void PathService::RegisterProvider(ProviderFunc func, int key_start,
318 PathData* path_data = GetPathData();
320 DCHECK_GT(key_end, key_start);
325 p->is_static = false;
328 p->key_start = key_start;
329 p->key_end = key_end;
332 AutoLock scoped_lock(path_data->lock);
335 Provider *iter = path_data->providers;
337 DCHECK(key_start >= iter->key_end || key_end <= iter->key_start) <<
338 "path provider collision";
343 p->next = path_data->providers;
344 path_data->providers = p;
348 void PathService::DisableCache() {
349 PathData* path_data = GetPathData();
352 AutoLock scoped_lock(path_data->lock);
353 path_data->cache.clear();
354 path_data->cache_disabled = true;