blob: 5501e2204bfb4bfb7ac8a4cd2d67c784a365d4ac [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2009 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//
Andrew de los Reyes80061062010-02-04 14:25:00 -080016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/extent_writer.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070018
Andrew de los Reyes80061062010-02-04 14:25:00 -080019#include <errno.h>
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080020#include <sys/types.h>
Andrew de los Reyes80061062010-02-04 14:25:00 -080021#include <unistd.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070022
Andrew de los Reyes80061062010-02-04 14:25:00 -080023#include <algorithm>
Alex Deymo161c4a12014-05-16 15:56:21 -070024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/utils.h"
26#include "update_engine/payload_consumer/payload_constants.h"
Andrew de los Reyes80061062010-02-04 14:25:00 -080027
28using std::min;
29
30namespace chromeos_update_engine {
31
Andrew de los Reyes80061062010-02-04 14:25:00 -080032bool DirectExtentWriter::Write(const void* bytes, size_t count) {
33 if (count == 0)
34 return true;
35 const char* c_bytes = reinterpret_cast<const char*>(bytes);
36 size_t bytes_written = 0;
37 while (count - bytes_written > 0) {
38 TEST_AND_RETURN_FALSE(next_extent_index_ < extents_.size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070039 uint64_t bytes_remaining_next_extent =
Andrew de los Reyes80061062010-02-04 14:25:00 -080040 extents_[next_extent_index_].num_blocks() * block_size_ -
41 extent_bytes_written_;
Mike Frysinger0f9547d2012-02-16 12:11:37 -050042 CHECK_NE(bytes_remaining_next_extent, static_cast<uint64_t>(0));
Andrew de los Reyes80061062010-02-04 14:25:00 -080043 size_t bytes_to_write =
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070044 static_cast<size_t>(min(static_cast<uint64_t>(count - bytes_written),
Andrew de los Reyes80061062010-02-04 14:25:00 -080045 bytes_remaining_next_extent));
46 TEST_AND_RETURN_FALSE(bytes_to_write > 0);
47
48 if (extents_[next_extent_index_].start_block() != kSparseHole) {
49 const off64_t offset =
50 extents_[next_extent_index_].start_block() * block_size_ +
51 extent_bytes_written_;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080052 TEST_AND_RETURN_FALSE_ERRNO(fd_->Seek(offset, SEEK_SET) !=
Andrew de los Reyes80061062010-02-04 14:25:00 -080053 static_cast<off64_t>(-1));
54 TEST_AND_RETURN_FALSE(
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070055 utils::WriteAll(fd_, c_bytes + bytes_written, bytes_to_write));
Andrew de los Reyes80061062010-02-04 14:25:00 -080056 }
57 bytes_written += bytes_to_write;
58 extent_bytes_written_ += bytes_to_write;
59 if (bytes_remaining_next_extent == bytes_to_write) {
60 // We filled this extent
61 CHECK_EQ(extent_bytes_written_,
62 extents_[next_extent_index_].num_blocks() * block_size_);
63 // move to next extent
64 extent_bytes_written_ = 0;
65 next_extent_index_++;
66 }
67 }
68 return true;
69}
70
71} // namespace chromeos_update_engine