+static bool list_nextvol(UAContext *ua)
+{
+ JOB *job;
+ JCR *jcr = ua->jcr;
+ POOL *pool;
+ RUN *run;
+ time_t runtime;
+ bool found = false;
+ MEDIA_DBR mr;
+
+ memset(&mr, 0, sizeof(mr));
+ int i = find_arg_with_value(ua, "job");
+ if (i <= 0) {
+ if ((job = select_job_resource(ua)) == NULL) {
+ return false;
+ }
+ } else {
+ job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
+ if (!job) {
+ Jmsg(jcr, M_ERROR, 0, _("%s is not a job name.\n"), ua->argv[i]);
+ if ((job = select_job_resource(ua)) == NULL) {
+ return false;
+ }
+ }
+ }
+ for (run=NULL; (run = find_next_run(run, job, runtime)); ) {
+ pool = run->pool ? run->pool : NULL;
+ if (!complete_jcr_for_job(jcr, job, pool)) {
+ return false;
+ }
+ mr.PoolId = jcr->PoolId;
+ if (run->storage) {
+ jcr->store = run->storage;
+ }
+ if (!find_next_volume_for_append(jcr, &mr, 0)) {
+ bsendmsg(ua, _("Could not find next Volume.\n"));
+ } else {
+ bsendmsg(ua, _("The next Volume to be used by Job \"%s\" will be %s\n"),
+ job->hdr.name, mr.VolumeName);
+ found = true;
+ }
+ if (jcr->db && jcr->db != ua->db) {
+ db_close_database(jcr, jcr->db);
+ jcr->db = NULL;
+ }
+ }
+ if (!found) {
+ bsendmsg(ua, _("Could not find next Volume.\n"));
+ return false;
+ }
+ return true;
+}
+
+
+/*
+ * For a given job, we examine all his run records
+ * to see if it is scheduled today or tomorrow.
+ */
+RUN *find_next_run(RUN *run, JOB *job, time_t &runtime)
+{
+ time_t now, tomorrow;
+ SCHED *sched;
+ struct tm tm;
+ int mday, wday, month, wom, tmday, twday, tmonth, twom, i;
+ int woy, twoy;
+ int tod, tom;
+
+ sched = job->schedule;
+ if (sched == NULL) { /* scheduled? */
+ return NULL; /* no nothing to report */
+ }
+ /* Break down current time into components */
+ now = time(NULL);
+ localtime_r(&now, &tm);
+ mday = tm.tm_mday - 1;
+ wday = tm.tm_wday;
+ month = tm.tm_mon;
+ wom = mday / 7;
+ woy = tm_woy(now);
+
+ /* Break down tomorrow into components */
+ tomorrow = now + 60 * 60 * 24;
+ localtime_r(&tomorrow, &tm);
+ tmday = tm.tm_mday - 1;
+ twday = tm.tm_wday;
+ tmonth = tm.tm_mon;
+ twom = tmday / 7;
+ twoy = tm_woy(tomorrow);
+
+ if (run == NULL) {
+ run = sched->run;
+ } else {
+ run = run->next;
+ }
+ for ( ; run; run=run->next) {
+ /*
+ * Find runs in next 24 hours
+ */
+ tod = bit_is_set(mday, run->mday) && bit_is_set(wday, run->wday) &&
+ bit_is_set(month, run->month) && bit_is_set(wom, run->wom) &&
+ bit_is_set(woy, run->woy);
+
+ tom = bit_is_set(tmday, run->mday) && bit_is_set(twday, run->wday) &&
+ bit_is_set(tmonth, run->month) && bit_is_set(twom, run->wom) &&
+ bit_is_set(twoy, run->woy);
+
+#ifdef xxx
+ Dmsg2(000, "tod=%d tom=%d\n", tod, tom);
+ Dmsg1(000, "bit_set_mday=%d\n", bit_is_set(mday, run->mday));
+ Dmsg1(000, "bit_set_wday=%d\n", bit_is_set(wday, run->wday));
+ Dmsg1(000, "bit_set_month=%d\n", bit_is_set(month, run->month));
+ Dmsg1(000, "bit_set_wom=%d\n", bit_is_set(wom, run->wom));
+ Dmsg1(000, "bit_set_woy=%d\n", bit_is_set(woy, run->woy));
+#endif
+ if (tod) { /* Jobs scheduled today (next 24 hours) */
+#ifdef xxx
+ char buf[300], num[10];
+ bsnprintf(buf, sizeof(buf), "tm.hour=%d hour=", tm.tm_hour);
+ for (i=0; i<24; i++) {
+ if (bit_is_set(i, run->hour)) {
+ bsnprintf(num, sizeof(num), "%d ", i);
+ bstrncat(buf, num, sizeof(buf));
+ }
+ }
+ bstrncat(buf, "\n", sizeof(buf));
+ Dmsg1(000, "%s", buf);
+#endif
+ /* find time (time_t) job is to be run */
+ localtime_r(&now, &tm);
+ for (i=tm.tm_hour; i < 24; i++) {
+ if (bit_is_set(i, run->hour)) {
+ tm.tm_hour = i;
+ tm.tm_min = run->minute;
+ tm.tm_sec = 0;
+ runtime = mktime(&tm);
+ Dmsg2(200, "now=%d runtime=%d\n", now, runtime);
+ if (runtime > now) {
+ Dmsg2(200, "Found it level=%d %c\n", run->level, run->level);
+ return run; /* found it, return run resource */
+ }
+ }
+ }
+ }
+
+// Dmsg2(200, "runtime=%d now=%d\n", runtime, now);
+ if (tom) { /* look at jobs scheduled tomorrow */
+ localtime_r(&tomorrow, &tm);
+ for (i=0; i < 24; i++) {
+ if (bit_is_set(i, run->hour)) {
+ tm.tm_hour = i;
+ tm.tm_min = run->minute;
+ tm.tm_sec = 0;
+ runtime = mktime(&tm);
+ Dmsg2(200, "now=%d runtime=%d\n", now, runtime);
+ if (runtime < tomorrow) {
+ Dmsg2(200, "Found it level=%d %c\n", run->level, run->level);
+ return run; /* found it, return run resource */
+ }
+ }
+ }
+ }
+ } /* end for loop over runs */
+ /* Nothing found */
+ return NULL;
+}
+/*
+ * Fill in the remaining fields of the jcr as if it
+ * is going to run the job.
+ */
+int complete_jcr_for_job(JCR *jcr, JOB *job, POOL *pool)
+{
+ POOL_DBR pr;
+
+ memset(&pr, 0, sizeof(POOL_DBR));
+ set_jcr_defaults(jcr, job);
+ if (pool) {
+ jcr->pool = pool; /* override */
+ }
+ jcr->db = jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
+ jcr->catalog->db_password, jcr->catalog->db_address,
+ jcr->catalog->db_port, jcr->catalog->db_socket,
+ jcr->catalog->mult_db_connections);
+ if (!jcr->db || !db_open_database(jcr, jcr->db)) {
+ Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
+ jcr->catalog->db_name);
+ if (jcr->db) {
+ Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
+ }
+ return 0;
+ }
+ bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name));
+ while (!db_get_pool_record(jcr, jcr->db, &pr)) { /* get by Name */
+ /* Try to create the pool */
+ if (create_pool(jcr, jcr->db, jcr->pool, POOL_OP_CREATE) < 0) {
+ Jmsg(jcr, M_FATAL, 0, _("Pool %s not in database. %s"), pr.Name,
+ db_strerror(jcr->db));
+ if (jcr->db) {
+ db_close_database(jcr, jcr->db);
+ jcr->db = NULL;
+ }
+ return 0;
+ } else {
+ Jmsg(jcr, M_INFO, 0, _("Pool %s created in database.\n"), pr.Name);
+ }
+ }
+ jcr->PoolId = pr.PoolId;
+ jcr->jr.PoolId = pr.PoolId;
+ return 1;
+}
+
+