blob: a84844013c2092c594217a989e86535ba9d9b970 [file] [log] [blame]
evanm@google.come41d3b32008-08-15 10:04:11 +09001// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include "base/base_paths_linux.h"
31
32#include <unistd.h>
33
tc@google.com5953c202008-08-19 05:40:04 +090034#include "base/file_util.h"
evanm@google.come41d3b32008-08-15 10:04:11 +090035#include "base/logging.h"
tc@google.com5953c202008-08-19 05:40:04 +090036#include "base/path_service.h"
deanm@google.coma65ec9f2008-08-19 22:19:24 +090037#include "base/string_piece.h"
evanm@google.come41d3b32008-08-15 10:04:11 +090038#include "base/sys_string_conversions.h"
39
40namespace base {
41
42bool PathProviderLinux(int key, std::wstring* result) {
tc@google.com5953c202008-08-19 05:40:04 +090043 std::wstring cur;
evanm@google.come41d3b32008-08-15 10:04:11 +090044 switch (key) {
45 case base::FILE_EXE:
46 case base::FILE_MODULE: { // TODO(evanm): is this correct?
47 char bin_dir[PATH_MAX + 1];
48 int bin_dir_size = readlink("/proc/self/exe", bin_dir, PATH_MAX);
49 if (bin_dir_size < 0 || bin_dir_size > PATH_MAX) {
50 NOTREACHED() << "Unable to resolve /proc/self/exe.";
51 return false;
52 }
53 bin_dir[bin_dir_size] = 0;
54 *result = base::SysNativeMBToWide(bin_dir);
55 return true;
56 }
tc@google.com5953c202008-08-19 05:40:04 +090057 case base::DIR_SOURCE_ROOT:
58 // On linux, unit tests execute two levels deep from the source root.
59 // For example: chrome/{Debug|Hammer}/net_unittest
60 PathService::Get(base::DIR_EXE, &cur);
61 file_util::UpOneDirectory(&cur);
62 file_util::UpOneDirectory(&cur);
63 *result = cur;
64 return true;
evanm@google.come41d3b32008-08-15 10:04:11 +090065 }
66 return false;
67}
68
69} // namespace base