Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: src/platform/update_engine/split_file_writer.cc

Issue 1800009: AU: SplitWriter class for parsing our full update files. (Closed) Base URL: ssh://git@chromiumos-git/chromeos
Patch Set: fixes for review Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "update_engine/split_file_writer.h"
6 #include <algorithm>
7
8 using std::min;
9
10 namespace chromeos_update_engine {
11
12 int SplitFileWriter::Open(const char* path, int flags, mode_t mode) {
13 int first_result = first_file_writer_->Open(first_path_,
14 first_flags_,
15 first_mode_);
16 if (first_result < 0) {
17 LOG(ERROR) << "Error opening first file " << first_path_;
18 return first_result;
19 }
20 int second_result = second_file_writer_->Open(path, flags, mode);
21 if (second_result < 0) {
22 LOG(ERROR) << "Error opening second file " << path;
23 first_file_writer_->Close();
24 return second_result;
25 }
26 return second_result;
27 }
28
29 namespace {
30 ssize_t PerformWrite(FileWriter* writer, const void* bytes, size_t count) {
31 int rc = writer->Write(bytes, count);
32 if (rc < 0) {
33 LOG(ERROR) << "Write failed to file.";
34 return rc;
35 }
36 if (rc != static_cast<int>(count)) {
37 LOG(ERROR) << "Not all bytes successfully written to file.";
38 return -EIO;
39 }
40 return rc;
41 }
42 }
43
44 ssize_t SplitFileWriter::Write(const void* bytes, size_t count) {
45 const size_t original_count = count;
46
47 // This first block is trying to read the first sizeof(uint64_t)
48 // bytes, which are the number of bytes that should be written
49 // to the first FileWriter.
50 if (bytes_received_ < static_cast<off_t>(sizeof(uint64_t))) {
51 // Write more to the initial buffer
52 size_t bytes_to_copy = min(count,
53 sizeof(first_length_buf_) - bytes_received_);
54 memcpy(&first_length_buf_[bytes_received_], bytes, bytes_to_copy);
55 bytes_received_ += bytes_to_copy;
56 count -= bytes_to_copy;
57 bytes = static_cast<const void*>(
58 static_cast<const char*>(bytes) + bytes_to_copy);
59
60 // See if we have all we need
61 if (bytes_received_ == sizeof(first_length_buf_)) {
62 // Parse first number
63 uint64_t big_endian_first_length;
64 memcpy(&big_endian_first_length, first_length_buf_,
65 sizeof(big_endian_first_length));
66 first_length_ = be64toh(big_endian_first_length);
67 }
68 if (count == 0)
69 return original_count;
70 }
71 CHECK_GE(bytes_received_, static_cast<off_t>(sizeof(uint64_t)));
72
73 // This block of code is writing to the first FileWriter.
74 if (bytes_received_ - static_cast<off_t>(sizeof(uint64_t)) < first_length_) {
75 // Write to first FileWriter
76 size_t bytes_to_write = min(
77 first_length_ -
78 (bytes_received_ - static_cast<off_t>(sizeof(uint64_t))),
79 static_cast<off_t>(count));
80
81 int rc = PerformWrite(first_file_writer_, bytes, bytes_to_write);
82 if (rc != static_cast<int>(bytes_to_write))
83 return rc;
84
85 bytes_received_ += bytes_to_write;
86 count -= bytes_to_write;
87 bytes = static_cast<const void*>(
88 static_cast<const char*>(bytes) + bytes_to_write);
89 if (count == 0)
90 return original_count;
91 }
92
93 CHECK_GE(static_cast<off_t>(bytes_received_),
94 first_length_ + static_cast<off_t>(sizeof(uint64_t)));
95 // Write to second FileWriter
96 int rc = PerformWrite(second_file_writer_, bytes, count);
97 if (rc != static_cast<int>(count))
98 return rc;
99 return original_count;
100 }
101
102 int SplitFileWriter::Close() {
103 int first_result = first_file_writer_->Close();
104 if (first_result < 0)
105 LOG(ERROR) << "Error Close()ing first file.";
106 int second_result = second_file_writer_->Close();
107 if (second_result < 0)
108 LOG(ERROR) << "Error Close()ing second file.";
109 // Return error if either had returned error.
110 return second_result < 0 ? second_result : first_result;
111 }
112
113 } // namespace chromeos_update_engine
OLDNEW
« no previous file with comments | « src/platform/update_engine/split_file_writer.h ('k') | src/platform/update_engine/split_file_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698