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 debmodernmimetypepk = 0;
170 int debsrcmimetypepk = 0;
181 rpmReadConfigFiles(NULL, NULL);
187 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-rpm' LIMIT 1;");
188 result = PQexec(
db_conn, sqlbuf);
195 mimetypepk = atoi(PQgetvalue(result, 0, 0));
197 if ( mimetypepk == 0 )
199 snprintf(sqlbuf,
sizeof(sqlbuf),
"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-rpm');");
200 result = PQexec(
db_conn, sqlbuf);
207 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-rpm' LIMIT 1;");
208 result = PQexec(
db_conn, sqlbuf);
215 mimetypepk = atoi(PQgetvalue(result, 0, 0));
217 if ( mimetypepk == 0 )
219 LOG_ERROR(
"pkgagent rpm mimetype not installed!");
225 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-package' LIMIT 1;");
226 result = PQexec(
db_conn, sqlbuf);
233 debmimetypepk = atoi(PQgetvalue(result, 0, 0));
235 if ( debmimetypepk == 0 )
237 snprintf(sqlbuf,
sizeof(sqlbuf),
"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-debian-package');");
238 result = PQexec(
db_conn, sqlbuf);
245 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-package' LIMIT 1;");
246 result = PQexec(
db_conn, sqlbuf);
253 debmimetypepk = atoi(PQgetvalue(result, 0, 0));
255 if ( debmimetypepk == 0 )
257 LOG_ERROR(
"pkgagent deb mimetype not installed!");
263 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/vnd.debian.binary-package' LIMIT 1;");
264 result = PQexec(
db_conn, sqlbuf);
271 debmodernmimetypepk = atoi(PQgetvalue(result, 0, 0));
273 if ( debmodernmimetypepk == 0 )
275 snprintf(sqlbuf,
sizeof(sqlbuf),
"INSERT INTO mimetype (mimetype_name) VALUES ('application/vnd.debian.binary-package');");
276 result = PQexec(
db_conn, sqlbuf);
279 printf(
"ERROR: unable to insert modern debian mimetype %s\n", sqlbuf);
284 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/vnd.debian.binary-package' LIMIT 1;");
285 result = PQexec(
db_conn, sqlbuf);
292 debmodernmimetypepk = atoi(PQgetvalue(result, 0, 0));
294 if ( debmodernmimetypepk == 0 )
296 LOG_ERROR(
"pkgagent modern debian mimetype not installed!");
302 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-source' LIMIT 1;");
303 result = PQexec(
db_conn, sqlbuf);
310 debsrcmimetypepk = atoi(PQgetvalue(result, 0, 0));
312 if ( debsrcmimetypepk == 0 )
314 snprintf(sqlbuf,
sizeof(sqlbuf),
"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-debian-source');");
315 result = PQexec(
db_conn, sqlbuf);
322 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-source' LIMIT 1;");
323 result = PQexec(
db_conn, sqlbuf);
330 debsrcmimetypepk = atoi(PQgetvalue(result, 0, 0));
332 if ( debsrcmimetypepk == 0 )
334 LOG_ERROR(
"pkgagent deb source mimetype not installed!");
350 snprintf(sqlbuf,
sizeof(sqlbuf),
351 "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' 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, debmodernmimetypepk);
352 result = PQexec(
db_conn, sqlbuf);
360 numrows = PQntuples(result);
361 for (i=0; i<numrows; i++)
363 char *repFile = NULL;
368 strcpy(mimetype, PQgetvalue(result, i, 2));
374 if (!strcasecmp(mimetype,
"application/x-rpm")) {
375 pi->
pFileFk = atoi(PQgetvalue(result, i, 0));
376 strncpy(pi->
pFile, PQgetvalue(result, i, 1),
sizeof(pi->
pFile)-1);
380 LOG_FATAL(
"pfile %ld PkgAgent unable to open file %s",
393 else if (!strcasecmp(mimetype,
"application/x-debian-package") ||
394 !strcasecmp(mimetype,
"application/vnd.debian.binary-package")){
395 dpi->
pFileFk = atoi(PQgetvalue(result, i, 0));
396 strncpy(dpi->
pFile, PQgetvalue(result, i, 1),
sizeof(dpi->
pFile)-1);
411 else if (!strcasecmp(mimetype,
"application/x-debian-source")){
412 dpi->
pFileFk = atoi(PQgetvalue(result, i, 0));
413 strncpy(dpi->
pFile, PQgetvalue(result, i, 1),
sizeof(dpi->
pFile)-1);
417 LOG_FATAL(
"pfile %ld PkgAgent unable to open file %s\n",
434 printf(
"LOG: Not RPM and DEBIAN package!\n");
461 rpm_count_t data_size;
463 for (i = 0; i < 14; i++) {
464 memset(fmt, 0,
sizeof(fmt));
466 strcat( fmt, rpmTagGetName(
tag[i]));
469 msgstr = headerFormat(header, fmt, &errstr);
472 printf(
"%s:%s\n",rpmTagGetName(
tag[i]),msgstr);
492 case RPMTAG_PACKAGER:
498 case RPMTAG_BUILDTIME:
510 case RPMTAG_SOURCERPM:
516 case RPMTAG_DESCRIPTION:
523 free((
void *)msgstr);
526 header_status = headerGet(header,
tag[14], &req, HEADERGET_DEFAULT);
528 data_size = rpmtdCount(&req);
529 pi->
requires = calloc(data_size,
sizeof(
char *));
530 for (j=0; j<(int)data_size;j++){
531 const char * temp = rpmtdNextString(&req);
533 strncpy(pi->
requires[j], temp, MAXCMD - 1);
566 vsflags = RPMVSF_DEFAULT;
567 ts = (rpmts) rpmtsCreate();
570 if ( fd == NULL ||Ferror(fd)){
571 rpmlog(RPMLOG_ERR,
"open of %s failed: %s\n", pkg, Fstrerror(fd));
578 vsflags |= _RPMVSF_NOSIGNATURES;
579 vsflags |= _RPMVSF_NODIGESTS;
580 vsflags |= RPMVSF_NOHDRCHK;
581 vsflags |= RPMVSF_NEEDPAYLOAD;
583 rpmtsSetVSFlags(ts, vsflags);
586 rpmrc = rpmReadPackageFile(ts, fd, pkg,&header);
588 ts = (rpmts) rpmtsFree(ts);
593 case RPMRC_NOTTRUSTED:
598 rpmlog(RPMLOG_ERR,
"%s cannot be read or is not an RPM.\n", pkg);
603 header = headerFree(header);
619 memset(
SQL,0,
sizeof(
SQL));
620 snprintf(
SQL,
sizeof(
SQL),
"SELECT pfile_fk FROM pkg_rpm WHERE pfile_fk = %ld;",pi->
pFileFk);
623 if (PQntuples(result) <=0)
626 memset(
SQL,0,
sizeof(
SQL));
627 result = PQexec(
db_conn,
"BEGIN;");
631 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);",
644 result = PQexec(
db_conn,
"SELECT currval('pkg_rpm_pkg_pk_seq'::regclass);");
646 pkg_pk = atoi(PQgetvalue(result,0,0));
649 if (
Verbose) { printf(
"pkg_pk:%d\n",pkg_pk);}
653 memset(
SQL,0,
sizeof(
SQL));
654 snprintf(
SQL,
sizeof(
SQL),
"INSERT INTO pkg_rpm_req (pkg_fk,req_value) values (%d,E'%s');",pkg_pk,
trim(pi->
requires[i]));
663 result = PQexec(
db_conn,
"COMMIT;");
683 memset(Field,0,MAXCMD);
684 memset(Value,0,MAXCMD);
689 for(
s=0; (Sin[
s] !=
'\0') && !isspace(Sin[
s]) && (Sin[
s] !=
':');
s++)
693 while(isspace(Sin[
s]))
s++;
699 while(isspace(Sin[
s]))
s++;
701 for( ; Sin[
s] !=
'\0';
s++)
705 if (
Verbose) { printf(
"Field is %s and Value is %s", Field, Value);}
709 if (
Verbose) { printf(
"ExValue is %s", Sin);}
726 unsigned long lft, rgt;
741 snprintf(
SQL,
sizeof(
SQL),
"SELECT lft,rgt FROM %s WHERE upload_fk = %ld AND pfile_fk = %ld limit 1",
745 if (PQntuples(result) == 0)
747 LOG_ERROR(
"Missing debian package (internal data inconsistancy). SQL: %s\n",
SQL);
751 lft = strtoul(PQgetvalue(result,0,0), NULL, 10);
752 rgt = strtoul(PQgetvalue(result,0,1), NULL, 10);
755 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')",
759 if (PQntuples(result) > 0)
761 filename = PQgetvalue(result,0,0);
764 LOG_FATAL(
"PkgAgent unable to open file %s\n",filename);
772 printf(
"LOG: Unable to find debian/control file! This file had wrong mimetype, ignore it!\n");
773 memset(
SQL,0,
sizeof(
SQL));
774 snprintf(
SQL,
sizeof(
SQL),
"UPDATE pfile SET pfile_mimetypefk = NULL WHERE pfile_pk = %ld;", pi->
pFileFk);
782 if ((fp = fopen(repfile,
"r")) == NULL){
783 LOG_FATAL(
"Unable to open debian/control file %s\n",repfile);
787 while (fgets(line,MAXCMD,fp)!=NULL)
791 if (!strcasecmp(field,
"Description")) {
796 if ((strlen(temp) + strlen(
s)) >= MAXCMD )
801 if (!strcasecmp(field,
"Package")) {
804 if (!strcasecmp(field,
"Version")) {
807 if (!strcasecmp(field,
"Architecture")) {
810 if (!strcasecmp(field,
"Maintainer")) {
813 if (!strcasecmp(field,
"Installed-Size")) {
816 if (!strcasecmp(field,
"Section")) {
819 if (!strcasecmp(field,
"Priority")) {
822 if (!strcasecmp(field,
"Homepage")) {
825 if (!strcasecmp(field,
"Source")) {
828 if (!strcasecmp(field,
"Depends")) {
829 char *depends = NULL;
830 char countcopy[MAXCMD];
831 char splitcopy[MAXCMD];
835 if (value[0] !=
'\0'){
836 strncpy(countcopy, value,
sizeof(countcopy)-1);
837 countcopy[
sizeof(countcopy)-1] =
'\0';
838 depends = strtok(countcopy,
",");
839 while (depends && (depends[0] !=
'\0')) {
840 if (strlen(depends) >= length)
841 length = strlen(depends) + 1;
842 depends = strtok(NULL,
",");
845 if (
Verbose) { printf(
"SIZE:%d\n", size);}
847 strncpy(splitcopy, value,
sizeof(splitcopy)-1);
848 splitcopy[
sizeof(splitcopy)-1] =
'\0';
849 pi->
depends = calloc(size,
sizeof(
char *));
850 depends = strtok(splitcopy,
",");
851 for (i=0; i<size; i++){
852 pi->
depends[i] = calloc(length,
sizeof(
char));
854 strncpy(pi->
depends[i], depends, length - 1);
855 pi->
depends[i][length - 1] =
'\0';
856 depends = strtok(NULL,
",");
881 memset(
SQL,0,
sizeof(
SQL));
882 snprintf(
SQL,
sizeof(
SQL),
"SELECT pfile_fk FROM pkg_deb WHERE pfile_fk = %ld;",pi->
pFileFk);
885 if (PQntuples(result) <=0)
888 memset(
SQL,0,
sizeof(
SQL));
889 result = PQexec(
db_conn,
"BEGIN;");
893 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);",
900 if ((result==0) || ((PQresultStatus(result) != PGRES_COMMAND_OK)))
902 LOG_FATAL(
"Error inserting, SQL is: %s, error code is:%s\n",
SQL, PQresultErrorField(result, PG_DIAG_SQLSTATE));
904 if (result) PQclear(result);
911 result = PQexec(
db_conn,
"SELECT currval('pkg_deb_pkg_pk_seq'::regclass);");
913 pkg_pk = atoi(PQgetvalue(result,0,0));
916 if (
Verbose) { printf(
"pkg_pk:%d\n",pkg_pk);}
920 memset(
SQL,0,
sizeof(
SQL));
921 snprintf(
SQL,
sizeof(
SQL),
"INSERT INTO pkg_deb_req (pkg_fk,req_value) values (%d,E'%s');",pkg_pk,
trim(pi->
depends[i]));
931 result = PQexec(
db_conn,
"COMMIT;");
957 if ((fp = fopen(repFile,
"r")) == NULL){
958 LOG_FATAL(
"Unable to open .dsc file %s\n",repFile);
962 while (fgets(line,MAXCMD,fp)!=NULL)
967 if (!strcasecmp(field,
"Format")) {
970 if (!strcasecmp(field,
"Source")) {
973 if (!strcasecmp(field,
"Binary")) {
976 if (!strcasecmp(field,
"Architecture")) {
979 if (!strcasecmp(field,
"Version")) {
983 if (!strcasecmp(field,
"Maintainer")) {
986 if (!strcasecmp(field,
"Homepage")) {
989 if (!strcasecmp(field,
"Uploaders")) {
992 if (!strcasecmp(field,
"Standards-Version")) {
995 if (!strcasecmp(field,
"Build-Depends")) {
996 char *depends = NULL;
997 char countcopy[MAXCMD];
998 char splitcopy[MAXCMD];
1002 if (value[0] !=
'\0'){
1003 strncpy(countcopy, value,
sizeof(countcopy)-1);
1004 countcopy[
sizeof(countcopy)-1] =
'\0';
1005 depends = strtok(countcopy,
",");
1006 while (depends && (depends[0] !=
'\0')) {
1007 if (strlen(depends) >= length)
1008 length = strlen(depends) + 1;
1009 depends = strtok(NULL,
",");
1012 if (
Verbose) { printf(
"SIZE:%d\n", size);}
1014 strncpy(splitcopy, value,
sizeof(splitcopy)-1);
1015 splitcopy[
sizeof(splitcopy)-1] =
'\0';
1016 pi->
depends = calloc(size,
sizeof(
char *));
1017 depends = strtok(splitcopy,
",");
1018 for (i=0; i<size; i++){
1019 pi->
depends[i] = calloc(length,
sizeof(
char));
1021 strncpy(pi->
depends[i], depends, length - 1);
1022 pi->
depends[i][length - 1] =
'\0';
1023 depends = strtok(NULL,
",");
1046 printf(
"Usage: %s [options] [file [file [...]]\n",Name);
1047 printf(
" -i :: initialize the database, then exit.\n");
1048 printf(
" -v :: verbose (-vv = more verbose)\n");
1049 printf(
" -c :: Specify the directory for the system configuration.\n");
1050 printf(
" -C :: run from command line.\n");
1051 printf(
" file :: if files are rpm package listed, display their package information.\n");
1052 printf(
" no file :: process data from the scheduler.\n");
1053 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.