+/*
+ Bacula® - The Network Backup Solution
+
+ Copyright (C) 2001-2009 Free Software Foundation Europe e.V.
+
+ The main author of Bacula is Kern Sibbald, with contributions from
+ many others, a complete list can be found in the file AUTHORS.
+ This program is Free Software; you can redistribute it and/or
+ modify it under the terms of version three of the GNU Affero General Public
+ License as published by the Free Software Foundation and included
+ in the file LICENSE.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA.
+
+ Bacula® is a registered trademark of Kern Sibbald.
+ The licensor of Bacula is the Free Software Foundation Europe
+ (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
+ Switzerland, email:ftf@fsfeurope.org.
+*/
/*
*
* Bacula Director -- User Agent Prompt and Selection code
*
* Kern Sibbald, October MMI
*
- * Version $Id$
- */
-/*
- Copyright (C) 2001-2006 Kern Sibbald
-
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- version 2 as amended with additional clauses defined in the
- file LICENSE in the main source directory.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- the file LICENSE for additional details.
-
*/
#include "bacula.h"
char ed1[100];
int val;
+ int yes_in_arg = find_arg(ua, NT_("yes"));
+
for ( ;; ) {
- bsendmsg(ua, _("The current %s retention period is: %s\n"),
+ ua->info_msg(_("The current %s retention period is: %s\n"),
msg, edit_utime(*ret, ed1, sizeof(ed1)));
+ if (yes_in_arg != -1) {
+ return 1;
+ }
if (!get_cmd(ua, _("Continue? (yes/mod/no): "))) {
return 0;
}
return 0;
}
if (!duration_to_utime(ua->cmd, ret)) {
- bsendmsg(ua, _("Invalid period.\n"));
+ ua->error_msg(_("Invalid period.\n"));
continue;
}
continue;
start_prompt(ua, _("The defined Storage resources are:\n"));
LockRes();
foreach_res(store, R_STORAGE) {
- if (acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
- add_prompt(ua, store->hdr.name);
+ if (acl_access_ok(ua, Storage_ACL, store->name())) {
+ add_prompt(ua, store->name());
}
}
UnlockRes();
start_prompt(ua, _("The defined FileSet resources are:\n"));
LockRes();
foreach_res(fs, R_FILESET) {
- if (acl_access_ok(ua, FileSet_ACL, fs->hdr.name)) {
- add_prompt(ua, fs->hdr.name);
+ if (acl_access_ok(ua, FileSet_ACL, fs->name())) {
+ add_prompt(ua, fs->name());
}
}
UnlockRes();
}
}
}
+ if (ua->gui && !catalog) {
+ LockRes();
+ catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
+ UnlockRes();
+ if (!catalog) {
+ ua->error_msg(_("Could not find a Catalog resource\n"));
+ return NULL;
+ } else if (!acl_access_ok(ua, Catalog_ACL, catalog->name())) {
+ ua->error_msg(_("You must specify a \"use <catalog-name>\" command before continuing.\n"));
+ return NULL;
+ }
+ return catalog;
+ }
if (!catalog) {
start_prompt(ua, _("The defined Catalog resources are:\n"));
LockRes();
foreach_res(catalog, R_CATALOG) {
- if (acl_access_ok(ua, Catalog_ACL, catalog->hdr.name)) {
- add_prompt(ua, catalog->hdr.name);
+ if (acl_access_ok(ua, Catalog_ACL, catalog->name())) {
+ add_prompt(ua, catalog->name());
}
}
UnlockRes();
}
+/*
+ * Select a job to enable or disable
+ */
+JOB *select_enable_disable_job_resource(UAContext *ua, bool enable)
+{
+ char name[MAX_NAME_LENGTH];
+ JOB *job;
+
+ LockRes();
+ start_prompt(ua, _("The defined Job resources are:\n"));
+ foreach_res(job, R_JOB) {
+ if (!acl_access_ok(ua, Job_ACL, job->name())) {
+ continue;
+ }
+ if (job->enabled == enable) { /* Already enabled/disabled? */
+ continue; /* yes, skip */
+ }
+ add_prompt(ua, job->name());
+ }
+ UnlockRes();
+ if (do_prompt(ua, _("Job"), _("Select Job resource"), name, sizeof(name)) < 0) {
+ return NULL;
+ }
+ job = (JOB *)GetResWithName(R_JOB, name);
+ return job;
+}
+
/*
* Select a Job resource from prompt list
*/
start_prompt(ua, _("The defined Job resources are:\n"));
LockRes();
foreach_res(job, R_JOB) {
- if (acl_access_ok(ua, Job_ACL, job->hdr.name)) {
- add_prompt(ua, job->hdr.name);
+ if (acl_access_ok(ua, Job_ACL, job->name())) {
+ add_prompt(ua, job->name());
}
}
UnlockRes();
return job;
}
+/*
+ * Select a Restore Job resource from argument or prompt
+ */
+JOB *get_restore_job(UAContext *ua)
+{
+ JOB *job;
+ int i = find_arg_with_value(ua, "restore_job");
+ if (i >= 0 && acl_access_ok(ua, Job_ACL, ua->argv[i])) {
+ job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
+ if (job && job->JobType == JT_RESTORE) {
+ return job;
+ }
+ ua->error_msg(_("Error: Restore Job resource \"%s\" does not exist.\n"),
+ ua->argv[i]);
+ }
+ return select_restore_job_resource(ua);
+}
+
/*
* Select a Restore Job resource from prompt list
*/
start_prompt(ua, _("The defined Restore Job resources are:\n"));
LockRes();
foreach_res(job, R_JOB) {
- if (job->JobType == JT_RESTORE && acl_access_ok(ua, Job_ACL, job->hdr.name)) {
- add_prompt(ua, job->hdr.name);
+ if (job->JobType == JT_RESTORE && acl_access_ok(ua, Job_ACL, job->name())) {
+ add_prompt(ua, job->name());
}
}
UnlockRes();
start_prompt(ua, _("The defined Client resources are:\n"));
LockRes();
foreach_res(client, R_CLIENT) {
- if (acl_access_ok(ua, Client_ACL, client->hdr.name)) {
- add_prompt(ua, client->hdr.name);
+ if (acl_access_ok(ua, Client_ACL, client->name())) {
+ add_prompt(ua, client->name());
}
}
UnlockRes();
if (client) {
return client;
}
- bsendmsg(ua, _("Error: Client resource %s does not exist.\n"), ua->argv[i]);
+ ua->error_msg(_("Error: Client resource %s does not exist.\n"), ua->argv[i]);
break;
}
}
* returns: 0 on error
* 1 on success and fills in CLIENT_DBR
*/
-int get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
+bool get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
{
int i;
if (db_get_client_record(ua->jcr, ua->db, cr)) {
return 1;
}
- bsendmsg(ua, _("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db));
+ ua->error_msg(_("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db));
}
for (i=1; i<ua->argc; i++) {
if ((strcasecmp(ua->argk[i], NT_("client")) == 0 ||
}
bstrncpy(cr->Name, ua->argv[i], sizeof(cr->Name));
if (!db_get_client_record(ua->jcr, ua->db, cr)) {
- bsendmsg(ua, _("Could not find Client \"%s\": ERR=%s"), ua->argv[i],
+ ua->error_msg(_("Could not find Client \"%s\": ERR=%s"), ua->argv[i],
db_strerror(ua->db));
cr->ClientId = 0;
break;
* Returns 1 on success
* 0 on failure
*/
-int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
+bool select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
{
CLIENT_DBR ocr;
char name[MAX_NAME_LENGTH];
cr->ClientId = 0;
if (!db_get_client_ids(ua->jcr, ua->db, &num_clients, &ids)) {
- bsendmsg(ua, _("Error obtaining client ids. ERR=%s\n"), db_strerror(ua->db));
+ ua->error_msg(_("Error obtaining client ids. ERR=%s\n"), db_strerror(ua->db));
return 0;
}
if (num_clients <= 0) {
- bsendmsg(ua, _("No clients defined. You must run a job before using this command.\n"));
+ ua->error_msg(_("No clients defined. You must run a job before using this command.\n"));
return 0;
}
bstrncpy(ocr.Name, name, sizeof(ocr.Name));
if (!db_get_client_record(ua->jcr, ua->db, &ocr)) {
- bsendmsg(ua, _("Could not find Client \"%s\": ERR=%s"), name, db_strerror(ua->db));
+ ua->error_msg(_("Could not find Client \"%s\": ERR=%s"), name, db_strerror(ua->db));
return 0;
}
memcpy(cr, &ocr, sizeof(ocr));
return 1;
}
-
-
/* Scan what the user has entered looking for:
*
- * pool=<pool-name>
+ * argk=<pool-name>
+ *
+ * where argk can be : pool, recyclepool, scratchpool, nextpool etc..
*
* if error or not found, put up a list of pool DBRs
* to choose from.
* returns: false on error
* true on success and fills in POOL_DBR
*/
-bool get_pool_dbr(UAContext *ua, POOL_DBR *pr)
+bool get_pool_dbr(UAContext *ua, POOL_DBR *pr, const char *argk)
{
if (pr->Name[0]) { /* If name already supplied */
if (db_get_pool_record(ua->jcr, ua->db, pr) &&
acl_access_ok(ua, Pool_ACL, pr->Name)) {
return true;
}
- bsendmsg(ua, _("Could not find Pool \"%s\": ERR=%s"), pr->Name, db_strerror(ua->db));
+ ua->error_msg(_("Could not find Pool \"%s\": ERR=%s"), pr->Name, db_strerror(ua->db));
}
- if (!select_pool_dbr(ua, pr)) { /* try once more */
+ if (!select_pool_dbr(ua, pr, argk)) { /* try once more */
return false;
}
return true;
}
/*
- * Select a Pool record from the catalog
+ * Select a Pool record from catalog
+ * argk can be pool, recyclepool, scratchpool etc..
*/
-bool select_pool_dbr(UAContext *ua, POOL_DBR *pr)
+bool select_pool_dbr(UAContext *ua, POOL_DBR *pr, const char *argk)
{
POOL_DBR opr;
char name[MAX_NAME_LENGTH];
uint32_t *ids;
for (i=1; i<ua->argc; i++) {
- if (strcasecmp(ua->argk[i], NT_("pool")) == 0 && ua->argv[i] &&
+ if (strcasecmp(ua->argk[i], argk) == 0 && ua->argv[i] &&
acl_access_ok(ua, Pool_ACL, ua->argv[i])) {
bstrncpy(pr->Name, ua->argv[i], sizeof(pr->Name));
if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
- bsendmsg(ua, _("Could not find Pool \"%s\": ERR=%s"), ua->argv[i],
+ ua->error_msg(_("Could not find Pool \"%s\": ERR=%s"), ua->argv[i],
db_strerror(ua->db));
pr->PoolId = 0;
break;
pr->PoolId = 0;
if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) {
- bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
+ ua->error_msg(_("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
return 0;
}
if (num_pools <= 0) {
- bsendmsg(ua, _("No pools defined. Use the \"create\" command to create one.\n"));
+ ua->error_msg(_("No pools defined. Use the \"create\" command to create one.\n"));
return false;
}
start_prompt(ua, _("Defined Pools:\n"));
+ if (bstrcmp(argk, NT_("recyclepool"))) {
+ add_prompt(ua, _("*None*"));
+ }
for (i=0; i < num_pools; i++) {
opr.PoolId = ids[i];
if (!db_get_pool_record(ua->jcr, ua->db, &opr) ||
if (do_prompt(ua, _("Pool"), _("Select the Pool"), name, sizeof(name)) < 0) {
return false;
}
- memset(&opr, 0, sizeof(opr));
- bstrncpy(opr.Name, name, sizeof(opr.Name));
- if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
- bsendmsg(ua, _("Could not find Pool \"%s\": ERR=%s"), name, db_strerror(ua->db));
- return false;
+ memset(&opr, 0, sizeof(opr));
+ /* *None* is only returned when selecting a recyclepool, and in that case
+ * the calling code is only interested in opr.Name, so then we can leave
+ * pr as all zero.
+ */
+ if (!bstrcmp(name, _("*None*"))) {
+ bstrncpy(opr.Name, name, sizeof(opr.Name));
+
+ if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
+ ua->error_msg(_("Could not find Pool \"%s\": ERR=%s"), name, db_strerror(ua->db));
+ return false;
+ }
}
+
memcpy(pr, &opr, sizeof(opr));
return true;
}
memset(pr, 0, sizeof(POOL_DBR));
pr->PoolId = mr->PoolId;
if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
- bsendmsg(ua, "%s", db_strerror(ua->db));
+ ua->error_msg("%s", db_strerror(ua->db));
return 0;
}
if (!acl_access_ok(ua, Pool_ACL, pr->Name)) {
- bsendmsg(ua, _("No access to Pool \"%s\"\n"), pr->Name);
+ ua->error_msg(_("No access to Pool \"%s\"\n"), pr->Name);
return 0;
}
return 1;
int select_media_dbr(UAContext *ua, MEDIA_DBR *mr)
{
int i;
+ int ret = 0;
+ POOLMEM *err = get_pool_memory(PM_FNAME);
+ *err=0;
memset(mr, 0, sizeof(MEDIA_DBR));
i = find_arg_with_value(ua, "volume");
if (i >= 0) {
- bstrncpy(mr->VolumeName, ua->argv[i], sizeof(mr->VolumeName));
+ if (is_name_valid(ua->argv[i], &err)) {
+ bstrncpy(mr->VolumeName, ua->argv[i], sizeof(mr->VolumeName));
+ } else {
+ goto bail_out;
+ }
}
if (mr->VolumeName[0] == 0) {
POOL_DBR pr;
memset(&pr, 0, sizeof(pr));
/* Get the pool from pool=<pool-name> */
if (!get_pool_dbr(ua, &pr)) {
- return 0;
+ goto bail_out;
}
mr->PoolId = pr.PoolId;
db_list_media_records(ua->jcr, ua->db, mr, prtit, ua, HORZ_LIST);
- if (!get_cmd(ua, _("Enter MediaId or Volume name: "))) {
- return 0;
+ if (!get_cmd(ua, _("Enter *MediaId or Volume name: "))) {
+ goto bail_out;
}
- if (is_a_number(ua->cmd)) {
- mr->MediaId = str_to_int64(ua->cmd);
- } else {
+ if (ua->cmd[0] == '*' && is_a_number(ua->cmd+1)) {
+ mr->MediaId = str_to_int64(ua->cmd+1);
+ } else if (is_name_valid(ua->cmd, &err)) {
bstrncpy(mr->VolumeName, ua->cmd, sizeof(mr->VolumeName));
+ } else {
+ goto bail_out;
}
}
if (!db_get_media_record(ua->jcr, ua->db, mr)) {
- bsendmsg(ua, "%s", db_strerror(ua->db));
- return 0;
+ pm_strcpy(err, db_strerror(ua->db));
+ goto bail_out;
}
- return 1;
+ ret = 1;
+
+bail_out:
+ if (!ret && *err) {
+ ua->error_msg("%s", err);
+ }
+ free_pool_memory(err);
+ return ret;
}
start_prompt(ua, _("The defined Pool resources are:\n"));
LockRes();
foreach_res(pool, R_POOL) {
- if (acl_access_ok(ua, Pool_ACL, pool->hdr.name)) {
- add_prompt(ua, pool->hdr.name);
+ if (acl_access_ok(ua, Pool_ACL, pool->name())) {
+ add_prompt(ua, pool->name());
}
}
UnlockRes();
if (pool) {
return pool;
}
- bsendmsg(ua, _("Error: Pool resource \"%s\" does not exist.\n"), ua->argv[i]);
+ ua->error_msg(_("Error: Pool resource \"%s\" does not exist.\n"), ua->argv[i]);
}
return select_pool_resource(ua);
}
}
jr->JobId = ua->int64_val;
if (!db_get_job_record(ua->jcr, ua->db, jr)) {
- bsendmsg(ua, "%s", db_strerror(ua->db));
+ ua->error_msg("%s", db_strerror(ua->db));
return 0;
}
return jr->JobId;
continue;
}
if (!db_get_job_record(ua->jcr, ua->db, jr)) {
- bsendmsg(ua, _("Could not find Job \"%s\": ERR=%s"), ua->argv[i],
+ ua->error_msg(_("Could not find Job \"%s\": ERR=%s"), ua->argv[i],
db_strerror(ua->db));
jr->JobId = 0;
break;
* is copied to prompt if not NULL
* prompt is set to the chosen prompt item string
*/
-int do_prompt(UAContext *ua, const char *automsg, const char *msg, char *prompt, int max_prompt)
+int do_prompt(UAContext *ua, const char *automsg, const char *msg,
+ char *prompt, int max_prompt)
{
int i, item;
char pmsg[MAXSTRING];
+ BSOCK *user = ua->UA_sock;
if (prompt) {
*prompt = 0;
if (prompt) {
bstrncpy(prompt, ua->prompt[1], max_prompt);
}
- bsendmsg(ua, _("Automatically selected %s: %s\n"), automsg, ua->prompt[1]);
+ ua->send_msg(_("Automatically selected %s: %s\n"), automsg, ua->prompt[1]);
goto done;
}
/* If running non-interactive, bail out */
if (ua->batch) {
- bsendmsg(ua, _("Cannot select %s in batch mode.\n"), automsg);
+ /* First print the choices he wanted to make */
+ ua->send_msg(ua->prompt[0]);
+ for (i=1; i < ua->num_prompts; i++) {
+ ua->send_msg("%6d: %s\n", i, ua->prompt[i]);
+ }
+ /* Now print error message */
+ ua->send_msg(_("Your request has multiple choices for \"%s\". Selection is not possible in batch mode.\n"), automsg);
item = -1;
goto done;
}
-// bnet_sig(ua->UA_sock, BNET_START_SELECT);
- bsendmsg(ua, ua->prompt[0]);
+ if (ua->api) user->signal(BNET_START_SELECT);
+ ua->send_msg(ua->prompt[0]);
for (i=1; i < ua->num_prompts; i++) {
- bsendmsg(ua, "%6d: %s\n", i, ua->prompt[i]);
+ if (ua->api) {
+ ua->send_msg("%s", ua->prompt[i]);
+ } else {
+ ua->send_msg("%6d: %s\n", i, ua->prompt[i]);
+ }
}
-// bnet_sig(ua->UA_sock, BNET_END_SELECT);
+ if (ua->api) user->signal(BNET_END_SELECT);
for ( ;; ) {
/* First item is the prompt string, not the items */
if (ua->num_prompts == 1) {
- bsendmsg(ua, _("Selection is empty!\n"));
+ ua->error_msg(_("Selection list for \"%s\" is empty!\n"), automsg);
item = -1; /* list is empty ! */
break;
}
if (ua->num_prompts == 2) {
item = 1;
- bsendmsg(ua, _("Item 1 selected automatically.\n"));
+ ua->send_msg(_("Automatically selected: %s\n"), ua->prompt[1]);
if (prompt) {
bstrncpy(prompt, ua->prompt[1], max_prompt);
}
sprintf(pmsg, "%s (1-%d): ", msg, ua->num_prompts-1);
}
/* Either a . or an @ will get you out of the loop */
+ if (ua->api) user->signal(BNET_SELECT_INPUT);
if (!get_pint(ua, pmsg)) {
item = -1; /* error */
- bsendmsg(ua, _("Selection aborted, nothing done.\n"));
+ ua->info_msg(_("Selection aborted, nothing done.\n"));
break;
}
item = ua->pint32_val;
if (item < 1 || item >= ua->num_prompts) {
- bsendmsg(ua, _("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
+ ua->warning_msg(_("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
continue;
}
if (prompt) {
}
/* Default argument is storage */
if (store_name) {
- bsendmsg(ua, _("Storage name given twice.\n"));
+ ua->error_msg(_("Storage name given twice.\n"));
return NULL;
}
store_name = ua->argk[i];
} else if (strcasecmp(ua->argk[i], NT_("jobid")) == 0) {
jobid = str_to_int64(ua->argv[i]);
if (jobid <= 0) {
- bsendmsg(ua, _("Expecting jobid=nn command, got: %s\n"), ua->argk[i]);
+ ua->error_msg(_("Expecting jobid=nn command, got: %s\n"), ua->argk[i]);
return NULL;
}
if (!(jcr=get_jcr_by_id(jobid))) {
- bsendmsg(ua, _("JobId %s is not running.\n"), edit_int64(jobid, ed1));
+ ua->error_msg(_("JobId %s is not running.\n"), edit_int64(jobid, ed1));
return NULL;
}
store = jcr->wstore;
} else if (strcasecmp(ua->argk[i], NT_("job")) == 0 ||
strcasecmp(ua->argk[i], NT_("jobname")) == 0) {
if (!ua->argv[i]) {
- bsendmsg(ua, _("Expecting job=xxx, got: %s.\n"), ua->argk[i]);
+ ua->error_msg(_("Expecting job=xxx, got: %s.\n"), ua->argk[i]);
return NULL;
}
if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
- bsendmsg(ua, _("Job \"%s\" is not running.\n"), ua->argv[i]);
+ ua->error_msg(_("Job \"%s\" is not running.\n"), ua->argv[i]);
return NULL;
}
store = jcr->wstore;
break;
} else if (strcasecmp(ua->argk[i], NT_("ujobid")) == 0) {
if (!ua->argv[i]) {
- bsendmsg(ua, _("Expecting ujobid=xxx, got: %s.\n"), ua->argk[i]);
+ ua->error_msg(_("Expecting ujobid=xxx, got: %s.\n"), ua->argk[i]);
return NULL;
}
if (!(jcr=get_jcr_by_full_name(ua->argv[i]))) {
- bsendmsg(ua, _("Job \"%s\" is not running.\n"), ua->argv[i]);
+ ua->error_msg(_("Job \"%s\" is not running.\n"), ua->argv[i]);
return NULL;
}
store = jcr->wstore;
}
}
}
- if (store && !acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
+ if (store && !acl_access_ok(ua, Storage_ACL, store->name())) {
store = NULL;
}
if (!store && store_name && store_name[0] != 0) {
store = (STORE *)GetResWithName(R_STORAGE, store_name);
if (!store) {
- bsendmsg(ua, _("Storage resource \"%s\": not found\n"), store_name);
+ ua->error_msg(_("Storage resource \"%s\": not found\n"), store_name);
}
}
- if (store && !acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
+ if (store && !acl_access_ok(ua, Storage_ACL, store->name())) {
store = NULL;
}
/* No keywords found, so present a selection list */
bool found = false;
for (int i=0; joblevels[i].level_name; i++) {
if (strcasecmp(level_name, joblevels[i].level_name) == 0) {
- jcr->JobLevel = joblevels[i].level;
+ jcr->setJobLevel(joblevels[i].level);
found = true;
break;
}
}
return found;
}
+
+/* Get a running job
+ * "reason" is used in user messages
+ * can be: cancel, limit, ...
+ * Returns: NULL on error
+ * JCR on success (should be free_jcr() after)
+ */
+JCR *select_running_job(UAContext *ua, const char *reason)
+{
+ int i;
+ int njobs = 0;
+ JCR *jcr = NULL;
+ char JobName[MAX_NAME_LENGTH];
+ char temp[256];
+
+ for (i=1; i<ua->argc; i++) {
+ if (strcasecmp(ua->argk[i], NT_("jobid")) == 0) {
+ uint32_t JobId;
+ JobId = str_to_int64(ua->argv[i]);
+ if (!JobId) {
+ break;
+ }
+ if (!(jcr=get_jcr_by_id(JobId))) {
+ ua->error_msg(_("JobId %s is not running. Use Job name to %s inactive jobs.\n"), ua->argv[i], _(reason));
+ return NULL;
+ }
+ break;
+ } else if (strcasecmp(ua->argk[i], NT_("job")) == 0) {
+ if (!ua->argv[i]) {
+ break;
+ }
+ if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
+ ua->warning_msg(_("Warning Job %s is not running. Continuing anyway ...\n"), ua->argv[i]);
+ jcr = new_jcr(sizeof(JCR), dird_free_jcr);
+ bstrncpy(jcr->Job, ua->argv[i], sizeof(jcr->Job));
+ }
+ break;
+ } else if (strcasecmp(ua->argk[i], NT_("ujobid")) == 0) {
+ if (!ua->argv[i]) {
+ break;
+ }
+ if (!(jcr=get_jcr_by_full_name(ua->argv[i]))) {
+ ua->warning_msg(_("Warning Job %s is not running. Continuing anyway ...\n"), ua->argv[i]);
+ jcr = new_jcr(sizeof(JCR), dird_free_jcr);
+ bstrncpy(jcr->Job, ua->argv[i], sizeof(jcr->Job));
+ }
+ break;
+ }
+
+ }
+ if (jcr) {
+ if (jcr->job && !acl_access_ok(ua, Job_ACL, jcr->job->name())) {
+ ua->error_msg(_("Unauthorized command from this console.\n"));
+ return NULL;
+ }
+ } else {
+ /*
+ * If we still do not have a jcr,
+ * throw up a list and ask the user to select one.
+ */
+ char buf[1000];
+ int tjobs = 0; /* total # number jobs */
+ /* Count Jobs running */
+ foreach_jcr(jcr) {
+ if (jcr->JobId == 0) { /* this is us */
+ continue;
+ }
+ tjobs++; /* count of all jobs */
+ if (!acl_access_ok(ua, Job_ACL, jcr->job->name())) {
+ continue; /* skip not authorized */
+ }
+ njobs++; /* count of authorized jobs */
+ }
+ endeach_jcr(jcr);
+
+ if (njobs == 0) { /* no authorized */
+ if (tjobs == 0) {
+ ua->send_msg(_("No Jobs running.\n"));
+ } else {
+ ua->send_msg(_("None of your jobs are running.\n"));
+ }
+ return NULL;
+ }
+
+ start_prompt(ua, _("Select Job:\n"));
+ foreach_jcr(jcr) {
+ char ed1[50];
+ if (jcr->JobId == 0) { /* this is us */
+ continue;
+ }
+ if (!acl_access_ok(ua, Job_ACL, jcr->job->name())) {
+ continue; /* skip not authorized */
+ }
+ bsnprintf(buf, sizeof(buf), _("JobId=%s Job=%s"), edit_int64(jcr->JobId, ed1), jcr->Job);
+ add_prompt(ua, buf);
+ }
+ endeach_jcr(jcr);
+ bsnprintf(temp, sizeof(temp), _("Choose Job to %s"), _(reason));
+ if (do_prompt(ua, _("Job"), temp, buf, sizeof(buf)) < 0) {
+ return NULL;
+ }
+ if (!strcmp(reason, "cancel")) {
+ if (ua->api && njobs == 1) {
+ char nbuf[1000];
+ bsnprintf(nbuf, sizeof(nbuf), _("Cancel: %s\n\n%s"), buf,
+ _("Confirm cancel?"));
+ if (!get_yesno(ua, nbuf) || ua->pint32_val == 0) {
+ return NULL;
+ }
+ } else {
+ if (njobs == 1) {
+ if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
+ return NULL;
+ }
+ }
+ }
+ }
+ sscanf(buf, "JobId=%d Job=%127s", &njobs, JobName);
+ jcr = get_jcr_by_full_name(JobName);
+ if (!jcr) {
+ ua->warning_msg(_("Job \"%s\" not found.\n"), JobName);
+ return NULL;
+ }
+ }
+ return jcr;
+}