12 #include "maintagent.h"
26 PGresult *
PQexecCheck(
int exitNumber,
char *
SQL,
char *file,
const int line)
55 long startTime, endTime;
56 char *
SQL =
"VACUUM ANALYZE";
58 startTime = (long)time(0);
63 endTime = (long)time(0);
64 printf(
"Vacuum Analyze took %ld seconds\n", endTime-startTime);
80 char *invalidUploadRefs =
"DELETE FROM foldercontents WHERE foldercontents_mode = 2 AND child_id NOT IN (SELECT upload_pk FROM upload)";
81 char *invalidUploadtreeRefs =
"DELETE FROM foldercontents WHERE foldercontents_mode = 4 AND child_id NOT IN (SELECT uploadtree_pk FROM uploadtree)";
82 char *unRefFolders =
"DELETE FROM folder WHERE folder_pk \
83 NOT IN (SELECT child_id FROM foldercontents WHERE foldercontents_mode = 1) AND folder_pk != '1'";
84 long startTime, endTime;
86 startTime = (long)time(0);
89 result =
PQexecCheck(-120, invalidUploadRefs, __FILE__, __LINE__);
90 countTuples = PQcmdTuples(result);
92 printf(
"%s Invalid folder upload References\n", countTuples);
95 result =
PQexecCheck(-121, invalidUploadtreeRefs, __FILE__, __LINE__);
96 countTuples = PQcmdTuples(result);
98 printf(
"%s Invalid folder uploadtree References\n", countTuples);
101 result =
PQexecCheck(-122, unRefFolders, __FILE__, __LINE__);
102 countTuples = PQcmdTuples(result);
104 printf(
"%s unreferenced folders\n", countTuples);
106 endTime = (long)time(0);
107 printf(
"Validate folders took %ld seconds\n", endTime-startTime);
144 LOG_NOTICE(
"Verify file permissions is not implemented yet");
159 long startTime, endTime;
161 char *
SQL =
"DELETE FROM upload WHERE upload_pk \
162 IN (SELECT upload_fk FROM uploadtree WHERE parent IS NULL AND pfile_fk IS NULL) \
163 OR (upload_pk NOT IN (SELECT upload_fk FROM uploadtree) \
164 AND (expire_action IS NULL OR expire_action != 'd') AND pfile_fk IS NOT NULL)";
166 startTime = (long)time(0);
169 countTuples = PQcmdTuples(result);
172 endTime = (long)time(0);
173 printf(
"%s Uploads with no pfiles (%ld seconds)\n", countTuples, endTime-startTime);
188 int droppedCount = 0;
190 long startTime, endTime;
192 char *
SQL =
"SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE'"
193 "AND table_schema = 'public' AND (table_name SIMILAR TO '^metaanalysis_[[:digit:]]+$'"
194 "OR table_name SIMILAR TO '^delup_%');";
196 startTime = (long)time(0);
200 countTuples = PQntuples(result);
202 for (row = 0; row < countTuples; row++) {
203 snprintf(SQLBuf, MAXSQL,
"DROP TABLE %s", PQgetvalue(result, row, 0));
209 endTime = (long)time(0);
210 printf(
"%d Orphaned temp tables were dropped (%ld seconds)\n", droppedCount, endTime-startTime);
233 LOG_NOTICE(
"Process expired uploads is not implemented yet");
248 long StartTime, EndTime;
250 char filesPath[myBUFSIZ];
252 StartTime = (long)time(0);
255 strncpy(filesPath, repoPath, myBUFSIZ - 1);
256 strncat(filesPath,
"/localhost/files", myBUFSIZ - 1);
258 if (access(filesPath, R_OK | W_OK) != 0)
260 LOG_ERROR(
"Files path is not readable/writeable: '%s'", filesPath);
267 EndTime = (long)time(0);
268 printf(
"Remove orphaned files from the repository took %ld seconds\n",
269 EndTime - StartTime);
283 long StartTime, EndTime;
285 char goldPath[myBUFSIZ];
287 StartTime = (long)time(0);
290 strncpy(goldPath, repoPath, myBUFSIZ - 1);
291 strncat(goldPath,
"/localhost/gold", myBUFSIZ - 1);
293 if (access(goldPath, R_OK | W_OK) != 0)
295 LOG_ERROR(
"Gold path is not readable/writeable: '%s'", goldPath);
302 EndTime = (long)time(0);
303 printf(
"Remove orphaned gold files from the repository took %ld seconds\n",
304 EndTime - StartTime);
315 long startTime, endTime;
317 char *SQL1 =
"CREATE TEMPORARY TABLE tmp_upload_prio(ordprio serial, uploadid int, groupid int)";
318 char *SQL2 =
"INSERT INTO tmp_upload_prio (uploadid, groupid) (SELECT upload_fk uploadid, group_fk groupid FROM upload_clearing ORDER BY priority ASC);";
319 char *SQL3 =
"UPDATE upload_clearing SET priority = ordprio FROM tmp_upload_prio WHERE uploadid=upload_fk AND group_fk=groupid;";
321 startTime = (long)time(0);
327 endTime = (long)time(0);
328 printf(
"Normalized upload priorities (%ld seconds)\n", endTime-startTime);
342 long startTime, endTime;
343 char *
SQL=
"SELECT table_catalog FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = 'public' AND (table_name SIMILAR TO 'upload%') LIMIT 1";
345 startTime = (long)time(0);
350 memset(SQLBuf,
'\0',
sizeof(SQLBuf));
351 snprintf(SQLBuf,
sizeof(SQLBuf),
"REINDEX DATABASE %s;", PQgetvalue(result, 0, 0));
355 endTime = (long)time(0);
356 printf(
"Time taken for reindexing the database : %ld seconds\n", endTime-startTime);
370 long startTime, endTime;
372 char *SQL1 =
"DELETE FROM uploadtree UT "
373 " WHERE NOT EXISTS ( "
376 " WHERE UT.upload_fk = U.upload_pk "
379 char *SQL2 =
"DELETE FROM clearing_decision AS CD "
380 " WHERE NOT EXISTS ( "
382 " FROM uploadtree UT "
383 " WHERE CD.uploadtree_fk = UT.uploadtree_pk "
384 " ) AND CD.scope = '0';";
386 char *SQL3 =
"DELETE FROM clearing_event CE "
387 " WHERE NOT EXISTS ( "
389 " FROM uploadtree UT "
390 " WHERE CE.uploadtree_fk = UT.uploadtree_pk "
391 " ) AND NOT EXISTS ( "
393 " FROM clearing_decision CD"
394 " WHERE CD.uploadtree_fk = CE.uploadtree_fk "
395 " AND CD.scope = '1'"
398 char *SQL4 =
"DELETE FROM clearing_decision_event CDE"
399 " WHERE NOT EXISTS ( "
401 " FROM clearing_event CE "
402 " WHERE CE.clearing_event_pk = CDE.clearing_event_fk "
405 char *SQL5 =
"DELETE FROM obligation_map OM "
406 " WHERE NOT EXISTS ( "
408 " FROM license_ref LR "
409 " WHERE OM.rf_fk = LR.rf_pk "
412 char *SQL6 =
"DELETE FROM obligation_candidate_map OCM "
413 " WHERE NOT EXISTS ( "
415 " FROM license_ref LR "
416 " WHERE OCM.rf_fk = LR.rf_pk "
419 startTime = (long)time(0);
421 result =
PQexecCheck(-200, SQL1, __FILE__, __LINE__);
422 countTuples = PQcmdTuples(result);
424 printf(
"%s Orphaned records have been removed from uploadtree table\n", countTuples);
427 result =
PQexecCheck(-201, SQL2, __FILE__, __LINE__);
428 countTuples = PQcmdTuples(result);
430 printf(
"%s Orphaned records have been removed from clearing_decision table\n", countTuples);
433 result =
PQexecCheck(-202, SQL3, __FILE__, __LINE__);
434 countTuples = PQcmdTuples(result);
436 printf(
"%s Orphaned records have been removed from clearing_event table\n", countTuples);
439 result =
PQexecCheck(-203, SQL4, __FILE__, __LINE__);
440 countTuples = PQcmdTuples(result);
442 printf(
"%s Orphaned records have been removed from clearing_decision_event table\n", countTuples);
445 result =
PQexecCheck(-204, SQL5, __FILE__, __LINE__);
446 countTuples = PQcmdTuples(result);
448 printf(
"%s Orphaned records have been removed from obligation_map table\n", countTuples);
451 result =
PQexecCheck(-205, SQL6, __FILE__, __LINE__);
452 countTuples = PQcmdTuples(result);
454 printf(
"%s Orphaned records have been removed from obligation_candidate_map table\n", countTuples);
457 endTime = (long)time(0);
459 printf(
"Time taken for removing orphaned rows from database : %ld seconds\n", endTime-startTime);
472 PGresult* updateResult;
475 int removedCount = 0;
478 long startTime, endTime;
482 char *
SQL =
"SELECT jq_pk, jq_log FROM job ja "
483 "INNER JOIN job jb ON ja.job_upload_fk = jb.job_upload_fk "
484 "INNER JOIN jobqueue jq ON jb.job_pk = jq.jq_job_fk "
485 "WHERE ja.job_name = 'Delete' AND jq_log IS NOT NULL "
486 "AND jq_log != 'removed';";
488 startTime = (long)time(0);
495 countTuples = PQntuples(result);
497 updateStatement = fo_dbManager_PrepareStamement(
dbManager,
498 "updateRemovedLogFromJobqueue",
499 "UPDATE jobqueue SET jq_log = 'removed' WHERE jq_pk = $1;",
int);
501 for (row = 0; row < countTuples; row++)
504 jobQueueId = atoi(PQgetvalue(result, row, 0));
505 logPath = PQgetvalue(result, row, 1);
506 if (stat(logPath, &statbuf) == -1)
508 LOG_NOTICE(
"Log file '%s' does not exists", logPath);
513 LOG_VERBOSE2(
"Removed file '%s'", logPath);
523 LOG_ERROR(
"Unable to update the value of jobqueue.jq_log to 'removed' "
524 "for jq_pk = %d", jobQueueId);
530 endTime = (long)time(0);
531 printf(
"%d / %d Orphaned log files were removed "
532 "(%ld seconds)\n", removedCount, countTuples, endTime - startTime);
546 long startTime, endTime;
548 time_t current_time = time(0);
549 time_t shifted_time = current_time - (retentionPeriod*24*60*60);
550 struct tm time_structure = *localtime(&shifted_time);
552 snprintf(
SQL,
sizeof(
SQL),
553 "DELETE FROM personal_access_tokens WHERE (active = 'FALSE' OR expire_on < '%d-%02d-%02d') AND client_id IS NULL",
554 time_structure.tm_year + 1900,
555 time_structure.tm_mon + 1,
556 time_structure.tm_mday
559 startTime = (long)time(0);
562 countTuples = PQcmdTuples(result);
564 printf(
"%s Expired personal access tokens have been removed from personal_access_tokens table\n", countTuples);
567 endTime = (long)time(0);
569 printf(
"Time taken for removing expired personal access tokens from database : %ld seconds\n", endTime-startTime);
585 long StartTime, EndTime;
586 int countTuples, row, remval;
587 int day, month, year;
591 day = month = year = -1;
592 sscanf(date,
"%4d-%2d-%2d", &year, &month, &day);
593 if (((year < 1900) || (year > 9999)) || ((month < 1) || (month > 12)) ||
594 ((day < 1) || (day > 31)))
596 LOG_FATAL(
"Invalid date! Require yyyy-mm-dd, '%s' given.", date);
600 snprintf(sql, MAXSQL,
601 "SELECT DISTINCT ON(pfile_pk) "
602 "CONCAT(LOWER(pfile_sha1), '.', LOWER(pfile_md5), '.', pfile_size) "
603 "AS filename FROM upload INNER JOIN pfile ON pfile_pk = pfile_fk "
604 "WHERE upload_ts < '%s' AND pfile_fk NOT IN ("
605 "SELECT pfile_fk FROM upload WHERE upload_ts > '%s' "
606 "AND pfile_fk IS NOT NULL);",
609 StartTime = (long)time(0);
611 result = PQexec(
pgConn, sql);
616 countTuples = PQntuples(result);
619 LOG_DEBUG(
"Read %d rows from DB.", countTuples);
622 for (row = 0; row < countTuples; row++)
624 filepath =
fo_RepMkPath(
"gold", PQgetvalue(result, row, 0));
625 remval = remove(filepath);
631 LOG_WARNING(
"Unable to remove '%s' file.", filepath);
642 EndTime = (long)time(0);
643 printf(
"Removed %d old gold files from the repository, took %ld seconds\n",
644 numrows, EndTime - StartTime);
662 time_t current_time = time(0);
664 time_t ellapsed_time;
667 unsigned int numfiles = 0;
668 long StartTime, EndTime;
673 StartTime = (long)time(0);
674 if (sscanf(olderThan,
"%d-%d-%d", &ti.tm_year, &ti.tm_mon, &ti.tm_mday) != 3)
676 LOG_FATAL(
"Unable to parse date '%s' in YYYY-MM-DD format.", olderThan);
681 shifted_time = mktime(&ti);
682 ellapsed_time = (current_time - shifted_time) / 60 / 60 / 24 - 1;
684 char file_template[] =
"/tmp/foss-XXXXXX";
685 int fd = mkstemp(file_template);
687 snprintf(cmd, myBUFSIZ,
"/usr/bin/find %s/logs -type f -mtime +%ld -fprint %s",
688 fo_sysconfig(
"FOSSOLOGY",
"path"), ellapsed_time, file_template);
689 retval = system(cmd);
690 if (!WIFEXITED(retval))
692 LOG_FATAL(
"Unable run find for logs files.");
693 unlink(file_template);
696 tempFile = fdopen(fd,
"r");
697 if (tempFile == NULL)
699 LOG_FATAL(
"Unable to open temp file.");
700 unlink(file_template);
703 while ((ch = fgetc(tempFile)) != EOF)
711 snprintf(cmd, myBUFSIZ,
"/usr/bin/xargs --arg-file=%s /bin/rm -f", file_template);
712 retval = system(cmd);
713 if (!WIFEXITED(retval))
715 LOG_FATAL(
"Unable delete log files with xargs.");
717 unlink(file_template);
721 unlink(file_template);
723 EndTime = (long)time(0);
725 printf(
"Removed %d log files.\n", numfiles);
728 "Removing log files older than '%s' from the repository took %ld "
730 olderThan, EndTime - StartTime);
char SQL[256]
SQL query to execute.
void exitNow(int exitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return.
PGconn * pgConn
Database connection.
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...
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
FUNCTION void removeOrphanedRows()
remove orphaned rows from fossology database
FUNCTION void verifyFilePerms(int fix)
Verify and optionally fix file permissions.
FUNCTION void deleteOldGold(char *date)
Delete gold files which are older than specified date.
FUNCTION void removeTemps()
Remove orphaned temp tables from deprecated pkgmettagetta and old delagent.
fo_dbManager * dbManager
fo_dbManager object
PGresult * PQexecCheck(int exitNumber, char *SQL, char *file, const int line)
simple wrapper which includes PQexec and fo_checkPQcommand
FUNCTION void reIndexAllTables()
reindex of all indexes in fossology database
FUNCTION void removeOldLogFiles(const char *olderThan)
FUNCTION void removeUploads()
Remove Uploads with no pfiles.
FUNCTION void removeOrphanedLogFiles()
FUNCTION void PQexecCheckClear(int exitNumber, char *SQL, char *file, const int line)
Execute SQL query and create the result and clear the result.
FUNCTION void processExpired()
Process expired uploads (slow)
FUNCTION void removeExpiredTokens(long int retentionPeriod)
remove expired personal access tokens from fossology database
FUNCTION void validateFolders()
Validate folder and foldercontents tables.
FUNCTION void removeOrphanedFiles()
Remove orphaned files from the repository (slow) Loop through each file in the repository and make su...
FUNCTION void vacAnalyze()
Do database vacuum and analyze.
FUNCTION void deleteOrphanGold()
Delete orphaned gold files from the repository.
FUNCTION void normalizeUploadPriorities()
Normalize priority of Uploads.
FUNCTION void recurseDir(const char *type, char *path, int level)
Recursively read directory till level is 0.
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.