blob: ec36d069767b301478ebad6dd18a58ebb9130afd [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
256bool PartitionWriter::Init(const InstallPlan* install_plan,
257 bool source_may_exist) {
258 const PartitionUpdate& partition = partition_update_;
259 uint32_t source_slot = install_plan->source_slot;
260 uint32_t target_slot = install_plan->target_slot;
261
262 // We shouldn't open the source partition in certain cases, e.g. some dynamic
263 // partitions in delta payload, partitions included in the full payload for
264 // partial updates. Use the source size as the indicator.
265 if (source_may_exist && install_part_.source_size > 0) {
266 source_path_ = install_part_.source_path;
267 int err;
268 source_fd_ = OpenFile(source_path_.c_str(), O_RDONLY, false, &err);
269 if (!source_fd_) {
270 LOG(ERROR) << "Unable to open source partition "
271 << partition.partition_name() << " on slot "
272 << BootControlInterface::SlotName(source_slot) << ", file "
273 << source_path_;
274 return false;
275 }
276 }
277
278 target_path_ = install_part_.target_path;
279 int err;
280
281 int flags = O_RDWR;
282 if (!interactive_)
283 flags |= O_DSYNC;
284
285 LOG(INFO) << "Opening " << target_path_ << " partition with"
286 << (interactive_ ? "out" : "") << " O_DSYNC";
287
288 target_fd_ = OpenFile(target_path_.c_str(), flags, true, &err);
289 if (!target_fd_) {
290 LOG(ERROR) << "Unable to open target partition "
291 << partition.partition_name() << " on slot "
292 << BootControlInterface::SlotName(target_slot) << ", file "
293 << target_path_;
294 return false;
295 }
296
297 LOG(INFO) << "Applying " << partition.operations().size()
298 << " operations to partition \"" << partition.partition_name()
299 << "\"";
300
301 // Discard the end of the partition, but ignore failures.
302 DiscardPartitionTail(target_fd_, install_part_.target_size);
303
304 return true;
305}
306
307bool PartitionWriter::PerformReplaceOperation(const InstallOperation& operation,
308 const void* data,
309 size_t count) {
310 // Setup the ExtentWriter stack based on the operation type.
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400311 std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400312
313 if (operation.type() == InstallOperation::REPLACE_BZ) {
314 writer.reset(new BzipExtentWriter(std::move(writer)));
315 } else if (operation.type() == InstallOperation::REPLACE_XZ) {
316 writer.reset(new XzExtentWriter(std::move(writer)));
317 }
318
319 TEST_AND_RETURN_FALSE(
320 writer->Init(target_fd_, operation.dst_extents(), block_size_));
321 TEST_AND_RETURN_FALSE(writer->Write(data, operation.data_length()));
322
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400323 return Flush();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400324}
325
326bool PartitionWriter::PerformZeroOrDiscardOperation(
327 const InstallOperation& operation) {
328#ifdef BLKZEROOUT
329 bool attempt_ioctl = true;
330 int request =
331 (operation.type() == InstallOperation::ZERO ? BLKZEROOUT : BLKDISCARD);
332#else // !defined(BLKZEROOUT)
333 bool attempt_ioctl = false;
334 int request = 0;
335#endif // !defined(BLKZEROOUT)
336
337 brillo::Blob zeros;
338 for (const Extent& extent : operation.dst_extents()) {
339 const uint64_t start = extent.start_block() * block_size_;
340 const uint64_t length = extent.num_blocks() * block_size_;
341 if (attempt_ioctl) {
342 int result = 0;
343 if (target_fd_->BlkIoctl(request, start, length, &result) && result == 0)
344 continue;
345 attempt_ioctl = false;
346 }
347 // In case of failure, we fall back to writing 0 to the selected region.
348 zeros.resize(16 * block_size_);
349 for (uint64_t offset = 0; offset < length; offset += zeros.size()) {
350 uint64_t chunk_length =
351 std::min(length - offset, static_cast<uint64_t>(zeros.size()));
Kelvin Zhang4b280242020-11-06 16:07:45 -0500352 TEST_AND_RETURN_FALSE(utils::WriteAll(
Kelvin Zhang50bac652020-09-28 15:51:41 -0400353 target_fd_, zeros.data(), chunk_length, start + offset));
354 }
355 }
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400356 return Flush();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400357}
358
359bool PartitionWriter::PerformSourceCopyOperation(
360 const InstallOperation& operation, ErrorCode* error) {
361 TEST_AND_RETURN_FALSE(source_fd_ != nullptr);
362
363 // The device may optimize the SOURCE_COPY operation.
364 // Being this a device-specific optimization let DynamicPartitionController
365 // decide it the operation should be skipped.
366 const PartitionUpdate& partition = partition_update_;
367 const auto& partition_control = dynamic_control_;
368
369 InstallOperation buf;
370 bool should_optimize = partition_control->OptimizeOperation(
371 partition.partition_name(), operation, &buf);
372 const InstallOperation& optimized = should_optimize ? buf : operation;
373
374 if (operation.has_src_sha256_hash()) {
375 bool read_ok;
376 brillo::Blob source_hash;
377 brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(),
378 operation.src_sha256_hash().end());
379
380 // We fall back to use the error corrected device if the hash of the raw
381 // device doesn't match or there was an error reading the source partition.
382 // Note that this code will also fall back if writing the target partition
383 // fails.
384 if (should_optimize) {
385 // Hash operation.src_extents(), then copy optimized.src_extents to
386 // optimized.dst_extents.
387 read_ok =
388 fd_utils::ReadAndHashExtents(
389 source_fd_, operation.src_extents(), block_size_, &source_hash) &&
390 fd_utils::CopyAndHashExtents(source_fd_,
391 optimized.src_extents(),
392 target_fd_,
393 optimized.dst_extents(),
394 block_size_,
395 nullptr /* skip hashing */);
396 } else {
397 read_ok = fd_utils::CopyAndHashExtents(source_fd_,
398 operation.src_extents(),
399 target_fd_,
400 operation.dst_extents(),
401 block_size_,
402 &source_hash);
403 }
404 if (read_ok && expected_source_hash == source_hash)
405 return true;
406 LOG(WARNING) << "Source hash from RAW device mismatched, attempting to "
407 "correct using ECC";
408 if (!OpenCurrentECCPartition()) {
409 // The following function call will return false since the source hash
410 // mismatches, but we still want to call it so it prints the appropriate
411 // log message.
412 return ValidateSourceHash(source_hash, operation, source_fd_, error);
413 }
414
415 LOG(WARNING) << "Source hash from RAW device mismatched: found "
416 << base::HexEncode(source_hash.data(), source_hash.size())
417 << ", expected "
418 << base::HexEncode(expected_source_hash.data(),
419 expected_source_hash.size());
420 if (should_optimize) {
421 TEST_AND_RETURN_FALSE(fd_utils::ReadAndHashExtents(
422 source_ecc_fd_, operation.src_extents(), block_size_, &source_hash));
423 TEST_AND_RETURN_FALSE(
424 fd_utils::CopyAndHashExtents(source_ecc_fd_,
425 optimized.src_extents(),
426 target_fd_,
427 optimized.dst_extents(),
428 block_size_,
429 nullptr /* skip hashing */));
430 } else {
431 TEST_AND_RETURN_FALSE(
432 fd_utils::CopyAndHashExtents(source_ecc_fd_,
433 operation.src_extents(),
434 target_fd_,
435 operation.dst_extents(),
436 block_size_,
437 &source_hash));
438 }
439 TEST_AND_RETURN_FALSE(
440 ValidateSourceHash(source_hash, operation, source_ecc_fd_, error));
441 // At this point reading from the error corrected device worked, but
442 // reading from the raw device failed, so this is considered a recovered
443 // failure.
444 source_ecc_recovered_failures_++;
445 } else {
446 // When the operation doesn't include a source hash, we attempt the error
447 // corrected device first since we can't verify the block in the raw device
448 // at this point, but we fall back to the raw device since the error
449 // corrected device can be shorter or not available.
450
451 if (OpenCurrentECCPartition() &&
452 fd_utils::CopyAndHashExtents(source_ecc_fd_,
453 optimized.src_extents(),
454 target_fd_,
455 optimized.dst_extents(),
456 block_size_,
457 nullptr)) {
458 return true;
459 }
460 TEST_AND_RETURN_FALSE(fd_utils::CopyAndHashExtents(source_fd_,
461 optimized.src_extents(),
462 target_fd_,
463 optimized.dst_extents(),
464 block_size_,
465 nullptr));
466 }
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400467 return Flush();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400468}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400469
Kelvin Zhang50bac652020-09-28 15:51:41 -0400470bool PartitionWriter::PerformSourceBsdiffOperation(
471 const InstallOperation& operation,
472 ErrorCode* error,
473 const void* data,
474 size_t count) {
475 FileDescriptorPtr source_fd = ChooseSourceFD(operation, error);
476 TEST_AND_RETURN_FALSE(source_fd != nullptr);
477
478 auto reader = std::make_unique<DirectExtentReader>();
479 TEST_AND_RETURN_FALSE(
480 reader->Init(source_fd, operation.src_extents(), block_size_));
481 auto src_file = std::make_unique<BsdiffExtentFile>(
482 std::move(reader),
483 utils::BlocksInExtents(operation.src_extents()) * block_size_);
484
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400485 auto writer = CreateBaseExtentWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400486 TEST_AND_RETURN_FALSE(
487 writer->Init(target_fd_, operation.dst_extents(), block_size_));
488 auto dst_file = std::make_unique<BsdiffExtentFile>(
489 std::move(writer),
490 utils::BlocksInExtents(operation.dst_extents()) * block_size_);
491
492 TEST_AND_RETURN_FALSE(bsdiff::bspatch(std::move(src_file),
493 std::move(dst_file),
494 reinterpret_cast<const uint8_t*>(data),
495 count) == 0);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400496 return Flush();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400497}
498
499bool PartitionWriter::PerformPuffDiffOperation(
500 const InstallOperation& operation,
501 ErrorCode* error,
502 const void* data,
503 size_t count) {
504 FileDescriptorPtr source_fd = ChooseSourceFD(operation, error);
505 TEST_AND_RETURN_FALSE(source_fd != nullptr);
506
507 auto reader = std::make_unique<DirectExtentReader>();
508 TEST_AND_RETURN_FALSE(
509 reader->Init(source_fd, operation.src_extents(), block_size_));
510 puffin::UniqueStreamPtr src_stream(new PuffinExtentStream(
511 std::move(reader),
512 utils::BlocksInExtents(operation.src_extents()) * block_size_));
513
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400514 auto writer = CreateBaseExtentWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400515 TEST_AND_RETURN_FALSE(
516 writer->Init(target_fd_, operation.dst_extents(), block_size_));
517 puffin::UniqueStreamPtr dst_stream(new PuffinExtentStream(
518 std::move(writer),
519 utils::BlocksInExtents(operation.dst_extents()) * block_size_));
520
521 constexpr size_t kMaxCacheSize = 5 * 1024 * 1024; // Total 5MB cache.
522 TEST_AND_RETURN_FALSE(
523 puffin::PuffPatch(std::move(src_stream),
524 std::move(dst_stream),
525 reinterpret_cast<const uint8_t*>(data),
526 count,
527 kMaxCacheSize));
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400528 return Flush();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400529}
530
531FileDescriptorPtr PartitionWriter::ChooseSourceFD(
532 const InstallOperation& operation, ErrorCode* error) {
533 if (source_fd_ == nullptr) {
534 LOG(ERROR) << "ChooseSourceFD fail: source_fd_ == nullptr";
535 return nullptr;
536 }
537
538 if (!operation.has_src_sha256_hash()) {
539 // When the operation doesn't include a source hash, we attempt the error
540 // corrected device first since we can't verify the block in the raw device
541 // at this point, but we first need to make sure all extents are readable
542 // since the error corrected device can be shorter or not available.
543 if (OpenCurrentECCPartition() &&
544 fd_utils::ReadAndHashExtents(
545 source_ecc_fd_, operation.src_extents(), block_size_, nullptr)) {
546 return source_ecc_fd_;
547 }
548 return source_fd_;
549 }
550
551 brillo::Blob source_hash;
552 brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(),
553 operation.src_sha256_hash().end());
554 if (fd_utils::ReadAndHashExtents(
555 source_fd_, operation.src_extents(), block_size_, &source_hash) &&
556 source_hash == expected_source_hash) {
557 return source_fd_;
558 }
559 // We fall back to use the error corrected device if the hash of the raw
560 // device doesn't match or there was an error reading the source partition.
561 if (!OpenCurrentECCPartition()) {
562 // The following function call will return false since the source hash
563 // mismatches, but we still want to call it so it prints the appropriate
564 // log message.
565 ValidateSourceHash(source_hash, operation, source_fd_, error);
566 return nullptr;
567 }
568 LOG(WARNING) << "Source hash from RAW device mismatched: found "
569 << base::HexEncode(source_hash.data(), source_hash.size())
570 << ", expected "
571 << base::HexEncode(expected_source_hash.data(),
572 expected_source_hash.size());
573
574 if (fd_utils::ReadAndHashExtents(
575 source_ecc_fd_, operation.src_extents(), block_size_, &source_hash) &&
576 ValidateSourceHash(source_hash, operation, source_ecc_fd_, error)) {
577 // At this point reading from the error corrected device worked, but
578 // reading from the raw device failed, so this is considered a recovered
579 // failure.
580 source_ecc_recovered_failures_++;
581 return source_ecc_fd_;
582 }
583 return nullptr;
584}
585
586bool PartitionWriter::OpenCurrentECCPartition() {
587 // No support for ECC for full payloads.
588 // Full payload should not have any opeartion that requires ECC partitions.
589 if (source_ecc_fd_)
590 return true;
591
592 if (source_ecc_open_failure_)
593 return false;
594
595#if USE_FEC
596 const PartitionUpdate& partition = partition_update_;
597 const InstallPlan::Partition& install_part = install_part_;
598 std::string path = install_part.source_path;
599 FileDescriptorPtr fd(new FecFileDescriptor());
600 if (!fd->Open(path.c_str(), O_RDONLY, 0)) {
601 PLOG(ERROR) << "Unable to open ECC source partition "
602 << partition.partition_name() << ", file " << path;
603 source_ecc_open_failure_ = true;
604 return false;
605 }
606 source_ecc_fd_ = fd;
607#else
608 // No support for ECC compiled.
609 source_ecc_open_failure_ = true;
610#endif // USE_FEC
611
612 return !source_ecc_open_failure_;
613}
614
615int PartitionWriter::Close() {
616 int err = 0;
617 if (source_fd_ && !source_fd_->Close()) {
618 err = errno;
619 PLOG(ERROR) << "Error closing source partition";
620 if (!err)
621 err = 1;
622 }
623 source_fd_.reset();
624 source_path_.clear();
625
626 if (target_fd_ && !target_fd_->Close()) {
627 err = errno;
628 PLOG(ERROR) << "Error closing target partition";
629 if (!err)
630 err = 1;
631 }
632 target_fd_.reset();
633 target_path_.clear();
634
635 if (source_ecc_fd_ && !source_ecc_fd_->Close()) {
636 err = errno;
637 PLOG(ERROR) << "Error closing ECC source partition";
638 if (!err)
639 err = 1;
640 }
641 source_ecc_fd_.reset();
642 source_ecc_open_failure_ = false;
643 return -err;
644}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400645
646std::unique_ptr<ExtentWriter> PartitionWriter::CreateBaseExtentWriter() {
647 return std::make_unique<DirectExtentWriter>();
648}
649
650bool PartitionWriter::Flush() {
651 return target_fd_->Flush();
652}
653
Kelvin Zhang50bac652020-09-28 15:51:41 -0400654} // namespace chromeos_update_engine