]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/newvol.c
1b1f277d615d0eba7188c2efd94c13487a4d7a29
[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) 2000-2003 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 /*
39  * Really crude automatic Volume name creation using
40  *  LabelFormat. We assume that if this routine is being
41  *  called the Volume will be labeled, so we set the LabelDate.
42  */
43 int newVolume(JCR *jcr, MEDIA_DBR *mr)
44 {
45    POOL_DBR pr;
46    char name[MAXSTRING];
47    char num[20];
48
49    memset(&pr, 0, sizeof(pr));
50
51    /* See if we can create a new Volume */
52    db_lock(jcr->db);
53    pr.PoolId = jcr->PoolId;
54    if (db_get_pool_record(jcr, jcr->db, &pr) && pr.LabelFormat[0] &&
55        pr.LabelFormat[0] != '*') {
56       if (pr.MaxVols == 0 || pr.NumVols < pr.MaxVols) {
57          set_pool_dbr_defaults_in_media_dbr(mr, &pr);
58          mr->LabelDate = time(NULL);
59          bstrncpy(mr->MediaType, jcr->store->media_type, sizeof(mr->MediaType));
60          bstrncpy(name, pr.LabelFormat, sizeof(name));
61          if (strchr(name, (int)'%') != NULL) {
62             Jmsg(jcr, M_ERROR, 0, _("Illegal character in Label Format\n"));
63             goto bail_out;
64          }
65          /* See if volume already exists */
66          mr->VolumeName[0] = 0;
67          for (int i=pr.NumVols+1; i<(int)pr.NumVols+11; i++) {
68             MEDIA_DBR tmr;
69             memset(&tmr, 0, sizeof(tmr));
70             sprintf(num, "%04d", i);
71             bstrncpy(tmr.VolumeName, name, sizeof(tmr.VolumeName));
72             bstrncat(tmr.VolumeName, num, sizeof(tmr.VolumeName));
73             if (db_get_media_record(jcr, jcr->db, &tmr)) {
74                Jmsg(jcr, M_WARNING, 0, 
75 _("Wanted to create Volume \"%s\", but it already exists. Trying again.\n"), 
76                     tmr.VolumeName);
77                continue;
78             }
79             bstrncpy(mr->VolumeName, tmr.VolumeName, sizeof(mr->VolumeName));
80          }
81          if (mr->VolumeName[0] == 0) {
82             Jmsg(jcr, M_ERROR, 0, _("Too many failures. Giving up creating Volume.\n"));
83             goto bail_out;
84          }
85          pr.NumVols++;
86          if (db_create_media_record(jcr, jcr->db, mr) &&
87             db_update_pool_record(jcr, jcr->db, &pr)) {
88             db_unlock(jcr->db);
89             Dmsg1(90, "Created new Volume=%s\n", mr->VolumeName);
90             return 1;
91          } else {
92             Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
93          }
94       }
95    }
96 bail_out:
97    db_unlock(jcr->db);
98    return 0;   
99 }