xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/util/runfiles_nobazel.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <unistd.h>
16 
17 #include <cstdlib>
18 
19 #include "absl/strings/strip.h"
20 #include "sandboxed_api/util/path.h"
21 #include "sandboxed_api/util/raw_logging.h"
22 #include "sandboxed_api/util/runfiles.h"
23 
24 namespace sapi {
25 
GetDataDependencyFilePath(absl::string_view relative_path)26 std::string GetDataDependencyFilePath(absl::string_view relative_path) {
27   static std::string* base_dir = []() {
28     std::string link_name(PATH_MAX, '\0');
29     SAPI_RAW_PCHECK(
30         readlink("/proc/self/exe", &link_name[0], link_name.size()) != -1, "");
31     link_name.resize(link_name.find_first_of('\0'));
32     return new std::string(file::SplitPath(link_name).first);
33   }();
34   absl::string_view resolved =
35       absl::StripSuffix(*base_dir, file::SplitPath(relative_path).first);
36   return file::JoinPath(resolved, relative_path);
37 }
38 
39 namespace internal {
40 
GetSapiDataDependencyFilePath(absl::string_view relative_path)41 std::string GetSapiDataDependencyFilePath(absl::string_view relative_path) {
42   // The Bazel version has an additional "com_google_sandboxed_api" path
43   // component.
44   return GetDataDependencyFilePath(
45       file::JoinPath("sandboxed_api", relative_path));
46 }
47 
48 }  // namespace internal
49 }  // namespace sapi
50