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

mendersoftware / mender / 950534094

pending completion
950534094

push

gitlab-ci

kacf
chore: Add `StartsWith` and `EndsWith` generic utility functions.

Also use the latter in `states.cpp`.

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

1 of 1 new or added line in 1 file covered. (100.0%)

4931 of 6276 relevant lines covered (78.57%)

196.18 hits per line

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

68.12
/common/events/events_io.cpp
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
#include <common/events_io.hpp>
16

17
namespace mender {
18
namespace common {
19
namespace events {
20
namespace io {
21

22
AsyncReaderFromReader::AsyncReaderFromReader(EventLoop &loop, mio::ReaderPtr reader) :
×
23
        reader_(reader),
24
        loop_(loop) {
×
25
}
×
26

27
AsyncReaderFromReader::~AsyncReaderFromReader() {
×
28
        Cancel();
×
29
}
×
30

31
error::Error AsyncReaderFromReader::AsyncRead(
514✔
32
        vector<uint8_t>::iterator start, vector<uint8_t>::iterator end, mio::AsyncIoHandler handler) {
33
        cancelled_ = make_shared<bool>(false);
514✔
34
        auto &cancelled = cancelled_;
514✔
35
        loop_.Post([this, cancelled, start, end, handler]() {
514✔
36
                if (!*cancelled) {
514✔
37
                        in_progress_ = true;
514✔
38
                        // Simple, "cheating" implementation, we just do it synchronously.
39
                        auto result = reader_->Read(start, end);
514✔
40
                        in_progress_ = false;
514✔
41
                        handler(result);
514✔
42
                }
43
        });
1,542✔
44

45
        return error::NoError;
514✔
46
}
47

48
void AsyncReaderFromReader::Cancel() {
42✔
49
        // Cancel() is not allowed on normal Readers.
50
        assert(!in_progress_);
42✔
51
        if (cancelled_) {
42✔
52
                *cancelled_ = true;
37✔
53
                cancelled_.reset();
37✔
54
        }
55
}
42✔
56

57
AsyncWriterFromWriter::AsyncWriterFromWriter(EventLoop &loop, mio::WriterPtr writer) :
×
58
        writer_(writer),
59
        loop_(loop) {
×
60
}
×
61

62
AsyncWriterFromWriter::~AsyncWriterFromWriter() {
×
63
        Cancel();
×
64
}
×
65

66
error::Error AsyncWriterFromWriter::AsyncWrite(
1✔
67
        vector<uint8_t>::const_iterator start,
68
        vector<uint8_t>::const_iterator end,
69
        mio::AsyncIoHandler handler) {
70
        cancelled_ = make_shared<bool>(false);
1✔
71
        auto &cancelled = cancelled_;
1✔
72
        loop_.Post([this, cancelled, start, end, handler]() {
1✔
73
                if (!*cancelled) {
1✔
74
                        in_progress_ = true;
1✔
75
                        // Simple, "cheating" implementation, we just do it synchronously.
76
                        auto result = writer_->Write(start, end);
1✔
77
                        in_progress_ = false;
1✔
78
                        handler(result);
1✔
79
                }
80
        });
3✔
81

82
        return error::NoError;
1✔
83
}
84

85
void AsyncWriterFromWriter::Cancel() {
1✔
86
        // Cancel() is not allowed on normal Writers.
87
        assert(!in_progress_);
1✔
88
        if (cancelled_) {
1✔
89
                *cancelled_ = true;
1✔
90
                cancelled_.reset();
1✔
91
        }
92
}
1✔
93

94
ReaderFromAsyncReader::ReaderFromAsyncReader(EventLoop &event_loop, mio::AsyncReaderPtr reader) :
×
95
        event_loop_(event_loop),
96
        reader_(reader) {
×
97
}
×
98

99
ReaderFromAsyncReader::ReaderFromAsyncReader(EventLoop &event_loop, mio::AsyncReader &reader) :
×
100
        event_loop_(event_loop),
101
        // For references, just use a destructor-less pointer.
102
        reader_(&reader, [](mio::AsyncReader *) {}) {
×
103
}
×
104

105
mio::ExpectedSize ReaderFromAsyncReader::Read(
20✔
106
        vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) {
107
        mio::ExpectedSize read;
20✔
108
        error::Error err;
40✔
109
        bool finished = false;
20✔
110
        err = reader_->AsyncRead(start, end, [this, &finished, &read](mio::ExpectedSize num_read) {
40✔
111
                read = num_read;
20✔
112
                finished = true;
20✔
113
                event_loop_.Stop();
20✔
114
        });
40✔
115
        if (err != error::NoError) {
20✔
116
                return expected::unexpected(err);
×
117
        }
118

119
        // Since the same event loop may have been used to call into this function, run the event
120
        // loop recursively to keep processing events.
121
        event_loop_.Run();
20✔
122

123
        if (!finished) {
20✔
124
                // If this happens then it means that the event loop was stopped by somebody
125
                // else. We have no choice now but to return error, since we have to get out of this
126
                // stack frame. We also need to re-stop the event loop, since the first stop was
127
                // spent on getting here.
128
                event_loop_.Stop();
×
129
                return expected::unexpected(
×
130
                        error::Error(make_error_condition(errc::operation_canceled), "Event loop was stopped"));
×
131
        }
132

133
        return read;
20✔
134
}
135

136
} // namespace io
137
} // namespace events
138
} // namespace common
139
} // namespace mender
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