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 #pragma once
17
18 #include <string>
19 #include <thread>
20 #include <vector>
21
22 #include "common/libs/fs/shared_fd.h"
23
24 namespace cuttlefish {
25
26 /**
27 * Reads from fd until it is closed or errors, storing all data in buf.
28 *
29 * On a successful read, returns the number of bytes read.
30 *
31 * If a read error is encountered, returns -1. buf will contain any data read
32 * up until that point and errno will be set.
33 *
34 */
35 ssize_t ReadAll(SharedFD fd, std::string* buf);
36
37 /**
38 * Reads from fd until reading buf->size() bytes or errors.
39 *
40 * On a successful read, returns buf->size().
41 *
42 * If a read error is encountered, returns -1. buf will contain any data read
43 * up until that point and errno will be set.
44 *
45 * If the size of buf is 0, read(fd, buf, 0) is effectively called, which means
46 * error(s) might be detected. If detected, the return value would be -1.
47 * If not detected, the return value will be 0.
48 *
49 */
50 ssize_t ReadExact(SharedFD fd, std::string* buf);
51
52 /**
53 * Reads from fd until reading buf->size() bytes or errors.
54 *
55 * On a successful read, returns buf->size().
56 *
57 * If a read error is encountered, returns -1. buf will contain any data read
58 * up until that point and errno will be set.
59 *
60 * If the size of buf is 0, read(fd, buf, 0) is effectively called, which means
61 * error(s) might be detected. If detected, the return value would be -1.
62 * If not detected, the return value will be 0.
63 *
64 */
65 ssize_t ReadExact(SharedFD fd, std::vector<char>* buf);
66
67 /**
68 * Reads from fd until reading `size` bytes or errors.
69 *
70 * On a successful read, returns buf->size().
71 *
72 * If a read error is encountered, returns -1. buf will contain any data read
73 * up until that point and errno will be set.
74 *
75 * When the size is 0, read(fd, buf, 0) is effectively called, which means
76 * error(s) might be detected. If detected, the return value would be -1.
77 * If not detected, the return value will be 0.
78 *
79 */
80 ssize_t ReadExact(SharedFD fd, char* buf, size_t size);
81
82 /*
83 * Reads from fd until reading `sizeof(T)` bytes or errors.
84 *
85 * On a successful read, returns `sizeof(T)`.
86 *
87 * If a read error is encountered, returns -1. buf will contain any data read
88 * up until that point and errno will be set.
89 */
90 template<typename T>
ReadExactBinary(SharedFD fd,T * binary_data)91 ssize_t ReadExactBinary(SharedFD fd, T* binary_data) {
92 return ReadExact(fd, (char*) binary_data, sizeof(*binary_data));
93 }
94
95 /**
96 * Writes to fd until writing all bytes in buf.
97 *
98 * On a successful write, returns buf.size().
99 *
100 * If a write error is encountered, returns -1. Some data may have already been
101 * written to fd at that point.
102 *
103 * If the size of buf is 0, WriteAll returns 0 with no error set unless
104 * the fd is a regular file. If fd is a regular file, write(fd, buf, 0) is
105 * effectively called. It may detect errors; if detected, errno is set and
106 * -1 is returned. If not detected, 0 is returned with errno unchanged.
107 *
108 */
109 ssize_t WriteAll(SharedFD fd, const std::string& buf);
110
111 /**
112 * Writes to fd until writing all bytes in buf.
113 *
114 * On a successful write, returns buf.size().
115 *
116 * If a write error is encountered, returns -1. Some data may have already been
117 * written to fd at that point.
118 *
119 * If the size of buf is 0, WriteAll returns 0 with no error set unless
120 * the fd is a regular file. If fd is a regular file, write(fd, buf, 0) is
121 * effectively called. It may detect errors; if detected, errno is set and
122 * -1 is returned. If not detected, 0 is returned with errno unchanged.
123 *
124 */
125 ssize_t WriteAll(SharedFD fd, const std::vector<char>& buf);
126
127 /**
128 * Writes to fd until `size` bytes are written from `buf`.
129 *
130 * On a successful write, returns `size`.
131 *
132 * If a write error is encountered, returns -1. Some data may have already been
133 * written to fd at that point.
134 *
135 * If size is 0, WriteAll returns 0 with no error set unless
136 * the fd is a regular file. If fd is a regular file, write(fd, buf, 0) is
137 * effectively called. It may detect errors; if detected, errno is set and
138 * -1 is returned. If not detected, 0 is returned with errno unchanged.
139 *
140 */
141 ssize_t WriteAll(SharedFD fd, const char* buf, size_t size);
142
143 /**
144 * Writes to fd until `sizeof(T)` bytes are written from binary_data.
145 *
146 * On a successful write, returns `sizeof(T)`.
147 *
148 * If a write error is encountered, returns -1. Some data may have already been
149 * written to fd at that point.
150 *
151 * If ever sizeof(T) is 0, WriteAll returns 0 with no error set unless
152 * the fd is a regular file. If fd is a regular file, write(fd, buf, 0) is
153 * effectively called. It may detect errors; if detected, errno is set and
154 * -1 is returned. If not detected, 0 is returned with errno unchanged.
155 *
156 */
157 template<typename T>
WriteAllBinary(SharedFD fd,const T * binary_data)158 ssize_t WriteAllBinary(SharedFD fd, const T* binary_data) {
159 return WriteAll(fd, (const char*) binary_data, sizeof(*binary_data));
160 }
161
162 /**
163 * Sends contents of msg through sock, checking for socket error conditions
164 *
165 * On successful Send, returns true
166 *
167 * If a Send error is encountered, returns false. Some data may have already
168 * been written to 'sock' at that point.
169 */
170 bool SendAll(SharedFD sock, const std::string& msg);
171
172 /**
173 * Receives 'count' bytes from sock, checking for socket error conditions
174 *
175 * On successful Recv, returns a string containing the received data
176 *
177 * If a Recv error is encountered, returns the empty string
178 */
179 std::string RecvAll(SharedFD sock, const size_t count);
180
181 } // namespace cuttlefish
182