Re-hash files if contents change. GitHub issue #148

dbus-notify
Zlatin Balevsky 2022-06-15 06:18:03 +01:00
parent d1e6ebfc9b
commit 6bb554e8ce
No known key found for this signature in database
GPG Key ID: A72832072D525E41
4 changed files with 20 additions and 9 deletions

View File

@ -9,6 +9,12 @@ class FileHashedEvent extends Event {
SharedFile sharedFile
String error
/**
* This will be non-null in case of a re-hash.
* Enriched by FileManager
*/
SharedFile duplicate
@Override
public String toString() {
super.toString() + " sharedFile " + sharedFile?.file?.getAbsolutePath() + " error: $error"

View File

@ -66,6 +66,9 @@ class FileManager {
if (sideCar.exists())
e.sharedFile.setComment(Base64.encode(DataUtil.encodei18nString(sideCar.text)))
}
e.duplicate = fileToSharedFile[f]
addToIndex(e.sharedFile)
}
@ -109,7 +112,7 @@ class FileManager {
existing = unique.toArray(existing)
}
rootToFiles.put(infoHash, existing);
fileToSharedFile.put(sf.file, sf)
positiveTree.add(sf.file, sf);

View File

@ -65,7 +65,7 @@ class HasherService {
void onFileSharedEvent(FileSharedEvent evt) {
if (!settings.shareHiddenFiles && evt.file.isHidden())
return
if (fileManager.fileToSharedFile.containsKey(evt.file))
if (fileManager.fileToSharedFile.containsKey(evt.file) && !evt.fromWatch)
return
if (negativeFiles.negativeTree.get(evt.file))
return
@ -77,12 +77,12 @@ class HasherService {
if (evt.file.getName().endsWith(".mwcomment")) {
if (evt.file.length() <= Constants.MAX_COMMENT_LENGTH)
eventBus.publish(new SideCarFileEvent(file : evt.file))
} else if (hashed.add(evt.file)) {
} else if (hashed.add(evt.file) || evt.fromWatch) {
File canonical = evt.file.getCanonicalFile()
if (canonical.isDirectory())
executor.execute({processDirectory(evt.file)} as Runnable)
else
throttlerExecutor.execute({ throttle(evt.file, canonical) } as Runnable)
throttlerExecutor.execute({ throttle(evt.file, canonical, evt.fromWatch) } as Runnable)
}
}
@ -96,13 +96,13 @@ class HasherService {
hashed.remove(dir)
}
private synchronized void throttle(File f, File canonical) {
private synchronized void throttle(File f, File canonical, boolean forceHash) {
while(currentHashes >= settings.hashingCores)
wait(10)
currentHashes++
if (++totalHashes % TARGET_Q_SIZE == 0)
System.gc()
executor.execute({processFile(f, canonical)} as Runnable)
executor.execute({processFile(f, canonical, forceHash)} as Runnable)
}
private void processDirectory(File f) {
@ -117,7 +117,7 @@ class HasherService {
}
}
private void processFile(File f, File canonical) {
private void processFile(File f, File canonical, boolean forceHash) {
try {
if (f.length() == 0) {
eventBus.publish new FileHashedEvent(error: "Not sharing empty file $f")
@ -128,8 +128,8 @@ class HasherService {
} else {
eventBus.publish new FileHashingEvent(hashingFile: f)
def hash = hashListFunction.apply(canonical)
if (hash == null) {
log.fine("did not find a hash list for $f => $canonical")
if (hash == null || forceHash) {
log.fine("will hash $f => $canonical force $forceHash")
hash = HASHER_TL.get().hashFile canonical
eventBus.publish new InfoHashEvent(file: canonical, infoHash: hash)
} else

View File

@ -494,6 +494,8 @@ class MainFrameModel {
hashingFiles--
if (e.sharedFile.file == hashingFile)
hashingFile = null
if (e.duplicate != null)
allSharedFiles.remove(e.duplicate)
allSharedFiles << e.sharedFile
insertIntoTree(e.sharedFile, allFilesTreeRoot, fileToNode)
if (filter(e.sharedFile)) {