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

mendersoftware / mender-flash / 1113387411

27 Nov 2023 02:52PM UTC coverage: 66.997%. Remained the same
1113387411

push

gitlab-ci

web-flow
Merge pull request #9 from kacf/test_fixes

Several fixes for UBI

4 of 6 new or added lines in 2 files covered. (66.67%)

203 of 303 relevant lines covered (67.0%)

13.22 hits per line

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

87.5
/libflash/optimized_writer.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 <optimized_writer.hpp>
16
#include <iostream>
17

18
using namespace mender::io;
19

20
OptimizedWriter::OptimizedWriter(
8✔
21
        io::FileReader &reader, io::FileReadWriterSeeker &writer, size_t blockSize, size_t volumeSize) :
8✔
22
        blockSize_(blockSize),
23
        reader_(reader),
24
        readWriter_(writer),
25
        volumeSize_(volumeSize) {
8✔
26
}
8✔
27

28
Error OptimizedWriter::Copy(bool optimized) {
10✔
29
        statistics_.blocksWritten_ = 0;
10✔
30
        statistics_.blocksOmitted_ = 0;
10✔
31
        statistics_.bytesWritten_ = 0;
10✔
32
        statistics_.bytesTotal_ = 0;
10✔
33

34
        io::Bytes rv(blockSize_);
20✔
35
        io::Bytes wv(blockSize_);
20✔
36

37
        size_t position = 0;
10✔
38

39
        while (true) {
40
                auto result = reader_.Read(rv.begin(), rv.end());
73✔
41
                if (!result) {
73✔
42
                        return result.error();
×
43
                }
44

45
                if (result.value() == 0) {
73✔
46
                        if (volumeSize_ && position < volumeSize_) {
7✔
47
                                return Error(
48
                                        std::errc::io_error,
49
                                        "Size of the destination volume not reached, source too short.");
1✔
50
                        } else {
51
                                return NoError;
6✔
52
                        }
53
                } else if (result.value() > rv.size()) {
66✔
54
                        return mender::common::error::MakeError(
55
                                mender::common::error::ProgrammingError,
56
                                "Read returned more bytes than requested. This is a bug in the Read function.");
×
57
                } else if (volumeSize_ && (position + result.value()) > volumeSize_) {
66✔
58
                        return Error(
59
                                std::errc::io_error, "Reached size of the destination volume, source too big.");
2✔
60
                }
61

62
                auto readBytes = result.value();
64✔
63

64
                bool skipWriting = false;
64✔
65
                if (optimized) {
64✔
66
                        // not checking for short read, it is not expected unless
67
                        // writing to an empty file
68
                        auto readResult = readWriter_.Read(wv.begin(), wv.begin() + readBytes);
34✔
69
                        if (readResult && readResult.value() == readBytes) {
34✔
70
                                wv.resize(readResult.value());
10✔
71
                                skipWriting = std::equal(rv.begin(), rv.end(), wv.data());
10✔
72
                                if (skipWriting) {
10✔
73
                                        ++statistics_.blocksOmitted_;
10✔
74
                                }
75
                        }
76

77
                        if (!skipWriting && NoError != readWriter_.SeekSet(position)) {
34✔
78
                                return Error(
NEW
79
                                        std::error_condition(std::errc::io_error),
×
NEW
80
                                        "Failed to set seek on the destination file");
×
81
                        }
82
                }
83

84
                if (!skipWriting) {
64✔
85
                        auto res = readWriter_.Write(rv.begin(), rv.begin() + readBytes);
54✔
86
                        if (res && res.value() == readBytes) {
54✔
87
                                ++statistics_.blocksWritten_;
53✔
88
                                statistics_.bytesWritten_ += res.value();
53✔
89
                        } else if (res && res.value() == 0) {
1✔
90
                                return Error(
91
                                        std::error_condition(std::errc::io_error), "Zero write while copying data");
×
92
                        } else if (res) {
1✔
93
                                return Error(
94
                                        std::error_condition(std::errc::io_error), "Short write while copying data");
×
95
                        } else {
96
                                return res.error();
1✔
97
                        }
98
                }
99

100
                position += readBytes;
63✔
101
                statistics_.bytesTotal_ += readBytes;
63✔
102
        }
63✔
103
        return NoError;
104
}
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