]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/job.c
Fix breaks in bextract + in restore print only volumes to be actually used
[bacula/bacula] / bacula / src / dird / job.c
1 /*
2  *
3  *   Bacula Director Job processing routines
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    Version $Id$
8  */
9 /*
10    Copyright (C) 2000-2003 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "dird.h"
31
32 /* Forward referenced subroutines */
33 static void *job_thread(void *arg);
34 static char *edit_run_codes(JCR *jcr, char *omsg, char *imsg);
35 static void release_resource_locks(JCR *jcr);
36 static int acquire_resource_locks(JCR *jcr);
37 #ifdef USE_SEMAPHORE
38 static void backoff_resource_locks(JCR *jcr, int count);
39 #endif
40
41 /* Exported subroutines */
42 void run_job(JCR *jcr);
43
44
45 /* Imported subroutines */
46 extern void term_scheduler();
47 extern void term_ua_server();
48 extern int do_backup(JCR *jcr);
49 extern int do_admin(JCR *jcr);
50 extern int do_restore(JCR *jcr);
51 extern int do_verify(JCR *jcr);
52
53 #ifdef USE_SEMAPHORE
54 static semlock_t job_lock;
55 static pthread_mutex_t mutex;
56 static pthread_cond_t  resource_wait;
57 static int waiting = 0;               /* count of waiting threads */
58 #else
59 /* Queue of jobs to be run */
60 workq_t job_wq;                   /* our job work queue */
61 #endif
62
63 void init_job_server(int max_workers)
64 {
65    int stat;
66 #ifdef USE_SEMAPHORE
67    if ((stat = sem_init(&job_lock, max_workers)) != 0) {
68       Emsg1(M_ABORT, 0, _("Could not init job lock: ERR=%s\n"), strerror(stat));
69    }
70    if ((stat = pthread_mutex_init(&mutex, NULL)) != 0) {
71       Emsg1(M_ABORT, 0, _("Could not init resource mutex: ERR=%s\n"), strerror(stat));
72    }
73    if ((stat = pthread_cond_init(&resource_wait, NULL)) != 0) {
74       Emsg1(M_ABORT, 0, _("Could not init resource wait: ERR=%s\n"), strerror(stat));
75    }
76
77 #else
78    if ((stat = workq_init(&job_wq, max_workers, job_thread)) != 0) {
79       Emsg1(M_ABORT, 0, _("Could not init job work queue: ERR=%s\n"), strerror(stat));
80    }
81 #endif
82    return;
83 }
84
85 /*
86  * Run a job -- typically called by the scheduler, but may also
87  *              be called by the UA (Console program).
88  *
89  */
90 void run_job(JCR *jcr)
91 {
92    int stat, errstat;
93 #ifdef USE_SEMAPHORE
94    pthread_t tid;
95 #else
96    workq_ele_t *work_item;
97 #endif
98
99    sm_check(__FILE__, __LINE__, True);
100    init_msg(jcr, jcr->messages);
101    create_unique_job_name(jcr, jcr->job->hdr.name);
102    set_jcr_job_status(jcr, JS_Created);
103    jcr->jr.SchedTime = jcr->sched_time;
104    jcr->jr.StartTime = jcr->start_time;
105    jcr->jr.Type = jcr->JobType;
106    jcr->jr.Level = jcr->JobLevel;
107    jcr->jr.JobStatus = jcr->JobStatus;
108    bstrncpy(jcr->jr.Name, jcr->job->hdr.name, sizeof(jcr->jr.Name));
109    bstrncpy(jcr->jr.Job, jcr->Job, sizeof(jcr->jr.Job));
110
111    /* Initialize termination condition variable */
112    if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
113       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
114       set_jcr_job_status(jcr, JS_ErrorTerminated);
115       free_jcr(jcr);
116       return;
117    }
118
119    /*
120     * Open database
121     */
122    Dmsg0(50, "Open database\n");
123    jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
124                             jcr->catalog->db_password, jcr->catalog->db_address,
125                             jcr->catalog->db_port, jcr->catalog->db_socket);
126    if (!db_open_database(jcr, jcr->db)) {
127       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
128       set_jcr_job_status(jcr, JS_ErrorTerminated);
129       free_jcr(jcr);
130       return;
131    }
132    Dmsg0(50, "DB opened\n");
133
134    /*
135     * Create Job record  
136     */
137    jcr->jr.JobStatus = jcr->JobStatus;
138    if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) {
139       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
140       set_jcr_job_status(jcr, JS_ErrorTerminated);
141       free_jcr(jcr);
142       return;
143    }
144    jcr->JobId = jcr->jr.JobId;
145    ASSERT(jcr->jr.JobId > 0);
146
147    Dmsg4(30, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", 
148        jcr->JobId, jcr->Job, jcr->jr.Type, jcr->jr.Level);
149    Dmsg0(200, "Add jrc to work queue\n");
150
151 #ifdef USE_SEMAPHORE
152   if ((stat = pthread_create(&tid, NULL, job_thread, (void *)jcr)) != 0) {
153       Emsg1(M_ABORT, 0, _("Unable to create job thread: ERR=%s\n"), strerror(stat));
154    }
155 #else
156    /* Queue the job to be run */
157    if ((stat = workq_add(&job_wq, (void *)jcr, &work_item, 0)) != 0) {
158       Emsg1(M_ABORT, 0, _("Could not add job to work queue: ERR=%s\n"), strerror(stat));
159    }
160    jcr->work_item = work_item;
161 #endif
162    Dmsg0(200, "Done run_job()\n");
163 }
164
165 /* 
166  * This is the engine called by workq_add() when we were pulled                
167  *  from the work queue.
168  *  At this point, we are running in our own thread 
169  */
170 static void *job_thread(void *arg)
171 {
172    JCR *jcr = (JCR *)arg;
173
174    pthread_detach(pthread_self());
175    sm_check(__FILE__, __LINE__, True);
176
177    for ( ;; ) {
178       if (!acquire_resource_locks(jcr)) {
179          set_jcr_job_status(jcr, JS_Canceled);
180       }
181
182       Dmsg0(200, "=====Start Job=========\n");
183       jcr->start_time = time(NULL);      /* set the real start time */
184       set_jcr_job_status(jcr, JS_Running);
185
186       if (job_canceled(jcr)) {
187          update_job_end_record(jcr);
188       } else if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
189           (utime_t)(jcr->start_time - jcr->sched_time)) {
190          Jmsg(jcr, M_FATAL, 0, _("Job canceled because max start delay time exceeded.\n"));
191          set_jcr_job_status(jcr, JS_Canceled);
192          update_job_end_record(jcr);
193       } else {
194
195          /* Run Job */
196          if (jcr->job->RunBeforeJob) {
197             POOLMEM *before = get_pool_memory(PM_FNAME);
198             int status;
199             BPIPE *bpipe;
200             char line[MAXSTRING];
201             
202             before = edit_run_codes(jcr, before, jcr->job->RunBeforeJob);
203             bpipe = open_bpipe(before, 0, "r");
204             free_pool_memory(before);
205             while (fgets(line, sizeof(line), bpipe->rfd)) {
206                Jmsg(jcr, M_INFO, 0, _("RunBefore: %s"), line);
207             }
208             status = close_bpipe(bpipe);
209             if (status != 0) {
210                Jmsg(jcr, M_FATAL, 0, _("RunBeforeJob returned non-zero status=%d\n"),
211                   status);
212                set_jcr_job_status(jcr, JS_FatalError);
213                update_job_end_record(jcr);
214                goto bail_out;
215             }
216          }
217          switch (jcr->JobType) {
218             case JT_BACKUP:
219                do_backup(jcr);
220                if (jcr->JobStatus == JS_Terminated) {
221                   do_autoprune(jcr);
222                }
223                break;
224             case JT_VERIFY:
225                do_verify(jcr);
226                if (jcr->JobStatus == JS_Terminated) {
227                   do_autoprune(jcr);
228                }
229                break;
230             case JT_RESTORE:
231                do_restore(jcr);
232                if (jcr->JobStatus == JS_Terminated) {
233                   do_autoprune(jcr);
234                }
235                break;
236             case JT_ADMIN:
237                do_admin(jcr);
238                if (jcr->JobStatus == JS_Terminated) {
239                   do_autoprune(jcr);
240                }
241                break;
242             default:
243                Pmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
244                break;
245             }
246          if (jcr->job->RunAfterJob) {
247             POOLMEM *after = get_pool_memory(PM_FNAME);
248             int status;
249             BPIPE *bpipe;
250             char line[MAXSTRING];
251             
252             after = edit_run_codes(jcr, after, jcr->job->RunAfterJob);
253             bpipe = open_bpipe(after, 0, "r");
254             free_pool_memory(after);
255             while (fgets(line, sizeof(line), bpipe->rfd)) {
256                Jmsg(jcr, M_INFO, 0, _("RunAfter: %s"), line);
257             }
258             status = close_bpipe(bpipe);
259             if (status != 0) {
260                Jmsg(jcr, M_FATAL, 0, _("RunAfterJob returned non-zero status=%d\n"),
261                   status);
262                set_jcr_job_status(jcr, JS_FatalError);
263                update_job_end_record(jcr);
264             }
265          }
266       }
267 bail_out:
268       release_resource_locks(jcr);
269       if (jcr->job->RescheduleOnError && 
270           jcr->JobStatus != JS_Terminated &&
271           jcr->JobStatus != JS_Canceled && 
272           jcr->job->RescheduleTimes > 0 && 
273           jcr->reschedule_count < jcr->job->RescheduleTimes) {
274
275           /*
276            * Reschedule this job by cleaning it up, but
277            *  reuse the same JobId if possible.
278            */
279          jcr->reschedule_count++;
280          jcr->sched_time = time(NULL) + jcr->job->RescheduleInterval;
281          Dmsg2(100, "Rescheduled Job %s to re-run in %d seconds.\n", jcr->Job,
282             (int)jcr->job->RescheduleInterval);
283          jcr->JobStatus = JS_Created; /* force new status */
284          dird_free_jcr(jcr);          /* partial cleanup old stuff */
285          if (jcr->JobBytes == 0) {
286             continue;                    /* reschedule the job */
287          }
288          /* 
289           * Something was actually backed up, so we cannot reuse
290           *   the old JobId or there will be database record
291           *   conflicts.  We now create a new job, copying the
292           *   appropriate fields.
293           */
294          JCR *njcr = new_jcr(sizeof(JCR), dird_free_jcr);
295          set_jcr_defaults(njcr, jcr->job);
296          njcr->reschedule_count = jcr->reschedule_count;
297          njcr->JobLevel = jcr->JobLevel;
298          njcr->JobStatus = jcr->JobStatus;
299          njcr->pool = jcr->pool;
300          njcr->store = jcr->store;
301          njcr->messages = jcr->messages;
302          run_job(njcr);
303       }
304       break;
305    }
306
307    if (jcr->db) {
308       Dmsg0(200, "Close DB\n");
309       db_close_database(jcr, jcr->db);
310       jcr->db = NULL;
311    }
312    free_jcr(jcr);
313    Dmsg0(50, "======== End Job ==========\n");
314    sm_check(__FILE__, __LINE__, True);
315    return NULL;
316 }
317
318 /*
319  * Acquire the resources needed. These locks limit the
320  *  number of jobs by each resource. We have limits on
321  *  Jobs, Clients, Storage, and total jobs.
322  */
323 static int acquire_resource_locks(JCR *jcr)
324 {
325    time_t now = time(NULL);
326    time_t wtime = jcr->sched_time - now;
327
328    /* Wait until scheduled time arrives */
329    if (wtime > 0 && verbose) {
330       Jmsg(jcr, M_INFO, 0, _("Job %s waiting %d seconds for scheduled start time.\n"), 
331          jcr->Job, wtime);
332       set_jcr_job_status(jcr, JS_WaitStartTime);
333    }
334    /* Check every 30 seconds if canceled */ 
335    while (wtime > 0) {
336       Dmsg2(100, "Waiting on sched time, jobid=%d secs=%d\n", jcr->JobId, wtime);
337       if (wtime > 30) {
338          wtime = 30;
339       }
340       bmicrosleep(wtime, 0);
341       if (job_canceled(jcr)) {
342          return 0;
343       }
344       wtime = jcr->sched_time - time(NULL);
345    }
346
347
348 #ifdef USE_SEMAPHORE
349    int stat;
350
351    /* Initialize semaphores */
352    if (jcr->store->sem.valid != SEMLOCK_VALID) {
353       if ((stat = sem_init(&jcr->store->sem, jcr->store->MaxConcurrentJobs)) != 0) {
354          Emsg1(M_ABORT, 0, _("Could not init Storage semaphore: ERR=%s\n"), strerror(stat));
355       }
356    }
357    if (jcr->client->sem.valid != SEMLOCK_VALID) {
358       if ((stat = sem_init(&jcr->client->sem, jcr->client->MaxConcurrentJobs)) != 0) {
359          Emsg1(M_ABORT, 0, _("Could not init Client semaphore: ERR=%s\n"), strerror(stat));
360       }
361    }
362    if (jcr->job->sem.valid != SEMLOCK_VALID) {
363       if ((stat = sem_init(&jcr->job->sem, jcr->job->MaxConcurrentJobs)) != 0) {
364          Emsg1(M_ABORT, 0, _("Could not init Job semaphore: ERR=%s\n"), strerror(stat));
365       }
366    }
367
368    for ( ;; ) {
369       /* Acquire semaphore */
370       set_jcr_job_status(jcr, JS_WaitJobRes);
371       if ((stat = sem_lock(&jcr->job->sem)) != 0) {
372          Emsg1(M_ABORT, 0, _("Could not acquire Job max jobs lock: ERR=%s\n"), strerror(stat));
373       }
374       set_jcr_job_status(jcr, JS_WaitClientRes);
375       if ((stat = sem_trylock(&jcr->client->sem)) != 0) {
376          if (stat == EBUSY) {
377             backoff_resource_locks(jcr, 1);
378             goto wait;
379          } else {
380             Emsg1(M_ABORT, 0, _("Could not acquire Client max jobs lock: ERR=%s\n"), strerror(stat));
381          }
382       }
383       set_jcr_job_status(jcr, JS_WaitStoreRes);
384       if ((stat = sem_trylock(&jcr->store->sem)) != 0) {
385          if (stat == EBUSY) {
386             backoff_resource_locks(jcr, 2);
387             goto wait;
388          } else {
389             Emsg1(M_ABORT, 0, _("Could not acquire Storage max jobs lock: ERR=%s\n"), strerror(stat));
390          }
391       }
392       set_jcr_job_status(jcr, JS_WaitMaxJobs);
393       if ((stat = sem_trylock(&job_lock)) != 0) {
394          if (stat == EBUSY) {
395             backoff_resource_locks(jcr, 3);
396             goto wait;
397          } else {
398             Emsg1(M_ABORT, 0, _("Could not acquire max jobs lock: ERR=%s\n"), strerror(stat));
399          }
400       }
401       break;
402
403 wait:
404       if (job_canceled(jcr)) {
405          return 0;
406       }
407       P(mutex);
408       /*
409        * Wait for a resource to be released either by backoff or
410        *  by a job terminating.
411        */
412       waiting++;
413       pthread_cond_wait(&resource_wait, &mutex);
414       waiting--;
415       V(mutex);
416       /* Try again */
417    }
418    jcr->acquired_resource_locks = 1;
419 #endif
420    return 1;
421 }
422
423 #ifdef USE_SEMAPHORE
424 /*
425  * We could not get all the resource locks because 
426  *  too many jobs are running, so release any locks
427  *  we did acquire, giving others a chance to use them
428  *  while we wait.
429  */
430 static void backoff_resource_locks(JCR *jcr, int count)
431 {
432    P(mutex);
433    switch (count) {
434    case 3:
435       sem_unlock(&jcr->store->sem);
436       /* Fall through wanted */
437    case 2:
438       sem_unlock(&jcr->client->sem);
439       /* Fall through wanted */
440    case 1:
441       sem_unlock(&jcr->job->sem);
442       break;
443    }
444    /*
445     * Since we released a lock, if there are any threads
446     *  waiting, wake them up so that they can try again.
447     */
448    if (waiting > 0) {
449       pthread_cond_broadcast(&resource_wait);
450    }
451    V(mutex);
452 }
453 #endif
454
455 /*
456  * This is called at the end of the job to release
457  *   any resource limits on the number of jobs. If
458  *   there are any other jobs waiting, we wake them
459  *   up so that they can try again.
460  */
461 static void release_resource_locks(JCR *jcr)
462 {
463    if (!jcr->acquired_resource_locks) {
464       return;                         /* Job canceled, no locks acquired */
465    }
466 #ifdef USE_SEMAPHORE
467    P(mutex);
468    sem_unlock(&jcr->store->sem);
469    sem_unlock(&jcr->client->sem);
470    sem_unlock(&jcr->job->sem);
471    sem_unlock(&job_lock);
472    if (waiting > 0) {
473       pthread_cond_broadcast(&resource_wait);
474    }
475    jcr->acquired_resource_locks = 0;
476    V(mutex);
477 #endif
478 }
479
480 /*
481  * Get or create a Client record for this Job
482  */
483 int get_or_create_client_record(JCR *jcr)
484 {
485    CLIENT_DBR cr;
486
487    memset(&cr, 0, sizeof(cr));
488    bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
489    cr.AutoPrune = jcr->client->AutoPrune;
490    cr.FileRetention = jcr->client->FileRetention;
491    cr.JobRetention = jcr->client->JobRetention;
492    if (!jcr->client_name) {
493       jcr->client_name = get_pool_memory(PM_NAME);
494    }
495    pm_strcpy(&jcr->client_name, jcr->client->hdr.name);
496    if (!db_create_client_record(jcr, jcr->db, &cr)) {
497       Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. ERR=%s\n"), 
498          db_strerror(jcr->db));
499       return 0;
500    }
501    jcr->jr.ClientId = cr.ClientId;
502    if (cr.Uname[0]) {
503       if (!jcr->client_uname) {
504          jcr->client_uname = get_pool_memory(PM_NAME);
505       }
506       pm_strcpy(&jcr->client_uname, cr.Uname);
507    }
508    Dmsg2(100, "Created Client %s record %d\n", jcr->client->hdr.name, 
509       jcr->jr.ClientId);
510    return 1;
511 }
512
513
514 /*
515  * Write status and such in DB
516  */
517 void update_job_end_record(JCR *jcr)
518 {
519    if (jcr->jr.EndTime == 0) {
520       jcr->jr.EndTime = time(NULL);
521    }
522    jcr->end_time = jcr->jr.EndTime;
523    jcr->jr.JobId = jcr->JobId;
524    jcr->jr.JobStatus = jcr->JobStatus;
525    jcr->jr.JobFiles = jcr->JobFiles;
526    jcr->jr.JobBytes = jcr->JobBytes;
527    jcr->jr.VolSessionId = jcr->VolSessionId;
528    jcr->jr.VolSessionTime = jcr->VolSessionTime;
529    if (!db_update_job_end_record(jcr, jcr->db, &jcr->jr)) {
530       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"), 
531          db_strerror(jcr->db));
532    }
533 }
534
535 /*
536  * Takes base_name and appends (unique) current
537  *   date and time to form unique job name.
538  *
539  *  Returns: unique job name in jcr->Job
540  *    date/time in jcr->start_time
541  */
542 void create_unique_job_name(JCR *jcr, char *base_name)
543 {
544    /* Job start mutex */
545    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
546    static time_t last_start_time = 0;
547    time_t now;
548    struct tm tm;
549    char dt[MAX_TIME_LENGTH];
550    char name[MAX_NAME_LENGTH];
551    char *p;
552
553    /* Guarantee unique start time -- maximum one per second, and
554     * thus unique Job Name 
555     */
556    P(mutex);                          /* lock creation of jobs */
557    now = time(NULL);
558    while (now == last_start_time) {
559       bmicrosleep(0, 500000);
560       now = time(NULL);
561    }
562    last_start_time = now;
563    V(mutex);                          /* allow creation of jobs */
564    jcr->start_time = now;
565    /* Form Unique JobName */
566    localtime_r(&now, &tm);
567    /* Use only characters that are permitted in Windows filenames */
568    strftime(dt, sizeof(dt), "%Y-%m-%d_%H.%M.%S", &tm); 
569    bstrncpy(name, base_name, sizeof(name));
570    name[sizeof(name)-22] = 0;          /* truncate if too long */
571    bsnprintf(jcr->Job, sizeof(jcr->Job), "%s.%s", name, dt); /* add date & time */
572    /* Convert spaces into underscores */
573    for (p=jcr->Job; *p; p++) {
574       if (*p == ' ') {
575          *p = '_';
576       }
577    }
578 }
579
580 /*
581  * Free the Job Control Record if no one is still using it.
582  *  Called from main free_jcr() routine in src/lib/jcr.c so
583  *  that we can do our Director specific cleanup of the jcr.
584  */
585 void dird_free_jcr(JCR *jcr)
586 {
587    Dmsg0(200, "Start dird free_jcr\n");
588
589    if (jcr->sd_auth_key) {
590       free(jcr->sd_auth_key);
591       jcr->sd_auth_key = NULL;
592    }
593    if (jcr->where) {
594       free(jcr->where);
595       jcr->where = NULL;
596    }
597    if (jcr->file_bsock) {
598       Dmsg0(200, "Close File bsock\n");
599       bnet_close(jcr->file_bsock);
600       jcr->file_bsock = NULL;
601    }
602    if (jcr->store_bsock) {
603       Dmsg0(200, "Close Store bsock\n");
604       bnet_close(jcr->store_bsock);
605       jcr->store_bsock = NULL;
606    }
607    if (jcr->fname) {  
608       Dmsg0(200, "Free JCR fname\n");
609       free_pool_memory(jcr->fname);
610       jcr->fname = NULL;
611    }
612    if (jcr->stime) {
613       Dmsg0(200, "Free JCR stime\n");
614       free_pool_memory(jcr->stime);
615       jcr->stime = NULL;
616    }
617    if (jcr->RestoreBootstrap) {
618       free(jcr->RestoreBootstrap);
619       jcr->RestoreBootstrap = NULL;
620    }
621    if (jcr->client_uname) {
622       free_pool_memory(jcr->client_uname);
623       jcr->client_uname = NULL;
624    }
625    Dmsg0(200, "End dird free_jcr\n");
626 }
627
628 /*
629  * Set some defaults in the JCR necessary to
630  * run. These items are pulled from the job
631  * definition as defaults, but can be overridden
632  * later either by the Run record in the Schedule resource,
633  * or by the Console program.
634  */
635 void set_jcr_defaults(JCR *jcr, JOB *job)
636 {
637    jcr->job = job;
638    jcr->JobType = job->JobType;
639    jcr->JobLevel = job->level;
640    jcr->store = job->storage;
641    jcr->client = job->client;
642    if (!jcr->client_name) {
643       jcr->client_name = get_pool_memory(PM_NAME);
644    }
645    pm_strcpy(&jcr->client_name, jcr->client->hdr.name);
646    jcr->pool = job->pool;
647    jcr->catalog = job->client->catalog;
648    jcr->fileset = job->fileset;
649    jcr->messages = job->messages; 
650    if (jcr->RestoreBootstrap) {
651       free(jcr->RestoreBootstrap);
652    }
653    /* This can be overridden by Console program */
654    if (job->RestoreBootstrap) {
655       jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
656    }
657    /* If no default level given, set one */
658    if (jcr->JobLevel == 0) {
659       switch (jcr->JobType) {
660       case JT_VERIFY:
661          jcr->JobLevel = L_VERIFY_CATALOG;
662          break;
663       case JT_BACKUP:
664          jcr->JobLevel = L_INCREMENTAL;
665          break;
666       case JT_RESTORE:
667       case JT_ADMIN:
668          jcr->JobLevel = L_FULL;
669          break;
670       default:
671          break;
672       }
673    }
674 }
675
676 /*
677  * Edit codes into Run command
678  *  %% = %
679  *  %c = Client's name
680  *  %d = Director's name
681  *  %i = JobId
682  *  %e = Job Exit
683  *  %j = Job
684  *  %l = Job Level
685  *  %n = Job name
686  *  %t = Job type
687  *
688  *  omsg = edited output message
689  *  imsg = input string containing edit codes (%x)
690  *
691  */
692 static char *edit_run_codes(JCR *jcr, char *omsg, char *imsg) 
693 {
694    char *p;
695    const char *str;
696    char add[20];
697
698    *omsg = 0;
699    Dmsg1(200, "edit_run_codes: %s\n", imsg);
700    for (p=imsg; *p; p++) {
701       if (*p == '%') {
702          switch (*++p) {
703          case '%':
704             str = "%";
705             break;
706          case 'c':
707             str = jcr->client_name;
708             if (!str) {
709                str = "";
710             }
711             break;
712          case 'd':
713             str = my_name;
714             break;
715          case 'e':
716             str = job_status_to_str(jcr->JobStatus);
717             break;
718          case 'i':
719             sprintf(add, "%d", jcr->JobId);
720             str = add;
721             break;
722          case 'j':                    /* Job */
723             str = jcr->Job;
724             break;
725          case 'l':
726             str = job_level_to_str(jcr->JobLevel);
727             break;
728          case 'n':
729             str = jcr->job->hdr.name;
730             break;
731          case 't':
732             str = job_type_to_str(jcr->JobType);
733             break;
734          default:
735             add[0] = '%';
736             add[1] = *p;
737             add[2] = 0;
738             str = add;
739             break;
740          }
741       } else {
742          add[0] = *p;
743          add[1] = 0;
744          str = add;
745       }
746       Dmsg1(200, "add_str %s\n", str);
747       pm_strcat(&omsg, (char *)str);
748       Dmsg1(200, "omsg=%s\n", omsg);
749    }
750    return omsg;
751 }