| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.util; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.io.InputStream; |
| 10 |
import java.nio.charset.StandardCharsets; |
| 11 |
import java.security.MessageDigest; |
| 12 |
import java.security.NoSuchAlgorithmException; |
| 13 |
import java.util.Collections; |
| 14 |
import java.util.HashMap; |
| 15 |
import java.util.List; |
| 16 |
|
| 17 |
import org.h2.store.fs.FileUtils; |
| 18 |
import org.h2.util.SortedProperties; |
| 19 |
import org.h2.util.StringUtils; |
| 20 |
|
| 21 |
/** |
| 22 |
* A utility to calculate the content hash of files. It should help detect |
| 23 |
* duplicate files and differences between directories. |
| 24 |
*/ |
| 25 |
public class FileContentHash { |
| 26 |
|
| 27 |
// find empty directories: |
| 28 |
// find . -type d -empty |
| 29 |
// find . -name .hash.prop -delete |
| 30 |
|
| 31 |
private static final boolean WRITE_HASH_INDEX = true; |
| 32 |
private static final String HASH_INDEX = ".hash.prop"; |
| 33 |
private static final int MIN_SIZE = 0; |
| 34 |
private final HashMap<String, String> hashes = new HashMap<>(); |
| 35 |
private long nextLog; |
| 36 |
|
| 37 |
/** |
| 38 |
* Run the viewer. |
| 39 |
* |
| 40 |
* @param args the command line arguments |
| 41 |
*/ |
| 42 |
public static void main(String... args) throws IOException { |
| 43 |
new FileContentHash().runTool(args); |
| 44 |
} |
| 45 |
|
| 46 |
private void runTool(String... args) throws IOException { |
| 47 |
if (args.length == 0) { |
| 48 |
System.out.println("Usage: java " + getClass().getName() + " <dir>"); |
| 49 |
return; |
| 50 |
} |
| 51 |
for (int i = 0; i < args.length; i++) { |
| 52 |
Info info = hash(args[i]); |
| 53 |
System.out.println("size: " + info.size); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
private static MessageDigest createMessageDigest() { |
| 58 |
try { |
| 59 |
return MessageDigest.getInstance("SHA-256"); |
| 60 |
} catch (NoSuchAlgorithmException e) { |
| 61 |
throw new RuntimeException(e); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
private Info hash(String path) throws IOException { |
| 66 |
if (FileUtils.isDirectory(path)) { |
| 67 |
long totalSize = 0; |
| 68 |
SortedProperties propOld; |
| 69 |
SortedProperties propNew = new SortedProperties(); |
| 70 |
String hashFileName = path + "/" + HASH_INDEX; |
| 71 |
if (FileUtils.exists(hashFileName)) { |
| 72 |
propOld = SortedProperties.loadProperties(hashFileName); |
| 73 |
} else { |
| 74 |
propOld = new SortedProperties(); |
| 75 |
} |
| 76 |
List<String> list = FileUtils.newDirectoryStream(path); |
| 77 |
Collections.sort(list); |
| 78 |
MessageDigest mdDir = createMessageDigest(); |
| 79 |
for (String f : list) { |
| 80 |
String name = FileUtils.getName(f); |
| 81 |
if (name.equals(HASH_INDEX)) { |
| 82 |
continue; |
| 83 |
} |
| 84 |
long length = FileUtils.size(f); |
| 85 |
String entry = "name_" + name + |
| 86 |
"-mod_" + FileUtils.lastModified(f) + |
| 87 |
"-size_" + length; |
| 88 |
String hash = propOld.getProperty(entry); |
| 89 |
if (hash == null || FileUtils.isDirectory(f)) { |
| 90 |
Info info = hash(f); |
| 91 |
byte[] b = info.hash; |
| 92 |
hash = StringUtils.convertBytesToHex(b); |
| 93 |
totalSize += info.size; |
| 94 |
entry = "name_" + name + |
| 95 |
"-mod_" + FileUtils.lastModified(f) + |
| 96 |
"-size_" + info.size; |
| 97 |
} else { |
| 98 |
totalSize += length; |
| 99 |
checkCollision(f, length, StringUtils.convertHexToBytes(hash)); |
| 100 |
} |
| 101 |
propNew.put(entry, hash); |
| 102 |
mdDir.update(entry.getBytes(StandardCharsets.UTF_8)); |
| 103 |
mdDir.update(hash.getBytes(StandardCharsets.UTF_8)); |
| 104 |
} |
| 105 |
String oldFile = propOld.toString(); |
| 106 |
String newFile = propNew.toString(); |
| 107 |
if (!oldFile.equals(newFile)) { |
| 108 |
if (WRITE_HASH_INDEX) { |
| 109 |
propNew.store(path + "/" + HASH_INDEX); |
| 110 |
} |
| 111 |
} |
| 112 |
Info info = new Info(); |
| 113 |
info.hash = mdDir.digest(); |
| 114 |
info.size = totalSize; |
| 115 |
return info; |
| 116 |
} |
| 117 |
MessageDigest md = createMessageDigest(); |
| 118 |
InputStream in = FileUtils.newInputStream(path); |
| 119 |
long length = FileUtils.size(path); |
| 120 |
byte[] buff = new byte[1024 * 1024]; |
| 121 |
while (true) { |
| 122 |
int len = in.read(buff); |
| 123 |
if (len < 0) { |
| 124 |
break; |
| 125 |
} |
| 126 |
md.update(buff, 0, len); |
| 127 |
long t = System.nanoTime(); |
| 128 |
if (nextLog == 0 || t > nextLog) { |
| 129 |
System.out.println("Checking " + path); |
| 130 |
nextLog = t + 5000 * 1000000L; |
| 131 |
} |
| 132 |
} |
| 133 |
in.close(); |
| 134 |
byte[] b = md.digest(); |
| 135 |
checkCollision(path, length, b); |
| 136 |
Info info = new Info(); |
| 137 |
info.hash = b; |
| 138 |
info.size = length; |
| 139 |
return info; |
| 140 |
} |
| 141 |
|
| 142 |
private void checkCollision(String path, long length, byte[] hash) { |
| 143 |
if (length < MIN_SIZE) { |
| 144 |
return; |
| 145 |
} |
| 146 |
String s = StringUtils.convertBytesToHex(hash); |
| 147 |
String old = hashes.get(s); |
| 148 |
if (old != null) { |
| 149 |
System.out.println("Collision: " + old + "\n" + path + "\n"); |
| 150 |
} else { |
| 151 |
hashes.put(s, path); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* The info for a file. |
| 157 |
*/ |
| 158 |
static class Info { |
| 159 |
|
| 160 |
/** |
| 161 |
* The content hash. |
| 162 |
*/ |
| 163 |
byte[] hash; |
| 164 |
|
| 165 |
/** |
| 166 |
* The size in bytes. |
| 167 |
*/ |
| 168 |
long size; |
| 169 |
} |
| 170 |
|
| 171 |
} |