]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/newvol.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[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-2004 Kern Sibbald and John Walker
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 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);
41
42
43 /*
44  * Automatic Volume name creation using the LabelFormat
45  */
46 bool newVolume(JCR *jcr, MEDIA_DBR *mr)
47 {
48    POOL_DBR pr;
49
50    memset(&pr, 0, sizeof(pr));
51
52    /* See if we can create a new Volume */
53    db_lock(jcr->db);
54    pr.PoolId = jcr->PoolId;
55    if (db_get_pool_record(jcr, jcr->db, &pr) && pr.LabelFormat[0] &&
56        pr.LabelFormat[0] != '*') {
57       if (pr.MaxVols == 0 || pr.NumVols < pr.MaxVols) {
58          memset(mr, 0, sizeof(MEDIA_DBR));
59          set_pool_dbr_defaults_in_media_dbr(mr, &pr);
60          jcr->VolumeName[0] = 0;
61          bstrncpy(mr->MediaType, jcr->store->media_type, sizeof(mr->MediaType));
62          if (generate_event(jcr, "NewVolume") == 1 && jcr->VolumeName[0]) {
63             bstrncpy(mr->VolumeName, jcr->VolumeName, sizeof(mr->VolumeName));
64          /* Check for special characters */
65          } else if (is_volume_name_legal(NULL, pr.LabelFormat)) {
66             /* No special characters, so apply simple algorithm */
67             if (!create_simple_name(jcr, mr, &pr)) {
68                goto bail_out;
69             }
70          } else {  /* try full substitution */
71             /* Found special characters, so try substitution */
72             if (!perform_full_name_substitution(jcr, mr, &pr)) {
73                goto bail_out;
74             }
75             if (!is_volume_name_legal(NULL, mr->VolumeName)) {
76                Jmsg(jcr, M_ERROR, 0, _("Illegal character in Volume name \"%s\"\n"),
77                   mr->VolumeName);
78                goto bail_out;
79             }
80          }
81          pr.NumVols++;
82          if (db_create_media_record(jcr, jcr->db, mr) &&
83             db_update_pool_record(jcr, jcr->db, &pr)) {
84             db_unlock(jcr->db);
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);
87             return true;
88          } else {
89             Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
90          }
91       }
92    }
93 bail_out:
94    db_unlock(jcr->db);
95    return false;
96 }
97
98 static int create_simple_name(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr)
99 {
100    char name[MAXSTRING];
101    char num[20];
102
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++) {
107       MEDIA_DBR tmr;
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"),
115              tmr.VolumeName);
116          continue;
117       }
118       bstrncpy(mr->VolumeName, name, sizeof(mr->VolumeName));
119       bstrncat(mr->VolumeName, num, sizeof(mr->VolumeName));
120       break;                    /* Got good name */
121    }
122    if (mr->VolumeName[0] == 0) {
123       Jmsg(jcr, M_ERROR, 0, _("Too many failures. Giving up creating Volume name.\n"));
124       return 0;
125    }
126    return 1;
127 }
128
129 /*
130  * Perform full substitution on Label
131  */
132 static int perform_full_name_substitution(JCR *jcr, MEDIA_DBR *mr, POOL_DBR *pr)
133 {
134    int stat = 0;
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));
139       stat = 1;
140    }
141    free_pool_memory(label);
142    return stat;
143 }