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

Side by Side Diff: omaha_hash_calculator.cc

Issue 3588015: AU: Include the old/new kernel/rootfs size/hash in the update metadata. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/update_engine.git
Patch Set: no need to close negative handles Created 10 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « omaha_hash_calculator.h ('k') | omaha_hash_calculator_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "update_engine/omaha_hash_calculator.h" 5 #include "update_engine/omaha_hash_calculator.h"
6 6
7 #include <fcntl.h>
8
9 #include <base/eintr_wrapper.h>
10 #include <base/logging.h>
7 #include <openssl/bio.h> 11 #include <openssl/bio.h>
8 #include <openssl/buffer.h> 12 #include <openssl/buffer.h>
9 #include <openssl/evp.h> 13 #include <openssl/evp.h>
10 #include "base/logging.h" 14
11 #include "update_engine/utils.h" 15 #include "update_engine/utils.h"
12 16
13 using std::string; 17 using std::string;
14 using std::vector; 18 using std::vector;
15 19
16 namespace chromeos_update_engine { 20 namespace chromeos_update_engine {
17 21
18 OmahaHashCalculator::OmahaHashCalculator() : valid_(false) { 22 OmahaHashCalculator::OmahaHashCalculator() : valid_(false) {
19 valid_ = (SHA256_Init(&ctx_) == 1); 23 valid_ = (SHA256_Init(&ctx_) == 1);
20 LOG_IF(ERROR, !valid_) << "SHA256_Init failed"; 24 LOG_IF(ERROR, !valid_) << "SHA256_Init failed";
21 } 25 }
22 26
23 // Update is called with all of the data that should be hashed in order. 27 // Update is called with all of the data that should be hashed in order.
24 // Mostly just passes the data through to OpenSSL's SHA256_Update() 28 // Mostly just passes the data through to OpenSSL's SHA256_Update()
25 bool OmahaHashCalculator::Update(const char* data, size_t length) { 29 bool OmahaHashCalculator::Update(const char* data, size_t length) {
26 TEST_AND_RETURN_FALSE(valid_); 30 TEST_AND_RETURN_FALSE(valid_);
27 TEST_AND_RETURN_FALSE(hash_.empty()); 31 TEST_AND_RETURN_FALSE(hash_.empty());
28 COMPILE_ASSERT(sizeof(size_t) <= sizeof(unsigned long), 32 COMPILE_ASSERT(sizeof(size_t) <= sizeof(unsigned long),
29 length_param_may_be_truncated_in_SHA256_Update); 33 length_param_may_be_truncated_in_SHA256_Update);
30 TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1); 34 TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1);
31 return true; 35 return true;
32 } 36 }
33 37
38 off_t OmahaHashCalculator::UpdateFile(const string& name, off_t length) {
39 int fd = HANDLE_EINTR(open(name.c_str(), O_RDONLY));
40 if (fd < 0) {
41 return -1;
42 }
43
44 const int kBufferSize = 128 * 1024; // 128 KiB
45 vector<char> buffer(kBufferSize);
46 off_t bytes_processed = 0;
47 while (length < 0 || bytes_processed < length) {
48 off_t bytes_to_read = buffer.size();
49 if (length >= 0 && bytes_to_read > length - bytes_processed) {
50 bytes_to_read = length - bytes_processed;
51 }
52 ssize_t rc = HANDLE_EINTR(read(fd, &buffer[0], bytes_to_read));
adlr 2010/10/08 01:21:58 I never heard of HANDLE_EINTR and just looked it u
petkov 2010/10/08 04:14:29 open/read/close can theoretically (but somewhat un
53 if (rc == 0) { // EOF
54 break;
55 }
56 if (rc < 0 || !Update(&buffer[0], rc)) {
57 bytes_processed = -1;
58 break;
59 }
60 bytes_processed += rc;
61 }
62 HANDLE_EINTR(close(fd));
63 return bytes_processed;
64 }
65
34 // Call Finalize() when all data has been passed in. This mostly just 66 // Call Finalize() when all data has been passed in. This mostly just
35 // calls OpenSSL's SHA256_Final() and then base64 encodes the hash. 67 // calls OpenSSL's SHA256_Final() and then base64 encodes the hash.
36 bool OmahaHashCalculator::Finalize() { 68 bool OmahaHashCalculator::Finalize() {
37 bool success = true; 69 bool success = true;
38 TEST_AND_RETURN_FALSE(hash_.empty()); 70 TEST_AND_RETURN_FALSE(hash_.empty());
39 TEST_AND_RETURN_FALSE(raw_hash_.empty()); 71 TEST_AND_RETURN_FALSE(raw_hash_.empty());
40 raw_hash_.resize(SHA256_DIGEST_LENGTH); 72 raw_hash_.resize(SHA256_DIGEST_LENGTH);
41 TEST_AND_RETURN_FALSE( 73 TEST_AND_RETURN_FALSE(
42 SHA256_Final(reinterpret_cast<unsigned char*>(&raw_hash_[0]), 74 SHA256_Final(reinterpret_cast<unsigned char*>(&raw_hash_[0]),
43 &ctx_) == 1); 75 &ctx_) == 1);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_)); 133 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_));
102 } 134 }
103 135
104 bool OmahaHashCalculator::SetContext(const std::string& context) { 136 bool OmahaHashCalculator::SetContext(const std::string& context) {
105 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_)); 137 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_));
106 memcpy(&ctx_, context.data(), sizeof(ctx_)); 138 memcpy(&ctx_, context.data(), sizeof(ctx_));
107 return true; 139 return true;
108 } 140 }
109 141
110 } // namespace chromeos_update_engine 142 } // namespace chromeos_update_engine
OLDNEW
« no previous file with comments | « omaha_hash_calculator.h ('k') | omaha_hash_calculator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698