1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "PathUtils.h"
18 
19 #include <cutils/log.h>
20 #include <libgen.h>
21 #include <unistd.h>
22 
23 namespace minikin {
24 
25 const char* SELF_EXE_PATH = "/proc/self/exe";
26 
getDirname(const std::string & path)27 std::string getDirname(const std::string& path) {
28     const char* result = dirname(path.c_str());
29     LOG_ALWAYS_FATAL_IF(result == nullptr, "dirname failed.");
30     return std::string(result);
31 }
32 
getBasename(const std::string & path)33 std::string getBasename(const std::string& path) {
34     const char* result = basename(path.c_str());
35     LOG_ALWAYS_FATAL_IF(result == nullptr, "basename failed.");
36     return std::string(result);
37 }
38 
getTestDataDir()39 std::string getTestDataDir() {
40     char buf[PATH_MAX] = {};
41     LOG_ALWAYS_FATAL_IF(readlink(SELF_EXE_PATH, buf, PATH_MAX) == -1, "readlink failed.");
42     return getDirname(buf) + "/data/";
43 }
44 
45 }  // namespace minikin
46