1 /*
2 * Copyright (C) 2019 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 <string>
18 #include <thread>
19 #include <vector>
20
21 #include "common/libs/fs/shared_fd.h"
22
23 namespace cvd {
24
25 /**
26 * Reads from fd until it is closed or errors, storing all data in buf.
27 *
28 * On a successful read, returns the number of bytes read.
29 *
30 * If a read error is encountered, returns -1. buf will contain any data read
31 * up until that point and errno will be set.
32 */
33 ssize_t ReadAll(SharedFD fd, std::string* buf);
34
35 /**
36 * Reads from fd until reading buf->size() bytes or errors.
37 *
38 * On a successful read, returns buf->size().
39 *
40 * If a read error is encountered, returns -1. buf will contain any data read
41 * up until that point and errno will be set.
42 */
43 ssize_t ReadExact(SharedFD fd, std::string* buf);
44
45 /**
46 * Reads from fd until reading buf->size() bytes or errors.
47 *
48 * On a successful read, returns buf->size().
49 *
50 * If a read error is encountered, returns -1. buf will contain any data read
51 * up until that point and errno will be set.
52 */
53 ssize_t ReadExact(SharedFD fd, std::vector<char>* buf);
54
55 /**
56 * Reads from fd until reading `size` bytes or errors.
57 *
58 * On a successful read, returns buf->size().
59 *
60 * If a read error is encountered, returns -1. buf will contain any data read
61 * up until that point and errno will be set.
62 */
63 ssize_t ReadExact(SharedFD fd, char* buf, size_t size);
64
65 /*
66 * Reads from fd until reading `sizeof(T)` bytes or errors.
67 *
68 * On a successful read, returns `sizeof(T)`.
69 *
70 * If a read error is encountered, returns -1. buf will contain any data read
71 * up until that point and errno will be set.
72 */
73 template<typename T>
ReadExactBinary(SharedFD fd,T * binary_data)74 ssize_t ReadExactBinary(SharedFD fd, T* binary_data) {
75 return ReadExact(fd, (char*) binary_data, sizeof(*binary_data));
76 }
77
78 /**
79 * Writes to fd until writing all bytes in buf.
80 *
81 * On a successful write, returns buf.size().
82 *
83 * If a write error is encountered, returns -1. Some data may have already been
84 * written to fd at that point.
85 */
86 ssize_t WriteAll(SharedFD fd, const std::string& buf);
87
88 /**
89 * Writes to fd until writing all bytes in buf.
90 *
91 * On a successful write, returns buf.size().
92 *
93 * If a write error is encountered, returns -1. Some data may have already been
94 * written to fd at that point.
95 */
96 ssize_t WriteAll(SharedFD fd, const std::vector<char>& buf);
97
98 /**
99 * Writes to fd until `size` bytes are written from `buf`.
100 *
101 * On a successful write, returns `size`.
102 *
103 * If a write error is encountered, returns -1. Some data may have already been
104 * written to fd at that point.
105 */
106 ssize_t WriteAll(SharedFD fd, const char* buf, size_t size);
107
108 /**
109 * Writes to fd until `sizeof(T)` bytes are written from binary_data.
110 *
111 * On a successful write, returns `sizeof(T)`.
112 *
113 * If a write error is encountered, returns -1. Some data may have already been
114 * written to fd at that point.
115 */
116 template<typename T>
WriteAllBinary(SharedFD fd,const T * binary_data)117 ssize_t WriteAllBinary(SharedFD fd, const T* binary_data) {
118 return WriteAll(fd, (const char*) binary_data, sizeof(*binary_data));
119 }
120
121 } // namespace cvd
122