package fileMonitor;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
public class Scan extends Thread
{
File file;
FileInfo MainFileInfo;
FileInfo FileParent = null;
Renamed renamed;
Modified modified;
PathChanged path_changed;
SizeChanged size_changed;
FileWriter summary = null;
FileWriter detail = null;
String CacheSummary = null;
HashMap<String,FileInfo> CacheM = new HashMap<String,FileInfo>();
public Scan (Single_Task ST)
{
this.file = ST.file;
MainFileInfo = new FileInfo(file);
this.renamed = ST.renamed;
this.modified = ST.modified;
this.path_changed = ST.path_changed;
this.size_changed = ST.size_changed;
if(!MainFileInfo.isDirectory)
file = new File(MainFileInfo.parent);
Vector<File> CacheFiles = new Vector<File>();
try
{
if(file.exists())
getFiles(file, CacheFiles);
} catch (IOException e)
{
System.out.println("error!");
System.exit(0);
}
for(File F : CacheFiles)
{
FileInfo Mf= new FileInfo(F);
CacheM.put(F.getAbsolutePath(),Mf);
}
}
public void getFiles(File file, Vector<File> FileList) throws IOException
{
if (file==null)
return;
File[] sub_files = file.listFiles();
try{
for(int i = 0; i < sub_files.length; i++)
{
if (sub_files[i].isDirectory())
getFiles(sub_files[i],FileList);
FileList.add(sub_files[i]);
}
}
catch(NullPointerException e)
{
System.out.println("sub_files.length = "+sub_files.length);
System.exit(0);
}
}
@SuppressWarnings({ "unused" })
public void run()
{
if(MainFileInfo.isDirectory)
{
while (true)
{
if(!file.exists())
{
System.out.println("Misoperation:Main Directory/File" +MainFileInfo.path+"is deleted!");
break;
}
{
long CurrentSize = FileInfo.getSize(file);
long CurrentModify = file.lastModified();
if(MainFileInfo.size!=FileInfo.getSize(file) && size_changed.set)
{
size_changed.number++;
if(size_changed.record_summary)
{
try {
Summary.size(MainFileInfo.path, size_changed.number);
} catch (IOException e) {
System.exit(0);
}
}
if(size_changed.record_detail)
{
try {
Detail.size(MainFileInfo.path, MainFileInfo.size, CurrentSize);
} catch (IOException e) {
System.exit(0);
}
}
}
if(MainFileInfo.modified!= CurrentModify && modified.set)
{
modified.number++;
if(modified.record_summary)
{
try {
Summary.modified(MainFileInfo.path, modified.number);
} catch (IOException e) {
System.exit(0);
}
}
if(modified.record_detail)
{
try {
Detail.modified(MainFileInfo.path, MainFileInfo.modified, CurrentModify);
} catch (IOException e) {
System.exit(0);
}
}
}
MainFileInfo.update(file);
}
Vector<File> DiskFiles = new Vector<File>();
try
{
getFiles(file, DiskFiles);
} catch (IOException e) {
System.out.println("error!");
System.exit(0);
}
Vector<String> IsIn = new Vector<String>();
for(int i = 0;i<DiskFiles.size();i++)
{
File CurrentFile = new File(DiskFiles.get(i).getAbsolutePath());
if(CurrentFile.exists())
{
FileInfo CurrentInfo = new FileInfo(CurrentFile);
FileInfo CacheFile = CacheM.get(CurrentInfo.path);
if(CacheFile != null)
{
IsIn.add(CurrentInfo.path);
if(size_changed.set && CacheFile.size != CurrentInfo.size)
{
size_changed.number++;
if(size_changed.record_summary)
{
try {
Summary.size(MainFileInfo.path, size_changed.number);
} catch (IOException e) {
System.exit(0);
}
}
if(size_changed.record_detail)
{
try {
Detail.size(CurrentInfo.path, CacheFile.size, CurrentInfo.size);
} catch (IOException e) {
System.exit(0);
}
}
}
if(modified.set && CacheFile.modified!=CurrentInfo.modified)
{
modified.number++;
if(modified.record_summary)
{
try {
Summary.modified(MainFileInfo.path, modified.number);
} catch (IOException e) {
System.exit(0);
}
}
if(modified.record_detail)
{
try {
Detail.modified(CurrentInfo.path, CacheFile.modified, CurrentInfo.modified);
} catch (IOException e) {
System.exit(0);
}
}
}
CacheM.put(CurrentInfo.path, CurrentInfo);
}
else
{
if(CurrentInfo.isDirectory)
{
System.out.println("Misoperation: Directory:\""+CurrentInfo.path+"\"Path Changed or Renamed!");
System.out.println("The Program will stop!");
System.exit(0);
}
Iterator<Entry<String, FileInfo>> iterator=CacheM.entrySet().iterator();
boolean Find = false;
while(iterator.hasNext())
{
Map.Entry<String, FileInfo> entry= iterator.next();
if (CurrentInfo.isChangePath(entry.getValue()))
{
Find = true;
if(path_changed.set)
{
path_changed.number++;
if(path_changed.record_summary)
{
try {
Summary.path(MainFileInfo.path, path_changed.number);
} catch (IOException e) {
System.exit(0);
}
}
if(path_changed.record_detail)
{
try {
评论0