Skip to content

Commit

Permalink
change job_code_callback_t to return optional<string>
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelBoerlin authored and arogge committed Aug 5, 2023
1 parent e86a527 commit e1808c1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 30 deletions.
37 changes: 17 additions & 20 deletions core/src/dird/dird_conf.cc
Expand Up @@ -3083,76 +3083,73 @@ static void StoreRunscript(LEX* lc, ResourceItem* item, int index, int pass)
* %m = Prev Backup JobId
* %M = New Backup JobId
*/
extern "C" char* job_code_callback_director(JobControlRecord* jcr,
extern "C" std::optional<std::string> job_code_callback_director(JobControlRecord* jcr,
const char* param)
{
static char yes[] = "yes";
static char no[] = "no";

static char str[50];
static const std::string yes = "yes";
static const std::string no = "no";
static const std::string none = "*None*";

switch (param[0]) {
case 'f':
if (jcr->dir_impl->res.fileset) {
return jcr->dir_impl->res.fileset->resource_name_;
return std::string(jcr->dir_impl->res.fileset->resource_name_);
}
break;
case 'h':
if (jcr->dir_impl->res.client) {
return jcr->dir_impl->res.client->address;
return std::string(jcr->dir_impl->res.client->address);
}
break;
case 'p':
if (jcr->dir_impl->res.pool) {
return jcr->dir_impl->res.pool->resource_name_;
return std::string(jcr->dir_impl->res.pool->resource_name_);
}
break;
case 'w':
if (jcr->dir_impl->res.write_storage) {
return jcr->dir_impl->res.write_storage->resource_name_;
return std::string(jcr->dir_impl->res.write_storage->resource_name_);
}
break;
case 'x':
return jcr->dir_impl->spool_data ? yes : no;
case 'C':
return jcr->dir_impl->cloned ? yes : no;
case 'D':
return my_name;
return std::string(my_name);
case 'V':
if (jcr) {
/* If this is a migration/copy we need the volume name from the
* mig_jcr. */
if (jcr->dir_impl->mig_jcr) { jcr = jcr->dir_impl->mig_jcr; }

if (jcr->VolumeName) {
return jcr->VolumeName;
return std::string(jcr->VolumeName);
} else {
return (char*)_("*None*");
return none;
}
} else {
return (char*)_("*None*");
return none;
}
break;
case 'm': /* Migration/copy job prev jobid */
if (jcr && jcr->dir_impl && jcr->dir_impl->previous_jr.JobId) {
Bsnprintf(str, sizeof(str), "%d", jcr->dir_impl->previous_jr.JobId);
return str;
return std::to_string(jcr->dir_impl->previous_jr.JobId);
} else {
return (char*)_("*None*");
return none;
}
break;
case 'M': /* Migration/copy job new jobid */
if (jcr && jcr->dir_impl && jcr->dir_impl->mig_jcr && jcr->dir_impl->mig_jcr->dir_impl
&& jcr->dir_impl->mig_jcr->dir_impl->jr.JobId) {
Bsnprintf(str, sizeof(str), "%d", jcr->dir_impl->mig_jcr->dir_impl->jr.JobId);
return str;
return std::to_string(jcr->dir_impl->mig_jcr->dir_impl->jr.JobId);
} else {
return (char*)_("*None*");
return none;
}
break;
}

return NULL;
return std::nullopt;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/dird/dird_conf.h
Expand Up @@ -679,7 +679,7 @@ bool print_datatype_schema_json(PoolMem& buffer,
json_t* json_datatype(const int type, ResourceItem items[]);
const char* AuthenticationProtocolTypeToString(uint32_t auth_protocol);
const char* JobLevelToString(int level);
extern "C" char* job_code_callback_director(JobControlRecord* jcr, const char*);
extern "C" std::optional<std::string> job_code_callback_director(JobControlRecord* jcr, const char*);
const char* GetUsageStringForConsoleConfigureCommand();
void DestroyConfigureUsageString();
bool PopulateDefs();
Expand Down
2 changes: 1 addition & 1 deletion core/src/filed/dir_cmd.cc
Expand Up @@ -94,7 +94,7 @@ static alist<pthread_t*>* client_initiated_connection_threads = nullptr;
extern bool AccurateCmd(JobControlRecord* jcr);
extern bool StatusCmd(JobControlRecord* jcr);
extern bool QstatusCmd(JobControlRecord* jcr);
extern "C" char* job_code_callback_filed(JobControlRecord* jcr,
extern "C" std::optional<std::string> job_code_callback_filed(JobControlRecord* jcr,
const char* param);

/* Forward referenced functions */
Expand Down
10 changes: 4 additions & 6 deletions core/src/filed/fileset.cc
Expand Up @@ -54,22 +54,20 @@ namespace filedaemon {
* %D = Director
* %m = Modification time (for incremental and differential)
*/
extern "C" char* job_code_callback_filed(JobControlRecord* jcr,
extern "C" std::optional<std::string> job_code_callback_filed(JobControlRecord* jcr,
const char* param)
{
static char str[50];

switch (param[0]) {
case 'D':
if (jcr->fd_impl->director) {
return jcr->fd_impl->director->resource_name_;
return std::string(jcr->fd_impl->director->resource_name_);
}
break;
case 'm':
return edit_uint64(jcr->fd_impl->since_time, str);
return std::to_string(jcr->fd_impl->since_time);
}

return NULL;
return std::nullopt;
}

bool InitFileset(JobControlRecord* jcr)
Expand Down
3 changes: 2 additions & 1 deletion core/src/lib/message.h
Expand Up @@ -38,11 +38,12 @@
#include <functional>
#include <string>
#include <vector>
#include <optional>

class JobControlRecord;

extern "C" {
typedef char* (*job_code_callback_t)(JobControlRecord*, const char*);
typedef std::optional<std::string> (*job_code_callback_t)(JobControlRecord*, const char*);
}

void Jmsg(JobControlRecord* jcr, int type, utime_t mtime, const char* fmt, ...);
Expand Down
8 changes: 7 additions & 1 deletion core/src/lib/util.cc
Expand Up @@ -924,7 +924,13 @@ POOLMEM* edit_job_codes(JobControlRecord* jcr,
break;
default:
str = NULL;
if (callback != NULL) { str = callback(jcr, p); }
if (callback != NULL) {
auto callback_result = callback(jcr, p);
if (callback_result) {
Bsnprintf(add, sizeof(add), "%s", callback_result->c_str());
str = add;
}
}

if (!str) {
add[0] = '%';
Expand Down

0 comments on commit e1808c1

Please sign in to comment.