<commit_before>package org.freecompany.redline.payload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Logger;
import org.freecompany.redline.ChannelWrapper.Key;
import org.freecompany.redline.ReadableChannelWrapper;
import org.freecompany.redline.Util;

import static java.util.Arrays.asList;
import static java.util.logging.Level.FINE;
import static java.util.logging.Logger.getLogger;
import static org.freecompany.redline.Util.normalizePath;
import static org.freecompany.redline.payload.CpioHeader.DEFAULT_DIRECTORY_PERMISSION;
import static org.freecompany.redline.payload.CpioHeader.DEFAULT_FILE_PERMISSION;
import static org.freecompany.redline.payload.CpioHeader.DEFAULT_USERNAME;
import static org.freecompany.redline.payload.CpioHeader.DEFAULT_GROUP;
import static org.freecompany.redline.payload.CpioHeader.DIR;
import static org.freecompany.redline.payload.CpioHeader.FILE;
import static org.freecompany.redline.payload.CpioHeader.SYMLINK;

/**
 * The contents of an RPM archive. These entries define the files and links that
 * the RPM contains as well as headers those files require. Note that the RPM format
 * requires that files in the archive be naturally ordered.
 */
public class Contents {

	private static final Set< String> builtin = new HashSet< String>();
	private static final Set< String> docDirs = new HashSet< String>();
	static {
		builtin.add( "/");
		builtin.add( "/bin");
		builtin.add( "/dev");
		builtin.add( "/etc");
		builtin.add( "/etc/bash_completion.d");
		builtin.add( "/etc/cron.d");
		builtin.add( "/etc/cron.daily");
		builtin.add( "/etc/cron.hourly");
		builtin.add( "/etc/cron.monthly");
		builtin.add( "/etc/cron.weekly");
		builtin.add( "/etc/default");
		builtin.add( "/etc/init.d");
		builtin.add( "/etc/logrotate.d");
		builtin.add( "/lib");
		builtin.add( "/usr");
		builtin.add( "/usr/bin");
		builtin.add( "/usr/lib");
		builtin.add( "/usr/local");
		builtin.add( "/usr/local/bin");
		builtin.add( "/usr/local/lib");
		builtin.add( "/usr/sbin");
		builtin.add( "/usr/share");
		builtin.add( "/usr/share/applications");
		builtin.add( "/root");
		builtin.add( "/sbin");
		builtin.add( "/opt");
		builtin.add( "/tmp");
		builtin.add( "/var");
		builtin.add( "/var/log");
		docDirs.add( "/usr/doc");
		docDirs.add( "/usr/man");
		docDirs.add( "/usr/X11R6/man");
		docDirs.add( "/usr/share/doc");
		docDirs.add( "/usr/share/man");
		docDirs.add( "/usr/share/info");
	}

	private Logger logger = getLogger( Contents.class.getName());
	private int inode = 1;

	protected final Set< CpioHeader> headers = new TreeSet< CpioHeader>( new HeaderComparator());
	protected final Set< String> files = new HashSet< String>();
	protected final Map< CpioHeader, Object> sources = new HashMap< CpioHeader, Object>();

	/**
	 * Adds a directory entry to the archive with the default permissions of 644.
	 *
	 * @param path the destination path for the installed file.
	 */
	public synchronized void addLink( final String path, final String target) {
		addLink( path, target, -1);
	}

	/**
	 * Adds a directory entry to the archive with the specified permissions.
	 *
	 * @param path the destination path for the installed file.
	 * @param permissions the permissions flags.
	 */
	public synchronized void addLink( String path, final String target, int permissions) {
		if ( files.contains( path)) return;
		files.add( path);
		logger.log( FINE, "Adding link ''{0}''.", path);
		CpioHeader header = new CpioHeader( path);
		header.setType( SYMLINK);
		header.setFileSize( target.length());
		header.setMtime( System.currentTimeMillis());
		if ( permissions != -1) header.setPermissions( permissions);
		headers.add( header);
		sources.put( header, target);
	}

	/**
	 * Adds a directory entry to the archive with the default permissions of 644.
	 *
	 * @param path the destination path for the installed file.
	 */
	public synchronized void addDirectory( final String path) {
		addDirectory( path, -1);
	}
	
	/**
	 * Adds a directory entry to the archive with the default permissions of 644.
	 *
	 * @param path the destination path for the installed file.
	 * @param directive directive indicating special handling for this directory.
	 */
	public synchronized void addDirectory( final String path, final Directive directive) {
		addDirectory( path, -1, directive, null, null);
	}
	
	/**
	 * Adds a directory entry to the archive with the specified permissions.
	 *
	 * @param path the destination path for the installed file.
	 * @param permissions the permissions flags.
	 */
	public synchronized void addDirectory( final String path, final int permissions) {
		addDirectory(path, permissions, null, null, null);
	}

