<commit_before>/* RPM - Copyright (C) 1995 Red Hat Software
 * 
 * prepack.c - routines for packaging
 */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>

#include "miscfn.h"
#include "spec.h"
#include "specP.h"
#include "stringbuf.h"
#include "build.h"
#include "messages.h"
#include "md5.h"
#include "myftw.h"
#include "header.h"
#include "files.h"
#include "names.h"
#include "rpmlead.h"
#include "rpmlib.h"
#include "misc.h"
#include "macro.h"

#define BINARY_HEADER 0
#define SOURCE_HEADER 1

struct file_entry {
    char file[1024];
    int isdoc;
    int conf;
    int isspecfile;
    int verify_flags;
    char *uname;  /* reference -- do not free */
    char *gname;  /* reference -- do not free */
    struct stat statbuf;
    struct file_entry *next;
};

static int add_file(struct file_entry **festack, const char *name,
		    int isdoc, int isconf, int isdir, int verify_flags,
		    char *Pmode, char *Uname, char *Gname, char *prefix);
static int compare_fe(const void *ap, const void *bp);
static int add_file_aux(const char *file, struct stat *sb, int flag);
static int glob_error(const char *foo, int bar);
static int glob_pattern_p (char *pattern);
static int parseForVerify(char *buf, int *verify_flags);
static int parseForConfig(char *buf, int *conf);
static int parseForAttr(char *origbuf, char **currPmode,
			char **currUname, char **currGname);

static void resetDocdir(void);
static void addDocdir(char *dirname);
static int isDoc(char *filename);

static int processFileListFailed;

