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 <sstream>
18 #include <string>
19 #include <thread>
20 #include <vector>
21 
22 #include "common/libs/fs/shared_buf.h"
23 #include "common/libs/fs/shared_fd.h"
24 
25 namespace cuttlefish {
26 
27 namespace {
28 
29 const size_t BUFF_SIZE = 1 << 14;
30 
31 } // namespace
32 
WriteAll(SharedFD fd,const char * buf,size_t size)33 ssize_t WriteAll(SharedFD fd, const char* buf, size_t size) {
34   size_t total_written = 0;
35   ssize_t written = 0;
36   while ((written = fd->Write((void*)&(buf[total_written]), size - total_written)) > 0) {
37     if (written < 0) {
38       errno = fd->GetErrno();
39       return written;
40     }
41     total_written += written;
42     if (total_written == size) {
43       break;
44     }
45   }
46   return total_written;
47 }
48 
ReadExact(SharedFD fd,char * buf,size_t size)49 ssize_t ReadExact(SharedFD fd, char* buf, size_t size) {
50   size_t total_read = 0;
51   ssize_t read = 0;
52   while ((read = fd->Read((void*)&(buf[total_read]), size - total_read)) > 0) {
53     if (read < 0) {
54       errno = fd->GetErrno();
55       return read;
56     }
57     total_read += read;
58     if (total_read == size) {
59       break;
60     }
61   }
62   return total_read;
63 }
64 
ReadAll(SharedFD fd,std::string * buf)65 ssize_t ReadAll(SharedFD fd, std::string* buf) {
66   char buff[BUFF_SIZE];
67   std::stringstream ss;
68   ssize_t read;
69   while ((read = fd->Read(buff, BUFF_SIZE - 1)) > 0) {
70     // this is necessary to avoid problems with having a '\0' in the middle of the buffer
71     ss << std::string(buff, read);
72   }
73   if (read < 0) {
74     errno = fd->GetErrno();
75     return read;
76   }
77   *buf = ss.str();
78   return buf->size();
79 }
80 
ReadExact(SharedFD fd,std::string * buf)81 ssize_t ReadExact(SharedFD fd, std::string* buf) {
82   return ReadExact(fd, buf->data(), buf->size());
83 }
84 
ReadExact(SharedFD fd,std::vector<char> * buf)85 ssize_t ReadExact(SharedFD fd, std::vector<char>* buf) {
86   return ReadExact(fd, buf->data(), buf->size());
87 }
88 
WriteAll(SharedFD fd,const std::string & buf)89 ssize_t WriteAll(SharedFD fd, const std::string& buf) {
90   return WriteAll(fd, buf.data(), buf.size());
91 }
92 
WriteAll(SharedFD fd,const std::vector<char> & buf)93 ssize_t WriteAll(SharedFD fd, const std::vector<char>& buf) {
94   return WriteAll(fd, buf.data(), buf.size());
95 }
96 
97 } // namespace cuttlefish
98