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 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
38 /* Forward referenced functions */
39 static int create_simple_name(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr);
40 static int perform_full_name_substitution(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr);
44 * Really crude automatic Volume name creation using
45 * LabelFormat. We assume that if this routine is being
46 * called the Volume will be labeled, so we set the LabelDate.
48 int newVolume(JCR *jcr, MEDIA_DBR *mr)
52 memset(&pr, 0, sizeof(pr));
54 /* See if we can create a new Volume */
56 pr.PoolId = jcr->PoolId;
57 if (db_get_pool_record(jcr, jcr->db, &pr) && pr.LabelFormat[0] &&
58 pr.LabelFormat[0] != '*') {
59 if (pr.MaxVols == 0 || pr.NumVols < pr.MaxVols) {
60 memset(mr, 0, sizeof(MEDIA_DBR));
61 set_pool_dbr_defaults_in_media_dbr(mr, &pr);
62 mr->LabelDate = time(NULL);
63 bstrncpy(mr->MediaType, jcr->store->media_type, sizeof(mr->MediaType));
64 /* Check for special characters */
65 if (is_volume_name_legal(NULL, pr.LabelFormat)) {
66 /* No special characters, so apply simple algorithm */
67 if (!create_simple_name(jcr, mr, &pr)) {
70 } else { /* try full substitution */
71 /* Found special characters, so try substitution */
72 if (!perform_full_name_substitution(jcr, mr, &pr)) {
75 if (!is_volume_name_legal(NULL, mr->VolumeName)) {
76 Jmsg(jcr, M_ERROR, 0, _("Illegal character in Volume name \"%s\"\n"),
82 if (db_create_media_record(jcr, jcr->db, mr) &&
83 db_update_pool_record(jcr, jcr->db, &pr)) {
85 Jmsg(jcr, M_INFO, 0, _("Created new Volume \"%s\" in catalog.\n"), mr->VolumeName);
86 Dmsg1(90, "Created new Volume=%s\n", mr->VolumeName);
89 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
98 static int create_simple_name(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr)
100 char name[MAXSTRING];
103 /* See if volume already exists */
104 mr->VolumeName[0] = 0;
105 bstrncpy(name, pr->LabelFormat, sizeof(name));
106 for (int i=pr->NumVols+1; i<(int)pr->NumVols+11; i++) {
108 memset(&tmr, 0, sizeof(tmr));
109 sprintf(num, "%04d", i);
110 bstrncpy(tmr.VolumeName, name, sizeof(tmr.VolumeName));
111 bstrncat(tmr.VolumeName, num, sizeof(tmr.VolumeName));
112 if (db_get_media_record(jcr, jcr->db, &tmr)) {
113 Jmsg(jcr, M_WARNING, 0,
114 _("Wanted to create Volume \"%s\", but it already exists. Trying again.\n"),
118 bstrncpy(mr->VolumeName, name, sizeof(mr->VolumeName));
119 bstrncat(mr->VolumeName, num, sizeof(mr->VolumeName));
120 break; /* Got good name */
122 if (mr->VolumeName[0] == 0) {
123 Jmsg(jcr, M_ERROR, 0, _("Too many failures. Giving up creating Volume name.\n"));
130 * Perform full substitution on Label
132 static int perform_full_name_substitution(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr)
135 POOLMEM *label = get_pool_memory(PM_FNAME);
136 jcr->NumVols = pr->NumVols;
137 if (variable_expansion(jcr, pr->LabelFormat, &label)) {
138 bstrncpy(mr->VolumeName, label, sizeof(mr->VolumeName));
141 free_pool_memory(label);