	/**
	 * Adds a directory entry to the archive with the specified permissions.
	 *
	 * @param path the destination path for the installed file.
	 * @param permissions the permissions flags.
	 * @param directive directive indicating special handling for this directory.
	 * @param uname user owner for the given file
	 * @param gname group owner for the given file
	 */
	public synchronized void addDirectory( final String path, final int permissions, final Directive directive, final String uname, final String gname) {
		addDirectory(path, permissions, directive, uname, gname, true);
	}

	/**
	 * Adds a directory entry to the archive with the specified permissions.
	 *
	 * @param path the destination path for the installed file.
	 * @param permissions the permissions flags.
	 * @param directive directive indicating special handling for this directory.
	 * @param uname user owner for the given file
	 * @param gname group owner for the given file
	 * @param addParents whether to add parent directories to the rpm
	 */
	public synchronized void addDirectory( final String path, final int permissions, final Directive directive, final String uname, final String gname, boolean addParents) {
		if ( files.contains( path)) return;

		if ( addParents) addParents( new File( path), permissions, uname, gname);
		files.add( path);
		logger.log( FINE, "Adding directory ''{0}''.", path);
		CpioHeader header = new CpioHeader( path);
		header.setType( DIR);
		header.setInode( inode++);
		if ( null == uname) {
			header.setUname(DEFAULT_USERNAME);
		} else if (0 == uname.length()) {
			header.setUname(DEFAULT_USERNAME);
		} else {
			header.setUname(uname);
		}
		if ( null == gname) {
			header.setUname(DEFAULT_GROUP);
		} else if (0 == gname.length()) {
			header.setUname(DEFAULT_GROUP);
		} else {
			header.setUname(gname);
		}
		header.setMtime( System.currentTimeMillis());
		if ( -1 == permissions) {
			header.setPermissions( DEFAULT_DIRECTORY_PERMISSION);
		} else {
			header.setPermissions( permissions);
		}
		headers.add( header);
		sources.put( header, "");
		if ( directive != null) header.setFlags( directive.flag());
	}<commit_msg>set the group name, not the username (my bad code)<commit_after>package org.freecompany.redline.payload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Logger;
import org.freecompany.redline.ChannelWrapper.Key;
import org.freecompany.redline.ReadableChannelWrapper;
import org.freecompany.redline.Util;

import static java.util.Arrays.asList;
import static java.util.logging.Level.FINE;
import static java.util.logging.Logger.getLogger;
import static org.freecompany.redline.Util.normalizePath;
import static org.freecompany.redline.payload.CpioHeader.DEFAULT_DIRECTORY_PERMISSION;
import static org.freecompany.redline.payload.CpioHeader.DEFAULT_FILE_PERMISSION;
import static org.freecompany.redline.payload.CpioHeader.DEFAULT_USERNAME;
import static org.freecompany.redline.payload.CpioHeader.DEFAULT_GROUP;
import static org.freecompany.redline.payload.CpioHeader.DIR;
import static org.freecompany.redline.payload.CpioHeader.FILE;
import static org.freecompany.redline.payload.CpioHeader.SYMLINK;

/**
 * The contents of an RPM archive. These entries define the files and links that
 * the RPM contains as well as headers those files require. Note that the RPM format
 * requires that files in the archive be naturally ordered.
 */
public class Contents {

	private static final Set< String> builtin = new HashSet< String>();
	private static final Set< String> docDirs = new HashSet< String>();
	static {
		builtin.add( "/");
		builtin.add( "/bin");
		builtin.add( "/dev");
		builtin.add( "/etc");
		builtin.add( "/etc/bash_completion.d");
		builtin.add( "/etc/cron.d");
		builtin.add( "/etc/cron.daily");
		builtin.add( "/etc/cron.hourly");
		builtin.add( "/etc/cron.monthly");
		builtin.add( "/etc/cron.weekly");
		builtin.add( "/etc/default");
		builtin.add( "/etc/init.d");
		builtin.add( "/etc/logrotate.d");
		builtin.add( "/lib");
		builtin.add( "/usr");
		builtin.add( "/usr/bin");
		builtin.add( "/usr/lib");
		builtin.add( "/usr/local");
		builtin.add( "/usr/local/bin");
		builtin.add( "/usr/local/lib");
		builtin.add( "/usr/sbin");
		builtin.add( "/usr/share");
		builtin.add( "/usr/share/applications");
		builtin.add( "/root");
		builtin.add( "/sbin");
		builtin.add( "/opt");
		builtin.add( "/tmp");
		builtin.add( "/var");
		builtin.add( "/var/log");
		docDirs.add( "/usr/doc");
		docDirs.add( "/usr/man");
		docDirs.add( "/usr/X11R6/man");
		docDirs.add( "/usr/share/doc");
		docDirs.add( "/usr/share/man");
		docDirs.add( "/usr/share/info");
	}

