evanm@google.com | e41d3b3 | 2008-08-15 10:04:11 +0900 | [diff] [blame] | 1 | // 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.com | 5953c20 | 2008-08-19 05:40:04 +0900 | [diff] [blame] | 34 | #include "base/file_util.h" |
evanm@google.com | e41d3b3 | 2008-08-15 10:04:11 +0900 | [diff] [blame] | 35 | #include "base/logging.h" |
tc@google.com | 5953c20 | 2008-08-19 05:40:04 +0900 | [diff] [blame] | 36 | #include "base/path_service.h" |
deanm@google.com | a65ec9f | 2008-08-19 22:19:24 +0900 | [diff] [blame^] | 37 | #include "base/string_piece.h" |
evanm@google.com | e41d3b3 | 2008-08-15 10:04:11 +0900 | [diff] [blame] | 38 | #include "base/sys_string_conversions.h" |
| 39 | |
| 40 | namespace base { |
| 41 | |
| 42 | bool PathProviderLinux(int key, std::wstring* result) { |
tc@google.com | 5953c20 | 2008-08-19 05:40:04 +0900 | [diff] [blame] | 43 | std::wstring cur; |
evanm@google.com | e41d3b3 | 2008-08-15 10:04:11 +0900 | [diff] [blame] | 44 | 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.com | 5953c20 | 2008-08-19 05:40:04 +0900 | [diff] [blame] | 57 | 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.com | e41d3b3 | 2008-08-15 10:04:11 +0900 | [diff] [blame] | 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | } // namespace base |