Move read_configuration from distromatic.c to functions.c

This commit is contained in:
Silvan Calarco 2013-10-06 19:53:57 +02:00
parent 36ed99b46e
commit d220dd75b0
4 changed files with 269 additions and 273 deletions

View File

@ -101,21 +101,10 @@ static int clearRecursionFlag(struct headerList *headerlist);
static int resolveRecursiveDependencies(
struct headerList *headerlist);
struct configTag*
findRepositoryByTag(const char *tag);
static int read_configuration(const char *confFile, const char *tag);
int compareRequiredList(const void *ptr1, const void *ptr2);
int handleObsoletedPackages(struct configTag *ct, int arch);
static struct configDefaults configdefaults;
static struct configTag *firstconfigtag = NULL, *configtag = NULL;
static const unsigned int bufsize = 1024;
static const unsigned int maxlinelenght = 1024;
static const char *copyright[] = {
PROGRAMNAME " version " PROGRAMVERSION,
"Copyright (C) 2004-2013 by Silvan Calarco <silvan.calarco@mambasoft.it>",
@ -848,268 +837,6 @@ resolveRecursiveDependencies(struct headerList *headerlist)
return 0;
}
struct configTag*
findRepositoryByTag(const char *tag)
{
struct configTag *ct = firstconfigtag;
while ((ct) && (strncmp(ct->tag,tag,PATH_MAX))) ct = ct->next;
if (ct) return ct; else return NULL;
}
static int
read_configuration(const char *confFile, const char *tag)
{
FILE *fin;
char *vartok, *valuetok;
char input[maxlinelenght];
char buf[bufsize];
int i, j, curraltrep = 0;
struct configTag *newconfigtag, *currconfigtag = NULL;
struct Packager *currmaintainer;
if ((fin = fopen(confFile, "r")) == NULL) {
return 1;
}
unsigned int configsection = 0;
configdefaults.html_basedir = NULL;
configdefaults.arch[0] = DEFAULT_ARCH;
for (i = 1; i<ARCHS_MAX; i++)
configdefaults.arch[i] = NULL;
while (fgets(input, maxlinelenght, fin)) {
if (input[0] == '#') {
continue;
}
vartok = (char *) strtok(input, "=\n");
if (!vartok) { /* skip blank lines */
continue;
}
strip_separators(vartok, " \t\n");
valuetok = (char *) strtok(NULL, "\n");
strip_separators(valuetok, " \t\n");
if ((vartok[0]=='[') && (vartok[strlen(vartok)-1]==']')) {
/* found a `[defaults]' tag */
if (!strncmp(&vartok[1], "defaults", strlen(vartok)-2)) {
configsection = CONF_DEFAULTS_SECTION;
} else if (!strncmp(&vartok[1], "maintainers", strlen(vartok)-2)) {
configsection = CONF_MAINTAINERS_SECTION;
} else {
configsection = CONF_REP_SECTION;
newconfigtag = malloc(sizeof(struct configTag));
if (!newconfigtag) {
fprintf(stderr, "The system is out of memory\n");
return 1;
}
newconfigtag->tag =
(char *) strndup(&vartok[1], strlen(vartok) - 2);
newconfigtag->repository_dir = NULL;
newconfigtag->repository_source_dir = NULL;
curraltrep = 0;
for (i = 0; i<ALT_REPS_MAX; i++) newconfigtag->repository[i] = NULL;
newconfigtag->html_dir = NULL;
newconfigtag->download_prefix = NULL;
newconfigtag->download_dir = NULL;
newconfigtag->showfile_prefix = NULL;
for (i = 0; i<ARCHS_MAX; i++) {
newconfigtag->arch[i] = configdefaults.arch[i];
newconfigtag->headerlist[i] = NULL;
newconfigtag->filetree[i]=NULL;
for (j = 0; j<PROVIDEDLIST_IDX_SIZE; j++)
newconfigtag->providedlist_idx[i][j]=NULL;
}
newconfigtag->configdefaults = &configdefaults;
newconfigtag->headersourcelist = NULL;
if (configdefaults.html_basedir) {
strncpy(buf, configdefaults.html_basedir, bufsize);
strncat(buf, newconfigtag->tag, bufsize);
strncat(buf, "/", bufsize);
newconfigtag->html_dir = (char *) strdup(buf);
}
if ((!tag) || (!strcmp(newconfigtag->tag, tag))) {
configtag = newconfigtag;
}
if (!currconfigtag) {
firstconfigtag = newconfigtag;
} else {
currconfigtag->next = newconfigtag;
}
currconfigtag = newconfigtag;
}
} else if (configsection == CONF_REP_SECTION) {
/* we are reading inside a repository section... */
if (!strcmp(vartok, "REPOSITORY_DIR")) {
currconfigtag->repository_dir =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "PARENT")) {
currconfigtag->repository[curraltrep]=findRepositoryByTag(valuetok);
if (!currconfigtag->repository[curraltrep]) {
fprintf(stderr,"Error: repository %s requested in %s was not previously configured; aborting.\n",
valuetok, currconfigtag->tag);
exit(1);
}
curraltrep++;
} else if (!strcmp(vartok, "HTML_DIR")) {
currconfigtag->html_dir = (char *) strdup(valuetok);
} else if (!strcmp(vartok, "DOWNLOAD_PREFIX")) {
currconfigtag->download_prefix = (char *) strdup(valuetok);
} else if (!strcmp(vartok, "DOWNLOAD_DIR")) {
currconfigtag->download_dir = (char *) strdup(valuetok);
} else if (!strcmp(vartok, "SHOWFILE_PREFIX")) {
currconfigtag->showfile_prefix = (char *) strdup(valuetok);
} else if (!strcmp(vartok, "REPOSITORY_SOURCE_DIR")) {
currconfigtag->repository_source_dir =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "DESCRIPTION")) {
currconfigtag->description =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "ARCHS")) {
vartok = (char *) strtok(valuetok, " ");
i = 0;
while ((vartok) && (i < ARCHS_MAX)) {
currconfigtag->arch[i] = malloc(sizeof vartok);
if (!currconfigtag->arch[i]) {
fprintf(stderr, "The system is out of memory\n");
exit(1);
}
strcpy(currconfigtag->arch[i], vartok);
vartok = strtok(NULL, " ");
i++;
}
if (vartok) {
fprintf(stderr,
"Error: exceeding number of archs defined; maximum is %d.",ARCHS_MAX);
return 1;
} else {
currconfigtag->arch[i] = NULL;
}
} else {
fprintf(stderr, "Undefined token: %s\n", vartok);
return 1;
}
} else if (configsection == CONF_DEFAULTS_SECTION) {
/* we are in the default section */
if (!strcmp(vartok, "HTML_BASEDIR")) {
configdefaults.html_basedir =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "DISTRIBUTION_NAME")) {
configdefaults.distribution_name =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "URL_ADDRESS")) {
configdefaults.url_address =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "URL_PREFIX")) {
configdefaults.url_prefix =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "URL_DIR")) {
configdefaults.url_dir =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "ARCHS")) {
vartok = (char *) strtok(valuetok, " ");
i = 0;
while ((vartok) && (i < ARCHS_MAX)) {
configdefaults.arch[i] = malloc(sizeof vartok);
if (!configdefaults.arch[i]) {
fprintf(stderr, "The system is out of memory\n");
exit(1);
}
strcpy(configdefaults.arch[i], vartok);
vartok = strtok(NULL, " ");
i++;
}
if (vartok) {
fprintf(stderr,
"Error: exceeding number of archs defined; maximum is %d.",ARCHS_MAX);
return 1;
}
} else {
fprintf(stderr,
"Invalid token for `defaults' tag: %s\n",
vartok);
return 1;
}
} else if (configsection == CONF_MAINTAINERS_SECTION) {
/* find or create maintainer */
currmaintainer = getPackagerByName(vartok, 1);
if (!currmaintainer) {
fprintf(stderr,
"Error: cannot create maintainers; aborting.");
return 1;
}
currmaintainer->role |= PACKAGER_ROLE_MAINTAINER;
vartok = (char *) strtok(valuetok, "\"");
i = 0;
while ((currmaintainer->alias[i]) && (i < PACKAGER_MAXALIASES)) i++;
while ((vartok) && (i < PACKAGER_MAXALIASES)) {
currmaintainer->alias[i]=strdup(vartok);
vartok = strtok(NULL, "\"");
vartok = strtok(NULL, "\"");
i++;
}
if (vartok) {
fprintf(stderr,
"Error: exceeding number of aliases defined for maintainer %s.",currmaintainer->name);
return 1;
}
}
}
/* check for errors and complete previous tag infos */
for(currconfigtag = firstconfigtag; currconfigtag;
currconfigtag = currconfigtag->next) {
if (!currconfigtag->repository_dir) {
fprintf(stderr,
"REPOSITORY_DIR not given for tag %s\n",
currconfigtag->tag);
return 1;
}
if (!currconfigtag->repository_source_dir) {
strncpy(buf, currconfigtag->repository_dir, bufsize);
strncat(buf, "/SRPMS.base/", bufsize);
currconfigtag->repository_source_dir =
(char *) strdup(buf);
}
currconfigtag->repository_level=0;
for (i = 0; i < ALT_REPS_MAX; i++) {
if (currconfigtag->repository[i]) {
currconfigtag->repository_level=i+1;
}
}
currconfigtag->repository[currconfigtag->repository_level]=currconfigtag;
if (!currconfigtag->description)
currconfigtag->description=currconfigtag->tag;
}
fclose(fin);
return 0;
}
void *threadArchScan(void* arg) {
int arch = *(int *)arg;

View File

@ -69,8 +69,272 @@
# endif
#endif
#include "functions.h"
#include "distromatic.h"
#include "changelog.h"
static int debug_log = 0;
struct configTag* findRepositoryByTag(const char *tag)
{
struct configTag *ct = firstconfigtag;
while ((ct) && (strncmp(ct->tag,tag,PATH_MAX))) ct = ct->next;
if (ct) return ct; else return NULL;
}
int read_configuration(const char *confFile, const char *tag)
{
FILE *fin;
char *vartok, *valuetok;
char input[PATH_MAX];
char buf[PATH_MAX];
int i, j, curraltrep = 0;
struct configTag *newconfigtag, *currconfigtag = NULL;
struct Packager *currmaintainer;
if ((fin = fopen(confFile, "r")) == NULL) {
return 1;
}
unsigned int configsection = 0;
configdefaults.html_basedir = NULL;
configdefaults.arch[0] = DEFAULT_ARCH;
for (i = 1; i<ARCHS_MAX; i++)
configdefaults.arch[i] = NULL;
while (fgets(input, PATH_MAX, fin)) {
if (input[0] == '#') {
continue;
}
vartok = (char *) strtok(input, "=\n");
if (!vartok) { /* skip blank lines */
continue;
}
strip_separators(vartok, " \t\n");
valuetok = (char *) strtok(NULL, "\n");
strip_separators(valuetok, " \t\n");
if ((vartok[0]=='[') && (vartok[strlen(vartok)-1]==']')) {
/* found a `[defaults]' tag */
if (!strncmp(&vartok[1], "defaults", strlen(vartok)-2)) {
configsection = CONF_DEFAULTS_SECTION;
} else if (!strncmp(&vartok[1], "maintainers", strlen(vartok)-2)) {
configsection = CONF_MAINTAINERS_SECTION;
} else {
configsection = CONF_REP_SECTION;
newconfigtag = malloc(sizeof(struct configTag));
if (!newconfigtag) {
fprintf(stderr, "The system is out of memory\n");
return 1;
}
newconfigtag->tag =
(char *) strndup(&vartok[1], strlen(vartok) - 2);
newconfigtag->repository_dir = NULL;
newconfigtag->repository_source_dir = NULL;
curraltrep = 0;
for (i = 0; i<ALT_REPS_MAX; i++) newconfigtag->repository[i] = NULL;
newconfigtag->html_dir = NULL;
newconfigtag->download_prefix = NULL;
newconfigtag->download_dir = NULL;
newconfigtag->showfile_prefix = NULL;
for (i = 0; i<ARCHS_MAX; i++) {
newconfigtag->arch[i] = configdefaults.arch[i];
newconfigtag->headerlist[i] = NULL;
newconfigtag->filetree[i]=NULL;
for (j = 0; j<PROVIDEDLIST_IDX_SIZE; j++)
newconfigtag->providedlist_idx[i][j]=NULL;
}
newconfigtag->configdefaults = &configdefaults;
newconfigtag->headersourcelist = NULL;
if (configdefaults.html_basedir) {
strncpy(buf, configdefaults.html_basedir, PATH_MAX);
strncat(buf, newconfigtag->tag, PATH_MAX);
strncat(buf, "/", PATH_MAX);
newconfigtag->html_dir = (char *) strdup(buf);
}
if ((!tag) || (!strcmp(newconfigtag->tag, tag))) {
configtag = newconfigtag;
}
if (!currconfigtag) {
firstconfigtag = newconfigtag;
} else {
currconfigtag->next = newconfigtag;
}
currconfigtag = newconfigtag;
}
} else if (configsection == CONF_REP_SECTION) {
/* we are reading inside a repository section... */
if (!strcmp(vartok, "REPOSITORY_DIR")) {
currconfigtag->repository_dir =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "PARENT")) {
currconfigtag->repository[curraltrep]=findRepositoryByTag(valuetok);
if (!currconfigtag->repository[curraltrep]) {
fprintf(stderr,"Error: repository %s requested in %s was not previously configured; aborting.\n",
valuetok, currconfigtag->tag);
exit(1);
}
curraltrep++;
} else if (!strcmp(vartok, "HTML_DIR")) {
currconfigtag->html_dir = (char *) strdup(valuetok);
} else if (!strcmp(vartok, "DOWNLOAD_PREFIX")) {
currconfigtag->download_prefix = (char *) strdup(valuetok);
} else if (!strcmp(vartok, "DOWNLOAD_DIR")) {
currconfigtag->download_dir = (char *) strdup(valuetok);
} else if (!strcmp(vartok, "SHOWFILE_PREFIX")) {
currconfigtag->showfile_prefix = (char *) strdup(valuetok);
} else if (!strcmp(vartok, "REPOSITORY_SOURCE_DIR")) {
currconfigtag->repository_source_dir =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "DESCRIPTION")) {
currconfigtag->description =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "ARCHS")) {
vartok = (char *) strtok(valuetok, " ");
i = 0;
while ((vartok) && (i < ARCHS_MAX)) {
currconfigtag->arch[i] = malloc(sizeof vartok);
if (!currconfigtag->arch[i]) {
fprintf(stderr, "The system is out of memory\n");
exit(1);
}
strcpy(currconfigtag->arch[i], vartok);
vartok = strtok(NULL, " ");
i++;
}
if (vartok) {
fprintf(stderr,
"Error: exceeding number of archs defined; maximum is %d.",ARCHS_MAX);
return 1;
} else {
currconfigtag->arch[i] = NULL;
}
} else {
fprintf(stderr, "Undefined token: %s\n", vartok);
return 1;
}
} else if (configsection == CONF_DEFAULTS_SECTION) {
/* we are in the default section */
if (!strcmp(vartok, "HTML_BASEDIR")) {
configdefaults.html_basedir =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "DISTRIBUTION_NAME")) {
configdefaults.distribution_name =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "URL_ADDRESS")) {
configdefaults.url_address =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "URL_PREFIX")) {
configdefaults.url_prefix =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "URL_DIR")) {
configdefaults.url_dir =
(char *) strdup(valuetok);
} else if (!strcmp(vartok, "ARCHS")) {
vartok = (char *) strtok(valuetok, " ");
i = 0;
while ((vartok) && (i < ARCHS_MAX)) {
configdefaults.arch[i] = malloc(sizeof vartok);
if (!configdefaults.arch[i]) {
fprintf(stderr, "The system is out of memory\n");
exit(1);
}
strcpy(configdefaults.arch[i], vartok);
vartok = strtok(NULL, " ");
i++;
}
if (vartok) {
fprintf(stderr,
"Error: exceeding number of archs defined; maximum is %d.",ARCHS_MAX);
return 1;
}
} else {
fprintf(stderr,
"Invalid token for `defaults' tag: %s\n",
vartok);
return 1;
}
} else if (configsection == CONF_MAINTAINERS_SECTION) {
/* find or create maintainer */
currmaintainer = getPackagerByName(vartok, 1);
if (!currmaintainer) {
fprintf(stderr,
"Error: cannot create maintainers; aborting.");
return 1;
}
currmaintainer->role |= PACKAGER_ROLE_MAINTAINER;
vartok = (char *) strtok(valuetok, "\"");
i = 0;
while ((currmaintainer->alias[i]) && (i < PACKAGER_MAXALIASES)) i++;
while ((vartok) && (i < PACKAGER_MAXALIASES)) {
currmaintainer->alias[i]=strdup(vartok);
vartok = strtok(NULL, "\"");
vartok = strtok(NULL, "\"");
i++;
}
if (vartok) {
fprintf(stderr,
"Error: exceeding number of aliases defined for maintainer %s.",currmaintainer->name);
return 1;
}
}
}
/* check for errors and complete previous tag infos */
for(currconfigtag = firstconfigtag; currconfigtag;
currconfigtag = currconfigtag->next) {
if (!currconfigtag->repository_dir) {
fprintf(stderr,
"REPOSITORY_DIR not given for tag %s\n",
currconfigtag->tag);
return 1;
}
if (!currconfigtag->repository_source_dir) {
strncpy(buf, currconfigtag->repository_dir, PATH_MAX);
strncat(buf, "/SRPMS.base/", PATH_MAX);
currconfigtag->repository_source_dir =
(char *) strdup(buf);
}
currconfigtag->repository_level=0;
for (i = 0; i < ALT_REPS_MAX; i++) {
if (currconfigtag->repository[i]) {
currconfigtag->repository_level=i+1;
}
}
currconfigtag->repository[currconfigtag->repository_level]=currconfigtag;
if (!currconfigtag->description)
currconfigtag->description=currconfigtag->tag;
}
fclose(fin);
return 0;
}
char **
dupnargv(char **argv, int count)
{

View File

@ -19,6 +19,7 @@ struct configDefaults {
char *url_dir;
char *arch[ARCHS_MAX];
};
static struct configDefaults configdefaults;
struct configTag {
struct configDefaults *configdefaults;
@ -40,5 +41,6 @@ struct configTag {
struct headerStats stats;
struct configTag *next;
};
static struct configTag *firstconfigtag = NULL, *configtag = NULL;
#endif

View File

@ -20,6 +20,9 @@ typedef char sizeString[SSSIZE];
#define LOG_DEBUG 4
#define LOG_MARK 8
int read_configuration(const char *confFile, const char *tag);
struct configTag* findRepositoryByTag(const char *tag);
char **dupnargv(char **argv, int count);
void *memndup(void *memp, size_t size);
void log_debug_set(int value);