-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.java
More file actions
124 lines (106 loc) · 3.69 KB
/
Copy pathWorker.java
File metadata and controls
124 lines (106 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package lineEndingChecker;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.PriorityQueue;
/**
*
* @author John
*/
public class Worker
{
private SimpleDateFormat DateFormat = null;
private String hostname = "";
public PriorityQueue<Path> DirectoryQueue = null;
public Worker()
{
try
{
DirectoryQueue = new PriorityQueue<>();
DateFormat = new SimpleDateFormat(Common.DateFormatString);
}
catch (Exception ex)
{
System.err.format("Error: Worker Constructor: %s\r\n", ex);
}
}
public void Run()
{
Charset charset = Charset.forName("UTF-8");
Path filePath = Paths.get(Common.ChecksumFilename);
try (BufferedWriter writer = Files.newBufferedWriter(filePath, charset))
{
System.out.println("Start: " + DateFormat.format(new Date()));
WalkTree(writer);
System.out.println("Finish: " + DateFormat.format(new Date()) + "\r\n");
}
catch (Exception ex)
{
System.err.format("Error: Worker.Run: %s\r\n", ex);
}
}
public void WalkTree(BufferedWriter writer) throws IOException
{
getHostname();
Path rootPath = Paths.get(Common.RootFolder);
DirectoryQueue.clear();
DirectoryQueue.add(rootPath);
writer.write("Host: " + hostname + "\r\n");
writer.write("Date: " + DateFormat.format(new Date()) + "\r\n");
writer.write("Root: " + Common.RootFolder + "\r\n");
//Process until every directory is done
while (DirectoryQueue.size() > 0)
{
//grab the next directory in the work queue
Path workingDir = DirectoryQueue.remove();
//show progress
System.out.println(workingDir.toString());
//Add this to the output files
writer.write("\r\n");
writer.write("Dir: " + rootPath.relativize(workingDir) + "\r\n");
//walk through all items in this directory
try (DirectoryStream<Path> stream = Files.newDirectoryStream(workingDir))
{
for (Path entry: stream)
{
if (Files.isDirectory(entry))
{
//Add directories to the work queue
DirectoryQueue.add(entry);
}
else if (Files.isRegularFile(entry))
{
//process files
FileChecker checker = new FileChecker();
checker.Load(entry);
writer.write(checker.toString() + "\r\n");
}
}
}
catch (Exception ex)
{
System.err.format("Error: Worker DirectoryStream: %s\r\n", ex);
}
//Flush the file to disk at the end of every directory
writer.flush();
}
System.out.println("Done.");
}
private void getHostname()
{
try
{
hostname = java.net.InetAddress.getLocalHost().getHostName();
}
catch (Exception ex)
{
System.err.format("Error: Worker HostName: %s\r\n", ex);
}
}
}