blob: bec2594e2d3932c835d609dc0fb6186f9aa9c101 [file] [log] [blame]
Kelvin Zhang50bac652020-09-28 15:51:41 -04001//
2// Copyright (C) 2020 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#include <update_engine/payload_consumer/partition_writer.h>
17
18#include <fcntl.h>
19#include <linux/fs.h>
20
21#include <algorithm>
22#include <initializer_list>
23#include <memory>
24#include <utility>
25#include <vector>
26
27#include <base/strings/string_number_conversions.h>
28#include <bsdiff/bspatch.h>
29#include <puffin/puffpatch.h>
30#include <bsdiff/file_interface.h>
31#include <puffin/stream.h>
32
33#include "update_engine/common/terminator.h"
34#include "update_engine/common/utils.h"
35#include "update_engine/payload_consumer/bzip_extent_writer.h"
36#include "update_engine/payload_consumer/cached_file_descriptor.h"
37#include "update_engine/payload_consumer/extent_reader.h"
38#include "update_engine/payload_consumer/extent_writer.h"
39#include "update_engine/payload_consumer/fec_file_descriptor.h"
40#include "update_engine/payload_consumer/file_descriptor_utils.h"
41#include "update_engine/payload_consumer/install_plan.h"
42#include "update_engine/payload_consumer/mount_history.h"
43#include "update_engine/payload_consumer/payload_constants.h"
44#include "update_engine/payload_consumer/xz_extent_writer.h"
45
46namespace chromeos_update_engine {
47
48namespace {
49constexpr uint64_t kCacheSize = 1024 * 1024; // 1MB
50
51// Discard the tail of the block device referenced by |fd|, from the offset
52// |data_size| until the end of the block device. Returns whether the data was
53// discarded.
54
55bool DiscardPartitionTail(const FileDescriptorPtr& fd, uint64_t data_size) {
56 uint64_t part_size = fd->BlockDevSize();
57 if (!part_size || part_size <= data_size)
58 return false;
59
60 struct blkioctl_request {
61 int number;
62 const char* name;
63 };
64 const std::initializer_list<blkioctl_request> blkioctl_requests = {
65 {BLKDISCARD, "BLKDISCARD"},
66 {BLKSECDISCARD, "BLKSECDISCARD"},
67#ifdef BLKZEROOUT
68 {BLKZEROOUT, "BLKZEROOUT"},
69#endif
70 };
71 for (const auto& req : blkioctl_requests) {
72 int error = 0;
73 if (fd->BlkIoctl(req.number, data_size, part_size - data_size, &error) &&
74 error == 0) {
75 return true;
76 }
77 LOG(WARNING) << "Error discarding the last "
78 << (part_size - data_size) / 1024 << " KiB using ioctl("
79 << req.name << ")";
80 }
81 return false;
82}
83
84} // namespace
85
86// Opens path for read/write. On success returns an open FileDescriptor
87// and sets *err to 0. On failure, sets *err to errno and returns nullptr.
88FileDescriptorPtr OpenFile(const char* path,
89 int mode,
90 bool cache_writes,
91 int* err) {
92 // Try to mark the block device read-only based on the mode. Ignore any
93 // failure since this won't work when passing regular files.
94 bool read_only = (mode & O_ACCMODE) == O_RDONLY;
95 utils::SetBlockDeviceReadOnly(path, read_only);
96
97 FileDescriptorPtr fd(new EintrSafeFileDescriptor());
98 if (cache_writes && !read_only) {
99 fd = FileDescriptorPtr(new CachedFileDescriptor(fd, kCacheSize));
100 LOG(INFO) << "Caching writes.";
101 }
102 if (!fd->Open(path, mode, 000)) {
103 *err = errno;
104 PLOG(ERROR) << "Unable to open file " << path;
105 return nullptr;
106 }
107 *err = 0;
108 return fd;
109}
110
111class BsdiffExtentFile : public bsdiff::FileInterface {
112 public:
113 BsdiffExtentFile(std::unique_ptr<ExtentReader> reader, size_t size)
114 : BsdiffExtentFile(std::move(reader), nullptr, size) {}
115 BsdiffExtentFile(std::unique_ptr<ExtentWriter> writer, size_t size)
116 : BsdiffExtentFile(nullptr, std::move(writer), size) {}
117
118 ~BsdiffExtentFile() override = default;
119
120 bool Read(void* buf, size_t count, size_t* bytes_read) override {
121 TEST_AND_RETURN_FALSE(reader_->Read(buf, count));
122 *bytes_read = count;
123 offset_ += count;
124 return true;
125 }
126
127 bool Write(const void* buf, size_t count, size_t* bytes_written) override {
128 TEST_AND_RETURN_FALSE(writer_->Write(buf, count));
129 *bytes_written = count;
130 offset_ += count;
131 return true;
132 }
133
134 bool Seek(off_t pos) override {
135 if (reader_ != nullptr) {
136 TEST_AND_RETURN_FALSE(reader_->Seek(pos));
137 offset_ = pos;
138 } else {
139 // For writes technically there should be no change of position, or it
140 // should be equivalent of current offset.
141 TEST_AND_RETURN_FALSE(offset_ == static_cast<uint64_t>(pos));
142 }
143 return true;
144 }
145
146 bool Close() override { return true; }
147
148 bool GetSize(uint64_t* size) override {
149 *size = size_;
150 return true;
151 }
152
153 private:
154 BsdiffExtentFile(std::unique_ptr<ExtentReader> reader,
155 std::unique_ptr<ExtentWriter> writer,
156 size_t size)
157 : reader_(std::move(reader)),
158 writer_(std::move(writer)),
159 size_(size),
160 offset_(0) {}
161
162 std::unique_ptr<ExtentReader> reader_;
163 std::unique_ptr<ExtentWriter> writer_;
164 uint64_t size_;
165 uint64_t offset_;
166
167 DISALLOW_COPY_AND_ASSIGN(BsdiffExtentFile);
168};
169// A class to be passed to |puffpatch| for reading from |source_fd_| and writing
170// into |target_fd_|.
171class PuffinExtentStream : public puffin::StreamInterface {
172 public:
173 // Constructor for creating a stream for reading from an |ExtentReader|.
174 PuffinExtentStream(std::unique_ptr<ExtentReader> reader, uint64_t size)
175 : PuffinExtentStream(std::move(reader), nullptr, size) {}
176
177 // Constructor for creating a stream for writing to an |ExtentWriter|.
178 PuffinExtentStream(std::unique_ptr<ExtentWriter> writer, uint64_t size)
179 : PuffinExtentStream(nullptr, std::move(writer), size) {}
180
181 ~PuffinExtentStream() override = default;
182
183 bool GetSize(uint64_t* size) const override {
184 *size = size_;
185 return true;
186 }
187
188 bool GetOffset(uint64_t* offset) const override {
189 *offset = offset_;
190 return true;
191 }
192
193 bool Seek(uint64_t offset) override {
194 if (is_read_) {
195 TEST_AND_RETURN_FALSE(reader_->Seek(offset));
196 offset_ = offset;
197 } else {
198 // For writes technically there should be no change of position, or it
199 // should equivalent of current offset.
200 TEST_AND_RETURN_FALSE(offset_ == offset);
201 }
202 return true;
203 }
204
205 bool Read(void* buffer, size_t count) override {
206 TEST_AND_RETURN_FALSE(is_read_);
207 TEST_AND_RETURN_FALSE(reader_->Read(buffer, count));
208 offset_ += count;
209 return true;
210 }
211
212 bool Write(const void* buffer, size_t count) override {
213 TEST_AND_RETURN_FALSE(!is_read_);
214 TEST_AND_RETURN_FALSE(writer_->Write(buffer, count));
215 offset_ += count;
216 return true;
217 }
218
219 bool Close() override { return true; }
220
221 private:
222 PuffinExtentStream(std::unique_ptr<ExtentReader> reader,
223 std::unique_ptr<ExtentWriter> writer,
224 uint64_t size)
225 : reader_(std::move(reader)),
226 writer_(std::move(writer)),
227 size_(size),
228 offset_(0),
229 is_read_(reader_ ? true : false) {}
230
231 std::unique_ptr<ExtentReader> reader_;
232 std::unique_ptr<ExtentWriter> writer_;
233 uint64_t size_;
234 uint64_t offset_;
235 bool is_read_;
236
237 DISALLOW_COPY_AND_ASSIGN(PuffinExtentStream);
238};
239
240PartitionWriter::PartitionWriter(
241 const PartitionUpdate& partition_update,
242 const InstallPlan::Partition& install_part,
243 DynamicPartitionControlInterface* dynamic_control,
244 size_t block_size,
245 bool is_interactive)
246 : partition_update_(partition_update),
247 install_part_(install_part),
248 dynamic_control_(dynamic_control),
249 interactive_(is_interactive),
Kelvin Zhang59928f12020-11-11 21:21:27 +0000250 block_size_(block_size) {}
Kelvin Zhang50bac652020-09-28 15:51:41 -0400251
252PartitionWriter::~PartitionWriter() {
253 Close();
254}
255
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500256bool PartitionWriter::OpenSourcePartition(uint32_t source_slot,
257 bool source_may_exist) {
258 source_path_.clear();
259 if (!source_may_exist) {
260 return true;
261 }
262 if (install_part_.source_size > 0 && !install_part_.source_path.empty()) {
263 source_path_ = install_part_.source_path;
264 int err;
265 source_fd_ = OpenFile(source_path_.c_str(), O_RDONLY, false, &err);
266 if (source_fd_ == nullptr) {
267 LOG(ERROR) << "Unable to open source partition " << install_part_.name
268 << " on slot " << BootControlInterface::SlotName(source_slot)
269 << ", file " << source_path_;
270 return false;
271 }
272 }
273 return true;
274}
275
Kelvin Zhang50bac652020-09-28 15:51:41 -0400276bool PartitionWriter::Init(const InstallPlan* install_plan,
277 bool source_may_exist) {
278 const PartitionUpdate& partition = partition_update_;
279 uint32_t source_slot = install_plan->source_slot;
280 uint32_t target_slot = install_plan->target_slot;
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500281 TEST_AND_RETURN_FALSE(OpenSourcePartition(source_slot, source_may_exist));
Kelvin Zhang50bac652020-09-28 15:51:41 -0400282
283 // We shouldn't open the source partition in certain cases, e.g. some dynamic
284 // partitions in delta payload, partitions included in the full payload for
285 // partial updates. Use the source size as the indicator.
Kelvin Zhang50bac652020-09-28 15:51:41 -0400286
287 target_path_ = install_part_.target_path;
288 int err;
289
290 int flags = O_RDWR;
291 if (!interactive_)
292 flags |= O_DSYNC;
293
294 LOG(INFO) << "Opening " << target_path_ << " partition with"
295 << (interactive_ ? "out" : "") << " O_DSYNC";
296
297 target_fd_ = OpenFile(target_path_.c_str(), flags, true, &err);
298 if (!target_fd_) {
299 LOG(ERROR) << "Unable to open target partition "
300 << partition.partition_name() << " on slot "
301 << BootControlInterface::SlotName(target_slot) << ", file "
302 << target_path_;
303 return false;
304 }
305
306 LOG(INFO) << "Applying " << partition.operations().size()
307 << " operations to partition \"" << partition.partition_name()
308 << "\"";
309
310 // Discard the end of the partition, but ignore failures.
311 DiscardPartitionTail(target_fd_, install_part_.target_size);
312
313 return true;
314}
315
316bool PartitionWriter::PerformReplaceOperation(const InstallOperation& operation,
317 const void* data,
318 size_t count) {
319 // Setup the ExtentWriter stack based on the operation type.
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400320 std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400321
322 if (operation.type() == InstallOperation::REPLACE_BZ) {
323 writer.reset(new BzipExtentWriter(std::move(writer)));
324 } else if (operation.type() == InstallOperation::REPLACE_XZ) {
325 writer.reset(new XzExtentWriter(std::move(writer)));
326 }
327
328 TEST_AND_RETURN_FALSE(
329 writer->Init(target_fd_, operation.dst_extents(), block_size_));
330 TEST_AND_RETURN_FALSE(writer->Write(data, operation.data_length()));
331
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400332 return Flush();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400333}
334
335bool PartitionWriter::PerformZeroOrDiscardOperation(
336 const InstallOperation& operation) {
337#ifdef BLKZEROOUT
338 bool attempt_ioctl = true;
339 int request =
340 (operation.type() == InstallOperation::ZERO ? BLKZEROOUT : BLKDISCARD);
341#else // !defined(BLKZEROOUT)
342 bool attempt_ioctl = false;
343 int request = 0;
344#endif // !defined(BLKZEROOUT)
345
346 brillo::Blob zeros;
347 for (const Extent& extent : operation.dst_extents()) {
348 const uint64_t start = extent.start_block() * block_size_;
349 const uint64_t length = extent.num_blocks() * block_size_;
350 if (attempt_ioctl) {
351 int result = 0;
352 if (target_fd_->BlkIoctl(request, start, length, &result) && result == 0)
353 continue;
354 attempt_ioctl = false;
355 }
356 // In case of failure, we fall back to writing 0 to the selected region.
357 zeros.resize(16 * block_size_);
358 for (uint64_t offset = 0; offset < length; offset += zeros.size()) {
359 uint64_t chunk_length =
360 std::min(length - offset, static_cast<uint64_t>(zeros.size()));
Kelvin Zhang4b280242020-11-06 16:07:45 -0500361 TEST_AND_RETURN_FALSE(utils::WriteAll(
Kelvin Zhang50bac652020-09-28 15:51:41 -0400362 target_fd_, zeros.data(), chunk_length, start + offset));
363 }
364 }
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400365 return Flush();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400366}
367
368bool PartitionWriter::PerformSourceCopyOperation(
369 const InstallOperation& operation, ErrorCode* error) {
370 TEST_AND_RETURN_FALSE(source_fd_ != nullptr);
371
372 // The device may optimize the SOURCE_COPY operation.
373 // Being this a device-specific optimization let DynamicPartitionController
374 // decide it the operation should be skipped.
375 const PartitionUpdate& partition = partition_update_;
376 const auto& partition_control = dynamic_control_;
377
378 InstallOperation buf;
379 bool should_optimize = partition_control->OptimizeOperation(
380 partition.partition_name(), operation, &buf);
381 const InstallOperation& optimized = should_optimize ? buf : operation;
382
383 if (operation.has_src_sha256_hash()) {
384 bool read_ok;
385 brillo::Blob source_hash;
386 brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(),
387 operation.src_sha256_hash().end());
388
389 // We fall back to use the error corrected device if the hash of the raw
390 // device doesn't match or there was an error reading the source partition.
391 // Note that this code will also fall back if writing the target partition
392 // fails.
393 if (should_optimize) {
394 // Hash operation.src_extents(), then copy optimized.src_extents to
395 // optimized.dst_extents.
396 read_ok =
397 fd_utils::ReadAndHashExtents(
398 source_fd_, operation.src_extents(), block_size_, &source_hash) &&
399 fd_utils::CopyAndHashExtents(source_fd_,
400 optimized.src_extents(),
401 target_fd_,
402 optimized.dst_extents(),
403 block_size_,
404 nullptr /* skip hashing */);
405 } else {
406 read_ok = fd_utils::CopyAndHashExtents(source_fd_,
407 operation.src_extents(),
408 target_fd_,
409 operation.dst_extents(),
410 block_size_,
411 &source_hash);
412 }
413 if (read_ok && expected_source_hash == source_hash)
414 return true;
415 LOG(WARNING) << "Source hash from RAW device mismatched, attempting to "
416 "correct using ECC";
417 if (!OpenCurrentECCPartition()) {
418 // The following function call will return false since the source hash
419 // mismatches, but we still want to call it so it prints the appropriate
420 // log message.
421 return ValidateSourceHash(source_hash, operation, source_fd_, error);
422 }
423
424 LOG(WARNING) << "Source hash from RAW device mismatched: found "
425 << base::HexEncode(source_hash.data(), source_hash.size())
426 << ", expected "
427 << base::HexEncode(expected_source_hash.data(),
428 expected_source_hash.size());
429 if (should_optimize) {
430 TEST_AND_RETURN_FALSE(fd_utils::ReadAndHashExtents(
431 source_ecc_fd_, operation.src_extents(), block_size_, &source_hash));
432 TEST_AND_RETURN_FALSE(
433 fd_utils::CopyAndHashExtents(source_ecc_fd_,
434 optimized.src_extents(),
435 target_fd_,
436 optimized.dst_extents(),
437 block_size_,
438 nullptr /* skip hashing */));
439 } else {
440 TEST_AND_RETURN_FALSE(
441 fd_utils::CopyAndHashExtents(source_ecc_fd_,
442 operation.src_extents(),
443 target_fd_,
444 operation.dst_extents(),
445 block_size_,
446 &source_hash));
447 }
448 TEST_AND_RETURN_FALSE(
449 ValidateSourceHash(source_hash, operation, source_ecc_fd_, error));
450 // At this point reading from the error corrected device worked, but
451 // reading from the raw device failed, so this is considered a recovered
452 // failure.
453 source_ecc_recovered_failures_++;
454 } else {
455 // When the operation doesn't include a source hash, we attempt the error
456 // corrected device first since we can't verify the block in the raw device
457 // at this point, but we fall back to the raw device since the error
458 // corrected device can be shorter or not available.
459
460 if (OpenCurrentECCPartition() &&
461 fd_utils::CopyAndHashExtents(source_ecc_fd_,
462 optimized.src_extents(),
463 target_fd_,
464 optimized.dst_extents(),
465 block_size_,
466 nullptr)) {
467 return true;
468 }
469 TEST_AND_RETURN_FALSE(fd_utils::CopyAndHashExtents(source_fd_,
470 optimized.src_extents(),
471 target_fd_,
472 optimized.dst_extents(),
473 block_size_,
474 nullptr));
475 }
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400476 return Flush();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400477}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400478
Kelvin Zhang50bac652020-09-28 15:51:41 -0400479bool PartitionWriter::PerformSourceBsdiffOperation(
480 const InstallOperation& operation,
481 ErrorCode* error,
482 const void* data,
483 size_t count) {
484 FileDescriptorPtr source_fd = ChooseSourceFD(operation, error);
485 TEST_AND_RETURN_FALSE(source_fd != nullptr);
486
487 auto reader = std::make_unique<DirectExtentReader>();
488 TEST_AND_RETURN_FALSE(
489 reader->Init(source_fd, operation.src_extents(), block_size_));
490 auto src_file = std::make_unique<BsdiffExtentFile>(
491 std::move(reader),
492 utils::BlocksInExtents(operation.src_extents()) * block_size_);
493
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400494 auto writer = CreateBaseExtentWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400495 TEST_AND_RETURN_FALSE(
496 writer->Init(target_fd_, operation.dst_extents(), block_size_));
497 auto dst_file = std::make_unique<BsdiffExtentFile>(
498 std::move(writer),
499 utils::BlocksInExtents(operation.dst_extents()) * block_size_);
500
501 TEST_AND_RETURN_FALSE(bsdiff::bspatch(std::move(src_file),
502 std::move(dst_file),
503 reinterpret_cast<const uint8_t*>(data),
504 count) == 0);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400505 return Flush();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400506}
507
508bool PartitionWriter::PerformPuffDiffOperation(
509 const InstallOperation& operation,
510 ErrorCode* error,
511 const void* data,
512 size_t count) {
513 FileDescriptorPtr source_fd = ChooseSourceFD(operation, error);
514 TEST_AND_RETURN_FALSE(source_fd != nullptr);
515
516 auto reader = std::make_unique<DirectExtentReader>();
517 TEST_AND_RETURN_FALSE(
518 reader->Init(source_fd, operation.src_extents(), block_size_));
519 puffin::UniqueStreamPtr src_stream(new PuffinExtentStream(
520 std::move(reader),
521 utils::BlocksInExtents(operation.src_extents()) * block_size_));
522
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400523 auto writer = CreateBaseExtentWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400524 TEST_AND_RETURN_FALSE(
525 writer->Init(target_fd_, operation.dst_extents(), block_size_));
526 puffin::UniqueStreamPtr dst_stream(new PuffinExtentStream(
527 std::move(writer),
528 utils::BlocksInExtents(operation.dst_extents()) * block_size_));
529
530 constexpr size_t kMaxCacheSize = 5 * 1024 * 1024; // Total 5MB cache.
531 TEST_AND_RETURN_FALSE(
532 puffin::PuffPatch(std::move(src_stream),
533 std::move(dst_stream),
534 reinterpret_cast<const uint8_t*>(data),
535 count,
536 kMaxCacheSize));
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400537 return Flush();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400538}
539
540FileDescriptorPtr PartitionWriter::ChooseSourceFD(
541 const InstallOperation& operation, ErrorCode* error) {
542 if (source_fd_ == nullptr) {
543 LOG(ERROR) << "ChooseSourceFD fail: source_fd_ == nullptr";
544 return nullptr;
545 }
546
547 if (!operation.has_src_sha256_hash()) {
548 // When the operation doesn't include a source hash, we attempt the error
549 // corrected device first since we can't verify the block in the raw device
550 // at this point, but we first need to make sure all extents are readable
551 // since the error corrected device can be shorter or not available.
552 if (OpenCurrentECCPartition() &&
553 fd_utils::ReadAndHashExtents(
554 source_ecc_fd_, operation.src_extents(), block_size_, nullptr)) {
555 return source_ecc_fd_;
556 }
557 return source_fd_;
558 }
559
560 brillo::Blob source_hash;
561 brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(),
562 operation.src_sha256_hash().end());
563 if (fd_utils::ReadAndHashExtents(
564 source_fd_, operation.src_extents(), block_size_, &source_hash) &&
565 source_hash == expected_source_hash) {
566 return source_fd_;
567 }
568 // We fall back to use the error corrected device if the hash of the raw
569 // device doesn't match or there was an error reading the source partition.
570 if (!OpenCurrentECCPartition()) {
571 // The following function call will return false since the source hash
572 // mismatches, but we still want to call it so it prints the appropriate
573 // log message.
574 ValidateSourceHash(source_hash, operation, source_fd_, error);
575 return nullptr;
576 }
577 LOG(WARNING) << "Source hash from RAW device mismatched: found "
578 << base::HexEncode(source_hash.data(), source_hash.size())
579 << ", expected "
580 << base::HexEncode(expected_source_hash.data(),
581 expected_source_hash.size());
582
583 if (fd_utils::ReadAndHashExtents(
584 source_ecc_fd_, operation.src_extents(), block_size_, &source_hash) &&
585 ValidateSourceHash(source_hash, operation, source_ecc_fd_, error)) {
586 // At this point reading from the error corrected device worked, but
587 // reading from the raw device failed, so this is considered a recovered
588 // failure.
589 source_ecc_recovered_failures_++;
590 return source_ecc_fd_;
591 }
592 return nullptr;
593}
594
595bool PartitionWriter::OpenCurrentECCPartition() {
596 // No support for ECC for full payloads.
597 // Full payload should not have any opeartion that requires ECC partitions.
598 if (source_ecc_fd_)
599 return true;
600
601 if (source_ecc_open_failure_)
602 return false;
603
604#if USE_FEC
605 const PartitionUpdate& partition = partition_update_;
606 const InstallPlan::Partition& install_part = install_part_;
607 std::string path = install_part.source_path;
608 FileDescriptorPtr fd(new FecFileDescriptor());
609 if (!fd->Open(path.c_str(), O_RDONLY, 0)) {
610 PLOG(ERROR) << "Unable to open ECC source partition "
611 << partition.partition_name() << ", file " << path;
612 source_ecc_open_failure_ = true;
613 return false;
614 }
615 source_ecc_fd_ = fd;
616#else
617 // No support for ECC compiled.
618 source_ecc_open_failure_ = true;
619#endif // USE_FEC
620
621 return !source_ecc_open_failure_;
622}
623
624int PartitionWriter::Close() {
625 int err = 0;
626 if (source_fd_ && !source_fd_->Close()) {
627 err = errno;
628 PLOG(ERROR) << "Error closing source partition";
629 if (!err)
630 err = 1;
631 }
632 source_fd_.reset();
633 source_path_.clear();
634
635 if (target_fd_ && !target_fd_->Close()) {
636 err = errno;
637 PLOG(ERROR) << "Error closing target partition";
638 if (!err)
639 err = 1;
640 }
641 target_fd_.reset();
642 target_path_.clear();
643
644 if (source_ecc_fd_ && !source_ecc_fd_->Close()) {
645 err = errno;
646 PLOG(ERROR) << "Error closing ECC source partition";
647 if (!err)
648 err = 1;
649 }
650 source_ecc_fd_.reset();
651 source_ecc_open_failure_ = false;
652 return -err;
653}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400654
655std::unique_ptr<ExtentWriter> PartitionWriter::CreateBaseExtentWriter() {
656 return std::make_unique<DirectExtentWriter>();
657}
658
659bool PartitionWriter::Flush() {
660 return target_fd_->Flush();
661}
662
Kelvin Zhang50bac652020-09-28 15:51:41 -0400663} // namespace chromeos_update_engine