1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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
16 #include "applypatch/block_writer.h"
17 #include <sys/types.h>
18 #include "applypatch/block_set.h"
19 #include "log/log.h"
20 #include "utils.h"
21
22 namespace Updater {
IsWriteDone() const23 bool BlockWriter::IsWriteDone() const
24 {
25 return blockIndex_ == bs_.CountOfRanges() && currentBlockLeft_ == 0;
26 }
27
GetBlocksSize() const28 size_t BlockWriter::GetBlocksSize() const
29 {
30 return bs_.TotalBlockSize() * H_BLOCK_SIZE;
31 }
32
GetTotalWritten() const33 size_t BlockWriter::GetTotalWritten() const
34 {
35 return totalWritten_;
36 }
37
Write(const uint8_t * addr,size_t len,const void * context)38 bool BlockWriter::Write(const uint8_t *addr, size_t len, [[maybe_unused]] const void *context)
39 {
40 if (IsWriteDone()) {
41 LOG(WARNING) << "BlockWriter: call writer while no more blocks need to write. skip " << len << " byte(s)";
42 return false;
43 }
44 LOG(DEBUG) << "BlockWriter: try to write " << len << " byte(s)";
45 while (len > 0) {
46 // Check if blocks can be written.
47 // All blocks are written.
48 LOG(DEBUG) << "blockIndex = " << blockIndex_;
49
50 // Current block written is done, get next block.
51 if (currentBlockLeft_ == 0) {
52 if (blockIndex_ >= bs_.CountOfRanges()) {
53 LOG(DEBUG) << "block write overflow";
54 return false;
55 }
56 // Get next block pair to write.
57 const BlockPair &bp = bs_[blockIndex_];
58 // where do we start to write.
59 off_t offset = static_cast<off_t>(bp.first) * H_BLOCK_SIZE;
60 currentBlockLeft_ = (bp.second - bp.first) * H_BLOCK_SIZE;
61 LOG(DEBUG) << "Init currentBlockLeft_ = " << currentBlockLeft_;
62 blockIndex_++;
63 // Move to right place to write.
64 if (lseek(fd_, offset, SEEK_SET) < 0) {
65 LOG(ERROR) << "BlockWriter: Cannot seek to offset: " << offset << " Block index: " << blockIndex_;
66 return false;
67 }
68 } else {
69 LOG(DEBUG) << "Current block " << blockIndex_ << " left " << currentBlockLeft_ << " to written.";
70 }
71 size_t written = len;
72 if (currentBlockLeft_ < len) {
73 written = currentBlockLeft_;
74 }
75 if (Updater::Utils::WriteFully(fd_, addr, written) == false) {
76 LOG(ERROR) << "BlockWriter: failed to write " << written << " byte(s).";
77 return false;
78 }
79 len -= written;
80 addr += written;
81 currentBlockLeft_ -= written;
82 totalWritten_ += written;
83 }
84 return true;
85 }
86 } // namespace Updater
87