Search in sources :

Example 1 with EOLFixingFileOutputStream

use of io.fabric8.patch.management.io.EOLFixingFileOutputStream in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method unzipKarafAdminJar.

/**
 * Unzips <code>bin</code> and <code>etc</code> from org.apache.karaf.admin.core.
 * @param artifact
 * @param targetDirectory
 * @throws IOException
 */
private void unzipKarafAdminJar(File artifact, File targetDirectory) throws IOException {
    ZipFile zf = new ZipFile(artifact);
    String prefix = "org/apache/karaf/admin/";
    try {
        for (Enumeration<ZipArchiveEntry> e = zf.getEntries(); e.hasMoreElements(); ) {
            ZipArchiveEntry entry = e.nextElement();
            String name = entry.getName();
            if (!name.startsWith(prefix)) {
                continue;
            }
            name = name.substring(prefix.length());
            if (!name.startsWith("bin") && !name.startsWith("etc")) {
                continue;
            }
            // flags from karaf.admin.core
            // see: org.apache.karaf.admin.internal.AdminServiceImpl.createInstance()
            boolean windows = System.getProperty("os.name").startsWith("Win");
            boolean cygwin = windows && new File(System.getProperty("karaf.home"), "bin/admin").exists();
            if (!entry.isDirectory() && !entry.isUnixSymlink()) {
                if (windows && !cygwin) {
                    if (name.startsWith("bin/") && !name.endsWith(".bat")) {
                        continue;
                    }
                } else {
                    if (name.startsWith("bin/") && name.endsWith(".bat")) {
                        continue;
                    }
                }
                File file = new File(targetDirectory, name);
                file.getParentFile().mkdirs();
                FileOutputStream output = new EOLFixingFileOutputStream(targetDirectory, file);
                IOUtils.copyLarge(zf.getInputStream(entry), output);
                IOUtils.closeQuietly(output);
                if (Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class) != null) {
                    if (name.startsWith("bin/") && !name.endsWith(".bat")) {
                        Files.setPosixFilePermissions(file.toPath(), getPermissionsFromUnixMode(file, 0775));
                    }
                }
            }
        }
    } finally {
        if (zf != null) {
            zf.close();
        }
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 2 with EOLFixingFileOutputStream

use of io.fabric8.patch.management.io.EOLFixingFileOutputStream in project fabric8 by jboss-fuse.

the class Utils method unpack.

/**
 * Unpacks a ZIP file to targetDirectory
 * @param zipFile
 * @param targetDirectory
 * @param skipInitialDirectories how many levels of a path to skip when unpacking (like skipping base directory inside ZIP)
 * @throws IOException
 */
public static void unpack(File zipFile, File targetDirectory, int skipInitialDirectories) throws IOException {
    ZipFile zf = new ZipFile(zipFile);
    try {
        for (Enumeration<ZipArchiveEntry> e = zf.getEntries(); e.hasMoreElements(); ) {
            ZipArchiveEntry entry = e.nextElement();
            String name = entry.getName();
            int skip = skipInitialDirectories;
            while (skip-- > 0) {
                name = name.substring(name.indexOf('/') + 1);
            }
            if (entry.isDirectory()) {
                new File(targetDirectory, name).mkdirs();
            } else /*if (!entry.isUnixSymlink())*/
            {
                File file = new File(targetDirectory, name);
                file.getParentFile().mkdirs();
                FileOutputStream output = new EOLFixingFileOutputStream(targetDirectory, file);
                IOUtils.copyLarge(zf.getInputStream(entry), output);
                IOUtils.closeQuietly(output);
                if (Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class) != null) {
                    Files.setPosixFilePermissions(file.toPath(), getPermissionsFromUnixMode(file, entry.getUnixMode()));
                }
            }
        }
    } finally {
        zf.close();
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 3 with EOLFixingFileOutputStream

use of io.fabric8.patch.management.io.EOLFixingFileOutputStream in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method unzipFabric8Distro.

/**
 * Unzips <code>bin</code> and <code>etc</code> everything we need from org.apache.karaf.admin.core.
 * @param rootDir
 * @param artifact
 * @param version
 * @param fork
 * @return branch name where the distro should be tracked, or <code>null</code> if it's already tracked
 * @throws IOException
 */
private String unzipFabric8Distro(String rootDir, File artifact, String version, Git fork) throws IOException, GitAPIException {
    ZipFile zf = new ZipFile(artifact);
    try {
        // first pass - what's this distro?
        boolean officialFabric8 = true;
        for (Enumeration<ZipArchiveEntry> e = zf.getEntries(); e.hasMoreElements(); ) {
            ZipArchiveEntry entry = e.nextElement();
            String name = entry.getName();
            if (name.startsWith(rootDir + "/")) {
                name = name.substring(rootDir.length() + 1);
            }
            if ("etc/startup.properties".equals(name)) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtils.copyLarge(zf.getInputStream(entry), baos);
                Properties props = new Properties();
                props.load(new ByteArrayInputStream(baos.toByteArray()));
                for (String p : props.stringPropertyNames()) {
                    if (p.startsWith("org/jboss/fuse/shared-commands/")) {
                        // we have Fuse!
                        officialFabric8 = false;
                        break;
                    }
                }
            }
        }
        // checkout correct branch
        if (officialFabric8) {
            if (gitPatchRepository.containsTag(fork, String.format("baseline-ssh-fabric8-%s", version))) {
                return null;
            }
            gitPatchRepository.checkout(fork).setName(gitPatchRepository.getFabric8SSHContainerPatchBranchName()).call();
        } else {
            if (gitPatchRepository.containsTag(fork, String.format("baseline-ssh-fuse-%s", version))) {
                return null;
            }
            gitPatchRepository.checkout(fork).setName(gitPatchRepository.getFuseSSHContainerPatchBranchName()).call();
        }
        for (String managedDirectory : MANAGED_DIRECTORIES) {
            FileUtils.deleteDirectory(new File(fork.getRepository().getWorkTree(), managedDirectory));
        }
        // second pass - unzip what we need
        for (Enumeration<ZipArchiveEntry> e = zf.getEntries(); e.hasMoreElements(); ) {
            ZipArchiveEntry entry = e.nextElement();
            String name = entry.getName();
            if (name.startsWith(rootDir + "/")) {
                name = name.substring(rootDir.length() + 1);
            }
            if (!(name.startsWith("bin") || name.startsWith("etc") || name.startsWith("fabric") || name.startsWith("lib") || name.startsWith("licenses") || name.startsWith("metatype"))) {
                continue;
            }
            if (!entry.isDirectory() && !entry.isUnixSymlink()) {
                File file = new File(fork.getRepository().getWorkTree(), name);
                file.getParentFile().mkdirs();
                FileOutputStream output = new EOLFixingFileOutputStream(fork.getRepository().getWorkTree(), file);
                IOUtils.copyLarge(zf.getInputStream(entry), output);
                IOUtils.closeQuietly(output);
                if (Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class) != null) {
                    if (name.startsWith("bin/") && !name.endsWith(".bat")) {
                        Files.setPosixFilePermissions(file.toPath(), getPermissionsFromUnixMode(file, 0775));
                    }
                }
            }
        }
        return officialFabric8 ? gitPatchRepository.getFabric8SSHContainerPatchBranchName() : gitPatchRepository.getFuseSSHContainerPatchBranchName();
    } finally {
        if (zf != null) {
            zf.close();
        }
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ByteArrayInputStream(java.io.ByteArrayInputStream) EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) Properties(java.util.Properties) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Aggregations

EOLFixingFileOutputStream (io.fabric8.patch.management.io.EOLFixingFileOutputStream)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)3 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)3 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Properties (java.util.Properties)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1