37 int tag[15] = { RPMTAG_NAME,
63 void EscapeString (
char *sourceString,
char *escString,
int esclen)
71 char *cp = (
char *)sourceString;
74 if (*cp ==
'\\') *cp =
' ';
82 len = strlen(sourceString);
96 PQescapeStringConn(
db_conn, escString, sourceString, len, &error);
98 printf(
"WARNING: %s line %d: Error escaping string with multibype character set?\n",__FILE__, __LINE__ );
114 char *Value,
int ValueMax,
char Separator)
119 memset(Field,0,FieldMax);
120 memset(Value,0,ValueMax);
122 while(isspace(Sin[0])) Sin++;
123 if (Sin[0]==
'\0')
return(NULL);
126 for(s=0; (Sin[
s] !=
'\0') && !isspace(Sin[s]) && (Sin[
s] !=
'='); s++)
130 while(isspace(Sin[s])) s++;
131 if (Sin[s] != Separator)
135 if (Sin[s]==
'\0')
return(NULL);
137 while(isspace(Sin[s])) s++;
138 if (Sin[s]==
'\0')
return(NULL);
141 if ((Sin[s]==
'\'') || (Sin[
s]==
'"'))
145 if (Sin[s]==
'\0')
return(NULL);
149 for( ; (Sin[
s] !=
'\0') && (Sin[s] != GotQuote); s++)
151 if (Sin[s]==
'\\') Value[v++]=Sin[++
s];
152 else Value[v++]=Sin[
s];
158 for( ; (Sin[
s] !=
'\0') && !isspace(Sin[s]); s++)
160 if (Sin[s]==
'\\') Value[v++]=Sin[++
s];
161 else Value[v++]=Sin[
s];
164 while(isspace(Sin[s])) s++;
180 int debmimetypepk = 0;
181 int debsrcmimetypepk = 0;
192 rpmReadConfigFiles(NULL, NULL);
198 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-rpm' LIMIT 1;");
199 result = PQexec(
db_conn, sqlbuf);
206 mimetypepk = atoi(PQgetvalue(result, 0, 0));
208 if ( mimetypepk == 0 )
210 snprintf(sqlbuf,
sizeof(sqlbuf),
"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-rpm');");
211 result = PQexec(
db_conn, sqlbuf);
218 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-rpm' LIMIT 1;");
219 result = PQexec(
db_conn, sqlbuf);
226 mimetypepk = atoi(PQgetvalue(result, 0, 0));
228 if ( mimetypepk == 0 )
230 LOG_ERROR(
"pkgagent rpm mimetype not installed!");
236 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-package' LIMIT 1;");
237 result = PQexec(
db_conn, sqlbuf);
244 debmimetypepk = atoi(PQgetvalue(result, 0, 0));
246 if ( debmimetypepk == 0 )
248 snprintf(sqlbuf,
sizeof(sqlbuf),
"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-debian-package');");
249 result = PQexec(
db_conn, sqlbuf);
256 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-package' LIMIT 1;");
257 result = PQexec(
db_conn, sqlbuf);
264 debmimetypepk = atoi(PQgetvalue(result, 0, 0));
266 if ( debmimetypepk == 0 )
268 LOG_ERROR(
"pkgagent deb mimetype not installed!");
274 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-source' LIMIT 1;");
275 result = PQexec(
db_conn, sqlbuf);
282 debsrcmimetypepk = atoi(PQgetvalue(result, 0, 0));
284 if ( debsrcmimetypepk == 0 )
286 snprintf(sqlbuf,
sizeof(sqlbuf),
"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-debian-source');");
287 result = PQexec(
db_conn, sqlbuf);
294 snprintf(sqlbuf,
sizeof(sqlbuf),
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name = 'application/x-debian-source' LIMIT 1;");
295 result = PQexec(
db_conn, sqlbuf);
302 debsrcmimetypepk = atoi(PQgetvalue(result, 0, 0));
304 if ( debsrcmimetypepk == 0 )
306 LOG_ERROR(
"pkgagent deb source mimetype not installed!");
320 if (NULL == uploadtree_tablename) uploadtree_tablename = strdup(
"uploadtree_a");
322 snprintf(sqlbuf,
sizeof(sqlbuf),
323 "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);
324 result = PQexec(
db_conn, sqlbuf);
332 numrows = PQntuples(result);
333 for (i=0; i<numrows; i++)
335 char *repFile = NULL;
340 strcpy(mimetype, PQgetvalue(result, i, 2));
346 if (!strcasecmp(mimetype,
"application/x-rpm")) {
347 pi->
pFileFk = atoi(PQgetvalue(result, i, 0));
348 strncpy(pi->
pFile, PQgetvalue(result, i, 1),
sizeof(pi->
pFile)-1);
352 LOG_FATAL(
"pfile %ld PkgAgent unable to open file %s",
365 else if (!strcasecmp(mimetype,
"application/x-debian-package")){
366 dpi->
pFileFk = atoi(PQgetvalue(result, i, 0));
367 strncpy(dpi->
pFile, PQgetvalue(result, i, 1),
sizeof(dpi->
pFile)-1);
378 else if (!strcasecmp(mimetype,
"application/x-debian-source")){
379 dpi->
pFileFk = atoi(PQgetvalue(result, i, 0));
380 strncpy(dpi->
pFile, PQgetvalue(result, i, 1),
sizeof(dpi->
pFile)-1);
384 LOG_FATAL(
"pfile %ld PkgAgent unable to open file %s\n",
397 printf(
"LOG: Not RPM and DEBIAN package!\n");
424 rpm_count_t data_size;
426 for (i = 0; i < 14; i++) {
427 memset(fmt, 0,
sizeof(fmt));
429 strcat( fmt, rpmTagGetName(
tag[i]));
432 msgstr = headerFormat(header, fmt, &errstr);
435 printf(
"%s:%s\n",rpmTagGetName(
tag[i]),msgstr);
455 case RPMTAG_PACKAGER:
461 case RPMTAG_BUILDTIME:
473 case RPMTAG_SOURCERPM:
479 case RPMTAG_DESCRIPTION:
486 free((
void *)msgstr);
489 header_status = headerGet(header,
tag[14], &req, HEADERGET_DEFAULT);
491 data_size = rpmtdCount(&req);
492 pi->
requires = calloc(data_size,
sizeof(
char *));
493 for (j=0; j<data_size;j++){
494 const char * temp = rpmtdNextString(&req);
528 vsflags = RPMVSF_DEFAULT;
529 ts = (rpmts) rpmtsCreate();
532 if ( fd == NULL ||Ferror(fd)){
533 rpmlog(RPMLOG_ERR,
"open of %s failed: %s\n", pkg, Fstrerror(fd));
540 vsflags |= _RPMVSF_NOSIGNATURES;
541 vsflags |= _RPMVSF_NODIGESTS;
542 vsflags |= RPMVSF_NOHDRCHK;
543 vsflags |= RPMVSF_NEEDPAYLOAD;
545 rpmtsSetVSFlags(ts, vsflags);
548 rpmrc = rpmReadPackageFile(ts, fd, pkg,&header);
550 ts = (rpmts) rpmtsFree(ts);
555 case RPMRC_NOTTRUSTED:
560 rpmlog(RPMLOG_ERR,
"%s cannot be read or is not an RPM.\n", pkg);
565 header = headerFree(header);
581 memset(SQL,0,
sizeof(SQL));
582 snprintf(SQL,
sizeof(SQL),
"SELECT pfile_fk FROM pkg_rpm WHERE pfile_fk = %ld;",pi->
pFileFk);
585 if (PQntuples(result) <=0)
588 memset(SQL,0,
sizeof(SQL));
589 result = PQexec(
db_conn,
"BEGIN;");
593 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);",
606 result = PQexec(
db_conn,
"SELECT currval('pkg_rpm_pkg_pk_seq'::regclass);");
608 pkg_pk = atoi(PQgetvalue(result,0,0));
611 if (
Verbose) { printf(
"pkg_pk:%d\n",pkg_pk);}
615 memset(SQL,0,
sizeof(SQL));
616 snprintf(SQL,
sizeof(SQL),
"INSERT INTO pkg_rpm_req (pkg_fk,req_value) values (%d,E'%s');",pkg_pk,
trim(pi->
requires[i]));
625 result = PQexec(
db_conn,
"COMMIT;");
645 memset(Field,0,MAXCMD);
646 memset(Value,0,MAXCMD);
651 for(s=0; (Sin[
s] !=
'\0') && !isspace(Sin[s]) && (Sin[
s] !=
':'); s++)
655 while(isspace(Sin[s])) s++;
661 while(isspace(Sin[s])) s++;
663 for( ; Sin[
s] !=
'\0'; s++)
667 if (
Verbose) { printf(
"Field is %s and Value is %s", Field, Value);}
671 if (
Verbose) { printf(
"ExValue is %s", Sin);}
688 unsigned long lft, rgt;
698 if (!upload_pk)
return -1;
700 if (NULL == uploadtree_tablename) uploadtree_tablename = strdup(
"uploadtree_a");
703 snprintf(SQL,
sizeof(SQL),
"SELECT lft,rgt FROM %s WHERE upload_fk = %ld AND pfile_fk = %ld limit 1",
704 uploadtree_tablename, upload_pk, pi->
pFileFk);
707 if (PQntuples(result) == 0)
709 LOG_ERROR(
"Missing debian package (internal data inconsistancy). SQL: %s\n", SQL);
713 lft = strtoul(PQgetvalue(result,0,0), NULL, 10);
714 rgt = strtoul(PQgetvalue(result,0,1), NULL, 10);
717 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')",
718 upload_pk, lft, rgt);
721 if (PQntuples(result) > 0)
723 filename = PQgetvalue(result,0,0);
726 LOG_FATAL(
"PkgAgent unable to open file %s\n",filename);
734 printf(
"LOG: Unable to find debian/control file! This file had wrong mimetype, ignore it!\n");
735 memset(SQL,0,
sizeof(SQL));
736 snprintf(SQL,
sizeof(SQL),
"UPDATE pfile SET pfile_mimetypefk = NULL WHERE pfile_pk = %ld;", pi->
pFileFk);
744 if ((fp = fopen(repfile,
"r")) == NULL){
745 LOG_FATAL(
"Unable to open debian/control file %s\n",repfile);
749 while (fgets(line,MAXCMD,fp)!=NULL)
753 if (!strcasecmp(field,
"Description")) {
758 if ((strlen(temp) + strlen(s)) >= MAXCMD )
763 if (!strcasecmp(field,
"Package")) {
766 if (!strcasecmp(field,
"Version")) {
769 if (!strcasecmp(field,
"Architecture")) {
772 if (!strcasecmp(field,
"Maintainer")) {
775 if (!strcasecmp(field,
"Installed-Size")) {
778 if (!strcasecmp(field,
"Section")) {
781 if (!strcasecmp(field,
"Priority")) {
784 if (!strcasecmp(field,
"Homepage")) {
787 if (!strcasecmp(field,
"Source")) {
790 if (!strcasecmp(field,
"Depends")) {
791 char *depends = NULL;
792 char tempvalue[MAXCMD];
796 if (value[0] !=
'\0'){
797 strncpy(tempvalue, value,
sizeof(tempvalue));
798 depends = strtok(value,
",");
799 while (depends && (depends[0] !=
'\0')) {
800 if (strlen(depends) >= length)
801 length = strlen(depends) + 1;
802 depends = strtok(NULL,
",");
805 if (
Verbose) { printf(
"SIZE:%d\n", size);}
807 pi->
depends = calloc(size,
sizeof(
char *));
808 pi->
depends[0] = calloc(length,
sizeof(
char));
809 strcpy(pi->
depends[0],strtok(tempvalue,
","));
810 for (i=1;i<size;i++){
811 pi->
depends[i] = calloc(length,
sizeof(
char));
812 strcpy(pi->
depends[i],strtok(NULL,
","));
836 memset(SQL,0,
sizeof(SQL));
837 snprintf(SQL,
sizeof(SQL),
"SELECT pfile_fk FROM pkg_deb WHERE pfile_fk = %ld;",pi->
pFileFk);
840 if (PQntuples(result) <=0)
843 memset(SQL,0,
sizeof(SQL));
844 result = PQexec(
db_conn,
"BEGIN;");
848 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);",
855 if ((result==0) || ((PQresultStatus(result) != PGRES_COMMAND_OK)))
857 LOG_FATAL(
"Error inserting, SQL is: %s, error code is:%s\n", SQL, PQresultErrorField(result, PG_DIAG_SQLSTATE));
859 if (result) PQclear(result);
866 result = PQexec(
db_conn,
"SELECT currval('pkg_deb_pkg_pk_seq'::regclass);");
868 pkg_pk = atoi(PQgetvalue(result,0,0));
871 if (
Verbose) { printf(
"pkg_pk:%d\n",pkg_pk);}
875 memset(SQL,0,
sizeof(SQL));
876 snprintf(SQL,
sizeof(SQL),
"INSERT INTO pkg_deb_req (pkg_fk,req_value) values (%d,E'%s');",pkg_pk,
trim(pi->
depends[i]));
886 result = PQexec(
db_conn,
"COMMIT;");
912 if ((fp = fopen(repFile,
"r")) == NULL){
913 LOG_FATAL(
"Unable to open .dsc file %s\n",repFile);
917 while (fgets(line,MAXCMD,fp)!=NULL)
922 if (!strcasecmp(field,
"Format")) {
925 if (!strcasecmp(field,
"Source")) {
928 if (!strcasecmp(field,
"Source")) {
931 if (!strcasecmp(field,
"Architecture")) {
934 if (!strcasecmp(field,
"Version")) {
938 if (!strcasecmp(field,
"Maintainer")) {
941 if (!strcasecmp(field,
"Homepage")) {
944 if (!strcasecmp(field,
"Uploaders")) {
947 if (!strcasecmp(field,
"Standards-Version")) {
950 if (!strcasecmp(field,
"Build-Depends")) {
951 char *depends = NULL;
952 char tempvalue[MAXCMD];
956 if (value[0] !=
'\0'){
957 strncpy(tempvalue, value,
sizeof(tempvalue));
958 depends = strtok(value,
",");
959 while (depends && (depends[0] !=
'\0')) {
960 if (strlen(depends) >= length)
961 length = strlen(depends) + 1;
962 depends = strtok(NULL,
",");
965 if (
Verbose) { printf(
"SIZE:%d\n", size);}
967 pi->
depends = calloc(size,
sizeof(
char *));
968 pi->
depends[0] = calloc(length,
sizeof(
char));
969 strcpy(pi->
depends[0],strtok(tempvalue,
","));
970 for (i=1;i<size;i++){
971 pi->
depends[i] = calloc(length,
sizeof(
char));
972 strcpy(pi->
depends[i],strtok(NULL,
","));
994 printf(
"Usage: %s [options] [file [file [...]]\n",Name);
995 printf(
" -i :: initialize the database, then exit.\n");
996 printf(
" -v :: verbose (-vv = more verbose)\n");
997 printf(
" -c :: Specify the directory for the system configuration.\n");
998 printf(
" -C :: run from command line.\n");
999 printf(
" file :: if files are rpm package listed, display their package information.\n");
1000 printf(
" no file :: process data from the scheduler.\n");
1001 printf(
" -V :: print the version info, then exit.\n");
char ** depends
Package dependency list.
int installedSize
Size of package after install.
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"...
char maintainer[MAXCMD]
Package maintainer.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
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.
char version[MAXCMD]
Package version.
char license[512]
RPM licenses.
char pFile[MAXCMD]
Package pfile hash.
char homepage[MAXCMD]
Package link.
long pFileFk
Package pfile in FOSSology.
char description[MAXCMD]
Package description.
int s
The socket that the CLI will use to communicate.
int RecordMetadataRPM(struct rpmpkginfo *pi)
Store rpm package info into database.
char packager[1024]
Packager.
char buildDate[128]
Package build date.
int dep_size
Package dependency list size.
char sourceRPM[256]
Package source.
char pkgName[256]
RPM package name.
Holds meta info of rpm packages.
char * ParseDebFile(char *Sin, char *Field, char *Value)
Parse Debian binary control file with Field/Value pairs.
char version[64]
Package version.
char standardsVersion[MAXCMD]
Package standards version.
char pkgArch[MAXCMD]
Package architecture.
Holds meta info of Debian packages.
char pkgName[MAXCMD]
Package name.
int GetMetadataDebSource(char *repFile, struct debpkginfo *pi)
Get debian source package info from .dsc file.
char summary[MAXCMD]
Package summary.
void Usage(char *Name)
Say how to run this program.
char description[MAXCMD]
Package description.
char ** requires
Package dependency list.
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 uploaders[MAXCMD]
Package contributors.
char SQL[256]
SQL query to execute.
char summary[MAXCMD]
Package summary.
char pkgAlias[256]
Package alias.
void EscapeString(char *sourceString, char *escString, int esclen)
Escaping special characters(single quote)
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
char pkgArch[64]
Package architecture.
char * uploadtree_tablename
upload.uploadtree_tablename
PGconn * db_conn
The connection to Database.
int Verbose
Verbose level.
#define MAXLENGTH
Buffer length.
void ReadHeaderInfo(Header header, struct rpmpkginfo *pi)
Get RPM package info from rpm file header use rpm library.
char rpmFilename[256]
RPM file name.
int GetMetadataDebBinary(long upload_pk, struct debpkginfo *pi)
Get debian binary package info.
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...
char vendor[128]
Package vendor.
char priority[MAXCMD]
Package priority.
char section[MAXCMD]
Package section.
int ProcessUpload(long upload_pk)
Get all pfile need to processed use upload_pk.
char group[128]
Package group.
char pFile[MAXCMD]
Package pfile hash.
char format[MAXCMD]
Package format.
int RecordMetadataDEB(struct debpkginfo *pi)
Store debian package info into database.
char release[64]
Package release.
char url[256]
Package link.
char source[MAXCMD]
Package source.
int req_size
Package dependency list size.
long pFileFk
Package pfile in FOSSology.
char * trim(char *ptext)
Trimming whitespace.