• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2006-2008 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 // This is really Posix minus Mac. Mac code is in base_paths_mac.mm.
6 
7 #include "base/base_paths.h"
8 
9 #include <unistd.h>
10 
11 #include "base/file_path.h"
12 #include "base/file_util.h"
13 #include "base/linux_util.h"
14 #include "base/logging.h"
15 #include "base/path_service.h"
16 #include "base/scoped_ptr.h"
17 #include "base/sys_string_conversions.h"
18 
19 namespace base {
20 
21 #if defined(OS_LINUX)
22 const char kSelfExe[] = "/proc/self/exe";
23 #elif defined(OS_FREEBSD)
24 const char kSelfExe[] = "/proc/curproc/file";
25 #endif
26 
PathProviderPosix(int key,FilePath * result)27 bool PathProviderPosix(int key, FilePath* result) {
28   FilePath path;
29   switch (key) {
30     case base::FILE_EXE:
31     case base::FILE_MODULE: {  // TODO(evanm): is this correct?
32       char bin_dir[PATH_MAX + 1];
33       int bin_dir_size = readlink(kSelfExe, bin_dir, PATH_MAX);
34       if (bin_dir_size < 0 || bin_dir_size > PATH_MAX) {
35         NOTREACHED() << "Unable to resolve " << kSelfExe << ".";
36         return false;
37       }
38       bin_dir[bin_dir_size] = 0;
39       *result = FilePath(bin_dir);
40       return true;
41     }
42     case base::DIR_SOURCE_ROOT:
43       // On POSIX, unit tests execute two levels deep from the source root.
44       // For example:  sconsbuild/{Debug|Release}/net_unittest
45       if (PathService::Get(base::DIR_EXE, &path)) {
46         path = path.DirName().DirName();
47         if (file_util::PathExists(path.Append("base/base_paths_posix.cc"))) {
48           *result = path;
49           return true;
50         }
51       }
52       // If that failed (maybe the build output is symlinked to a different
53       // drive) try assuming the current directory is the source root.
54       if (file_util::GetCurrentDirectory(&path) &&
55           file_util::PathExists(path.Append("base/base_paths_posix.cc"))) {
56         *result = path;
57         return true;
58       }
59       LOG(ERROR) << "Couldn't find your source root.  "
60                  << "Try running from your chromium/src directory.";
61       return false;
62     case base::DIR_USER_CACHE:
63       scoped_ptr<base::EnvironmentVariableGetter> env(
64           base::EnvironmentVariableGetter::Create());
65       FilePath cache_dir(base::GetXDGDirectory(env.get(), "XDG_CACHE_HOME",
66                                                ".cache"));
67       *result = cache_dir;
68       return true;
69   }
70   return false;
71 }
72 
73 }  // namespace base
74