+ move hash to util

pull/1/merge
io42630 3 years ago
parent b28155a752
commit ad31b66671

@ -108,7 +108,6 @@ public class SyncDirectory {
* List is cleared and created each time.
*/
public Map<String, SyncFile> makeListOfLocallyModifiedFiles(Map<String, SyncFile> listFileSystem, Record record) {
return listFileSystem.entrySet().stream().filter(
fileEntry -> {
String fileKey = fileEntry.getKey();
@ -121,23 +120,6 @@ public class SyncDirectory {
).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}
private String getHash(Path path) {
var thisFc = LockKeeper.getFc(path);
try (var is = Channels.newInputStream(thisFc)) {
var m = MessageDigest.getInstance("SHA256");
byte[] buffer = new byte[262144];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
m.update(buffer, 0, bytesRead);
}
var i = new BigInteger(1, m.digest());
return String.format("%1$032X", i);
} catch (Exception e) {
LogU.warnPlain("Failed to create Hash.\n%s", e.getMessage());
return null;
}
}
/**
* QUERY state of the filesystem at realPath.
* WRITE the state of the filesystem to file.
@ -241,8 +223,8 @@ public class SyncDirectory {
LogU.infoPlain(" to: " + otherFile.toPath());
if (!thisFile.isFile()) { return; }
if (otherFile.exists()) {
var thisHash = getHash(thisFile.toPath());
var otherHash = getHash(otherFile.toPath());
var thisHash = HashUtil.getHash(thisFile.toPath());
var otherHash = HashUtil.getHash(otherFile.toPath());
if (thisHash == null || otherHash == null) { return; }
if (thisHash.equals(otherHash)) {
dropAge(thisFile, otherFile);

@ -0,0 +1,30 @@
package com.olexyn.ensync.util;
import com.olexyn.ensync.lock.LockKeeper;
import com.olexyn.min.log.LogU;
import java.math.BigInteger;
import java.nio.channels.Channels;
import java.nio.file.Path;
import java.security.MessageDigest;
public class HashUtil {
public static String getHash(Path path) {
var thisFc = LockKeeper.getFc(path);
try (var is = Channels.newInputStream(thisFc)) {
var m = MessageDigest.getInstance("SHA256");
byte[] buffer = new byte[262144];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
m.update(buffer, 0, bytesRead);
}
var i = new BigInteger(1, m.digest());
return String.format("%1$032X", i);
} catch (Exception e) {
LogU.warnPlain("Failed to create Hash.\n%s", e.getMessage());
return null;
}
}
}
Loading…
Cancel
Save