	private Logger logger = getLogger( Contents.class.getName());
	private int inode = 1;

	protected final Set< CpioHeader> headers = new TreeSet< CpioHeader>( new HeaderComparator());
	protected final Set< String> files = new HashSet< String>();
	protected final Map< CpioHeader, Object> sources = new HashMap< CpioHeader, Object>();

	/**
	 * Adds a directory entry to the archive with the default permissions of 644.
	 *
	 * @param path the destination path for the installed file.
	 */
	public synchronized void addLink( final String path, final String target) {
		addLink( path, target, -1);
	}

	/**
	 * Adds a directory entry to the archive with the specified permissions.
	 *
	 * @param path the destination path for the installed file.
	 * @param permissions the permissions flags.
	 */
	public synchronized void addLink( String path, final String target, int permissions) {
		if ( files.contains( path)) return;
		files.add( path);
		logger.log( FINE, "Adding link ''{0}''.", path);
		CpioHeader header = new CpioHeader( path);
		header.setType( SYMLINK);
		header.setFileSize( target.length());
		header.setMtime( System.currentTimeMillis());
		if ( permissions != -1) header.setPermissions( permissions);
		headers.add( header);
		sources.put( header, target);
	}

	/**
	 * Adds a directory entry to the archive with the default permissions of 644.
	 *
	 * @param path the destination path for the installed file.
	 */
	public synchronized void addDirectory( final String path) {
		addDirectory( path, -1);
	}
	
	/**
	 * Adds a directory entry to the archive with the default permissions of 644.
	 *
	 * @param path the destination path for the installed file.
	 * @param directive directive indicating special handling for this directory.
	 */
	public synchronized void addDirectory( final String path, final Directive directive) {
		addDirectory( path, -1, directive, null, null);
	}
	
	/**
	 * Adds a directory entry to the archive with the specified permissions.
	 *
	 * @param path the destination path for the installed file.
	 * @param permissions the permissions flags.
	 */
	public synchronized void addDirectory( final String path, final int permissions) {
		addDirectory(path, permissions, null, null, null);
	}

	/**
	 * Adds a directory entry to the archive with the specified permissions.
	 *
	 * @param path the destination path for the installed file.
	 * @param permissions the permissions flags.
	 * @param directive directive indicating special handling for this directory.
	 * @param uname user owner for the given file
	 * @param gname group owner for the given file
	 */
	public synchronized void addDirectory( final String path, final int permissions, final Directive directive, final String uname, final String gname) {
		addDirectory(path, permissions, directive, uname, gname, true);
	}

	/**
	 * Adds a directory entry to the archive with the specified permissions.
	 *
	 * @param path the destination path for the installed file.
	 * @param permissions the permissions flags.
	 * @param directive directive indicating special handling for this directory.
	 * @param uname user owner for the given file
	 * @param gname group owner for the given file
	 * @param addParents whether to add parent directories to the rpm
	 */
	public synchronized void addDirectory( final String path, final int permissions, final Directive directive, final String uname, final String gname, boolean addParents) {
		if ( files.contains( path)) return;

		if ( addParents) addParents( new File( path), permissions, uname, gname);
		files.add( path);
		logger.log( FINE, "Adding directory ''{0}''.", path);
		CpioHeader header = new CpioHeader( path);
		header.setType( DIR);
		header.setInode( inode++);
		if ( null == uname) {
			header.setUname(DEFAULT_USERNAME);
		} else if (0 == uname.length()) {
			header.setUname(DEFAULT_USERNAME);
		} else {
			header.setUname(uname);
		}
		if ( null == gname) {
			header.setGname(DEFAULT_GROUP);
		} else if (0 == gname.length()) {
			header.setGname(DEFAULT_GROUP);
		} else {
			header.setGname(gname);
		}
		header.setMtime( System.currentTimeMillis());
		if ( -1 == permissions) {
			header.setPermissions( DEFAULT_DIRECTORY_PERMISSION);
		} else {
			header.setPermissions( permissions);
		}
		headers.add( header);
		sources.put( header, "");
		if ( directive != null) header.setFlags( directive.flag());
	}
{'fraction_non_alphanumeric': 0.12244479387434197, 'fraction_numerical': 0.003008135639570657, 'mean_word_length': 4.216833095577746, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5449504', 'n_tokens_mistral': 4639, 'n_tokens_neox': 4392, 'n_words': 1401}