41 fo_dbManager_PrepareStamement(
43 "getUploadTreeTableName",
44 "SELECT uploadtree_tablename from upload where upload_pk=$1 limit 1",
50 result = g_strdup(
"uploadtree");
54 if (PQntuples(resTableName) == 0)
56 PQclear(resTableName);
57 result = g_strdup(
"uploadtree");
61 result = g_strdup(PQgetvalue(resTableName, 0, 0));
62 PQclear(resTableName);
81 if (strcmp(uploadtreeTableName,
"uploadtree_a") == 0)
83 queryName = g_strdup_printf(
"queryFileIdsForUpload.%s", uploadtreeTableName);
84 g_snprintf(SQL,
sizeof(SQL),
"select distinct(pfile_fk) from %s join pfile on pfile_pk=pfile_fk where upload_fk=$1 and (ufile_mode&x'3C000000'::int)=0",
88 queryName = g_strdup_printf(
"queryFileIdsForUpload.%s", uploadtreeTableName);
89 g_snprintf(SQL,
sizeof(SQL),
"select distinct(pfile_fk) from %s join pfile on pfile_pk=pfile_fk where (ufile_mode&x'3C000000'::int)=0",
93 if (ignoreFilesWithMimeType)
95 queryName = g_strdup_printf(
"%s.%s", queryName,
"WithMimeType");
96 strcat(SQL,
" AND (pfile_mimetypefk not in (SELECT mimetype_pk from mimetype where mimetype_name=any(string_to_array(( \ 97 SELECT conf_value from sysconfig where variablename='SkipFiles'),','))))");
100 if (strcmp(uploadtreeTableName,
"uploadtree_a") == 0)
103 fo_dbManager_PrepareStamement(
115 fo_dbManager_PrepareStamement(
125 g_free(uploadtreeTableName);
139 fo_dbManager_PrepareStamement(
141 "queryPFileForFileId",
142 "select pfile_sha1 || '.' || pfile_md5 ||'.'|| pfile_size AS pfilename from pfile where pfile_pk=$1",
147 if (PQntuples(fileNameResult) == 0)
149 PQclear(fileNameResult);
153 char* pFile = g_strdup(PQgetvalue(fileNameResult, 0, 0));
154 PQclear(fileNameResult);
172 FUNCTION
int fo_GetAgentKey(PGconn*
pgConn,
const char* agent_name,
long Upload_pk,
const char* rev,
const char* agent_desc)
181 sprintf(sqlselect,
"SELECT agent_pk,agent_desc FROM agent WHERE agent_name ='%s' order by agent_ts desc limit 1",
183 result = PQexec(pgConn, sqlselect);
184 if (
fo_checkPQresult(pgConn, result, sqlselect, __FILE__, __LINE__))
return 0;
185 if (PQntuples(result) == 0)
189 sprintf(sql,
"INSERT INTO agent (agent_name,agent_desc,agent_enabled,agent_rev) VALUES ('%s',E'%s','%d', '%s')",
190 agent_name, agent_desc, 1, rev);
191 result = PQexec(pgConn, sql);
194 result = PQexec(pgConn, sqlselect);
195 if (
fo_checkPQresult(pgConn, result, sqlselect, __FILE__, __LINE__))
return 0;
198 Agent_pk = atol(PQgetvalue(result, 0, 0));
200 if(!(strcmp(PQgetvalue(result, 0, 1),agent_desc) == 0)){
202 sprintf(sqlupdate,
"UPDATE agent SET agent_desc = E'%s' where agent_pk = '%d'",agent_desc, Agent_pk);
203 result = PQexec(pgConn, sqlupdate);
229 const char* tableName,
const char* ars_status,
int ars_success)
243 snprintf(sql,
sizeof(sql),
"insert into %s (agent_fk, upload_fk) values(%d,%d)",
244 tableName, agent_pk, upload_pk);
245 result = PQexec(pgConn, sql);
249 snprintf(sql,
sizeof(sql),
"SELECT currval('nomos_ars_ars_pk_seq')");
250 result = PQexec(pgConn, sql);
253 ars_pk = atoi(PQgetvalue(result, 0, 0));
261 snprintf(sql,
sizeof(sql),
"update %s set ars_success=%s, ars_status='%s',ars_endtime=now() where ars_pk = %d",
262 tableName, ars_success ?
"True" :
"False", ars_status, ars_pk);
266 snprintf(sql,
sizeof(sql),
"update %s set ars_success=%s, ars_endtime=now() where ars_pk = %d",
267 tableName, ars_success ?
"True" :
"False", ars_pk);
269 result = PQexec(pgConn, sql);
291 snprintf(sql,
sizeof(sql),
"create table %s() inherits(ars_master);\ 292 ALTER TABLE ONLY %s ADD CONSTRAINT %s_agent_fk_fkc FOREIGN KEY (agent_fk) REFERENCES agent(agent_pk);\ 293 ALTER TABLE ONLY %s ADD CONSTRAINT %s_upload_fk_fkc FOREIGN KEY (upload_fk) REFERENCES upload(upload_pk) ON DELETE CASCADE;",
294 tableName, tableName, tableName, tableName, tableName);
298 result = PQexec(pgConn, sql);
309 FUNCTION
int max(
int permGroup,
int permPublic)
311 return ( permGroup > permPublic ) ? permGroup : permPublic;
320 FUNCTION
int min(
int user_perm,
int permExternal)
322 return ( user_perm < permExternal ) ? user_perm: permExternal;
339 int permGroup=0, permPublic=0;
343 snprintf(SQL,
sizeof(SQL),
344 "select max(perm) as perm \ 345 from perm_upload, group_user_member \ 346 where perm_upload.upload_fk=%ld \ 348 and group_user_member.group_fk=perm_upload.group_fk",
350 result = PQexec(pgConn, SQL);
352 && PQntuples(result) > 0)
354 permGroup = atoi(PQgetvalue(result, 0, 0));
359 snprintf(SQL,
sizeof(SQL),
360 "select public_perm \ 362 where upload_pk=%ld",
364 result = PQexec(pgConn, SQL);
367 && PQntuples(result) > 0)
369 permPublic = atoi(PQgetvalue(result, 0, 0));
373 return min(user_perm,
max(permGroup, permPublic));
392 snprintf(SQL,
sizeof(SQL),
"select user_perm from users where user_pk='%d'", user_pk);
393 result = PQexec(pgConn, SQL);
395 if (PQntuples(result) < 1)
397 LOG_ERROR(
"No records returned in %s", SQL);
400 user_perm = atoi(PQgetvalue(result, 0, 0));
428 snprintf(SQL,
sizeof(SQL),
"select uploadtree_tablename from upload where upload_pk='%d'", upload_pk);
429 result = PQexec(pgConn, SQL);
431 if (PQntuples(result) == 1) uploadtree_tablename = g_strdup(PQgetvalue(result, 0, 0));
434 return (uploadtree_tablename);
454 snprintf(SQL,
sizeof(SQL),
455 "select ars_pk from ars_master,agent \ 456 where agent_pk=agent_fk and ars_success=true \ 457 and upload_fk='%d' and agent_fk='%d'",
459 result = PQexec(pgConn, SQL);
481 snprintf(SQL,
sizeof(SQL),
482 "SELECT pfile_pk, pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfilename \ 483 FROM (SELECT distinct(pfile_fk) AS PF FROM uploadtree WHERE upload_fk='%d' and (ufile_mode&x'3C000000'::int)=0) as SS \ 484 left outer join license_file on (PF=pfile_fk and agent_fk='%d') inner join pfile on PF=pfile_pk \ 485 WHERE (fl_pk IS null or agent_fk <>'%d')",
486 uploadPk, agentPk, agentPk);
488 if (ignoreFilesWithMimeType)
490 strcat(SQL,
" AND (pfile_mimetypefk not in ( \ 491 SELECT mimetype_pk from mimetype where mimetype_name=any(string_to_array(( \ 492 SELECT conf_value from sysconfig where variablename='SkipFiles'),','))))");
494 result = PQexec(pgConn, SQL);
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.
char * queryPFileForFileId(fo_dbManager *dbManager, long fileId)
Get the pfile name for a given file ID.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
PGconn * pgConn
Database connection.
char * getUploadTreeTableName(fo_dbManager *dbManager, int uploadId)
Get the upload tree table name for a given upload.
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
#define PERM_NONE
User has no permission (not logged in)
FUNCTION int min(int user_perm, int permExternal)
Get the minimum permission level required.
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 SQL[256]
SQL query to execute.
int Agent_pk
agent identifier
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
The main FOSSology C library.
FUNCTION int getEffectivePermissionOnUpload(PGconn *pgConn, long UploadPk, int user_pk, int user_perm)
Get users permission to this upload.
char * uploadtree_tablename
upload.uploadtree_tablename
FUNCTION int fo_CreateARSTable(PGconn *pgConn, const char *tableName)
Create ars table if it doesn't already exist.
fo_dbManager * dbManager
fo_dbManager object
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is 'fossology'.
FUNCTION int fo_GetAgentKey(PGconn *pgConn, const char *agent_name, long Upload_pk, const char *rev, const char *agent_desc)
Get the latest enabled agent key (agent_pk) from the database.
FUNCTION int fo_WriteARS(PGconn *pgConn, int ars_pk, int upload_pk, int agent_pk, const char *tableName, const char *ars_status, int ars_success)
Write ars record.
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
PGresult * queryFileIdsForUpload(fo_dbManager *dbManager, int uploadId, bool ignoreFilesWithMimeType)
Get all file IDs (pfile_fk) for a given upload.
PGresult * getSelectedPFiles(PGconn *pgConn, int uploadPk, int agentPk, bool ignoreFilesWithMimeType)
Get the upload_pk, agent_pk and ignoreFilesWithMimeType to get all the file Ids for nomos...
#define PERM_ADMIN
Administrator.
PGresult * checkDuplicateReq(PGconn *pgConn, int uploadPk, int agentPk)
Get the upload_pk and agent_pk to find out the agent has already scanned the package.