extent_writer.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  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. //
  16. #include "update_engine/payload_consumer/extent_writer.h"
  17. #include <errno.h>
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. #include <algorithm>
  21. #include "update_engine/common/utils.h"
  22. #include "update_engine/payload_consumer/payload_constants.h"
  23. using std::min;
  24. namespace chromeos_update_engine {
  25. bool DirectExtentWriter::Write(const void* bytes, size_t count) {
  26. if (count == 0)
  27. return true;
  28. const char* c_bytes = reinterpret_cast<const char*>(bytes);
  29. size_t bytes_written = 0;
  30. while (bytes_written < count) {
  31. TEST_AND_RETURN_FALSE(cur_extent_ != extents_.end());
  32. uint64_t bytes_remaining_cur_extent =
  33. cur_extent_->num_blocks() * block_size_ - extent_bytes_written_;
  34. CHECK_NE(bytes_remaining_cur_extent, static_cast<uint64_t>(0));
  35. size_t bytes_to_write =
  36. static_cast<size_t>(min(static_cast<uint64_t>(count - bytes_written),
  37. bytes_remaining_cur_extent));
  38. TEST_AND_RETURN_FALSE(bytes_to_write > 0);
  39. if (cur_extent_->start_block() != kSparseHole) {
  40. const off64_t offset =
  41. cur_extent_->start_block() * block_size_ + extent_bytes_written_;
  42. TEST_AND_RETURN_FALSE_ERRNO(fd_->Seek(offset, SEEK_SET) !=
  43. static_cast<off64_t>(-1));
  44. TEST_AND_RETURN_FALSE(
  45. utils::WriteAll(fd_, c_bytes + bytes_written, bytes_to_write));
  46. }
  47. bytes_written += bytes_to_write;
  48. extent_bytes_written_ += bytes_to_write;
  49. if (bytes_remaining_cur_extent == bytes_to_write) {
  50. // We filled this extent
  51. CHECK_EQ(extent_bytes_written_, cur_extent_->num_blocks() * block_size_);
  52. // move to next extent
  53. extent_bytes_written_ = 0;
  54. cur_extent_++;
  55. }
  56. }
  57. return true;
  58. }
  59. } // namespace chromeos_update_engine