<commit_before>/*-
 * Copyright (c) 2012 Red Hat, Inc.
 *
 * 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
 *
 *     http://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.fedoraproject.maven.rpminstall.plugin;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.fedoraproject.maven.Configuration;
import org.fedoraproject.maven.Rule;
import org.fedoraproject.maven.model.Artifact;
import org.fedoraproject.maven.utils.FileUtils;

public class Package
    implements Comparable<Package>
{
    private final String suffix;

    private boolean pureDevelPackage = true;

    public Package( String name )
    {
        suffix = name.equals( "" ) ? "" : "-" + name;
    }

    private final FragmentFile depmap = new FragmentFile();

    class TargetFile
    {
        Path sourceFile;

        Path dirPath;

        Path targetName;
    }

    private final List<TargetFile> targetFiles = new LinkedList<>();

    public void addFile( Path file, Path dirPath, Path fileName )
    {
        TargetFile target = new TargetFile();
        target.sourceFile = file;
        target.dirPath = dirPath;
        target.targetName = fileName;
        targetFiles.add( target );
    }

    public void addFile( Path file, Path target )
    {
        addFile( file, target.getParent(), target.getFileName() );
    }

    public void addPomFile( Path file, Path jppGroupId, Path jppArtifactId )
    {
        Path pomName = Paths.get( jppGroupId.toString().replace( '/', '.' ) + "-" + jppArtifactId + ".pom" );
        addFile( file, Configuration.getInstallPomDir(), pomName );
    }

    private static boolean containsNativeCode( Path jar )
    {
        // From /usr/include/linux/elf.h
        final int ELFMAG0 = 0x7F;
        final int ELFMAG1 = 'E';
        final int ELFMAG2 = 'L';
        final int ELFMAG3 = 'F';

        try (ZipInputStream jis = new ZipInputStream( new FileInputStream( jar.toFile() ) ))
        {
            ZipEntry ent;
            while ( ( ent = jis.getNextEntry() ) != null )
            {
                if ( ent.isDirectory() )
                    continue;
                if ( jis.read() == ELFMAG0 && jis.read() == ELFMAG1 && jis.read() == ELFMAG2 && jis.read() == ELFMAG3 )
                    return true;
            }
        }
        catch ( IOException e )
        {
        }

        return false;
    }

    public void addJarFile( Path file, Path baseFile, Collection<Path> symlinks, BigDecimal javaVersion )
        throws IOException
    {
        pureDevelPackage = false;

        Path jarDir = containsNativeCode( file ) ? Configuration.getInstallJniDir() : Configuration.getInstallJarDir();
        addFile( file, jarDir.resolve( baseFile ) );

        for ( Path symlink : symlinks )
        {
            Path target = Paths.get( "/" ).resolve( jarDir ).resolve( baseFile );
            Path symlinkFile = FileUtils.createAnonymousSymlink( target );
            if ( !symlink.isAbsolute() )
                symlink = jarDir.resolve( symlink );
            addFile( symlinkFile, symlink );
        }

        depmap.addJavaVersionRequirement( javaVersion );
    }

    private void installFiles( Installer installer )
        throws IOException<commit_msg>containsNativeCode: Don't catch IOException<commit_after>/*-
 * Copyright (c) 2012 Red Hat, Inc.
 *
 * 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
 *
 *     http://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.fedoraproject.maven.rpminstall.plugin;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.fedoraproject.maven.Configuration;
import org.fedoraproject.maven.Rule;
import org.fedoraproject.maven.model.Artifact;
import org.fedoraproject.maven.utils.FileUtils;

public class Package
    implements Comparable<Package>
{
    private final String suffix;

    private boolean pureDevelPackage = true;

    public Package( String name )
    {
        suffix = name.equals( "" ) ? "" : "-" + name;
    }

    private final FragmentFile depmap = new FragmentFile();

    class TargetFile
    {
        Path sourceFile;

        Path dirPath;

        Path targetName;
    }

    private final List<TargetFile> targetFiles = new LinkedList<>();

    public void addFile( Path file, Path dirPath, Path fileName )
    {
        TargetFile target = new TargetFile();
        target.sourceFile = file;
        target.dirPath = dirPath;
        target.targetName = fileName;
        targetFiles.add( target );
    }

    public void addFile( Path file, Path target )
    {
        addFile( file, target.getParent(), target.getFileName() );
    }

    public void addPomFile( Path file, Path jppGroupId, Path jppArtifactId )
    {
        Path pomName = Paths.get( jppGroupId.toString().replace( '/', '.' ) + "-" + jppArtifactId + ".pom" );
        addFile( file, Configuration.getInstallPomDir(), pomName );
    }

    private static boolean containsNativeCode( Path jar )
        throws IOException
    {
        // From /usr/include/linux/elf.h
        final int ELFMAG0 = 0x7F;
        final int ELFMAG1 = 'E';
        final int ELFMAG2 = 'L';
        final int ELFMAG3 = 'F';

        try (ZipInputStream jis = new ZipInputStream( new FileInputStream( jar.toFile() ) ))
        {
            ZipEntry ent;
            while ( ( ent = jis.getNextEntry() ) != null )
            {
                if ( ent.isDirectory() )
                    continue;
                if ( jis.read() == ELFMAG0 && jis.read() == ELFMAG1 && jis.read() == ELFMAG2 && jis.read() == ELFMAG3 )
                    return true;
            }
        }

        return false;
    }

    public void addJarFile( Path file, Path baseFile, Collection<Path> symlinks, BigDecimal javaVersion )
        throws IOException
    {
        pureDevelPackage = false;

        Path jarDir = containsNativeCode( file ) ? Configuration.getInstallJniDir() : Configuration.getInstallJarDir();
        addFile( file, jarDir.resolve( baseFile ) );

        for ( Path symlink : symlinks )
        {
            Path target = Paths.get( "/" ).resolve( jarDir ).resolve( baseFile );
            Path symlinkFile = FileUtils.createAnonymousSymlink( target );
            if ( !symlink.isAbsolute() )
                symlink = jarDir.resolve( symlink );
            addFile( symlinkFile, symlink );
        }

        depmap.addJavaVersionRequirement( javaVersion );
    }

    private void installFiles( Installer installer )
        throws IOException
{'fraction_non_alphanumeric': 0.10070869078701977, 'fraction_numerical': 0.004475941812756434, 'mean_word_length': 2.5265234546251643, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6819336', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2276, 'n_words': 657}