]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/newvol.c
Cleanup Python build so that Python is not dragged
[bacula/bacula] / bacula / src / dird / newvol.c
1 /*
2  *
3  *   Bacula Director -- newvol.c -- creates new Volumes in
4  *    catalog Media table from the LabelFormat specification.
5  *
6  *     Kern Sibbald, May MMI
7  *
8  *    This routine runs as a thread and must be thread reentrant.
9  *
10  *  Basic tasks done here:
11  *      If possible create a new Media entry
12  *
13  *   Version $Id$
14  */
15 /*
16    Copyright (C) 2001-2005 Kern Sibbald
17
18    This program is free software; you can redistribute it and/or
19    modify it under the terms of the GNU General Public License as
20    published by the Free Software Foundation; either version 2 of
21    the License, or (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26    General Public License for more details.
27
28    You should have received a copy of the GNU General Public
29    License along with this program; if not, write to the Free
30    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31    MA 02111-1307, USA.
32
33  */
34
35 #include "bacula.h"
36 #include "dird.h"
37
38 /* Forward referenced functions */
39 static bool create_simple_name(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr);
40 static bool perform_full_name_substitution(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr);
41
42
43 /*
44  * Automatic Volume name creation using the LabelFormat
45  *
46  *  The media record must have the PoolId filled in when
47  *   calling this routine.
48  */
49 bool newVolume(JCR *jcr, MEDIA_DBR *mr)
50 {
51    POOL_DBR pr;
52
53    memset(&pr, 0, sizeof(pr));
54
55    /* See if we can create a new Volume */
56    db_lock(jcr->db);
57    pr.PoolId = mr->PoolId;
58    if (!db_get_pool_record(jcr, jcr->db, &pr)) {
59       goto bail_out;
60    }
61    if (pr.MaxVols == 0 || pr.NumVols < pr.MaxVols) {
62       memset(mr, 0, sizeof(MEDIA_DBR));
63       set_pool_dbr_defaults_in_media_dbr(mr, &pr);
64       jcr->VolumeName[0] = 0;
65       bstrncpy(mr->MediaType, jcr->store->media_type, sizeof(mr->MediaType));
66       if (generate_job_event(jcr, "NewVolume") == 1 && jcr->VolumeName[0] &&
67           is_volume_name_legal(NULL, jcr->VolumeName)) {
68          bstrncpy(mr->VolumeName, jcr->VolumeName, sizeof(mr->VolumeName));
69       /* Check for special characters */
70       } else if (pr.LabelFormat[0] && pr.LabelFormat[0] != '*') {
71          if (is_volume_name_legal(NULL, pr.LabelFormat)) {
72             /* No special characters, so apply simple algorithm */
73             if (!create_simple_name(jcr, mr, &pr)) {
74                goto bail_out;
75             }
76          } else {  /* try full substitution */
77             /* Found special characters, so try substitution */
78             if (!perform_full_name_substitution(jcr, mr, &pr)) {
79                goto bail_out;
80             }
81             if (!is_volume_name_legal(NULL, mr->VolumeName)) {
82                Jmsg(jcr, M_ERROR, 0, _("Illegal character in Volume name \"%s\"\n"),
83                   mr->VolumeName);
84                goto bail_out;
85             }
86          }
87       } else {                                       
88          goto bail_out;
89       }
90       pr.NumVols++;
91       if (db_create_media_record(jcr, jcr->db, mr) &&
92          db_update_pool_record(jcr, jcr->db, &pr)) {
93          db_unlock(jcr->db);
94          Jmsg(jcr, M_INFO, 0, _("Created new Volume \"%s\" in catalog.\n"), mr->VolumeName);
95          Dmsg1(90, "Created new Volume=%s\n", mr->VolumeName);
96          return true;
97       } else {
98          Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
99       }
100    }
101 bail_out:
102    db_unlock(jcr->db);
103    return false;
104 }
105
106 static bool create_simple_name(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr)
107 {
108    char name[MAXSTRING];
109    char num[20];
110
111    /* See if volume already exists */
112    mr->VolumeName[0] = 0;
113    bstrncpy(name, pr->LabelFormat, sizeof(name));
114    for (int i=pr->NumVols+1; i<(int)pr->NumVols+11; i++) {
115       MEDIA_DBR tmr;
116       memset(&tmr, 0, sizeof(tmr));
117       sprintf(num, "%04d", i);
118       bstrncpy(tmr.VolumeName, name, sizeof(tmr.VolumeName));
119       bstrncat(tmr.VolumeName, num, sizeof(tmr.VolumeName));
120       if (db_get_media_record(jcr, jcr->db, &tmr)) {
121          Jmsg(jcr, M_WARNING, 0,
122              _("Wanted to create Volume \"%s\", but it already exists. Trying again.\n"),
123              tmr.VolumeName);
124          continue;
125       }
126       bstrncpy(mr->VolumeName, name, sizeof(mr->VolumeName));
127       bstrncat(mr->VolumeName, num, sizeof(mr->VolumeName));
128       break;                    /* Got good name */
129    }
130    if (mr->VolumeName[0] == 0) {
131       Jmsg(jcr, M_ERROR, 0, _("Too many failures. Giving up creating Volume name.\n"));
132       return false;
133    }
134    return true;
135 }
136
137 /*
138  * Perform full substitution on Label
139  */
140 static bool perform_full_name_substitution(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr)
141 {
142    bool ok = false;
143    POOLMEM *label = get_pool_memory(PM_FNAME);
144    jcr->NumVols = pr->NumVols;
145    if (variable_expansion(jcr, pr->LabelFormat, &label)) {
146       bstrncpy(mr->VolumeName, label, sizeof(mr->VolumeName));
147       ok = true;
148    }
149    free_pool_memory(label);
150    return ok;
151 }