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

Side by Side Diff: tools/android/md5sum/md5sum.cc

Issue 1128733002: Update from https://crrev.com/328418 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 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
« no previous file with comments | « third_party/re2/util/util.h ('k') | tools/android/run_pie/run_pie.gyp » ('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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium 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 // Md5sum implementation for Android. This version handles files as well as 5 // Md5sum implementation for Android. This version handles files as well as
6 // directories. Its output is sorted by file path. 6 // directories. Its output is sorted by file path.
7 7
8 #include <fstream> 8 #include <fstream>
9 #include <iostream> 9 #include <iostream>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 12
13 #include "base/files/file_enumerator.h" 13 #include "base/files/file_enumerator.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/md5.h" 17 #include "base/md5.h"
18 18
19 namespace { 19 namespace {
20 20
21 const int kBufferSize = 1024;
22
23 // Returns whether |path|'s MD5 was successfully written to |digest_string|. 21 // Returns whether |path|'s MD5 was successfully written to |digest_string|.
24 bool MD5Sum(const char* path, std::string* digest_string) { 22 bool MD5Sum(const char* path, std::string* digest_string) {
25 std::ifstream stream(path); 23 base::ScopedFILE file(fopen(path, "rb"));
26 if (!stream.good()) { 24 if (!file) {
27 LOG(ERROR) << "Could not open file " << path; 25 LOG(ERROR) << "Could not open file " << path;
28 return false; 26 return false;
29 } 27 }
30 base::MD5Context ctx; 28 base::MD5Context ctx;
31 base::MD5Init(&ctx); 29 base::MD5Init(&ctx);
32 char buf[kBufferSize]; 30 const size_t kBufferSize = 1 << 16;
33 while (stream.good()) { 31 scoped_ptr<char[]> buf(new char[kBufferSize]);
34 std::streamsize bytes_read = stream.readsome(buf, sizeof(buf)); 32 size_t len;
35 if (bytes_read == 0) 33 while ((len = fread(buf.get(), 1, kBufferSize, file.get())) > 0)
36 break; 34 base::MD5Update(&ctx, base::StringPiece(buf.get(), len));
37 base::MD5Update(&ctx, base::StringPiece(buf, bytes_read)); 35 if (ferror(file.get())) {
38 }
39 if (stream.fail()) {
40 LOG(ERROR) << "Error reading file " << path; 36 LOG(ERROR) << "Error reading file " << path;
41 return false; 37 return false;
42 } 38 }
43 base::MD5Digest digest; 39 base::MD5Digest digest;
44 base::MD5Final(&digest, &ctx); 40 base::MD5Final(&digest, &ctx);
45 *digest_string = base::MD5DigestToBase16(digest); 41 *digest_string = base::MD5DigestToBase16(digest);
46 return true; 42 return true;
47 } 43 }
48 44
49 // Returns the set of all files contained in |files|. This handles directories 45 // Returns the set of all files contained in |files|. This handles directories
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 for (std::set<std::string>::const_iterator it = files.begin(); 80 for (std::set<std::string>::const_iterator it = files.begin();
85 it != files.end(); ++it) { 81 it != files.end(); ++it) {
86 if (!MD5Sum(it->c_str(), &digest)) 82 if (!MD5Sum(it->c_str(), &digest))
87 failed = true; 83 failed = true;
88 base::FilePath file_path(*it); 84 base::FilePath file_path(*it);
89 std::cout << digest << " " 85 std::cout << digest << " "
90 << base::MakeAbsoluteFilePath(file_path).value() << std::endl; 86 << base::MakeAbsoluteFilePath(file_path).value() << std::endl;
91 } 87 }
92 return failed; 88 return failed;
93 } 89 }
OLDNEW
« no previous file with comments | « third_party/re2/util/util.h ('k') | tools/android/run_pie/run_pie.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698