1 /*
2 * Copyright (C) 2020 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 "common/libs/fs/shared_fd_stream.h"
18
19 #include <cstdio>
20 #include <streambuf>
21
22 #include "common/libs/fs/shared_buf.h"
23
24 namespace cuttlefish {
25
SharedFDStreambuf(SharedFD shared_fd)26 SharedFDStreambuf::SharedFDStreambuf(SharedFD shared_fd)
27 : shared_fd_(shared_fd) {}
28
underflow()29 int SharedFDStreambuf::underflow() {
30 if (gptr() < egptr()) {
31 return *gptr();
32 }
33
34 size_t unget_size = 0;
35 constexpr size_t bytes_to_read = kBufferSize - kUngetSize;
36 if (read_buffer_ == nullptr) {
37 read_buffer_ = std::make_unique<char[]>(kBufferSize);
38 } else {
39 unget_size = std::min(gptr() - eback(), kUngetSize);
40 std::memcpy(read_buffer_.get(),
41 read_buffer_.get() + kBufferSize - unget_size,
42 unget_size);
43 }
44
45 ssize_t bytes_read = ReadExact(shared_fd_,
46 read_buffer_.get() + unget_size,
47 bytes_to_read);
48
49 setg(read_buffer_.get(),
50 read_buffer_.get() + unget_size,
51 read_buffer_.get() + unget_size + bytes_read);
52
53 if (bytes_read <= 0 || in_avail() == 0) {
54 return EOF;
55 }
56
57 return static_cast<int>(*gptr());
58 }
59
xsgetn(char * dst,std::streamsize count)60 std::streamsize SharedFDStreambuf::xsgetn(char* dst, std::streamsize count) {
61 std::streamsize bytes_read = 0;
62 while (bytes_read < count) {
63 if (in_avail() == 0) {
64 if (underflow() == EOF) {
65 break;
66 }
67 }
68 std::streamsize buffer_count =
69 std::min(static_cast<std::streamsize>(in_avail()), count - bytes_read);
70 std::memcpy(dst + bytes_read, gptr(), buffer_count);
71 gbump(buffer_count);
72 bytes_read += buffer_count;
73 }
74 return bytes_read;
75 }
76
overflow(int c)77 int SharedFDStreambuf::overflow(int c) {
78 if (c != EOF) {
79 char z = c;
80 if (WriteAll(shared_fd_, &z, 1) != 1) {
81 return EOF;
82 }
83 }
84 return c;
85 }
86
xsputn(const char * src,std::streamsize count)87 std::streamsize SharedFDStreambuf::xsputn(const char* src,
88 std::streamsize count) {
89 return static_cast<std::streamsize>(
90 WriteAll(shared_fd_, src, static_cast<std::size_t>(count)));
91 }
92
pbackfail(int c)93 int SharedFDStreambuf::pbackfail(int c) {
94 if (c != EOF) {
95 if (gptr() != eback()) {
96 gbump(-1);
97 *(gptr()) = c;
98 return c;
99 }
100 }
101 return EOF;
102 }
103
SharedFDOstream(SharedFD shared_fd)104 SharedFDOstream::SharedFDOstream(SharedFD shared_fd)
105 : std::ostream(nullptr), buf_(shared_fd) { rdbuf(&buf_); }
106
SharedFDIstream(SharedFD shared_fd)107 SharedFDIstream::SharedFDIstream(SharedFD shared_fd)
108 : std::istream(nullptr), buf_(shared_fd) { rdbuf(&buf_); }
109
110 } // namespace cuttlefish