1 /*
2 * Copyright (C) 2023 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 "file.h"
18
19 #ifdef BINDER_NO_LIBBASE
20
21 #include "Utils.h"
22
23 #include <stdint.h>
24
25 // clang-format off
26
27 namespace android::binder {
28
ReadFully(borrowed_fd fd,void * data,size_t byte_count)29 bool ReadFully(borrowed_fd fd, void* data, size_t byte_count) {
30 uint8_t* p = reinterpret_cast<uint8_t*>(data);
31 size_t remaining = byte_count;
32 while (remaining > 0) {
33 ssize_t n = TEMP_FAILURE_RETRY(read(fd.get(), p, remaining));
34 if (n == 0) { // EOF
35 errno = ENODATA;
36 return false;
37 }
38 if (n == -1) return false;
39 p += n;
40 remaining -= n;
41 }
42 return true;
43 }
44
WriteFully(borrowed_fd fd,const void * data,size_t byte_count)45 bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count) {
46 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
47 size_t remaining = byte_count;
48 while (remaining > 0) {
49 ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, remaining));
50 if (n == -1) return false;
51 p += n;
52 remaining -= n;
53 }
54 return true;
55 }
56
57 } // namespace android::binder
58
59 #endif // BINDER_NO_LIBBASE
60