/*
* rdf.c : implementation for the RDF encoding/decoding of RPM informations.
*
* See Copyright for the status of this software.
*
* $Id: rdf.c,v 1.1.1.1 1998/06/04 04:34:01 daniel Exp $
*/
#include "config.h"
#include <sys/stat.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include "rpmdata.h"
#include "language.h"
#include "rdf_api.h"
#include "rdf.h"
/*
* Open an RDF file call the parser to create a XML tree
* Then walk the tree and build an rpmData structure for
* the corresponding package.
*/
rpmDataPtr rpmOpenRdfFile(char *file) {
char nameBuffer[200];
rdfSchema rdf;
rdfNamespace rpmNs;
rdfNamespace rdfNs;
rdfDescription desc;
rdfBag bag;
rdfElement elem;
char *value;
rpmDataPtr rpm;
struct stat buf;
int i;
rdf = rdfRead(file);
if (rdf == NULL) return(NULL);
/*
* Start the analyze, check that's an RDf for RPM packages.
*/
rdfNs = rdfGetNamespace(rdf, "http://www.w3.org/TR/WD-rdf-syntax#");
if (rdfNs == NULL) {
fprintf(stderr, "%s is not an RDF schema\n", file);
rdfDestroySchema(rdf);
return(NULL);
}
rpmNs = rdfGetNamespace(rdf, "http://www.rpm.org/");
if (rdfNs == NULL) {
fprintf(stderr, "%s is not an RPM specific RDF schema\n", file);
rdfDestroySchema(rdf);
return(NULL);
}
desc = rdfFirstDescription(rdf);
if (rdfNs == NULL) {
fprintf(stderr, "%s RDF schema seems empty\n", file);
rdfDestroySchema(rdf);
return(NULL);
}
/*
* We are pretty sure that it will be a valid schema,
* Allocate the rpmData structure, initialize it.
*/
rpm = (rpmDataPtr) malloc(sizeof(rpmData));
if (rpm == NULL) {
fprintf(stderr, "rpmOpenRdf : out of memory !\n");
rdfDestroySchema(rdf);
return(NULL);
}
memset(rpm, 0, sizeof(rpmData));
stat(file, &buf);
rpm->stamp = buf.st_mtime;
/*
* Now extract all the metadata informations from the RDF tree
*/
rdfGetValue(desc, "Name", rpmNs, &value, NULL);
if (value != NULL) rpm->name = strdup(value);
else {
fprintf(stderr, "%s RDF schema invalid : no Name\n", file);
rdfDestroySchema(rdf);
return(NULL);
}
rdfGetValue(desc, "Version", rpmNs, &value, NULL);
if (value != NULL) rpm->version = strdup(value);
else {
fprintf(stderr, "%s RDF schema invalid : no Version\n", file);
rdfDestroySchema(rdf);
return(NULL);
}
rdfGetValue(desc, "Release", rpmNs, &value, NULL);
if (value != NULL) rpm->release = strdup(value);
else {
fprintf(stderr, "%s RDF schema invalid : no Release\n", file);
rdfDestroySchema(rdf);
return(NULL);
}
rdfGetValue(desc, "URL", rpmNs, &value, NULL);
if (value != NULL) rpm->url = strdup(value);
else rpm->url = NULL;
rdfGetValue(desc, "Arch", rpmNs, &value, NULL);
if (value != NULL) rpm->arch = strdup(value);
else rpm->arch = strdup(localizedStrings[LANG_NONE]);
rdfGetValue(desc, "Os", rpmNs, &value, NULL);
if (value != NULL) rpm->os = strdup(value);
else rpm->os = strdup("linux");
rdfGetValue(desc, "Distribution", rpmNs, &value, NULL);
if (value != NULL) rpm->distribution = strdup(value);
else rpm->distribution = strdup(localizedStrings[LANG_UNKNOWN]);
rdfGetValue(desc, "Vendor", rpmNs, &value, NULL);
if (value != NULL) rpm->vendor = strdup(value);
else rpm->vendor = strdup(localizedStrings[LANG_UNKNOWN]);
rdfGetValue(desc, "Packager", rpmNs, &value, NULL);
if (value != NULL) rpm->packager = strdup(value);
else rpm->packager = NULL;
rdfGetValue(desc, "Group", rpmNs, &value, NULL);
if (value != NULL) rpm->group = strdup(value);
else rpm->group = strdup(localizedStrings[LANG_NO_GROUP]);
rdfGetValue(desc, "Summary", rpmNs, &value, NULL);
if (value != NULL) rpm->summary = strdup(value);
else rpm->summary = strdup(localizedStrings[LANG_NO_SUMMARY]);
rdfGetValue(desc, "Description", rpmNs, &value, NULL);
if (value != NULL) rpm->description = strdup(value);
else rpm->description = strdup(localizedStrings[LANG_NO_DESCRIPTION]);
rdfGetValue(desc, "Copyright", rpmNs, &value, NULL);
if (value != NULL) rpm->copyright = strdup(value);
else rpm->copyright = NULL;
rdfGetValue(desc, "Changelog", rpmNs, &value, NULL);
if (value != NULL) rpm->changelog = strdup(value);
else rpm->changelog = NULL;
rdfGetValue(desc, "Sources", rpmNs, &value, NULL);
if (value != NULL) {
rpm->srcrpm = strdup(value);
} else rpm->srcrpm = strdup("");
rdfGetValue(desc, "Size", rpmNs, &value, NULL);
if (value != NULL) {
if (sscanf(value, "%d", &(rpm->size)) != 1)
rpm->size = 0;
} else rpm->size = 0;
rdfGetValue(desc, "Date", rpmNs, &value, NULL);
if (value != NULL) {
if (sscanf(value, "%d", &i) != 1)
rpm->date = 0;
else
rpm->date = i;
} else rpm->date = 0;
rdfGetValue(desc, "BuildHost", rpmNs, &value, NULL);
if (value != NULL) rpm->host = strdup(value);
else rpm->host = strdup(localizedStrings[LANG_NO_HOST]);
rdfGetValue(desc, "Files", rpmNs, &value, NULL);
if (value != NULL) rpm->filelist = strdup(value);
else rpm->filelist = NULL;
/*
* Fetching packages provided is a bit more tricky, one have to
* find the RDF Bag, and scan it's values.
*/
rpm->nb_resources = 0;
rdfGetValue(desc, "Provides", rpmNs, NULL, &bag);
if (bag != NULL) {
elem = rdfFirstChild(bag);
rpm->max_resources = 5;
rpm->resources = (char * *) malloc(rpm->max_resources *
sizeof(char *));
if (rpm->resources == NULL) {
fprintf(stderr, "rpmOpenRdf : ran out of memory\n");
exit(1);
}
i = 0;
while (elem != NULL) {
if (i >= rpm->max_resources) {
rpm->max_resources *= 2;
rpm->resources = (char * *) realloc(rpm->resources,
rpm->max_resources * sizeof(char *));
if (rpm->resources == NULL) {
fprintf(stderr, "rpmOpenRdf : ran out of memory\n");
exit(1);
}
}
/*
* Check that we are scanning an RPM Resource.
*/
if ((!strcmp(rdfElemGetPropertyName(elem), "Resource")) &&
(rdfElemGetNamespace(elem) == rpmNs)) {
value = rdfElemGetValue(elem);
if (value != NULL) {
rpm->resources[i] = strdup(value);
i++;
rpm->nb_resources++;
} else if (rpmfindVerbose > 1) {
fprintf(stderr, "%s : malformed Resource element !\n", file);
}
} else if (rpmfindVerbose > 1) {
fprintf(stderr, "%s : malformed Provides bag !\n", file);
}
elem = rdfNextElem(elem);
}
} else if (rpmfindVerbose > 1) {
fprintf(stderr, "%s doesn't export any resource\n", file);
}
/*
* idem for the dependencies.
*/
rpm->nb_requires = 0;
rdfGetValue(desc, "Requires", rpmNs, NULL, &bag);
if (bag != NULL) {
elem = rdfFirstChild(bag);
rpm->max_requires = 5;
rpm->requires = (char * *) malloc(rpm->max_requires *
sizeof(char *));
if (rpm->requires == NULL) {
fprintf(stderr, "rpmOpenRdf : ran out of memory\n");
exit(1);
}
i = 0;
while (elem != NULL) {
if (i >= rpm->max_requires) {
rpm->max_requires *= 2;
rpm->requires = (char * *) realloc(rpm->requires,
rpm->max_requires * sizeof(char *));
if (rpm->requires == NULL) {
fprintf(stderr, "rpmOpenRdf : ran out of memory\n");
exit(1);
}
}
/*
* Check that we are scanning an RPM Resource.
*/
if ((!strcmp(rdfElemGetPropertyName(elem), "Resource")) &&
(rdfElemGetNamespace(elem) == rpmNs)) {
value = rdfElemGetValue(elem);
if (value != NULL) {
rpm->requires[i] = strdup(value);
i++;
rpm->nb_requires++;
} else if (rpmfindVerbose > 1) {
fprintf(stderr, "%s : malformed Resource element !\n", file);
}
} else if (rpmfindVerbose > 1) {
fprintf(stderr, "%s : malformed Provides bag !\n", file);
}
elem = rdfNextElem(elem);
}
}
/*
* Finish filling the rpmData structure.
*/
#ifdef HAVE_SNPRINTF
sprintf(nameBuffer, "%s-%s-%s.rpm",
rpm->name, rpm->version, rpm->release);
#else
snprintf(nameBuffer, 200, "%s-%s-%s.rpm",
rpm->name, rpm->version, rpm->release);
#endif
rpm->filename = strdup(nameBuffer);
/*
* Cleanup.
*/
rdfDestroySchema(rdf);
return(rpm);
}
Webmaster