FileWatcher debugging.

This commit is contained in:
didfet
2015-03-12 23:46:51 +01:00
parent 708c05d931
commit 797f6f41bb
3 changed files with 59 additions and 42 deletions

View File

@@ -23,7 +23,7 @@ import java.io.RandomAccessFile;
public class FileState { public class FileState {
private File file; private File file;
private String filePath; private String directory;
private long lastModified; private long lastModified;
private long size; private long size;
private boolean deleted = false; private boolean deleted = false;
@@ -37,7 +37,7 @@ public class FileState {
public FileState(File file) throws IOException { public FileState(File file) throws IOException {
this.file = file; this.file = file;
filePath = file.getCanonicalPath(); directory = file.getCanonicalFile().getParent();
fileName = file.getName(); fileName = file.getName();
randomAccessFile = new RandomAccessFile(file, "r"); randomAccessFile = new RandomAccessFile(file, "r");
lastModified = file.lastModified(); lastModified = file.lastModified();
@@ -56,8 +56,8 @@ public class FileState {
return size; return size;
} }
public String getFilePath() { public String getDirectory() {
return filePath; return directory;
} }
public boolean isDeleted() { public boolean isDeleted() {

View File

@@ -68,7 +68,7 @@ public class FileWatcher implements FileAlterationListener {
public void checkFiles() throws IOException { public void checkFiles() throws IOException {
logger.debug("Checking files"); logger.debug("Checking files");
printWatchMap(); //printWatchMap();
for(FileAlterationObserver observer : observerList) { for(FileAlterationObserver observer : observerList) {
observer.checkAndNotify(); observer.checkAndNotify();
} }
@@ -78,14 +78,17 @@ public class FileWatcher implements FileAlterationListener {
} }
private void processModifications() throws IOException { private void processModifications() throws IOException {
removeMarkedFilesFromWatchMap();
for(File file : changedWatchMap.keySet()) { for(File file : changedWatchMap.keySet()) {
FileState state = changedWatchMap.get(file); FileState state = changedWatchMap.get(file);
logger.trace("Checking file : " + file.getCanonicalPath()); logger.trace("Checking file : " + file.getCanonicalPath());
logger.trace("-- Last modified : " + state.getLastModified());
logger.trace("-- Size : " + state.getSize());
logger.trace("-- Directory : " + state.getDirectory());
logger.trace("-- Filename : " + state.getFileName());
// Determine if file is still the same // Determine if file is still the same
logger.trace("Determine if file name has not changed"); logger.trace("Determine if file has just been written to");
FileState oldState = watchMap.get(file); FileState oldState = watchMap.get(file);
if(oldState != null) { if(oldState != null) {
if(oldState.getSize() > state.getSize()) { if(oldState.getSize() > state.getSize()) {
@@ -96,6 +99,7 @@ public class FileWatcher implements FileAlterationListener {
// File is the same // File is the same
state.setOldFileState(oldState); state.setOldFileState(oldState);
logger.trace("Same signature size and value : file is the same"); logger.trace("Same signature size and value : file is the same");
continue;
} else if(oldState.getSignatureLength() < state.getSignatureLength()){ } else if(oldState.getSignatureLength() < state.getSignatureLength()){
// Compute the signature on the new file // Compute the signature on the new file
long signature = FileSigner.computeSignature(state.getRandomAccessFile(), oldState.getSignatureLength()); long signature = FileSigner.computeSignature(state.getRandomAccessFile(), oldState.getSignatureLength());
@@ -103,6 +107,7 @@ public class FileWatcher implements FileAlterationListener {
// File is the same // File is the same
state.setOldFileState(oldState); state.setOldFileState(oldState);
logger.trace("Same signature : file is the same"); logger.trace("Same signature : file is the same");
continue;
} else { } else {
// File can't be the same // File can't be the same
logger.trace("Signature different : file can't be the same"); logger.trace("Signature different : file can't be the same");
@@ -114,36 +119,19 @@ public class FileWatcher implements FileAlterationListener {
} }
} }
// Determine if there was a file with the same size and last modification date in the same directory and with a different name // Determine if file has been renamed and/or written to
logger.trace("Determine if file has just been renamed");
if(state.getOldFileState() == null) { if(state.getOldFileState() == null) {
for(File otherFile : changedWatchMap.keySet()) { logger.trace("Determine if file has been renamed and/or written to");
for(File otherFile : watchMap.keySet()) {
FileState otherState = watchMap.get(otherFile); FileState otherState = watchMap.get(otherFile);
if(otherState != null if(otherState != null && state.getSize() >= otherState.getSize() && state.getDirectory().equals(otherState.getDirectory())) {
&& state.getLastModified() == otherState.getLastModified()
&& state.getSize() == otherState.getSize()
&& state.getFilePath().equals(otherState.getFilePath())
&& ! state.getFileName().equals(otherState.getFileName())) {
logger.trace("Comparing to : " + otherFile.getCanonicalPath());
// Assume file has been renamed
state.setOldFileState(otherState);
logger.trace("Same directory, same size and last modification date : file has been renamed to : " + otherFile.getCanonicalPath());
}
}
}
// Determine if file has been renamed and appended
logger.trace("Determine if file has been renamed and appended");
if(state.getOldFileState() == null) {
for(File otherFile : changedWatchMap.keySet()) {
FileState otherState = watchMap.get(otherFile);
if(otherState != null && state.getSize() > otherState.getSize() && state.getFilePath().equals(otherState.getFilePath())) {
logger.trace("Comparing to : " + otherFile.getCanonicalPath()); logger.trace("Comparing to : " + otherFile.getCanonicalPath());
// File in the same directory which was smaller // File in the same directory which was smaller
if(otherState.getSignatureLength() == state.getSignatureLength() && otherState.getSignature() == state.getSignature()) { if(otherState.getSignatureLength() == state.getSignatureLength() && otherState.getSignature() == state.getSignature()) {
// File is the same // File is the same
state.setOldFileState(otherState); state.setOldFileState(otherState);
logger.trace("Same signature size and value : file is the same"); logger.trace("Same signature size and value : file is the same");
break;
} else if(otherState.getSignatureLength() < state.getSignatureLength()){ } else if(otherState.getSignatureLength() < state.getSignatureLength()){
// Compute the signature on the new file // Compute the signature on the new file
long signature = FileSigner.computeSignature(state.getRandomAccessFile(), otherState.getSignatureLength()); long signature = FileSigner.computeSignature(state.getRandomAccessFile(), otherState.getSignatureLength());
@@ -151,6 +139,7 @@ public class FileWatcher implements FileAlterationListener {
// File is the same // File is the same
state.setOldFileState(otherState); state.setOldFileState(otherState);
logger.trace("Same signature : file is the same"); logger.trace("Same signature : file is the same");
break;
} else { } else {
// File can't be the same // File can't be the same
logger.trace("Signature different : file can't be the same"); logger.trace("Signature different : file can't be the same");
@@ -165,7 +154,7 @@ public class FileWatcher implements FileAlterationListener {
} }
// Refresh file state // Refresh file state
logger.trace("refreshing file state"); logger.trace("Refreshing file state");
for(File file : changedWatchMap.keySet()) { for(File file : changedWatchMap.keySet()) {
logger.trace("Refreshing file : " + file.getCanonicalPath()); logger.trace("Refreshing file : " + file.getCanonicalPath());
FileState state = changedWatchMap.get(file); FileState state = changedWatchMap.get(file);
@@ -182,11 +171,15 @@ public class FileWatcher implements FileAlterationListener {
// Replacing old state // Replacing old state
logger.trace("Replacing old state"); logger.trace("Replacing old state");
for(File file : changedWatchMap.keySet()) { for(File file : changedWatchMap.keySet()) {
logger.trace("Replacing file : " + file.getCanonicalPath()); //logger.trace("Replacing file : " + file.getCanonicalPath());
FileState state = changedWatchMap.get(file); FileState state = changedWatchMap.get(file);
watchMap.put(file, state); watchMap.put(file, state);
} }
// Truncating changedWatchMap
changedWatchMap.clear();
removeMarkedFilesFromWatchMap();
} }
private void watchFile(String fileToWatch) throws Exception { private void watchFile(String fileToWatch) throws Exception {
@@ -272,7 +265,7 @@ public class FileWatcher implements FileAlterationListener {
logger.trace("WatchMap contents : "); logger.trace("WatchMap contents : ");
for(File file : watchMap.keySet()) { for(File file : watchMap.keySet()) {
FileState state = watchMap.get(file); FileState state = watchMap.get(file);
logger.trace("\tFile : " + state.getFilePath() + " marked for deletion : " + state.isDeleted()); logger.trace("\tFile : " + state.getDirectory() + " marked for deletion : " + state.isDeleted());
} }
} }
} }
@@ -293,11 +286,19 @@ public class FileWatcher implements FileAlterationListener {
for(File file : markedList) { for(File file : markedList) {
FileState state = watchMap.remove(file); FileState state = watchMap.remove(file);
state.getRandomAccessFile().close(); state.getRandomAccessFile().close();
logger.trace("\tFile : " + state.getFilePath() + " removed"); logger.trace("\tFile : " + file.getCanonicalFile() + " removed");
} }
} }
} }
public void close() throws IOException {
logger.debug("Closing all files");
for(File file : watchMap.keySet()) {
FileState state = watchMap.get(file);
state.getRandomAccessFile().close();
}
}
public void onDirectoryChange(File directory) { public void onDirectoryChange(File directory) {
// Do nothing // Do nothing
} }
@@ -317,4 +318,5 @@ public class FileWatcher implements FileAlterationListener {
public void onStop(FileAlterationObserver observer) { public void onStop(FileAlterationObserver observer) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
} }

View File

@@ -47,20 +47,35 @@ public class FileWatcherTest {
File file3 = new File("test3.txt"); File file3 = new File("test3.txt");
File file4 = new File("test4.txt"); File file4 = new File("test4.txt");
File testDir = new File("test");
//FileUtils.forceMkdir(new File("test"));
watcher.checkFiles(); watcher.checkFiles();
Thread.sleep(500); Thread.sleep(100);
FileUtils.write(file1, "line 1\n", true); FileUtils.write(file1, "file 1 line 1\n", true);
Thread.sleep(500); Thread.sleep(100);
watcher.checkFiles(); watcher.checkFiles();
FileUtils.forceDeleteOnExit(file1); FileUtils.write(file1, "file 1 line 2\n", true);
// FileUtils.touch(file2); FileUtils.write(file2, "file 2 line 1\n", true);
// Thread.sleep(500); Thread.sleep(1000);
// watcher.checkFiles(); watcher.checkFiles();
FileUtils.moveFileToDirectory(file1, testDir, true);
FileUtils.write(file2, "file 2 line 2\n", true);
FileUtils.moveFile(file2, file1);
FileUtils.write(file2, "file 3 line 1\n", true);
// FileUtils.touch(file3); // FileUtils.touch(file3);
// FileUtils.forceDelete(file1); // FileUtils.forceDelete(file1);
// FileUtils.forceDelete(file2); // FileUtils.forceDelete(file2);
// Thread.sleep(500); Thread.sleep(1000);
// watcher.checkFiles(); watcher.checkFiles();
watcher.close();
FileUtils.forceDelete(file1);
FileUtils.forceDelete(file2);
FileUtils.forceDelete(testDir);
// FileUtils.moveFile(file3, file4); // FileUtils.moveFile(file3, file4);
// FileUtils.touch(file3); // FileUtils.touch(file3);
// Thread.sleep(500); // Thread.sleep(500);