• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-flash / 1001644023

22 Jun 2023 08:46AM UTC coverage: 67.647% (-1.1%) from 68.75%
1001644023

push

gitlab-ci

web-flow
Merge pull request #4 from lluiscampos/ci-rename-job

ci: Rename unit tests job for consistency with the other projects

207 of 306 relevant lines covered (67.65%)

17.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/common/io.hpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#ifndef MENDER_COMMON_IO_HPP
16
#define MENDER_COMMON_IO_HPP
17

18
#include <common/error.hpp>
19
#include <common/expected.hpp>
20

21
#include <memory>
22
#include <system_error>
23
#include <vector>
24
#include <istream>
25
#include <sstream>
26
#include <string>
27

28
namespace mender {
29
namespace common {
30
namespace io {
31

32
using namespace std;
33
namespace expected = mender::common::expected;
34

35
using mender::common::error::Error;
36
using mender::common::error::NoError;
37

38
using ExpectedSize = expected::expected<size_t, Error>;
39

40
class Reader {
41
public:
42
        virtual ~Reader() {};
×
43

44
        virtual ExpectedSize Read(vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) = 0;
45
};
46
using ReaderPtr = shared_ptr<Reader>;
47
using ExpectedReaderPtr = expected::expected<ReaderPtr, Error>;
48

49
class Writer {
50
public:
51
        virtual ~Writer() {};
×
52

53
        virtual ExpectedSize Write(
54
                vector<uint8_t>::const_iterator start, vector<uint8_t>::const_iterator end) = 0;
55
};
56
using WriterPtr = shared_ptr<Writer>;
57
using ExpectedWriterPtr = expected::expected<WriterPtr, Error>;
58

59
class ReadWriter : virtual public Reader, virtual public Writer {};
60
using ReadWriterPtr = shared_ptr<ReadWriter>;
61
using ExpectedReadWriterPtr = expected::expected<ReadWriterPtr, Error>;
62

63
/**
64
 * Stream the data from `src` to `dst` until encountering EOF or an error.
65
 */
66
Error Copy(Writer &dst, Reader &src);
67

68
/**
69
 * Stream the data from `src` to `dst` until encountering EOF or an error, using `buffer` as an
70
 * intermediate. The block size will be the size of `buffer`.
71
 */
72
Error Copy(Writer &dst, Reader &src, vector<uint8_t> &buffer);
73

74
class StreamReader : virtual public Reader {
75
private:
76
        std::istream &is_;
77

78
public:
79
        StreamReader(std::istream &stream) :
80
                is_ {stream} {
81
        }
82
        StreamReader(std::istream &&stream) :
83
                is_ {stream} {
84
        }
85
        ExpectedSize Read(vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) override {
86
                is_.read(reinterpret_cast<char *>(&*start), end - start);
87
                if (is_.bad()) {
88
                        int int_error = errno;
89
                        return expected::unexpected(Error(
90
                                std::error_code(int_error, std::generic_category()).default_error_condition(), ""));
91
                }
92
                return is_.gcount();
93
        }
94
};
95

96
/* Discards all data written to it */
97
class Discard : virtual public Writer {
98
        ExpectedSize Write(
99
                vector<uint8_t>::const_iterator start, vector<uint8_t>::const_iterator end) override {
100
                return end - start;
101
        }
102
};
103

104
class StringReader : virtual public Reader {
105
private:
106
        std::stringstream s_;
107
        StreamReader reader_;
108

109
public:
110
        StringReader(string &str) :
111
                s_ {str},
112
                reader_ {s_} {
113
        }
114
        StringReader(string &&str) :
115
                s_ {str},
116
                reader_ {s_} {
117
        }
118

119
        ExpectedSize Read(vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) override {
120
                return reader_.Read(start, end);
121
        }
122
};
123

124
} // namespace io
125
} // namespace common
126
} // namespace mender
127

128
#endif // MENDER_COMMON_IO_HPP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc