/
FileUtils.java
476 lines (411 loc) · 18.3 KB
/
FileUtils.java
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
472
473
474
475
476
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2021 The JReleaser authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jreleaser.util;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.jreleaser.bundle.RB;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystemLoopException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.zip.ZipOutputStream;
import static java.nio.file.FileVisitResult.CONTINUE;
import static java.nio.file.FileVisitResult.SKIP_SUBTREE;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static org.jreleaser.util.StringUtils.isNotBlank;
/**
* @author Andres Almiray
* @since 0.1.0
*/
public final class FileUtils {
private static final String[] LICENSE_FILE_NAMES = {
"LICENSE",
"LICENSE.txt",
"LICENSE.md",
"LICENSE.adoc"
};
private static final String TGZ = ".tgz";
private static final String TAR_BZ2 = ".tar.bz2";
private static final String TAR_GZ = ".tar.gz";
private FileUtils() {
//noop
}
public static Optional<Path> findLicenseFile(Path basedir) {
for (String licenseFilename : Arrays.asList(LICENSE_FILE_NAMES)) {
Path path = basedir.resolve(licenseFilename);
if (Files.exists(path)) {
return Optional.of(path);
}
path = basedir.resolve(licenseFilename.toLowerCase());
if (Files.exists(path)) {
return Optional.of(path);
}
}
return Optional.empty();
}
public static Path resolveOutputDirectory(Path basedir, Path outputdir, String baseOutput) {
String od = Env.resolve("OUTPUT_DIRECTORY", "");
if (isNotBlank(od)) {
return basedir.resolve(od).resolve("jreleaser");
}
if (null != outputdir) {
return basedir.resolve(outputdir).resolve("jreleaser");
}
return basedir.resolve(baseOutput).resolve("jreleaser");
}
public static void zip(Path src, Path dest) throws IOException {
try (ZipArchiveOutputStream out = new ZipArchiveOutputStream(dest.toFile())) {
out.setMethod(ZipOutputStream.DEFLATED);
Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
String entryName = src.relativize(file).toString();
File inputFile = file.toFile();
ZipArchiveEntry archiveEntry = new ZipArchiveEntry(inputFile, entryName);
archiveEntry.setMethod(ZipOutputStream.DEFLATED);
if (inputFile.isFile() && Files.isExecutable(file)) {
archiveEntry.setUnixMode(0100755);
}
out.putArchiveEntry(archiveEntry);
if (inputFile.isFile()) {
out.write(Files.readAllBytes(file));
}
out.closeArchiveEntry();
return FileVisitResult.CONTINUE;
}
});
}
}
public static void tar(Path src, Path dest) throws IOException {
try (TarArchiveOutputStream out = new TarArchiveOutputStream(Files.newOutputStream(dest, CREATE, TRUNCATE_EXISTING))) {
tar(src, out);
}
}
public static void tgz(Path src, Path dest) throws IOException {
try (TarArchiveOutputStream out = new TarArchiveOutputStream(
new GzipCompressorOutputStream(Files.newOutputStream(dest, CREATE, TRUNCATE_EXISTING)))) {
tar(src, out);
}
}
public static void bz2(Path src, Path dest) throws IOException {
try (TarArchiveOutputStream out = new TarArchiveOutputStream(
new BZip2CompressorOutputStream(Files.newOutputStream(dest, CREATE, TRUNCATE_EXISTING)))) {
tar(src, out);
}
}
private static void tar(Path src, TarArchiveOutputStream out) throws IOException {
out.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
String entryName = src.relativize(file).toString();
File inputFile = file.toFile();
TarArchiveEntry archiveEntry = (TarArchiveEntry) out.createArchiveEntry(inputFile, entryName);
if (inputFile.isFile() && Files.isExecutable(file)) {
archiveEntry.setMode(0100755);
}
out.putArchiveEntry(archiveEntry);
if (inputFile.isFile()) {
out.write(Files.readAllBytes(file));
}
out.closeArchiveEntry();
return FileVisitResult.CONTINUE;
}
});
}
public static void unpackArchive(Path src, Path dest) throws IOException {
String filename = src.getFileName().toString();
if (filename.endsWith(TGZ) || filename.endsWith(TAR_GZ) || filename.endsWith(TAR_BZ2)) {
unpackArchiveCompressed(src, dest);
return;
}
deleteFiles(dest, true);
File destinationDir = dest.toFile();
try (InputStream fi = Files.newInputStream(src);
InputStream bi = new BufferedInputStream(fi);
ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(bi)) {
// subtract .zip, .tar
filename = filename.substring(0, filename.length() - 4);
unpackArchive(filename + "/", destinationDir, in);
} catch (ArchiveException e) {
throw new IOException(e.getMessage(), e);
}
}
public static void unpackArchiveCompressed(Path src, Path dest) throws IOException {
deleteFiles(dest, true);
File destinationDir = dest.toFile();
String filename = src.getFileName().toString();
boolean bzip2 = filename.endsWith(TAR_BZ2);
boolean tgz = filename.endsWith(TGZ);
try (InputStream fi = Files.newInputStream(src);
InputStream bi = new BufferedInputStream(fi);
InputStream gzi = bzip2 ? new BZip2CompressorInputStream(bi) : new GzipCompressorInputStream(bi);
ArchiveInputStream in = new TarArchiveInputStream(gzi)) {
// subtract extension
int offset = bzip2 ? TAR_BZ2.length() : (tgz ? TGZ.length() : TAR_GZ.length());
filename = filename.substring(0, filename.length() - 7);
unpackArchive(filename + "/", destinationDir, in);
}
}
private static void unpackArchive(String basename, File destinationDir, ArchiveInputStream in) throws IOException {
ArchiveEntry entry = null;
while ((entry = in.getNextEntry()) != null) {
if (!in.canReadEntryData(entry)) {
// log something?
continue;
}
String entryName = entry.getName();
if (entryName.startsWith(basename) && entryName.length() > basename.length() + 1) {
entryName = entryName.substring(basename.length());
}
File file = new File(destinationDir, entryName);
String destDirPath = destinationDir.getCanonicalPath();
String destFilePath = file.getCanonicalPath();
if (!destFilePath.startsWith(destDirPath + File.separator)) {
throw new IOException(RB.$("ERROR_files_unpack_outside_target", entry.getName()));
}
if (entry.isDirectory()) {
if (!file.isDirectory() && !file.mkdirs()) {
throw new IOException(RB.$("ERROR_files_unpack_fail_dir", file));
}
} else {
File parent = file.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException(RB.$("ERROR_files_unpack_fail_dir", parent));
}
try (OutputStream o = Files.newOutputStream(file.toPath())) {
IOUtils.copy(in, o);
// TODO: make it a generic solution
// zipEntry.unixMode returns 0 most times even if the
// entry is executable
// https://github.com/jreleaser/jreleaser/issues/358
if ("bin".equalsIgnoreCase(file.getParentFile().getName())) {
grantExecutableAccess(file.toPath());
}
}
}
}
}
public static List<String> inspectArchive(Path src) throws IOException {
String filename = src.getFileName().toString();
if (filename.endsWith(TGZ) || filename.endsWith(TAR_GZ) || filename.endsWith(TAR_BZ2)) {
return inspectArchiveCompressed(src);
}
try (InputStream fi = Files.newInputStream(src);
InputStream bi = new BufferedInputStream(fi);
ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(bi)) {
return inspectArchive(in);
} catch (ArchiveException e) {
throw new IOException(e.getMessage(), e);
}
}
public static List<String> inspectArchiveCompressed(Path src) throws IOException {
String filename = src.getFileName().toString();
boolean bzip2 = filename.endsWith(TAR_BZ2);
try (InputStream fi = Files.newInputStream(src);
InputStream bi = new BufferedInputStream(fi);
InputStream gzi = bzip2 ? new BZip2CompressorInputStream(bi) : new GzipCompressorInputStream(bi);
ArchiveInputStream in = new TarArchiveInputStream(gzi)) {
return inspectArchive(in);
}
}
private static List<String> inspectArchive(ArchiveInputStream in) throws IOException {
List<String> entries = new ArrayList<>();
ArchiveEntry entry = null;
while ((entry = in.getNextEntry()) != null) {
if (!in.canReadEntryData(entry)) {
// log something?
continue;
}
entries.add(entry.getName());
}
return entries;
}
public static void deleteFiles(Path path) throws IOException {
deleteFiles(path, false);
}
public static void deleteFiles(Path path, boolean keepRoot) throws IOException {
if (Files.exists(path)) {
Files.walk(path)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
if (!keepRoot) Files.deleteIfExists(path);
}
}
public static void createDirectoriesWithFullAccess(Path path) throws IOException {
createDirectories(path, "rwxrwxrwx");
}
public static void createDirectories(Path path, String accessRights) throws IOException {
Set<PosixFilePermission> perms = PosixFilePermissions.fromString(accessRights);
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
Files.createDirectories(path, attr);
}
public static void grantFullAccess(Path path) throws IOException {
grantAccess(path, "rwxrwxrwx");
}
public static void grantExecutableAccess(Path path) throws IOException {
grantAccess(path, "r-xr-xr-x");
}
public static void grantAccess(Path path, String accessRights) throws IOException {
Set<PosixFilePermission> perms = PosixFilePermissions.fromString(accessRights);
Files.setPosixFilePermissions(path, perms);
}
public static void copyPermissions(Path src, Path dest) throws IOException {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(src);
Files.setPosixFilePermissions(dest, perms);
}
public static void copyFiles(JReleaserLogger logger, Path source, Path target) throws IOException {
copyFiles(logger, source, target, null);
}
public static void copyFiles(JReleaserLogger logger, Path source, Path target, Predicate<Path> filter) throws IOException {
Predicate<Path> actualFilter = filter != null ? filter : path -> true;
IOException[] thrown = new IOException[1];
Files.list(source)
.filter(Files::isRegularFile)
.filter(actualFilter)
.forEach(child -> {
try {
Files.copy(child, target.resolve(child.getFileName()), REPLACE_EXISTING);
} catch (IOException e) {
logger.error(RB.$("ERROR_files_copy"), child, e);
if (null == thrown[0]) thrown[0] = e;
}
});
if (thrown[0] != null) {
throw thrown[0];
}
}
public static boolean copyFilesRecursive(JReleaserLogger logger, Path source, Path target) throws IOException {
return copyFilesRecursive(logger, source, target, null);
}
public static boolean copyFilesRecursive(JReleaserLogger logger, Path source, Path target, Predicate<Path> filter) throws IOException {
FileTreeCopy copier = new FileTreeCopy(logger, source, target, filter);
Files.walkFileTree(source, copier);
return copier.isSuccessful();
}
private static class FileTreeCopy implements FileVisitor<Path> {
private final JReleaserLogger logger;
private final Path source;
private final Path target;
private final Predicate<Path> filter;
private boolean success = true;
FileTreeCopy(JReleaserLogger logger, Path source, Path target, Predicate<Path> filter) {
this.logger = logger;
this.source = source;
this.target = target;
this.filter = filter;
}
private boolean filtered(Path path) {
if (null != filter) {
return filter.test(path);
}
return false;
}
public boolean isSuccessful() {
return success;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
if (filtered(dir)) return SKIP_SUBTREE;
Path newdir = target.resolve(source.relativize(dir));
try {
Files.copy(dir, newdir);
FileUtils.grantFullAccess(newdir);
} catch (FileAlreadyExistsException ignored) {
// noop
} catch (IOException e) {
logger.error(RB.$("ERROR_files_create"), newdir, e);
success = false;
return SKIP_SUBTREE;
}
return CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (filtered(file)) return CONTINUE;
try {
Path newfile = target.resolve(source.relativize(file));
Files.copy(file, newfile, REPLACE_EXISTING);
FileUtils.copyPermissions(file, newfile);
} catch (IOException e) {
logger.error(RB.$("ERROR_files_copy"), source, e);
success = false;
}
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
if (filtered(dir)) return CONTINUE;
if (exc == null) {
Path newdir = target.resolve(source.relativize(dir));
try {
FileTime time = Files.getLastModifiedTime(dir);
Files.setLastModifiedTime(newdir, time);
} catch (IOException e) {
logger.warn(RB.$("ERROR_files_copy_attributes"), newdir, e);
}
}
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException e) {
if (e instanceof FileSystemLoopException) {
logger.error(RB.$("ERROR_files_cycle"), file);
} else {
logger.error(RB.$("ERROR_files_copy"), file, e);
}
success = false;
return CONTINUE;
}
}
}