Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2015, 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.packager.rpm.build;
import static java.util.Comparator.comparing;
import static java.util.Optional.of;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.ToLongFunction;
import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
import org.apache.commons.compress.archivers.cpio.CpioConstants;
import org.apache.commons.compress.compressors.zstandard.ZstdUtils;
import org.eclipse.packager.rpm.Architecture;
import org.eclipse.packager.rpm.FileFlags;
import org.eclipse.packager.rpm.OperatingSystem;
import org.eclipse.packager.rpm.PathName;
import org.eclipse.packager.rpm.RpmTag;
import org.eclipse.packager.rpm.RpmVersion;
import org.eclipse.packager.rpm.Rpms;
import org.eclipse.packager.rpm.build.PayloadRecorder.Result;
import org.eclipse.packager.rpm.deps.Dependencies;
import org.eclipse.packager.rpm.deps.Dependency;
import org.eclipse.packager.rpm.deps.RpmDependencyFlags;
import org.eclipse.packager.rpm.header.Header;
import org.eclipse.packager.rpm.signature.SignatureProcessor;
import org.eclipse.packager.rpm.signature.SignatureProcessors;
/**
* Build RPM files
*
* This class takes care of most tasks building RPM files. The constructor only
* requests the require attributes. There are a few more meta information
* entries which can be set using the {@link PackageInformation} class and the
* methods {@link #setInformation(PackageInformation)} and
* {@link #getInformation()}.
*
*
* In order to build an RPM file, create a new instance of the
* {@link RpmBuilder} class, set package information, add files by using a
* context created by {@link #newContext()} and finally call {@link #build()}.
* The RPM file will only be built once the {@link #build()} method is called.
* Closing the instance of {@link RpmBuilder} will not write the RPM
* file, but simply clean up temporary files. Closing this instance will also
* not delete target RPM file.
*
*
* The implementation of this class uses the {@link PayloadRecorder} to create
* the payload archive, {@link Header} class for the signature and package
* header and the {@link RpmWriter} to finally write the RPM file.
*
*
Signature processors
*
* The RPM builder uses a default set of {@link SignatureProcessor}s. In order
* to add additional ones use the {@link #addDefaultSignatureProcessors()}. It
* is possible to remove all already registered processors (including the
* default ones) using {@link #removeAllSignatureProcessors()}.
*
*
* @author Jens Reimann
*/
public class RpmBuilder implements AutoCloseable
{
@FunctionalInterface
private interface RecorderFunction
{
public Result record ( PayloadRecorder recorder, String targetName, T data, Consumer customizer ) throws IOException;
}
/**
* Known versions of RPM.
*
* This is a enum of known versions of the RPM tool itself. It is incomplete
* and should be used to ensure compatibility of created RPM file with
* certain versions of RPM. The first known version we track is "4.11",
* which may not be correct either.
*/
public static enum Version
{
V4_11 ( "4.11" ),
V4_12 ( "4.12" ),
V4_14 ( "4.14" );
private final String versionString;
private Version ( final String versionString )
{
this.versionString = versionString;
}
@Override
public String toString ()
{
return this.versionString;
}
public static Optional fromVersionString ( final String versionString )
{
if ( versionString == null )
{
return Optional.empty ();
}
for ( final Version version : Version.values () )
{
if ( version.versionString.equals ( versionString ) )
{
return Optional.of ( version );
}
}
return Optional.empty ();
}
}
public static class Feature extends Dependency
{
private String description;
public Feature ( final String name, final String version, final String description )
{
super ( "rpmlib(" + name + ")", version, RpmDependencyFlags.RPMLIB, RpmDependencyFlags.EQUAL );
this.description = description;
}
public String getDescription ()
{
return this.description;
}
public void setDescription ( final String description )
{
this.description = description;
}
@Override
public int hashCode ()
{
final int prime = 31;
int result = 1;
result = prime * result + ( getFlags () == null ? 0 : getFlags ().hashCode () );
result = prime * result + ( getName () == null ? 0 : getName ().hashCode () );
result = prime * result + ( getVersion () == null ? 0 : getVersion ().hashCode () );
result = prime * result + ( getDescription () == null ? 0 : getDescription ().hashCode () );
return result;
}
@Override
public boolean equals ( final Object obj )
{
if ( this == obj )
{
return true;
}
if ( obj == null )
{
return false;
}
if ( getClass () != obj.getClass () )
{
return false;
}
final Feature other = (Feature)obj;
if ( getFlags () == null )
{
if ( other.getFlags () != null )
{
return false;
}
}
else if ( !getFlags ().equals ( other.getFlags () ) )
{
return false;
}
if ( getName () == null )
{
if ( other.getName () != null )
{
return false;
}
}
else if ( !getName ().equals ( other.getName () ) )
{
return false;
}
if ( getVersion () == null )
{
if ( other.getVersion () != null )
{
return false;
}
}
else if ( !getVersion ().equals ( other.getVersion () ) )
{
return false;
}
if ( getDescription () == null )
{
if ( other.getDescription () != null )
{
return false;
}
}
else if ( !getDescription ().equals ( other.getDescription () ) )
{
return false;
}
return true;
}
@Override
public String toString ()
{
return String.format ( "[%s, %s, %s, %s]", getName (), getVersion (), getFlags (), getDescription () );
}
}
private static List features = new ArrayList<> ();
static
{
features.add ( new Feature ( "VersionedDependencies", "3.0.3-1", "PreReq:, Provides:, and Obsoletes: dependencies support versions." ) );
features.add ( new Feature ( "CompressedFileNames", "3.0.4-1", "file name(s) stored as (dirName,baseName,dirIndex) tuple, not as path." ) );
features.add ( new Feature ( "PayloadIsBzip2", "3.0.5-1", "package payload can be compressed using bzip2." ) );
features.add ( new Feature ( "ExplicitPackageProvide", "4.0-1", "package name-version-release is not implicitly provided." ) );
features.add ( new Feature ( "HeaderLoadSortsTags", "4.0.1-1", "header tags are always sorted after being loaded." ) );
features.add ( new Feature ( "PayloadFilesHavePrefix", "4.0-1", "package payload file(s) have \"./\" prefix." ) );
features.add ( new Feature ( "PayloadIsLzma", "4.4.2-1", "package payload can be compressed using lzma." ) );
features.add ( new Feature ( "PayloadIsXz", "5.2-1", "package payload can be compressed using xz." ) );
if ( ZstdUtils.isZstdCompressionAvailable () )
{
features.add ( new Feature ( "PayloadIsZstd", "5.4.18-1", "package payload can be compressed using zstd." ) );
}
features = Collections.unmodifiableList ( features );
}
public static class FileEntry
{
private long size;
private String user;
private String group;
private String linkTo;
private short mode;
private short rdevs;
private int flags;
private int modificationTime;
private String digest;
private int verifyFlags = -1;
private String lang;
private int device;
private int inode;
private PathName targetName;
private long targetSize;
public void setSize ( final long size )
{
this.size = size;
}
public long getSize ()
{
return this.size;
}
public void setUser ( final String user )
{
this.user = user;
}
public String getUser ()
{
return this.user;
}
public void setGroup ( final String group )
{
this.group = group;
}
public String getGroup ()
{
return this.group;
}
public void setLinkTo ( final String linkTo )
{
this.linkTo = linkTo;
}
public String getLinkTo ()
{
return this.linkTo;
}
public short getMode ()
{
return this.mode;
}
public void setMode ( final short mode )
{
this.mode = mode;
}
public short getRdevs ()
{
return this.rdevs;
}
public void setRdevs ( final short rdevs )
{
this.rdevs = rdevs;
}
public int getFlags ()
{
return this.flags;
}
public void setFlags ( final int flags )
{
this.flags = flags;
}
public void setModificationTime ( final int modificationTime )
{
this.modificationTime = modificationTime;
}
public int getModificationTime ()
{
return this.modificationTime;
}
public void setDigest ( final String digest )
{
this.digest = digest;
}
public String getDigest ()
{
return this.digest;
}
public void setVerifyFlags ( final int verifyFlags )
{
this.verifyFlags = verifyFlags;
}
public int getVerifyFlags ()
{
return this.verifyFlags;
}
public void setLang ( final String lang )
{
this.lang = lang;
}
public String getLang ()
{
return this.lang;
}
public void setDevice ( final int device )
{
this.device = device;
}
public int getDevice ()
{
return this.device;
}
public void setInode ( final int inode )
{
this.inode = inode;
}
public int getInode ()
{
return this.inode;
}
public void setTargetName ( final PathName targetName )
{
this.targetName = targetName;
}
public PathName getTargetName ()
{
return this.targetName;
}
public void setTargetSize ( final long targetSize )
{
this.targetSize = targetSize;
}
public long getTargetSize ()
{
return this.targetSize;
}
}
public static class PackageInformation
{
private String distribution;
private String packager;
private String vendor;
private String license = "unspecified";
private String buildHost = "localhost";
private String summary = "Unspecified";
private String description = "Unspecified";
private String group = "Unspecified";
private String operatingSystem = "linux";
private String url;
private String sourcePackage;
private List prefixes;
public void setDistribution ( final String distribution )
{
this.distribution = distribution;
}
public String getDistribution ()
{
return this.distribution;
}
public String getPackager ()
{
return this.packager;
}
public void setPackager ( final String packager )
{
this.packager = packager;
}
public String getVendor ()
{
return this.vendor;
}
public void setVendor ( final String vendor )
{
this.vendor = vendor;
}
public void setLicense ( final String license )
{
this.license = license;
}
public String getLicense ()
{
return this.license;
}
public void setBuildHost ( final String buildHost )
{
this.buildHost = buildHost;
}
public String getBuildHost ()
{
return this.buildHost;
}
public void setDescription ( final String description )
{
this.description = description;
}
public String getDescription ()
{
return this.description;
}
public void setSummary ( final String summary )
{
this.summary = summary;
}
public String getSummary ()
{
return this.summary;
}
public void setGroup ( final String group )
{
this.group = group;
}
public String getGroup ()
{
return this.group;
}
public void setOperatingSystem ( final String operatingSystem )
{
this.operatingSystem = operatingSystem;
}
public String getOperatingSystem ()
{
return this.operatingSystem;
}
public void setUrl ( final String url )
{
this.url = url;
}
public String getUrl ()
{
return this.url;
}
public void setSourcePackage ( final String sourcePackage )
{
this.sourcePackage = sourcePackage;
}
public String getSourcePackage ()
{
return this.sourcePackage;
}
public void setPrefixes ( final List prefixes )
{
this.prefixes = prefixes;
}
public List getPrefixes ()
{
return this.prefixes;
}
}
private static abstract class BuilderContextImpl implements BuilderContext
{
private FileInformationProvider