blob: bac667e4e58c2ca16c1d9771a7342727227cbd67 [file] [log] [blame]
Tomasz Wasilczyk26db5e42023-11-02 11:45:11 -07001/*
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 <stdint.h>
22
23// clang-format off
24
25namespace android::binder {
26
27bool ReadFully(borrowed_fd fd, void* data, size_t byte_count) {
28 uint8_t* p = reinterpret_cast<uint8_t*>(data);
29 size_t remaining = byte_count;
30 while (remaining > 0) {
31 ssize_t n = TEMP_FAILURE_RETRY(read(fd.get(), p, remaining));
32 if (n == 0) { // EOF
33 errno = ENODATA;
34 return false;
35 }
36 if (n == -1) return false;
37 p += n;
38 remaining -= n;
39 }
40 return true;
41}
42
43bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count) {
44 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
45 size_t remaining = byte_count;
46 while (remaining > 0) {
47 ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, remaining));
48 if (n == -1) return false;
49 p += n;
50 remaining -= n;
51 }
52 return true;
53}
54
55} // namespace android::binder
56
57#endif // BINDER_NO_LIBBASE