OLD | NEW |
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 <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <fstream> | 10 #include <fstream> |
11 #include <iostream> | 11 #include <iostream> |
| 12 #include <memory> |
12 #include <set> | 13 #include <set> |
13 #include <string> | 14 #include <string> |
14 | 15 |
15 #include "base/files/file_enumerator.h" | 16 #include "base/files/file_enumerator.h" |
16 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
17 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
18 #include "base/logging.h" | 19 #include "base/logging.h" |
19 #include "base/md5.h" | 20 #include "base/md5.h" |
20 #include "base/memory/scoped_ptr.h" | |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // Returns whether |path|'s MD5 was successfully written to |digest_string|. | 24 // Returns whether |path|'s MD5 was successfully written to |digest_string|. |
25 bool MD5Sum(const char* path, std::string* digest_string) { | 25 bool MD5Sum(const char* path, std::string* digest_string) { |
26 base::ScopedFILE file(fopen(path, "rb")); | 26 base::ScopedFILE file(fopen(path, "rb")); |
27 if (!file) { | 27 if (!file) { |
28 LOG(ERROR) << "Could not open file " << path; | 28 LOG(ERROR) << "Could not open file " << path; |
29 return false; | 29 return false; |
30 } | 30 } |
31 base::MD5Context ctx; | 31 base::MD5Context ctx; |
32 base::MD5Init(&ctx); | 32 base::MD5Init(&ctx); |
33 const size_t kBufferSize = 1 << 16; | 33 const size_t kBufferSize = 1 << 16; |
34 scoped_ptr<char[]> buf(new char[kBufferSize]); | 34 std::unique_ptr<char[]> buf(new char[kBufferSize]); |
35 size_t len; | 35 size_t len; |
36 while ((len = fread(buf.get(), 1, kBufferSize, file.get())) > 0) | 36 while ((len = fread(buf.get(), 1, kBufferSize, file.get())) > 0) |
37 base::MD5Update(&ctx, base::StringPiece(buf.get(), len)); | 37 base::MD5Update(&ctx, base::StringPiece(buf.get(), len)); |
38 if (ferror(file.get())) { | 38 if (ferror(file.get())) { |
39 LOG(ERROR) << "Error reading file " << path; | 39 LOG(ERROR) << "Error reading file " << path; |
40 return false; | 40 return false; |
41 } | 41 } |
42 base::MD5Digest digest; | 42 base::MD5Digest digest; |
43 base::MD5Final(&digest, &ctx); | 43 base::MD5Final(&digest, &ctx); |
44 *digest_string = base::MD5DigestToBase16(digest); | 44 *digest_string = base::MD5DigestToBase16(digest); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 for (std::set<std::string>::const_iterator it = files.begin(); | 82 for (std::set<std::string>::const_iterator it = files.begin(); |
83 it != files.end(); ++it) { | 83 it != files.end(); ++it) { |
84 if (!MD5Sum(it->c_str(), &digest)) { | 84 if (!MD5Sum(it->c_str(), &digest)) { |
85 failed = true; | 85 failed = true; |
86 } else { | 86 } else { |
87 std::cout << digest << " " << *it << std::endl; | 87 std::cout << digest << " " << *it << std::endl; |
88 } | 88 } |
89 } | 89 } |
90 return failed; | 90 return failed; |
91 } | 91 } |
OLD | NEW |