25 int tag[15] = { RPMTAG_NAME,
51 void EscapeString (
char *sourceString,
char *escString,
int esclen)
59 char *cp = (
char *)sourceString;
62 if (*cp ==
'\\') *cp =
' ';
70 len = strlen(sourceString);
84 PQescapeStringConn(
db_conn, escString, sourceString, len, &error);
86 printf(
"WARNING: %s line %d: Error escaping string with multibype character set?\n",__FILE__, __LINE__ );
102 char *Value,
int ValueMax,
char Separator)
107 memset(Field,0,FieldMax);
108 memset(Value,0,ValueMax);
110 while(isspace(Sin[0])) Sin++;
111 if (Sin[0]==
'\0')
return(NULL);
114 for(
s=0; (Sin[
s] !=
'\0') && !isspace(Sin[
s]) && (Sin[
s] !=
'=');
s++)
118 while(isspace(Sin[
s]))
s++;
119 if (Sin[
s] != Separator)
123 if (Sin[
s]==
'\0')
return(NULL);
125 while(isspace(Sin[
s]))
s++;
126 if (Sin[
s]==
'\0')
return(NULL);
129 if ((Sin[
s]==
'\'') || (Sin[
s]==
'"'))
133 if (Sin[
s]==
'\0')
return(NULL);
137 for( ; (Sin[
s] !=
'\0') && (Sin[
s] != GotQuote);
s++)
139 if (Sin[
s]==
'\\') Value[v++]=Sin[++
s];
140 else Value[v++]=Sin[
s];
146 for( ; (Sin[
s] !=
'\0') && !isspace(Sin[
s]);
s++)
148 if (Sin[
s]==
'\\') Value[v++]=Sin[++
s];
149 else Value[v++]=Sin[
s];
152 while(isspace(Sin[
s]))
s++;
168 int debmimetypepk = 0;
169 int debsrcmimetypepk = 0;
180 rpmReadConfigFiles(NULL, NULL);
186 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-rpm' LIMIT 1;");
187 result = PQexec(
db_conn, sqlbuf);
194 mimetypepk = atoi(PQgetvalue(result, 0, 0));
196 if ( mimetypepk == 0 )
198 snprintf(sqlbuf,
sizeof(sqlbuf),
"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-rpm');");
199 result = PQexec(
db_conn, sqlbuf);
206 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-rpm' LIMIT 1;");
207 result = PQexec(
db_conn, sqlbuf);
214 mimetypepk = atoi(PQgetvalue(result, 0, 0));
216 if ( mimetypepk == 0 )
218 LOG_ERROR(
"pkgagent rpm mimetype not installed!");
224 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-package' LIMIT 1;");
225 result = PQexec(
db_conn, sqlbuf);
232 debmimetypepk = atoi(PQgetvalue(result, 0, 0));
234 if ( debmimetypepk == 0 )
236 snprintf(sqlbuf,
sizeof(sqlbuf),
"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-debian-package');");
237 result = PQexec(
db_conn, sqlbuf);
244 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-package' LIMIT 1;");
245 result = PQexec(
db_conn, sqlbuf);
252 debmimetypepk = atoi(PQgetvalue(result, 0, 0));
254 if ( debmimetypepk == 0 )
256 LOG_ERROR(
"pkgagent deb mimetype not installed!");
262 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-source' LIMIT 1;");
263 result = PQexec(
db_conn, sqlbuf);
270 debsrcmimetypepk = atoi(PQgetvalue(result, 0, 0));
272 if ( debsrcmimetypepk == 0 )
274 snprintf(sqlbuf,
sizeof(sqlbuf),
"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-debian-source');");
275 result = PQexec(
db_conn, sqlbuf);
282 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-source' LIMIT 1;");
283 result = PQexec(
db_conn, sqlbuf);
290 debsrcmimetypepk = atoi(PQgetvalue(result, 0, 0));
292 if ( debsrcmimetypepk == 0 )
294 LOG_ERROR(
"pkgagent deb source mimetype not installed!");
310 snprintf(sqlbuf,
sizeof(sqlbuf),
311 "SELECT pfile_pk as pfile_pk, pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfilename, mimetype_name as mimetype from pfile, mimetype, (SELECT distinct(pfile_fk) as PF from %s where upload_fk='%ld') as SS where PF=pfile_pk and (pfile_mimetypefk='%d' or pfile_mimetypefk='%d' OR pfile_mimetypefk='%d') and mimetype_pk=pfile_mimetypefk and (not exists (SELECT 1 from pkg_rpm where pkg_rpm.pfile_fk = pfile_pk)) and (not exists (SELECT 1 from pkg_deb where pkg_deb.pfile_fk = pfile_pk))",
uploadtree_tablename,
upload_pk, mimetypepk, debmimetypepk, debsrcmimetypepk);
312 result = PQexec(
db_conn, sqlbuf);
320 numrows = PQntuples(result);
321 for (i=0; i<numrows; i++)
323 char *repFile = NULL;
328 strcpy(mimetype, PQgetvalue(result, i, 2));
334 if (!strcasecmp(mimetype,
"application/x-rpm")) {
335 pi->
pFileFk = atoi(PQgetvalue(result, i, 0));
336 strncpy(pi->
pFile, PQgetvalue(result, i, 1),
sizeof(pi->
pFile)-1);
340 LOG_FATAL(
"pfile %ld PkgAgent unable to open file %s",
353 else if (!strcasecmp(mimetype,
"application/x-debian-package")){
354 dpi->
pFileFk = atoi(PQgetvalue(result, i, 0));
355 strncpy(dpi->
pFile, PQgetvalue(result, i, 1),
sizeof(dpi->
pFile)-1);
366 else if (!strcasecmp(mimetype,
"application/x-debian-source")){
367 dpi->
pFileFk = atoi(PQgetvalue(result, i, 0));
368 strncpy(dpi->
pFile, PQgetvalue(result, i, 1),
sizeof(dpi->
pFile)-1);
372 LOG_FATAL(
"pfile %ld PkgAgent unable to open file %s\n",
385 printf(
"LOG: Not RPM and DEBIAN package!\n");
412 rpm_count_t data_size;
414 for (i = 0; i < 14; i++) {
415 memset(fmt, 0,
sizeof(fmt));
417 strcat( fmt, rpmTagGetName(
tag[i]));
420 msgstr = headerFormat(header, fmt, &errstr);
423 printf(
"%s:%s\n",rpmTagGetName(
tag[i]),msgstr);
443 case RPMTAG_PACKAGER:
449 case RPMTAG_BUILDTIME:
461 case RPMTAG_SOURCERPM:
467 case RPMTAG_DESCRIPTION:
474 free((
void *)msgstr);
477 header_status = headerGet(header,
tag[14], &req, HEADERGET_DEFAULT);
479 data_size = rpmtdCount(&req);
480 pi->
requires = calloc(data_size,
sizeof(
char *));
481 for (j=0; j<(int)data_size;j++){
482 const char * temp = rpmtdNextString(&req);
516 vsflags = RPMVSF_DEFAULT;
517 ts = (rpmts) rpmtsCreate();
520 if ( fd == NULL ||Ferror(fd)){
521 rpmlog(RPMLOG_ERR,
"open of %s failed: %s\n", pkg, Fstrerror(fd));
528 vsflags |= _RPMVSF_NOSIGNATURES;
529 vsflags |= _RPMVSF_NODIGESTS;
530 vsflags |= RPMVSF_NOHDRCHK;
531 vsflags |= RPMVSF_NEEDPAYLOAD;
533 rpmtsSetVSFlags(ts, vsflags);
536 rpmrc = rpmReadPackageFile(ts, fd, pkg,&header);
538 ts = (rpmts) rpmtsFree(ts);
543 case RPMRC_NOTTRUSTED:
548 rpmlog(RPMLOG_ERR,
"%s cannot be read or is not an RPM.\n", pkg);
553 header = headerFree(header);
569 memset(
SQL,0,
sizeof(
SQL));
570 snprintf(
SQL,
sizeof(
SQL),
"SELECT pfile_fk FROM pkg_rpm WHERE pfile_fk = %ld;",pi->
pFileFk);
573 if (PQntuples(result) <=0)
576 memset(
SQL,0,
sizeof(
SQL));
577 result = PQexec(
db_conn,
"BEGIN;");
581 snprintf(
SQL,
sizeof(
SQL),
"INSERT INTO pkg_rpm (pkg_name,pkg_alias,pkg_arch,version,rpm_filename,license,pkg_group,packager,release,build_date,vendor,url,source_rpm,summary,description,pfile_fk) values (E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',%ld);",
594 result = PQexec(
db_conn,
"SELECT currval('pkg_rpm_pkg_pk_seq'::regclass);");
596 pkg_pk = atoi(PQgetvalue(result,0,0));
599 if (
Verbose) { printf(
"pkg_pk:%d\n",pkg_pk);}
603 memset(
SQL,0,
sizeof(
SQL));
604 snprintf(
SQL,
sizeof(
SQL),
"INSERT INTO pkg_rpm_req (pkg_fk,req_value) values (%d,E'%s');",pkg_pk,
trim(pi->
requires[i]));
613 result = PQexec(
db_conn,
"COMMIT;");
633 memset(Field,0,MAXCMD);
634 memset(Value,0,MAXCMD);
639 for(
s=0; (Sin[
s] !=
'\0') && !isspace(Sin[
s]) && (Sin[
s] !=
':');
s++)
643 while(isspace(Sin[
s]))
s++;
649 while(isspace(Sin[
s]))
s++;
651 for( ; Sin[
s] !=
'\0';
s++)
655 if (
Verbose) { printf(
"Field is %s and Value is %s", Field, Value);}
659 if (
Verbose) { printf(
"ExValue is %s", Sin);}
676 unsigned long lft, rgt;
691 snprintf(
SQL,
sizeof(
SQL),
"SELECT lft,rgt FROM %s WHERE upload_fk = %ld AND pfile_fk = %ld limit 1",
695 if (PQntuples(result) == 0)
697 LOG_ERROR(
"Missing debian package (internal data inconsistancy). SQL: %s\n",
SQL);
701 lft = strtoul(PQgetvalue(result,0,0), NULL, 10);
702 rgt = strtoul(PQgetvalue(result,0,1), NULL, 10);
705 snprintf(
SQL,
sizeof(
SQL),
"SELECT pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size FROM pfile, uploadtree where (pfile_pk=pfile_fk) and (upload_fk = %ld) AND (lft > %ld) AND (rgt < %ld) AND (ufile_name = 'control')",
709 if (PQntuples(result) > 0)
711 filename = PQgetvalue(result,0,0);
714 LOG_FATAL(
"PkgAgent unable to open file %s\n",filename);
722 printf(
"LOG: Unable to find debian/control file! This file had wrong mimetype, ignore it!\n");
723 memset(
SQL,0,
sizeof(
SQL));
724 snprintf(
SQL,
sizeof(
SQL),
"UPDATE pfile SET pfile_mimetypefk = NULL WHERE pfile_pk = %ld;", pi->
pFileFk);
732 if ((fp = fopen(repfile,
"r")) == NULL){
733 LOG_FATAL(
"Unable to open debian/control file %s\n",repfile);
737 while (fgets(line,MAXCMD,fp)!=NULL)
741 if (!strcasecmp(field,
"Description")) {
746 if ((strlen(temp) + strlen(
s)) >= MAXCMD )
751 if (!strcasecmp(field,
"Package")) {
754 if (!strcasecmp(field,
"Version")) {
757 if (!strcasecmp(field,
"Architecture")) {
760 if (!strcasecmp(field,
"Maintainer")) {
763 if (!strcasecmp(field,
"Installed-Size")) {
766 if (!strcasecmp(field,
"Section")) {
769 if (!strcasecmp(field,
"Priority")) {
772 if (!strcasecmp(field,
"Homepage")) {
775 if (!strcasecmp(field,
"Source")) {
778 if (!strcasecmp(field,
"Depends")) {
779 char *depends = NULL;
780 char tempvalue[MAXCMD];
784 if (value[0] !=
'\0'){
785 strncpy(tempvalue, value,
sizeof(tempvalue));
786 depends = strtok(value,
",");
787 while (depends && (depends[0] !=
'\0')) {
788 if (strlen(depends) >= length)
789 length = strlen(depends) + 1;
790 depends = strtok(NULL,
",");
793 if (
Verbose) { printf(
"SIZE:%d\n", size);}
795 pi->
depends = calloc(size,
sizeof(
char *));
796 pi->
depends[0] = calloc(length,
sizeof(
char));
797 strcpy(pi->
depends[0],strtok(tempvalue,
","));
798 for (i=1;i<size;i++){
799 pi->
depends[i] = calloc(length,
sizeof(
char));
800 strcpy(pi->
depends[i],strtok(NULL,
","));
824 memset(
SQL,0,
sizeof(
SQL));
825 snprintf(
SQL,
sizeof(
SQL),
"SELECT pfile_fk FROM pkg_deb WHERE pfile_fk = %ld;",pi->
pFileFk);
828 if (PQntuples(result) <=0)
831 memset(
SQL,0,
sizeof(
SQL));
832 result = PQexec(
db_conn,
"BEGIN;");
836 snprintf(
SQL,
sizeof(
SQL),
"INSERT INTO pkg_deb (pkg_name,pkg_arch,version,maintainer,installed_size,section,priority,homepage,source,summary,description,format,uploaders,standards_version,pfile_fk) values (E'%s',E'%s',E'%s',E'%s',%d,E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',E'%s',%ld);",
843 if ((result==0) || ((PQresultStatus(result) != PGRES_COMMAND_OK)))
845 LOG_FATAL(
"Error inserting, SQL is: %s, error code is:%s\n",
SQL, PQresultErrorField(result, PG_DIAG_SQLSTATE));
847 if (result) PQclear(result);
854 result = PQexec(
db_conn,
"SELECT currval('pkg_deb_pkg_pk_seq'::regclass);");
856 pkg_pk = atoi(PQgetvalue(result,0,0));
859 if (
Verbose) { printf(
"pkg_pk:%d\n",pkg_pk);}
863 memset(
SQL,0,
sizeof(
SQL));
864 snprintf(
SQL,
sizeof(
SQL),
"INSERT INTO pkg_deb_req (pkg_fk,req_value) values (%d,E'%s');",pkg_pk,
trim(pi->
depends[i]));
874 result = PQexec(
db_conn,
"COMMIT;");
900 if ((fp = fopen(repFile,
"r")) == NULL){
901 LOG_FATAL(
"Unable to open .dsc file %s\n",repFile);
905 while (fgets(line,MAXCMD,fp)!=NULL)
910 if (!strcasecmp(field,
"Format")) {
913 if (!strcasecmp(field,
"Source")) {
916 if (!strcasecmp(field,
"Source")) {
919 if (!strcasecmp(field,
"Architecture")) {
922 if (!strcasecmp(field,
"Version")) {
926 if (!strcasecmp(field,
"Maintainer")) {
929 if (!strcasecmp(field,
"Homepage")) {
932 if (!strcasecmp(field,
"Uploaders")) {
935 if (!strcasecmp(field,
"Standards-Version")) {
938 if (!strcasecmp(field,
"Build-Depends")) {
939 char *depends = NULL;
940 char tempvalue[MAXCMD];
944 if (value[0] !=
'\0'){
945 strncpy(tempvalue, value,
sizeof(tempvalue));
946 depends = strtok(value,
",");
947 while (depends && (depends[0] !=
'\0')) {
948 if (strlen(depends) >= length)
949 length = strlen(depends) + 1;
950 depends = strtok(NULL,
",");
953 if (
Verbose) { printf(
"SIZE:%d\n", size);}
955 pi->
depends = calloc(size,
sizeof(
char *));
956 pi->
depends[0] = calloc(length,
sizeof(
char));
957 strcpy(pi->
depends[0],strtok(tempvalue,
","));
958 for (i=1;i<size;i++){
959 pi->
depends[i] = calloc(length,
sizeof(
char));
960 strcpy(pi->
depends[i],strtok(NULL,
","));
982 printf(
"Usage: %s [options] [file [file [...]]\n",Name);
983 printf(
" -i :: initialize the database, then exit.\n");
984 printf(
" -v :: verbose (-vv = more verbose)\n");
985 printf(
" -c :: Specify the directory for the system configuration.\n");
986 printf(
" -C :: run from command line.\n");
987 printf(
" file :: if files are rpm package listed, display their package information.\n");
988 printf(
" no file :: process data from the scheduler.\n");
989 printf(
" -V :: print the version info, then exit.\n");
char SQL[256]
SQL query to execute.
char * uploadtree_tablename
upload.uploadtree_tablename
int s
The socket that the CLI will use to communicate.
char * trim(char *ptext)
Trimming whitespace.
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
void fo_scheduler_heart(int i)
This function must be called by agents to let the scheduler know they are alive and how many items th...
#define MAXLENGTH
Buffer length.
char * ParseDebFile(char *Sin, char *Field, char *Value)
Parse Debian binary control file with Field/Value pairs.
void Usage(char *Name)
Say how to run this program.
char * GetFieldValue(char *Sin, char *Field, int FieldMax, char *Value, int ValueMax, char Separator)
Given a string that contains field='value' pairs, save the items.
int GetMetadata(char *pkg, struct rpmpkginfo *pi)
Get RPM package info.
int GetMetadataDebSource(char *repFile, struct debpkginfo *pi)
Get debian source package info from .dsc file.
void EscapeString(char *sourceString, char *escString, int esclen)
Escaping special characters(single quote)
int ProcessUpload(long upload_pk)
Get all pfile need to processed use upload_pk.
PGconn * db_conn
The connection to Database.
int RecordMetadataDEB(struct debpkginfo *pi)
Store debian package info into database.
int Verbose
Verbose level.
int RecordMetadataRPM(struct rpmpkginfo *pi)
Store rpm package info into database.
int GetMetadataDebBinary(long upload_pk, struct debpkginfo *pi)
Get debian binary package info.
void ReadHeaderInfo(Header header, struct rpmpkginfo *pi)
Get RPM package info from rpm file header use rpm library.
Holds meta info of Debian packages.
char section[MAXCMD]
Package section.
char priority[MAXCMD]
Package priority.
char standardsVersion[MAXCMD]
Package standards version.
long pFileFk
Package pfile in FOSSology.
char ** depends
Package dependency list.
char summary[MAXCMD]
Package summary.
char pFile[MAXCMD]
Package pfile hash.
char version[MAXCMD]
Package version.
char format[MAXCMD]
Package format.
char uploaders[MAXCMD]
Package contributors.
char maintainer[MAXCMD]
Package maintainer.
int installedSize
Size of package after install.
char pkgArch[MAXCMD]
Package architecture.
char description[MAXCMD]
Package description.
int dep_size
Package dependency list size.
char source[MAXCMD]
Package source.
char pkgName[MAXCMD]
Package name.
char homepage[MAXCMD]
Package link.
Holds meta info of rpm packages.
char buildDate[128]
Package build date.
char pFile[MAXCMD]
Package pfile hash.
char group[128]
Package group.
char version[64]
Package version.
char summary[MAXCMD]
Package summary.
char pkgName[256]
RPM package name.
char description[MAXCMD]
Package description.
char rpmFilename[256]
RPM file name.
char license[512]
RPM licenses.
char pkgArch[64]
Package architecture.
char sourceRPM[256]
Package source.
int req_size
Package dependency list size.
char pkgAlias[256]
Package alias.
char url[256]
Package link.
long pFileFk
Package pfile in FOSSology.
char release[64]
Package release.
char packager[1024]
Packager.
char ** requires
Package dependency list.
char vendor[128]
Package vendor.