xref: /aosp_15_r20/frameworks/native/libs/binder/file.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2023 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include "file.h"
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #ifdef BINDER_NO_LIBBASE
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker #include "Utils.h"
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #include <stdint.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker // clang-format off
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker namespace android::binder {
28*38e8c45fSAndroid Build Coastguard Worker 
ReadFully(borrowed_fd fd,void * data,size_t byte_count)29*38e8c45fSAndroid Build Coastguard Worker bool ReadFully(borrowed_fd fd, void* data, size_t byte_count) {
30*38e8c45fSAndroid Build Coastguard Worker   uint8_t* p = reinterpret_cast<uint8_t*>(data);
31*38e8c45fSAndroid Build Coastguard Worker   size_t remaining = byte_count;
32*38e8c45fSAndroid Build Coastguard Worker   while (remaining > 0) {
33*38e8c45fSAndroid Build Coastguard Worker     ssize_t n = TEMP_FAILURE_RETRY(read(fd.get(), p, remaining));
34*38e8c45fSAndroid Build Coastguard Worker     if (n == 0) {  // EOF
35*38e8c45fSAndroid Build Coastguard Worker       errno = ENODATA;
36*38e8c45fSAndroid Build Coastguard Worker       return false;
37*38e8c45fSAndroid Build Coastguard Worker     }
38*38e8c45fSAndroid Build Coastguard Worker     if (n == -1) return false;
39*38e8c45fSAndroid Build Coastguard Worker     p += n;
40*38e8c45fSAndroid Build Coastguard Worker     remaining -= n;
41*38e8c45fSAndroid Build Coastguard Worker   }
42*38e8c45fSAndroid Build Coastguard Worker   return true;
43*38e8c45fSAndroid Build Coastguard Worker }
44*38e8c45fSAndroid Build Coastguard Worker 
WriteFully(borrowed_fd fd,const void * data,size_t byte_count)45*38e8c45fSAndroid Build Coastguard Worker bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count) {
46*38e8c45fSAndroid Build Coastguard Worker   const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
47*38e8c45fSAndroid Build Coastguard Worker   size_t remaining = byte_count;
48*38e8c45fSAndroid Build Coastguard Worker   while (remaining > 0) {
49*38e8c45fSAndroid Build Coastguard Worker     ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, remaining));
50*38e8c45fSAndroid Build Coastguard Worker     if (n == -1) return false;
51*38e8c45fSAndroid Build Coastguard Worker     p += n;
52*38e8c45fSAndroid Build Coastguard Worker     remaining -= n;
53*38e8c45fSAndroid Build Coastguard Worker   }
54*38e8c45fSAndroid Build Coastguard Worker   return true;
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker 
57*38e8c45fSAndroid Build Coastguard Worker }  // namespace android::binder
58*38e8c45fSAndroid Build Coastguard Worker 
59*38e8c45fSAndroid Build Coastguard Worker #endif // BINDER_NO_LIBBASE
60