2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Bacula Director -- newvol.c -- creates new Volumes in
31 * catalog Media table from the LabelFormat specification.
33 * Kern Sibbald, May MMI
35 * This routine runs as a thread and must be thread reentrant.
37 * Basic tasks done here:
38 * If possible create a new Media entry
46 /* Forward referenced functions */
47 static bool create_simple_name(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr);
48 static bool perform_full_name_substitution(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr);
52 * Automatic Volume name creation using the LabelFormat
54 * The media record must have the PoolId filled in when
55 * calling this routine.
57 bool newVolume(JCR *jcr, MEDIA_DBR *mr)
61 memset(&pr, 0, sizeof(pr));
63 /* See if we can create a new Volume */
65 pr.PoolId = mr->PoolId;
66 if (!db_get_pool_record(jcr, jcr->db, &pr)) {
69 if (pr.MaxVols == 0 || pr.NumVols < pr.MaxVols) {
70 memset(mr, 0, sizeof(MEDIA_DBR));
71 set_pool_dbr_defaults_in_media_dbr(mr, &pr);
72 jcr->VolumeName[0] = 0;
73 bstrncpy(mr->MediaType, jcr->wstore->media_type, sizeof(mr->MediaType));
74 generate_job_event(jcr, "NewVolume"); /* return bool */
75 generate_plugin_event(jcr, bEventNewVolume); /* return void... */
76 if (jcr->VolumeName[0] && is_volume_name_legal(NULL, jcr->VolumeName)) {
77 bstrncpy(mr->VolumeName, jcr->VolumeName, sizeof(mr->VolumeName));
78 /* Check for special characters */
79 } else if (pr.LabelFormat[0] && pr.LabelFormat[0] != '*') {
80 if (is_volume_name_legal(NULL, pr.LabelFormat)) {
81 /* No special characters, so apply simple algorithm */
82 if (!create_simple_name(jcr, mr, &pr)) {
85 } else { /* try full substitution */
86 /* Found special characters, so try substitution */
87 if (!perform_full_name_substitution(jcr, mr, &pr)) {
90 if (!is_volume_name_legal(NULL, mr->VolumeName)) {
91 Jmsg(jcr, M_ERROR, 0, _("Illegal character in Volume name \"%s\"\n"),
101 if (db_create_media_record(jcr, jcr->db, mr) &&
102 db_update_pool_record(jcr, jcr->db, &pr)) {
104 Jmsg(jcr, M_INFO, 0, _("Created new Volume \"%s\" in catalog.\n"), mr->VolumeName);
105 Dmsg1(90, "Created new Volume=%s\n", mr->VolumeName);
108 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
116 static bool create_simple_name(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr)
118 char name[MAXSTRING];
121 POOL_MEM query(PM_MESSAGE);
124 /* See if volume already exists */
125 mr->VolumeName[0] = 0;
126 bstrncpy(name, pr->LabelFormat, sizeof(name));
128 Mmsg(query, "SELECT MAX(MediaId) FROM Media,Pool WHERE Pool.PoolId=%s",
129 edit_int64(pr->PoolId, ed1));
130 if (!db_sql_query(jcr->db, query.c_str(), db_int64_handler, (void *)&ctx)) {
131 Jmsg(jcr, M_WARNING, 0, _("SQL failed, but ignored. ERR=%s\n"), db_strerror(jcr->db));
132 ctx.value = pr->NumVols+1;
134 for (int i=(int)ctx.value+1; i<(int)ctx.value+100; i++) {
136 memset(&tmr, 0, sizeof(tmr));
137 sprintf(num, "%04d", i);
138 bstrncpy(tmr.VolumeName, name, sizeof(tmr.VolumeName));
139 bstrncat(tmr.VolumeName, num, sizeof(tmr.VolumeName));
140 if (db_get_media_record(jcr, jcr->db, &tmr)) {
141 Jmsg(jcr, M_WARNING, 0,
142 _("Wanted to create Volume \"%s\", but it already exists. Trying again.\n"),
146 bstrncpy(mr->VolumeName, name, sizeof(mr->VolumeName));
147 bstrncat(mr->VolumeName, num, sizeof(mr->VolumeName));
148 break; /* Got good name */
150 if (mr->VolumeName[0] == 0) {
151 Jmsg(jcr, M_ERROR, 0, _("Too many failures. Giving up creating Volume name.\n"));
158 * Perform full substitution on Label
160 static bool perform_full_name_substitution(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr)
163 POOLMEM *label = get_pool_memory(PM_FNAME);
164 jcr->NumVols = pr->NumVols;
165 if (variable_expansion(jcr, pr->LabelFormat, &label)) {
166 bstrncpy(mr->VolumeName, label, sizeof(mr->VolumeName));
169 free_pool_memory(label);