xref: /aosp_15_r20/system/libbase/abi_compatibility.cpp (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker  *
4*8f0ba417SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker  *
8*8f0ba417SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker  *
10*8f0ba417SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker  * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker  */
16*8f0ba417SAndroid Build Coastguard Worker 
17*8f0ba417SAndroid Build Coastguard Worker #include <memory>
18*8f0ba417SAndroid Build Coastguard Worker 
19*8f0ba417SAndroid Build Coastguard Worker #include "android-base/cmsg.h"
20*8f0ba417SAndroid Build Coastguard Worker #include "android-base/file.h"
21*8f0ba417SAndroid Build Coastguard Worker #include "android-base/mapped_file.h"
22*8f0ba417SAndroid Build Coastguard Worker #include "android-base/unique_fd.h"
23*8f0ba417SAndroid Build Coastguard Worker 
24*8f0ba417SAndroid Build Coastguard Worker namespace android {
25*8f0ba417SAndroid Build Coastguard Worker namespace base {
26*8f0ba417SAndroid Build Coastguard Worker 
27*8f0ba417SAndroid Build Coastguard Worker // These ABI-compatibility shims are in a separate file for two reasons:
28*8f0ba417SAndroid Build Coastguard Worker //   1. If they were in the file with the actual functions, it prevents calls to
29*8f0ba417SAndroid Build Coastguard Worker //      those functions by other functions in the file, due to ambiguity.
30*8f0ba417SAndroid Build Coastguard Worker //   2. We will hopefully be able to delete these quickly.
31*8f0ba417SAndroid Build Coastguard Worker 
32*8f0ba417SAndroid Build Coastguard Worker #if !defined(_WIN32)
SendFileDescriptorVector(int sockfd,const void * data,size_t len,const std::vector<int> & fds)33*8f0ba417SAndroid Build Coastguard Worker ssize_t SendFileDescriptorVector(int sockfd, const void* data, size_t len,
34*8f0ba417SAndroid Build Coastguard Worker                                  const std::vector<int>& fds) {
35*8f0ba417SAndroid Build Coastguard Worker   return SendFileDescriptorVector(borrowed_fd(sockfd), data, len, fds);
36*8f0ba417SAndroid Build Coastguard Worker }
37*8f0ba417SAndroid Build Coastguard Worker 
ReceiveFileDescriptorVector(int sockfd,void * data,size_t len,size_t max_fds,std::vector<unique_fd> * fds)38*8f0ba417SAndroid Build Coastguard Worker ssize_t ReceiveFileDescriptorVector(int sockfd, void* data, size_t len, size_t max_fds,
39*8f0ba417SAndroid Build Coastguard Worker                                     std::vector<unique_fd>* fds) {
40*8f0ba417SAndroid Build Coastguard Worker   return ReceiveFileDescriptorVector(borrowed_fd(sockfd), data, len, max_fds, fds);
41*8f0ba417SAndroid Build Coastguard Worker }
42*8f0ba417SAndroid Build Coastguard Worker #endif
43*8f0ba417SAndroid Build Coastguard Worker 
ReadFdToString(int fd,std::string * content)44*8f0ba417SAndroid Build Coastguard Worker bool ReadFdToString(int fd, std::string* content) {
45*8f0ba417SAndroid Build Coastguard Worker   return ReadFdToString(borrowed_fd(fd), content);
46*8f0ba417SAndroid Build Coastguard Worker }
47*8f0ba417SAndroid Build Coastguard Worker 
WriteStringToFd(const std::string & content,int fd)48*8f0ba417SAndroid Build Coastguard Worker bool WriteStringToFd(const std::string& content, int fd) {
49*8f0ba417SAndroid Build Coastguard Worker   return WriteStringToFd(std::string_view(content), borrowed_fd(fd));
50*8f0ba417SAndroid Build Coastguard Worker }
51*8f0ba417SAndroid Build Coastguard Worker 
WriteStringToFd(const std::string & content,borrowed_fd fd)52*8f0ba417SAndroid Build Coastguard Worker bool WriteStringToFd(const std::string& content, borrowed_fd fd) {
53*8f0ba417SAndroid Build Coastguard Worker   return WriteStringToFd(std::string_view(content), fd);
54*8f0ba417SAndroid Build Coastguard Worker }
55*8f0ba417SAndroid Build Coastguard Worker 
ReadFully(int fd,void * data,size_t byte_count)56*8f0ba417SAndroid Build Coastguard Worker bool ReadFully(int fd, void* data, size_t byte_count) {
57*8f0ba417SAndroid Build Coastguard Worker   return ReadFully(borrowed_fd(fd), data, byte_count);
58*8f0ba417SAndroid Build Coastguard Worker }
59*8f0ba417SAndroid Build Coastguard Worker 
ReadFullyAtOffset(int fd,void * data,size_t byte_count,off64_t offset)60*8f0ba417SAndroid Build Coastguard Worker bool ReadFullyAtOffset(int fd, void* data, size_t byte_count, off64_t offset) {
61*8f0ba417SAndroid Build Coastguard Worker   return ReadFullyAtOffset(borrowed_fd(fd), data, byte_count, offset);
62*8f0ba417SAndroid Build Coastguard Worker }
63*8f0ba417SAndroid Build Coastguard Worker 
WriteFully(int fd,const void * data,size_t byte_count)64*8f0ba417SAndroid Build Coastguard Worker bool WriteFully(int fd, const void* data, size_t byte_count) {
65*8f0ba417SAndroid Build Coastguard Worker   return WriteFully(borrowed_fd(fd), data, byte_count);
66*8f0ba417SAndroid Build Coastguard Worker }
67*8f0ba417SAndroid Build Coastguard Worker 
Basename(const std::string & path)68*8f0ba417SAndroid Build Coastguard Worker std::string Basename(const std::string& path) {
69*8f0ba417SAndroid Build Coastguard Worker   return Basename(std::string_view(path));
70*8f0ba417SAndroid Build Coastguard Worker }
71*8f0ba417SAndroid Build Coastguard Worker 
Dirname(const std::string & path)72*8f0ba417SAndroid Build Coastguard Worker std::string Dirname(const std::string& path) {
73*8f0ba417SAndroid Build Coastguard Worker   return Dirname(std::string_view(path));
74*8f0ba417SAndroid Build Coastguard Worker }
75*8f0ba417SAndroid Build Coastguard Worker 
76*8f0ba417SAndroid Build Coastguard Worker #if defined(__LP64__)
77*8f0ba417SAndroid Build Coastguard Worker #define MAPPEDFILE_FROMFD _ZN10MappedFile6FromFdEilmi
78*8f0ba417SAndroid Build Coastguard Worker #else
79*8f0ba417SAndroid Build Coastguard Worker #define MAPPEDFILE_FROMFD _ZN10MappedFile6FromFdEixmi
80*8f0ba417SAndroid Build Coastguard Worker #endif
81*8f0ba417SAndroid Build Coastguard Worker 
82*8f0ba417SAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
MAPPEDFILE_FROMFD(int fd,off64_t offset,size_t length,int prot)83*8f0ba417SAndroid Build Coastguard Worker extern "C" std::unique_ptr<MappedFile> MAPPEDFILE_FROMFD(int fd, off64_t offset, size_t length,
84*8f0ba417SAndroid Build Coastguard Worker                                                          int prot) {
85*8f0ba417SAndroid Build Coastguard Worker   return MappedFile::FromFd(fd, offset, length, prot);
86*8f0ba417SAndroid Build Coastguard Worker }
87*8f0ba417SAndroid Build Coastguard Worker 
88*8f0ba417SAndroid Build Coastguard Worker }  // namespace base
89*8f0ba417SAndroid Build Coastguard Worker }  // namespace android
90