1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.net.ftp.parser;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FilenameFilter;
24 import java.io.InputStream;
25 import java.util.Arrays;
26 import java.util.Calendar;
27 import java.util.Comparator;
28 import java.util.TimeZone;
29
30 import org.apache.commons.net.ftp.FTP;
31 import org.apache.commons.net.ftp.FTPClientConfig;
32 import org.apache.commons.net.ftp.FTPFile;
33 import org.apache.commons.net.ftp.FTPFileFilters;
34 import org.apache.commons.net.ftp.FTPListParseEngine;
35
36 import org.junit.Test;
37
38
39
40
41
42
43 public class MLSDComparison {
44
45 private final Comparator<FTPFile> cmp = new Comparator<FTPFile>() {
46
47 public int compare(FTPFile o1, FTPFile o2) {
48 String n1 = o1.getName();
49 String n2 = o2.getName();
50 return n1.compareTo(n2);
51 }
52 };
53
54 @Test
55 public void testFile() throws Exception{
56 File path = new File(DownloadListings.DOWNLOAD_DIR);
57 FilenameFilter filter = new FilenameFilter(){
58
59 public boolean accept(File dir, String name) {
60 return name.endsWith("_mlsd.txt");
61 }
62
63 };
64 for (File mlsd : path.listFiles(filter)){
65 System.out.println(mlsd);
66 InputStream is = new FileInputStream(mlsd);
67 FTPListParseEngine engine = new FTPListParseEngine(MLSxEntryParser.getInstance());
68 engine.readServerList(is, FTP.DEFAULT_CONTROL_ENCODING);
69 FTPFile [] mlsds = engine.getFiles(FTPFileFilters.ALL);
70 is.close();
71 File list = new File(mlsd.getParentFile(),mlsd.getName().replace("_mlsd", "_list"));
72
73 System.out.println(list);
74 is = new FileInputStream(list);
75 FTPClientConfig cfg = new FTPClientConfig();
76 cfg.setServerTimeZoneId("GMT");
77 UnixFTPEntryParser parser = new UnixFTPEntryParser(cfg);
78 engine = new FTPListParseEngine(parser);
79 engine.readServerList(is, FTP.DEFAULT_CONTROL_ENCODING);
80 FTPFile [] lists = engine.getFiles(FTPFileFilters.ALL);
81 is.close();
82 compareSortedLists(mlsds, lists);
83 }
84 }
85
86 private void compareSortedLists(FTPFile[] lst, FTPFile[] mlst){
87 Arrays.sort(lst, cmp );
88 Arrays.sort(mlst, cmp );
89 FTPFile first, second;
90 int firstl=lst.length;
91 int secondl=mlst.length;
92 int one=0, two=0;
93 first = lst[one++];
94 second = mlst[two++];
95 int cmp;
96 while (one < firstl || two < secondl) {
97
98
99 String rl1 = first.getRawListing();
100 String rl2 = second.getRawListing();
101 cmp = first.getName().compareTo(second.getName());
102 if (cmp == 0) {
103 if (first.getName().endsWith("HEADER.html")){
104 cmp = 0;
105 }
106 if (!areEquivalent(first, second)){
107
108
109 long tdiff = first.getTimestamp().getTimeInMillis()-second.getTimestamp().getTimeInMillis();
110 System.out.println("Minutes diff "+tdiff/(1000*60));
111
112
113
114
115 }
116 if (one < firstl) {
117 first = lst[one++];
118 }
119 if (two < secondl) {
120 second = mlst[two++];
121 }
122 } else if (cmp < 0) {
123 if (!first.getName().startsWith(".")) {
124 System.out.println("1: "+rl1);
125 }
126 if (one < firstl) {
127 first = lst[one++];
128 }
129 } else {
130 System.out.println("2: "+rl2);
131 if (two < secondl) {
132 second = mlst[two++];
133 }
134 }
135 }
136 }
137
138
139
140
141
142
143
144
145 public boolean areEquivalent(FTPFile a, FTPFile b) {
146 return
147 a.getName().equals(b.getName()) &&
148 areSame(a.getSize(), b.getSize(), -1L) &&
149
150
151 areSame(a.getTimestamp(), b.getTimestamp()) &&
152
153
154
155 true
156 ;
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178 private boolean areSame(Calendar a, Calendar b) {
179 return a == null || b == null || areSameDateTime(a, b);
180 }
181
182 private boolean areSameDateTime(Calendar a, Calendar b) {
183 TimeZone UTC = TimeZone.getTimeZone("UTC");
184 Calendar ac = Calendar.getInstance(UTC);
185 ac.setTime(a.getTime());
186 Calendar bc = Calendar.getInstance(UTC);
187 bc.setTime(b.getTime());
188 return isSameDay(ac, bc) && isSameTime(ac, bc);
189 }
190
191 private boolean isSameDay(Calendar a, Calendar b) {
192 int ad = a.get(Calendar.DAY_OF_MONTH);
193 int bd = b.get(Calendar.DAY_OF_MONTH);
194 return
195 a.get(Calendar.YEAR) == b.get(Calendar.YEAR) &&
196 a.get(Calendar.MONTH) == b.get(Calendar.MONTH) &&
197 ad == bd
198 ;
199 }
200 private boolean isSameTime(Calendar a, Calendar b) {
201 int ah = a.get(Calendar.HOUR_OF_DAY);
202 int bh = b.get(Calendar.HOUR_OF_DAY);
203 int am = a.get(Calendar.MINUTE);
204 int bm = b.get(Calendar.MINUTE);
205 int as = a.get(Calendar.SECOND);
206 int bs = b.get(Calendar.SECOND);
207 return
208 (ah == 0 && am == 0 && as ==0) ||
209 (bh == 0 && bm == 0 && bs ==0) ||
210 (ah == bh && am == bm)
211 ;
212 }
213
214
215 private boolean areSame(long a, long b, long d) {
216 return a == d || b == d || a == b;
217 }
218
219
220
221
222
223
224
225
226 }