int process_filelist(Header header, struct PackageRec *pr,
		     StringBuf sb, int *size, char *name,
		     char *version, char *release, int type,
		     char *prefix, char *specFile)
{
    char buf[1024];
    char **files, **fp;
    struct file_entry *fes, *fest;
    struct file_entry **file_entry_array;
    int isdoc, conf, isdir, verify_flags;
    char *currPmode=NULL;	/* hold info from %attr() */
    char *currUname=NULL;	/* hold info from %attr() */
    char *currGname=NULL;	/* hold info from %attr() */
    char *filename, *s;
    char *str;
    int count = 0;
    int c, x;
    glob_t glob_result;
    int special_doc;
    int passed_special_doc = 0;
    FILE *file;
    int tc;
    char *tcs;
    int currentTime;

    processFileListFailed = 0;
    fes = NULL;
    *size = 0;

    resetDocdir();

    if (type == RPMLEAD_BINARY && pr->fileFile) {
	sprintf(buf, "%s/%s/%s", rpmGetVar(RPMVAR_BUILDDIR),
		build_subdir, pr->fileFile);
	rpmMessage(RPMMESS_DEBUG, "Reading file names from: %s\n", buf);
	if ((file = fopen(buf, "r")) == NULL) {
	    rpmError(RPMERR_BADSPEC, "unable to open filelist: %s\n", buf);
	    return(RPMERR_BADSPEC);
	}
	while (fgets(buf, sizeof(buf), file)) {
	    expandMacros(buf);
	    appendStringBuf(sb, buf);
	}
	fclose(file);
    }
    
    str = getStringBuf(sb);
    files = splitString(str, strlen(str), '\n');
    fp = files;

    while (*fp) {
	strcpy(buf, *fp);  /* temp copy */
	isdoc = 0;
	special_doc = 0;
	conf = 0;
	isdir = 0;
	if (currPmode) {
	    free (currPmode);
	    currPmode = NULL;
	}
	if (currUname) {
	    free (currUname);
	    currUname = NULL;
	}
	if (currGname) {
	    free (currGname);
	    currGname = NULL;
	}
	verify_flags = RPMVERIFY_ALL;
	filename = NULL;

	/* First preparse buf for %verify() */
	if (parseForVerify(buf, &verify_flags)) {
	    processFileListFailed = 1;
	    fp++; continue;
	}
	
	/* Next parse for %attr() */
	if (parseForAttr(buf, &currPmode, &currUname, &currGname)) {
	    processFileListFailed = 1;
	    fp++; continue;
	}

	/* Then parse for %config or %config() */
	if (parseForConfig(buf, &conf)) {
	    processFileListFailed = 1;
	    fp++; continue;
	}

	s = strtok(buf, " \t\n");
	while (s) {
	    if (!strcmp(s, "%docdir")) {
	        s = strtok(NULL, " \t\n");
		addDocdir(s);
		break;
	    } else if (!strcmp(s, "%doc")) {
		isdoc = 1;
	    } else if (!strcmp(s, "%dir")) {
		isdir = 1;
	    } else {
		if (filename) {
		    /* We already got a file -- error */
		    rpmError(RPMERR_BADSPEC,
			  "Two files on one line: %s", filename);
		    processFileListFailed = 1;
		    fp++; continue;
		}
		if (*s != '/') {
		    if (isdoc) {
			special_doc = 1;
		    } else {
			/* not in %doc, does not begin with / -- error */
			rpmError(RPMERR_BADSPEC,
			      "File must begin with \"/\": %s", s);
			processFileListFailed = 1;
			fp++; continue;
		    }
		} else {
		    filename = s;
		}
	    }
	    s = strtok(NULL, " \t\n");
	}
	if (special_doc) {
	    if (passed_special_doc) {<commit_msg>oops - a failed listcheck never terminated<commit_after>/* RPM - Copyright (C) 1995 Red Hat Software
 * 
 * prepack.c - routines for packaging
 */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>

#include "miscfn.h"
#include "spec.h"
#include "specP.h"
#include "stringbuf.h"
#include "build.h"
#include "messages.h"
#include "md5.h"
#include "myftw.h"
#include "header.h"
#include "files.h"
#include "names.h"
#include "rpmlead.h"
#include "rpmlib.h"
#include "misc.h"
#include "macro.h"

#define BINARY_HEADER 0
#define SOURCE_HEADER 1

struct file_entry {
    char file[1024];
    int isdoc;
    int conf;
    int isspecfile;
    int verify_flags;
    char *uname;  /* reference -- do not free */
    char *gname;  /* reference -- do not free */
    struct stat statbuf;
    struct file_entry *next;
};

static int add_file(struct file_entry **festack, const char *name,
		    int isdoc, int isconf, int isdir, int verify_flags,
		    char *Pmode, char *Uname, char *Gname, char *prefix);
static int compare_fe(const void *ap, const void *bp);
static int add_file_aux(const char *file, struct stat *sb, int flag);
static int glob_error(const char *foo, int bar);
static int glob_pattern_p (char *pattern);
static int parseForVerify(char *buf, int *verify_flags);
static int parseForConfig(char *buf, int *conf);
static int parseForAttr(char *origbuf, char **currPmode,
			char **currUname, char **currGname);

static void resetDocdir(void);
static void addDocdir(char *dirname);
static int isDoc(char *filename);

static int processFileListFailed;

int process_filelist(Header header, struct PackageRec *pr,
		     StringBuf sb, int *size, char *name,
		     char *version, char *release, int type,
		     char *prefix, char *specFile)
{
    char buf[1024];
    char **files, **fp;
    struct file_entry *fes, *fest;
    struct file_entry **file_entry_array;
    int isdoc, conf, isdir, verify_flags;
    char *currPmode=NULL;	/* hold info from %attr() */
    char *currUname=NULL;	/* hold info from %attr() */
    char *currGname=NULL;	/* hold info from %attr() */
    char *filename, *s;
    char *str;
    int count = 0;
    int c, x;
    glob_t glob_result;
    int special_doc;
    int passed_special_doc = 0;
    FILE *file;
    int tc;
    char *tcs;
    int currentTime;

    processFileListFailed = 0;
    fes = NULL;
    *size = 0;

    resetDocdir();

    if (type == RPMLEAD_BINARY && pr->fileFile) {
	sprintf(buf, "%s/%s/%s", rpmGetVar(RPMVAR_BUILDDIR),
		build_subdir, pr->fileFile);
	rpmMessage(RPMMESS_DEBUG, "Reading file names from: %s\n", buf);
	if ((file = fopen(buf, "r")) == NULL) {
	    rpmError(RPMERR_BADSPEC, "unable to open filelist: %s\n", buf);
	    return(RPMERR_BADSPEC);
	}
	while (fgets(buf, sizeof(buf), file)) {
	    expandMacros(buf);
	    appendStringBuf(sb, buf);
	}
	fclose(file);
    }
    
    str = getStringBuf(sb);
    files = splitString(str, strlen(str), '\n');
    fp = files;

    while (*fp) {
	strcpy(buf, *fp);  /* temp copy */
	isdoc = 0;
	special_doc = 0;
	conf = 0;
	isdir = 0;
	if (currPmode) {
	    free (currPmode);
	    currPmode = NULL;
	}
	if (currUname) {
	    free (currUname);
	    currUname = NULL;
	}
	if (currGname) {
	    free (currGname);
	    currGname = NULL;
	}
	verify_flags = RPMVERIFY_ALL;
	filename = NULL;

	/* First preparse buf for %verify() */
	if (parseForVerify(buf, &verify_flags)) {
	    processFileListFailed = 1;
	    fp++; continue;
	}
	
	/* Next parse for %attr() */
	if (parseForAttr(buf, &currPmode, &currUname, &currGname)) {
	    processFileListFailed = 1;
	    fp++; continue;
	}

	/* Then parse for %config or %config() */
	if (parseForConfig(buf, &conf)) {
	    processFileListFailed = 1;
	    fp++; continue;
	}

	s = strtok(buf, " \t\n");
	while (s) {
	    if (!strcmp(s, "%docdir")) {
	        s = strtok(NULL, " \t\n");
		addDocdir(s);
		break;
	    } else if (!strcmp(s, "%doc")) {
		isdoc = 1;
	    } else if (!strcmp(s, "%dir")) {
		isdir = 1;
	    } else {
		if (filename) {
		    /* We already got a file -- error */
		    rpmError(RPMERR_BADSPEC,
			  "Two files on one line: %s", filename);
		    processFileListFailed = 1;
		}
		if (*s != '/') {
		    if (isdoc) {
			special_doc = 1;
		    } else {
			/* not in %doc, does not begin with / -- error */
			rpmError(RPMERR_BADSPEC,
			      "File must begin with \"/\": %s", s);
			processFileListFailed = 1;
		    }
		} else {
		    filename = s;
		}
	    }
	    s = strtok(NULL, " \t\n");
	}
	if (special_doc) {
	    if (passed_special_doc) {
{'fraction_non_alphanumeric': 0.1485419864064898, 'fraction_numerical': 0.006796755097566323, 'mean_word_length': 3.052865393158596, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 17, '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': '2521223', 'n_tokens_mistral': 3588, 'n_tokens_neox': 3336, 'n_words': 939}