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
3 // found in the LICENSE file.
5 // Md5sum implementation for Android. This version handles files as well as
6 // directories. Its output is sorted by file path.
13 #include "base/files/file_enumerator.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
21 const int kBufferSize
= 1024;
23 // Returns whether |path|'s MD5 was successfully written to |digest_string|.
24 bool MD5Sum(const char* path
, std::string
* digest_string
) {
25 std::ifstream
stream(path
);
27 LOG(ERROR
) << "Could not open file " << path
;
32 char buf
[kBufferSize
];
33 while (stream
.good()) {
34 std::streamsize bytes_read
= stream
.readsome(buf
, sizeof(buf
));
37 base::MD5Update(&ctx
, base::StringPiece(buf
, bytes_read
));
40 LOG(ERROR
) << "Error reading file " << path
;
43 base::MD5Digest digest
;
44 base::MD5Final(&digest
, &ctx
);
45 *digest_string
= base::MD5DigestToBase16(digest
);
49 // Returns the set of all files contained in |files|. This handles directories
50 // by walking them recursively. Excludes, .svn directories and file under them.
51 std::set
<std::string
> MakeFileSet(const char** files
) {
52 const std::string svn_dir_component
= FILE_PATH_LITERAL("/.svn/");
53 std::set
<std::string
> file_set
;
54 for (const char** file
= files
; *file
; ++file
) {
55 base::FilePath
file_path(*file
);
56 if (base::DirectoryExists(file_path
)) {
57 base::FileEnumerator
file_enumerator(
58 file_path
, true /* recurse */, base::FileEnumerator::FILES
);
59 for (base::FilePath child
, empty
;
60 (child
= file_enumerator
.Next()) != empty
; ) {
61 // If the path contains /.svn/, ignore it.
62 if (child
.value().find(svn_dir_component
) == std::string::npos
) {
63 child
= base::MakeAbsoluteFilePath(child
);
64 file_set
.insert(child
.value());
68 file_set
.insert(*file
);
76 int main(int argc
, const char* argv
[]) {
78 LOG(ERROR
) << "Usage: md5sum <path/to/file_or_dir>...";
81 const std::set
<std::string
> files
= MakeFileSet(argv
+ 1);
84 for (std::set
<std::string
>::const_iterator it
= files
.begin();
85 it
!= files
.end(); ++it
) {
86 if (!MD5Sum(it
->c_str(), &digest
))
88 base::FilePath
file_path(*it
);
89 std::cout
<< digest
<< " "
90 << base::MakeAbsoluteFilePath(file_path
).value() << std::endl
;