3 * Bacula Director -- newvol.c -- creates new Volumes in
4 * catalog Media table from the LabelFormat specification.
6 * Kern Sibbald, May MMI
8 * This routine runs as a thread and must be thread reentrant.
10 * Basic tasks done here:
11 * If possible create a new Media entry
16 Bacula® - The Network Backup Solution
18 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
20 The main author of Bacula is Kern Sibbald, with contributions from
21 many others, a complete list can be found in the file AUTHORS.
22 This program is Free Software; you can redistribute it and/or
23 modify it under the terms of version two of the GNU General Public
24 License as published by the Free Software Foundation and included
27 This program is distributed in the hope that it will be useful, but
28 WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
37 Bacula® is a registered trademark of John Walker.
38 The licensor of Bacula is the Free Software Foundation Europe
39 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
40 Switzerland, email:ftf@fsfeurope.org.
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 if (generate_job_event(jcr, "NewVolume") == 1 && jcr->VolumeName[0] &&
75 is_volume_name_legal(NULL, jcr->VolumeName)) {
76 bstrncpy(mr->VolumeName, jcr->VolumeName, sizeof(mr->VolumeName));
77 /* Check for special characters */
78 } else if (pr.LabelFormat[0] && pr.LabelFormat[0] != '*') {
79 if (is_volume_name_legal(NULL, pr.LabelFormat)) {
80 /* No special characters, so apply simple algorithm */
81 if (!create_simple_name(jcr, mr, &pr)) {
84 } else { /* try full substitution */
85 /* Found special characters, so try substitution */
86 if (!perform_full_name_substitution(jcr, mr, &pr)) {
89 if (!is_volume_name_legal(NULL, mr->VolumeName)) {
90 Jmsg(jcr, M_ERROR, 0, _("Illegal character in Volume name \"%s\"\n"),
100 if (db_create_media_record(jcr, jcr->db, mr) &&
101 db_update_pool_record(jcr, jcr->db, &pr)) {
103 Jmsg(jcr, M_INFO, 0, _("Created new Volume \"%s\" in catalog.\n"), mr->VolumeName);
104 Dmsg1(90, "Created new Volume=%s\n", mr->VolumeName);
107 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
115 static bool create_simple_name(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr)
117 char name[MAXSTRING];
120 /* See if volume already exists */
121 mr->VolumeName[0] = 0;
122 bstrncpy(name, pr->LabelFormat, sizeof(name));
123 for (int i=pr->NumVols+1; i<(int)pr->NumVols+11; i++) {
125 memset(&tmr, 0, sizeof(tmr));
126 sprintf(num, "%04d", i);
127 bstrncpy(tmr.VolumeName, name, sizeof(tmr.VolumeName));
128 bstrncat(tmr.VolumeName, num, sizeof(tmr.VolumeName));
129 if (db_get_media_record(jcr, jcr->db, &tmr)) {
130 Jmsg(jcr, M_WARNING, 0,
131 _("Wanted to create Volume \"%s\", but it already exists. Trying again.\n"),
135 bstrncpy(mr->VolumeName, name, sizeof(mr->VolumeName));
136 bstrncat(mr->VolumeName, num, sizeof(mr->VolumeName));
137 break; /* Got good name */
139 if (mr->VolumeName[0] == 0) {
140 Jmsg(jcr, M_ERROR, 0, _("Too many failures. Giving up creating Volume name.\n"));
147 * Perform full substitution on Label
149 static bool perform_full_name_substitution(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr)
152 POOLMEM *label = get_pool_memory(PM_FNAME);
153 jcr->NumVols = pr->NumVols;
154 if (variable_expansion(jcr, pr->LabelFormat, &label)) {
155 bstrncpy(mr->VolumeName, label, sizeof(mr->VolumeName));
158 free_pool_memory(label);