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 : #include "base/base_paths_linux.h"
6 :
7 : #include <unistd.h>
8 :
9 : #include "base/file_path.h"
10 : #include "base/file_util.h"
11 : #include "base/logging.h"
12 : #include "base/path_service.h"
13 : #include "base/string_piece.h"
14 : #include "base/sys_string_conversions.h"
15 :
16 : namespace base {
17 :
18 0 : bool PathProviderLinux(int key, FilePath* result) {
19 0 : FilePath path;
20 0 : switch (key) {
21 : case base::FILE_EXE:
22 : case base::FILE_MODULE: { // TODO(evanm): is this correct?
23 : char bin_dir[PATH_MAX + 1];
24 0 : int bin_dir_size = readlink("/proc/self/exe", bin_dir, PATH_MAX);
25 0 : if (bin_dir_size < 0 || bin_dir_size > PATH_MAX) {
26 0 : NOTREACHED() << "Unable to resolve /proc/self/exe.";
27 0 : return false;
28 : }
29 0 : bin_dir[bin_dir_size] = 0;
30 0 : *result = FilePath(bin_dir);
31 0 : return true;
32 : }
33 : case base::DIR_SOURCE_ROOT:
34 : // On linux, unit tests execute two levels deep from the source root.
35 : // For example: sconsbuild/{Debug|Release}/net_unittest
36 0 : if (!PathService::Get(base::DIR_EXE, &path))
37 0 : return false;
38 0 : path = path.Append(FilePath::kParentDirectory)
39 0 : .Append(FilePath::kParentDirectory);
40 0 : *result = path;
41 0 : return true;
42 : }
43 0 : return false;
44 : }
45 :
46 : } // namespace base
|