|
| 1 | +package io.projectenv.core.commons.archive.impl; |
| 2 | + |
| 3 | +import io.projectenv.core.commons.archive.ArchiveExtractor; |
| 4 | +import io.projectenv.core.commons.archive.impl.accessor.ArchiveAccessor; |
| 5 | +import io.projectenv.core.commons.archive.impl.accessor.ArchiveAccessorFactory; |
| 6 | +import io.projectenv.core.commons.archive.impl.accessor.ArchiveEntry; |
| 7 | +import io.projectenv.core.commons.system.OperatingSystem; |
| 8 | +import org.apache.commons.io.FileUtils; |
| 9 | +import org.apache.commons.io.IOUtils; |
| 10 | + |
| 11 | +import java.io.*; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.attribute.PosixFilePermission; |
| 14 | +import java.nio.file.attribute.PosixFilePermissions; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.regex.Pattern; |
| 19 | + |
| 20 | +public class DefaultArchiveExtractor implements ArchiveExtractor { |
| 21 | + |
| 22 | + private static final List<Pattern> IGNORED_ARCHIVE_ENTRIES = Collections.singletonList( |
| 23 | + // dot underscore files which are created on macOS systems and hold metadata |
| 24 | + Pattern.compile(".*/\\._.+") |
| 25 | + ); |
| 26 | + |
| 27 | + public void extractArchive(File archive, File targetDirectory) throws IOException { |
| 28 | + try (ArchiveAccessor archiveAccessor = ArchiveAccessorFactory.createArchiveAccessor(archive)) { |
| 29 | + |
| 30 | + ArchiveEntry entry; |
| 31 | + |
| 32 | + while ((entry = archiveAccessor.getNextEntry()) != null) { |
| 33 | + if (!shouldExtractEntry(entry)) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + File target = new File(targetDirectory, entry.getName()); |
| 38 | + checkThatPathIsInsideBasePath(target, targetDirectory); |
| 39 | + |
| 40 | + if (entry.isDirectory()) { |
| 41 | + createDirectory(target); |
| 42 | + } else if (entry.isSymbolicLink()) { |
| 43 | + createSymbolicLink(entry, target, targetDirectory); |
| 44 | + } else { |
| 45 | + createFile(entry, target); |
| 46 | + } |
| 47 | + |
| 48 | + setPermissions(entry, target); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private boolean shouldExtractEntry(ArchiveEntry archiveEntry) { |
| 54 | + return IGNORED_ARCHIVE_ENTRIES.stream().noneMatch(pattern -> pattern.matcher(archiveEntry.getName()).matches()); |
| 55 | + } |
| 56 | + |
| 57 | + private void checkThatPathIsInsideBasePath(File file, File baseDirectory) throws IOException { |
| 58 | + if (!file.getCanonicalPath().startsWith(baseDirectory.getCanonicalPath())) { |
| 59 | + throw new IllegalStateException("path " + file.getPath() + " is pointing to a location outside " + baseDirectory.getCanonicalPath()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + protected void createDirectory(File target) throws IOException { |
| 64 | + FileUtils.forceMkdir(target.getCanonicalFile()); |
| 65 | + } |
| 66 | + |
| 67 | + protected void createSymbolicLink(ArchiveEntry archiveEntry, File target, File targetDirectory) throws IOException { |
| 68 | + File linkDestination = new File(archiveEntry.getLinkName()); |
| 69 | + checkThatPathIsInsideBasePath(new File(target.isDirectory() ? target : target.getParentFile(), archiveEntry.getLinkName()), targetDirectory); |
| 70 | + |
| 71 | + FileUtils.forceMkdirParent(target.getCanonicalFile()); |
| 72 | + |
| 73 | + Files.createSymbolicLink(target.toPath(), linkDestination.toPath()); |
| 74 | + } |
| 75 | + |
| 76 | + private void createFile(ArchiveEntry archiveEntry, File target) throws IOException { |
| 77 | + FileUtils.forceMkdirParent(target.getCanonicalFile()); |
| 78 | + |
| 79 | + try (InputStream inputStream = archiveEntry.createInputStream(); |
| 80 | + OutputStream outputStream = new FileOutputStream(target)) { |
| 81 | + |
| 82 | + IOUtils.copy(inputStream, outputStream); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void setPermissions(ArchiveEntry archiveEntry, File target) throws IOException { |
| 87 | + // we do not set any permissions on Windows |
| 88 | + if (OperatingSystem.getCurrentOperatingSystem() == OperatingSystem.WINDOWS) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (archiveEntry.isSymbolicLink()) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + Integer mode = archiveEntry.getMode(); |
| 97 | + if (mode != null) { |
| 98 | + Files.setPosixFilePermissions(target.toPath(), posixFilePermissionsFromMode(mode)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private Set<PosixFilePermission> posixFilePermissionsFromMode(int decimalMode) { |
| 103 | + // to determine the Posix permission flags, we only need the last 12 bits |
| 104 | + int relevantPermissionBits = decimalMode & 0b111111111; |
| 105 | + |
| 106 | + char[] permissionFlags = Integer.toOctalString(relevantPermissionBits).toCharArray(); |
| 107 | + |
| 108 | + StringBuilder posixPermissions = new StringBuilder(); |
| 109 | + for (char permissionFlag : permissionFlags) { |
| 110 | + if ((permissionFlag & 0b100) != 0) { |
| 111 | + posixPermissions.append('r'); |
| 112 | + } else { |
| 113 | + posixPermissions.append('-'); |
| 114 | + } |
| 115 | + |
| 116 | + if ((permissionFlag & 0b010) != 0) { |
| 117 | + posixPermissions.append('w'); |
| 118 | + } else { |
| 119 | + posixPermissions.append('-'); |
| 120 | + } |
| 121 | + |
| 122 | + if ((permissionFlag & 0b001) != 0) { |
| 123 | + posixPermissions.append('x'); |
| 124 | + } else { |
| 125 | + posixPermissions.append('-'); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return PosixFilePermissions.fromString(posixPermissions.toString()); |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments