12 #include "maintagent.h"
15 double now_monotonic_seconds(
void)
18 #if defined(CLOCK_MONOTONIC)
19 clock_gettime(CLOCK_MONOTONIC, &ts);
21 clock_gettime(CLOCK_REALTIME, &ts);
23 return ts.tv_sec + ts.tv_nsec / 1e9;
27 void log_action_start(
const char* action)
29 time_t t = time(NULL);
33 if (localtime_r(&t, &tm)) {
34 strftime(timestr,
sizeof(timestr),
"%Y-%m-%dT%H:%M:%S%z", &tm);
36 snprintf(timestr,
sizeof(timestr),
"%ld", (
long)t);
40 const char *fullpath = __FILE__;
41 const char *shortpath = strstr(fullpath,
"maintagent/");
42 if (!shortpath) shortpath = fullpath;
44 LOG_NOTICE(
"START %s %ld", action, (
long)t);
47 LOG_NOTICE(
"%s %s Action %s started at %ld", timestr, shortpath, action, (
long)t);
52 void log_action_end(
const char* action,
double start)
54 double end = now_monotonic_seconds();
55 double dur = end -
start;
56 time_t t = time(NULL);
60 if (localtime_r(&t, &tm)) {
61 strftime(timestr,
sizeof(timestr),
"%Y-%m-%dT%H:%M:%S%z", &tm);
63 snprintf(timestr,
sizeof(timestr),
"%ld", (
long)t);
67 const char *fullpath = __FILE__;
68 const char *shortpath = strstr(fullpath,
"maintagent/");
69 if (!shortpath) shortpath = fullpath;
71 LOG_NOTICE(
"END %s %ld duration=%.3f", action, (
long)t, dur);
74 LOG_NOTICE(
"%s %s Action %s ended at %ld (duration=%.3f s)", timestr, shortpath, action, (
long)t, dur);
90 if (exitVal) LOG_ERROR(
"Exiting with status %d", exitVal);
106 while ((*p = toupper(*p)))
131 FUNCTION
void recurseDir(
const char* type,
char* path,
int level)
134 struct dirent* entry;
138 LOG_ERROR(
"Unable to open dir: '%s'", path);
139 LOG_ERROR(
"Error: %s", strerror(errno));
142 while ((entry = readdir(dir)) != NULL)
144 if (strcmp(entry->d_name,
".") != 0 && strcmp(entry->d_name,
"..") != 0)
146 char nextpath[myBUFSIZ];
150 strncpy(nextpath, path, myBUFSIZ - 1);
151 strncat(nextpath,
"/", myBUFSIZ - 1);
152 strncat(nextpath, entry->d_name, myBUFSIZ - 1);
165 memset(sha1,
'\0', 41);
166 memset(md5,
'\0', 33);
167 strncpy(nextpath, entry->d_name, myBUFSIZ - 1);
168 ptr = strtok(nextpath, delim);
171 LOG_FATAL(
"Unable to split path '%s' for pfile.", nextpath);
174 strncpy(sha1, ptr, 40);
175 ptr = strtok(NULL, delim);
176 strncpy(md5, ptr, 32);
177 ptr = strtok(NULL, delim);
202 "SELECT pfile_pk FROM pfile "
203 "WHERE pfile_md5 = $1 AND pfile_sha1 = $2 AND pfile_size = $3) "
204 "SELECT 1 AS exists FROM uploadtree INNER JOIN pf "
205 "ON pf.pfile_pk = pfile_fk "
207 "SELECT 1 AS exists FROM upload INNER JOIN pf "
208 "ON pf.pfile_pk = pfile_fk;";
210 existsStatement = fo_dbManager_PrepareStamement(
dbManager,
"checkPfileExists",
211 sql,
char*,
char*,
long);
219 countTuples = PQntuples(result);
244 char filename[myBUFSIZ];
247 snprintf(filename, myBUFSIZ,
"%s.%s.%ld", sha1, md5, fsize);
249 unlink(goldFilePath);
PGconn * pgConn
Database connection.
int s
The socket that the CLI will use to communicate.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
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...
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
fo_dbManager * dbManager
fo_dbManager object
FUNCTION char * strtoupper(char *s)
Helper function to upper case a string.
FUNCTION void exitNow(int exitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return.
FUNCTION void checkPFileExists(char *sha1, char *md5, long fsize, const char *type)
Check if given checksums exists in DB, if not call deleteRepoFile()
FUNCTION void deleteRepoFile(char *sha1, char *md5, long fsize, const char *type)
Take a file checksum, generate repo path and call unlink()
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.
void fo_dbManager_free(fo_dbManager *dbManager)
Un-allocate the memory from a DB manager.
start($application)
start the application Assumes application is restartable via /etc/init.d/<script>....