29 fo_dbManager_PrepareStamement(
31 "getUploadTreeTableName",
32 "SELECT uploadtree_tablename from upload where upload_pk=$1 limit 1",
38 result = g_strdup(
"uploadtree");
42 if (PQntuples(resTableName) == 0)
44 PQclear(resTableName);
45 result = g_strdup(
"uploadtree");
49 result = g_strdup(PQgetvalue(resTableName, 0, 0));
50 PQclear(resTableName);
69 if (strcmp(uploadtreeTableName,
"uploadtree_a") == 0)
71 queryName = g_strdup_printf(
"queryFileIdsForUpload.%s", uploadtreeTableName);
72 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",
76 queryName = g_strdup_printf(
"queryFileIdsForUpload.%s", uploadtreeTableName);
77 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",
81 if (ignoreFilesWithMimeType)
83 queryName = g_strdup_printf(
"%s.%s", queryName,
"WithMimeType");
84 strcat(
SQL,
" AND (pfile_mimetypefk not in (SELECT mimetype_pk from mimetype where mimetype_name=any(string_to_array(( \
85 SELECT conf_value from sysconfig where variablename='SkipFiles'),','))))");
88 if (strcmp(uploadtreeTableName,
"uploadtree_a") == 0)
91 fo_dbManager_PrepareStamement(
103 fo_dbManager_PrepareStamement(
111 g_free(uploadtreeTableName);
125 fo_dbManager_PrepareStamement(
127 "queryPFileForFileId",
128 "select pfile_sha1 || '.' || pfile_md5 ||'.'|| pfile_size AS pfilename from pfile where pfile_pk=$1",
133 if (PQntuples(fileNameResult) == 0)
135 PQclear(fileNameResult);
139 char* pFile = g_strdup(PQgetvalue(fileNameResult, 0, 0));
140 PQclear(fileNameResult);
158 FUNCTION
int fo_GetAgentKey(PGconn*
pgConn,
const char* agent_name,
long Upload_pk,
const char* rev,
const char* agent_desc)
167 sprintf(sqlselect,
"SELECT agent_pk,agent_desc FROM agent WHERE agent_name ='%s' order by agent_ts desc limit 1",
169 result = PQexec(
pgConn, sqlselect);
171 if (PQntuples(result) == 0)
175 sprintf(sql,
"INSERT INTO agent (agent_name,agent_desc,agent_enabled,agent_rev) VALUES ('%s',E'%s','%d', '%s')",
176 agent_name, agent_desc, 1, rev);
177 result = PQexec(
pgConn, sql);
180 result = PQexec(
pgConn, sqlselect);
184 Agent_pk = atol(PQgetvalue(result, 0, 0));
186 if(!(strcmp(PQgetvalue(result, 0, 1),agent_desc) == 0)){
188 sprintf(sqlupdate,
"UPDATE agent SET agent_desc = E'%s' where agent_pk = '%d'",agent_desc,
Agent_pk);
189 result = PQexec(
pgConn, sqlupdate);
215 const char* tableName,
const char* ars_status,
int ars_success)
229 snprintf(sql,
sizeof(sql),
"insert into %s (agent_fk, upload_fk) values(%d,%d)",
231 result = PQexec(
pgConn, sql);
235 snprintf(sql,
sizeof(sql),
"SELECT currval('nomos_ars_ars_pk_seq')");
236 result = PQexec(
pgConn, sql);
239 ars_pk = atoi(PQgetvalue(result, 0, 0));
247 snprintf(sql,
sizeof(sql),
"update %s set ars_success=%s, ars_status='%s',ars_endtime=now() where ars_pk = %d",
248 tableName, ars_success ?
"True" :
"False", ars_status, ars_pk);
252 snprintf(sql,
sizeof(sql),
"update %s set ars_success=%s, ars_endtime=now() where ars_pk = %d",
253 tableName, ars_success ?
"True" :
"False", ars_pk);
255 result = PQexec(
pgConn, sql);
277 snprintf(sql,
sizeof(sql),
"create table %s() inherits(ars_master);\
278 ALTER TABLE ONLY %s ADD CONSTRAINT %s_agent_fk_fkc FOREIGN KEY (agent_fk) REFERENCES agent(agent_pk);\
279 ALTER TABLE ONLY %s ADD CONSTRAINT %s_upload_fk_fkc FOREIGN KEY (upload_fk) REFERENCES upload(upload_pk) ON DELETE CASCADE;",
280 tableName, tableName, tableName, tableName, tableName);
284 result = PQexec(
pgConn, sql);
295 FUNCTION
int max(
int permGroup,
int permPublic)
297 return ( permGroup > permPublic ) ? permGroup : permPublic;
306 FUNCTION
int min(
int user_perm,
int permExternal)
308 return ( user_perm < permExternal ) ? user_perm: permExternal;
325 int permGroup=0, permPublic=0;
329 snprintf(
SQL,
sizeof(
SQL),
330 "select max(perm) as perm \
331 from perm_upload, group_user_member \
332 where perm_upload.upload_fk=%ld \
334 and group_user_member.group_fk=perm_upload.group_fk",
338 && PQntuples(result) > 0)
340 permGroup = atoi(PQgetvalue(result, 0, 0));
345 snprintf(
SQL,
sizeof(
SQL),
346 "select public_perm \
348 where upload_pk=%ld",
353 && PQntuples(result) > 0)
355 permPublic = atoi(PQgetvalue(result, 0, 0));
365 return min(user_perm,
max(permGroup, permPublic));
385 snprintf(
SQL,
sizeof(
SQL),
"select user_perm from users where user_pk='%d'", user_pk);
388 if (PQntuples(result) < 1)
390 LOG_ERROR(
"No records returned in %s",
SQL);
393 user_perm = atoi(PQgetvalue(result, 0, 0));
421 snprintf(
SQL,
sizeof(
SQL),
"select uploadtree_tablename from upload where upload_pk='%d'",
upload_pk);
447 snprintf(
SQL,
sizeof(
SQL),
448 "select ars_pk from ars_master,agent \
449 where agent_pk=agent_fk and ars_success=true \
450 and upload_fk='%d' and agent_fk='%d'",
474 snprintf(
SQL,
sizeof(
SQL),
475 "SELECT pfile_pk, pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfilename \
476 FROM (SELECT distinct(pfile_fk) AS PF FROM uploadtree WHERE upload_fk='%d' and (ufile_mode&x'3C000000'::int)=0) as SS \
477 left outer join license_file on (PF=pfile_fk and agent_fk='%d') inner join pfile on PF=pfile_pk \
478 WHERE (fl_pk IS null or agent_fk <>'%d')",
479 uploadPk, agentPk, agentPk);
481 if (ignoreFilesWithMimeType)
483 strcat(
SQL,
" AND (pfile_mimetypefk not in ( \
484 SELECT mimetype_pk from mimetype where mimetype_name=any(string_to_array(( \
485 SELECT conf_value from sysconfig where variablename='SkipFiles'),','))))");
char SQL[256]
SQL query to execute.
PGconn * pgConn
Database connection.
char * uploadtree_tablename
upload.uploadtree_tablename
int Agent_pk
agent identifier
char * queryPFileForFileId(fo_dbManager *dbManager, long fileId)
Get the pfile name for a given file ID.
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".
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
char * getUploadTreeTableName(fo_dbManager *dbManager, int uploadId)
Get the upload tree table name for a given upload.
FUNCTION int fo_CreateARSTable(PGconn *pgConn, const char *tableName)
Create ars table if it doesn't already exist.
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.
PGresult * queryFileIdsForUpload(fo_dbManager *dbManager, int uploadId, bool ignoreFilesWithMimeType)
Get all file IDs (pfile_fk) for a given upload.
FUNCTION int getEffectivePermissionOnUpload(PGconn *pgConn, long UploadPk, int user_pk, int user_perm)
Get users permission to this upload.
FUNCTION int min(int user_perm, int permExternal)
Get the minimum permission level required.
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 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.
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.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is 'fossology'.
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...
The main FOSSology C library.
#define PERM_ADMIN
Administrator.
#define PERM_NONE
User has no permission (not logged in)
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
fo_dbManager * dbManager
fo_dbManager object
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.