FileUtil.java
15.7 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* Decompiled with CFR 0_118.
*/
package com.day.crx.core.backup.crx;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public final class FileUtil {
static final boolean NIO_FILE_COPY = Boolean.valueOf(System.getProperty("com.day.crx.NioFileCopy", "false"));
private FileUtil() {
}
public static long sumFileSizes(File file) {
if (!file.exists()) {
return 0;
}
if (file.isFile()) {
return file.length();
}
long size = 0;
File[] list = file.listFiles();
if (list != null) {
for (File f : list) {
size += FileUtil.sumFileSizes(f);
}
}
return size;
}
public static void writeZipStream(File directory, OutputStream out, FileCopyListener listener) throws IOException {
ZipOutputStream zipOut = new ZipOutputStream(out);
zipOut.setLevel(1);
FileUtil.addFiles(directory, directory, zipOut, listener);
zipOut.finish();
zipOut.close();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static void addFiles(File base, File file, ZipOutputStream out, FileCopyListener listener) throws IOException {
if (file.isDirectory()) {
for (File f : FileUtil.listFiles(file)) {
FileUtil.addFiles(base, f, out, listener);
}
} else {
if (listener != null && listener.canSkip(file)) {
return;
}
long total = file.length();
FileInputStream in = new FileInputStream(file);
try {
int len;
String path = file.getAbsolutePath().substring(base.getAbsolutePath().length());
if (File.separatorChar == '\\') {
path = path.replace('\\', '/');
}
if (path.startsWith("/")) {
path = path.substring(1);
}
if (listener != null) {
listener.fileCopyStart(file, true);
}
ZipEntry ze = new ZipEntry(path);
ze.setTime(file.lastModified());
out.putNextEntry(ze);
long pos = 0;
byte[] buf = new byte[4096];
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
pos += (long)len;
if (listener == null) continue;
listener.copied(pos, total);
}
if (listener != null) {
listener.fileCopyEnd();
}
out.closeEntry();
}
finally {
in.close();
}
}
}
public static void copy(File src, File dest, FileCopyListener listener, long onlyNewerThan, boolean onlyDifferent) throws IOException {
if (listener != null && listener.canSkip(src)) {
return;
}
if (!src.canRead()) {
return;
}
if (src.isDirectory()) {
boolean done;
if (dest.isFile()) {
throw new IOException("Can't copy a folder to a file");
}
if (!dest.exists() && !(done = dest.mkdirs())) {
throw new IOException("Could not create the folder " + dest.getAbsolutePath());
}
if (!dest.canWrite()) {
throw new IOException("Can't write to " + dest.getPath());
}
for (File child : FileUtil.listFiles(src)) {
FileUtil.copy(child, new File(dest, child.getName()), listener, onlyNewerThan, onlyDifferent);
}
} else {
File destParent;
if (dest.isDirectory()) {
destParent = dest;
dest = new File(destParent, src.getName());
} else {
destParent = dest.getParentFile();
}
if (!destParent.canWrite()) {
throw new IOException("can't write to " + destParent.getPath());
}
boolean copy = true;
if (dest.exists()) {
boolean isSameLength;
boolean bl = isSameLength = dest.length() == src.length();
if (onlyNewerThan != 0) {
if (isSameLength) {
copy = src.lastModified() < onlyNewerThan ? false : !FileUtil.fileContentMatches(src, dest, listener);
}
} else {
boolean isReadOnly = false;
if (listener != null) {
isReadOnly = listener.isReadOnly(src);
}
if (onlyDifferent && isSameLength && src.lastModified() == dest.lastModified()) {
boolean bl2 = isReadOnly ? false : (copy = !FileUtil.fileContentMatches(src, dest, listener));
}
}
}
if (listener != null) {
listener.fileCopyStart(src, false);
}
if (copy) {
if (NIO_FILE_COPY) {
boolean success = FileUtil.copyFileFast(src, dest, listener);
if (!success) {
FileUtil.copyFileSlow(src, dest, listener);
}
} else {
FileUtil.copyFileSlow(src, dest, listener);
}
} else if (listener != null) {
listener.skip();
}
dest.setLastModified(src.lastModified());
if (listener != null) {
listener.fileCopyEnd();
}
}
}
/*
* Exception decompiling
*/
private static boolean fileContentMatches(File src, File dest, FileCopyListener listener) throws IOException {
// This method has failed to decompile. When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
// org.benf.cfr.reader.util.ConfusedCFRException: Tried to end blocks [1[TRYBLOCK]], but top level block is 10[WHILELOOP]
// org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.processEndingBlocks(Op04StructuredStatement.java:397)
// org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:449)
// org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:2877)
// org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:825)
// org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:217)
// org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:162)
// org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:95)
// org.benf.cfr.reader.entities.Method.analyse(Method.java:355)
// org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:768)
// org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:700)
// org.benf.cfr.reader.Main.doJar(Main.java:134)
// org.benf.cfr.reader.Main.main(Main.java:189)
throw new IllegalStateException("Decompilation failed");
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static boolean copyFileSlow(File src, File dest, FileCopyListener listener) throws IOException {
FileInputStream fis = new FileInputStream(src);
long total = src.length();
try {
FileOutputStream fos = new FileOutputStream(dest);
try {
byte[] buffer = new byte[8192];
int read = 0;
long pos = 0;
while ((read = fis.read(buffer)) > 0) {
fos.write(buffer, 0, read);
if (listener == null) continue;
listener.copied(pos += (long)read, total);
}
}
catch (IOException e) {
boolean read;
try {
if (listener != null) {
listener.onError(e);
}
read = false;
}
catch (Throwable var11_11) {
fos.close();
throw var11_11;
}
fos.close();
fis.close();
return read;
}
fos.close();
}
finally {
fis.close();
}
return true;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static boolean copyFileFast(File src, File dest, FileCopyListener listener) throws IOException {
FileChannel input = new FileInputStream(src).getChannel();
try {
FileChannel output = new FileOutputStream(dest).getChannel();
try {
long len = input.size();
output.transferFrom(input, 0, len);
listener.copied(len, len);
}
catch (IOException e) {
boolean bl;
try {
if (listener != null) {
listener.onError(e);
}
bl = false;
}
catch (Throwable var7_8) {
output.close();
throw var7_8;
}
output.close();
input.close();
return bl;
}
output.close();
}
finally {
input.close();
}
return true;
}
public static void deleteOld(File target, File source, String[] excludeSuffixes) throws IOException {
if (target.isDirectory()) {
if (source.exists() && source.isDirectory()) {
for (File child : FileUtil.listFiles(target)) {
FileUtil.deleteOld(child, new File(source, child.getName()), excludeSuffixes);
}
} else {
FileUtil.delete(target);
}
} else if (source.exists()) {
if (source.isDirectory()) {
FileUtil.delete(target);
}
} else {
String n = target.getAbsolutePath();
boolean delete = true;
for (String s : excludeSuffixes) {
if (!n.endsWith(s)) continue;
delete = false;
break;
}
if (delete) {
FileUtil.delete(target);
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static void delete(File f) throws IOException {
if (!f.exists()) {
return;
}
if (f.isDirectory()) {
for (File child : FileUtil.listFiles(f)) {
FileUtil.delete(child);
}
}
if (!f.delete() && f.exists()) {
try {
f.setWritable(true);
f.delete();
}
catch (SecurityException e) {}
finally {
if (f.exists()) {
System.gc();
f.delete();
}
}
if (f.exists()) {
throw new IOException("Unable to delete " + f.getPath());
}
}
}
private static File[] listFiles(File dir) throws IOException {
if (!dir.isDirectory()) {
throw new IOException("Not a directory: " + dir.getAbsolutePath());
}
File[] list = dir.listFiles();
if (list == null) {
throw new IOException("Could not read directory " + dir.getAbsolutePath() + ", please check the file access rights.");
}
return list;
}
public static synchronized File createTempDirectory(String prefix, String suffix, File directory) throws IOException {
if (prefix == null || prefix.length() < 3) {
throw new IllegalArgumentException("Prefix string too short: " + prefix);
}
if (suffix == null) {
suffix = ".tmp";
}
if (directory == null) {
directory = new File(System.getProperty("java.io.tmpdir"));
}
Random random = new Random();
for (int i = 0; i < 10; ++i) {
String name = prefix + Integer.toHexString(random.nextInt() & 65535) + suffix;
File f = new File(directory, name);
if (f.exists() || !f.mkdirs() || !f.isDirectory()) continue;
return f;
}
throw new IOException("Could not create a temporary directory; prefix= " + prefix + " suffix=" + suffix + " directory=" + directory.getAbsolutePath());
}
public static String readableBytes(long bytes) {
BigDecimal b = BigDecimal.valueOf(bytes);
BigDecimal k = BigDecimal.valueOf(1024);
String unit = "bytes";
if (b.compareTo(k) >= 0) {
b = b.divide(k, 0, 5);
unit = "kb";
}
if (b.compareTo(k) >= 0) {
b = b.divide(k, 2, 5);
unit = "mb";
}
if (b.compareTo(k) >= 0) {
b = b.divide(k, 3, 5);
unit = "gb";
}
return b + " " + unit;
}
public static String readableTime(long ms) {
double s;
if (ms <= 10) {
return "" + ms + "ms";
}
double t = (double)ms / 1000.0;
StringBuilder sb = new StringBuilder();
if (t >= 3600.0) {
int h = (int)(t / 3600.0);
t %= 3600.0;
sb.append(h);
sb.append("h");
}
if (t >= 60.0) {
int m = (int)(t / 60.0);
t %= 60.0;
if (sb.length() > 0) {
sb.append(" : ");
}
sb.append(m);
sb.append("m");
}
if ((s = (double)((int)(t * 100.0)) / 100.0) > 0.0) {
if (sb.length() > 0) {
sb.append(" : ");
}
sb.append(s);
sb.append("s");
}
return sb.toString();
}
public class FileCopyAdapter
implements FileCopyListener {
@Override
public void fileCopyStart(File src, boolean compress) {
}
@Override
public boolean canSkip(File src) {
return false;
}
@Override
public boolean isReadOnly(File src) {
return false;
}
@Override
public void copied(long pos, long total) {
}
@Override
public void skip() {
}
@Override
public void onError(IOException e) {
}
@Override
public void fileCopyEnd() {
}
@Override
public long totalBytesCopied() {
return -1;
}
@Override
public long totalBytesSkipped() {
return -1;
}
}
public static interface FileCopyListener {
public void fileCopyStart(File var1, boolean var2);
public boolean canSkip(File var1);
public boolean isReadOnly(File var1);
public void copied(long var1, long var3);
public void skip();
public void onError(IOException var1);
public void fileCopyEnd();
public long totalBytesCopied();
public long totalBytesSkipped();
}
}