]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/butil.c
Massive SD calling sequence reorganization
[bacula/bacula] / bacula / src / stored / butil.c
1 /*
2  *
3  *  Utility routines for "tool" programs such as bscan, bls,
4  *    bextract, ...  Some routines also used by Bacula.
5  *
6  *    Kern Sibbald, MM
7  * 
8  *  Normally nothing in this file is called by the Storage   
9  *    daemon because we interact more directly with the user
10  *    i.e. printf, ...
11  *
12  *   Version $Id$
13  */
14 /*
15    Copyright (C) 2000-2004 Kern Sibbald and John Walker
16
17    This program is free software; you can redistribute it and/or
18    modify it under the terms of the GNU General Public License as
19    published by the Free Software Foundation; either version 2 of
20    the License, or (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public
28    License along with this program; if not, write to the Free
29    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30    MA 02111-1307, USA.
31
32  */
33
34 #include "bacula.h"
35 #include "stored.h"
36
37 /* Forward referenced functions */
38 static DCR *setup_to_access_device(JCR *jcr, char *dev_name, const char *VolumeName, int mode);
39 static DEVRES *find_device_res(char *device_name, int mode);
40 static void my_free_jcr(JCR *jcr);
41
42 /* Imported variables -- eliminate some day */
43 extern char *configfile;
44
45 #ifdef DEBUG
46 char *rec_state_to_str(DEV_RECORD *rec)
47 {
48    static char buf[200]; 
49    buf[0] = 0;
50    if (rec->state & REC_NO_HEADER) {
51       strcat(buf, "Nohdr,");
52    }
53    if (is_partial_record(rec)) {
54       strcat(buf, "partial,");
55    }
56    if (rec->state & REC_BLOCK_EMPTY) {
57       strcat(buf, "empty,");
58    }
59    if (rec->state & REC_NO_MATCH) {
60       strcat(buf, "Nomatch,");
61    }
62    if (rec->state & REC_CONTINUATION) {
63       strcat(buf, "cont,");
64    }
65    if (buf[0]) {
66       buf[strlen(buf)-1] = 0;
67    }
68    return buf;
69 }
70 #endif
71
72 /*
73  * Setup a "daemon" JCR for the various standalone
74  *  tools (e.g. bls, bextract, bscan, ...)
75  */
76 JCR *setup_jcr(const char *name, char *dev_name, BSR *bsr,
77                const char *VolumeName, int mode)
78 {
79    DCR *dcr;
80    JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr);
81    jcr->bsr = bsr;
82    jcr->VolSessionId = 1;
83    jcr->VolSessionTime = (uint32_t)time(NULL);
84    jcr->NumVolumes = 0;
85    jcr->JobId = 0;
86    jcr->JobType = JT_CONSOLE;
87    jcr->JobLevel = L_FULL;
88    jcr->JobStatus = JS_Terminated;
89    jcr->where = bstrdup("");
90    jcr->job_name = get_pool_memory(PM_FNAME);
91    pm_strcpy(jcr->job_name, "Dummy.Job.Name");
92    jcr->client_name = get_pool_memory(PM_FNAME);
93    pm_strcpy(jcr->client_name, "Dummy.Client.Name");
94    bstrncpy(jcr->Job, name, sizeof(jcr->Job));
95    jcr->fileset_name = get_pool_memory(PM_FNAME);
96    pm_strcpy(jcr->fileset_name, "Dummy.fileset.name");
97    jcr->fileset_md5 = get_pool_memory(PM_FNAME);
98    pm_strcpy(jcr->fileset_md5, "Dummy.fileset.md5");
99
100    dcr = setup_to_access_device(jcr, dev_name, VolumeName, mode);
101    if (!dcr) {
102       return NULL;
103    }
104    if (!bsr && VolumeName) {
105       bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
106    }
107    strcpy(dcr->pool_name, "Default");
108    strcpy(dcr->pool_type, "Backup");
109    return jcr;
110 }
111
112 /*
113  * Setup device, jcr, and prepare to access device.
114  *   If the caller wants read access, acquire the device, otherwise,
115  *     the caller will do it.
116  */
117 static DCR *setup_to_access_device(JCR *jcr, char *dev_name, const char *VolumeName, int mode)
118 {
119    DEVICE *dev;
120    char *p;
121    DEVRES *device;
122    DCR *dcr;
123    char VolName[MAX_NAME_LENGTH];
124
125    /*
126     * If no volume name already given and no bsr, and it is a file,
127     * try getting name from Filename  
128     */
129    VolName[0] = 0;
130    if (!jcr->bsr && VolumeName[0] == 0) {
131       if (strncmp(dev_name, "/dev/", 5) != 0) {
132          /* Try stripping file part */
133          p = dev_name + strlen(dev_name);
134
135          while (p >= dev_name && *p != '/')
136             p--;
137          if (*p == '/') {
138             bstrncpy(VolName, p+1, sizeof(VolName));
139             *p = 0;
140          }
141       }
142    }
143
144    if ((device=find_device_res(dev_name, mode)) == NULL) {
145       Jmsg2(jcr, M_FATAL, 0, _("Cannot find device \"%s\" in config file %s.\n"), 
146            dev_name, configfile);
147       return NULL;
148    }
149    jcr->device = device;
150    
151    dev = init_dev(NULL, device);
152    if (!dev) {
153       Jmsg1(jcr, M_FATAL, 0, _("Cannot init device %s\n"), dev_name);
154       return NULL;
155    }
156    device->dev = dev;
157    dcr = new_dcr(jcr, dev);        
158    if (VolName[0]) {
159       bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
160    }
161    bstrncpy(dcr->dev_name, device->device_name, sizeof(dcr->dev_name));
162    if (!first_open_device(dev)) {
163       Jmsg1(jcr, M_FATAL, 0, _("Cannot open %s\n"), dcr->dev_name);
164       return NULL;
165    }
166    Dmsg0(90, "Device opened for read.\n");
167
168    create_vol_list(jcr);
169
170    if (mode) {                        /* read only access? */
171       if (!acquire_device_for_read(jcr)) {
172          return NULL;
173       }
174    }
175    return dcr;
176 }
177
178
179 /*
180  * Called here when freeing JCR so that we can get rid 
181  *  of "daemon" specific memory allocated.
182  */
183 static void my_free_jcr(JCR *jcr)
184 {
185    if (jcr->job_name) {
186       free_pool_memory(jcr->job_name);
187       jcr->job_name = NULL;
188    }
189    if (jcr->client_name) {
190       free_pool_memory(jcr->client_name);
191       jcr->client_name = NULL;
192    }
193    if (jcr->fileset_name) {
194       free_pool_memory(jcr->fileset_name);
195       jcr->fileset_name = NULL;
196    }
197    if (jcr->fileset_md5) {
198       free_pool_memory(jcr->fileset_md5);
199       jcr->fileset_md5 = NULL;
200    }
201    if (jcr->VolList) {
202       free_vol_list(jcr);
203    }  
204    if (jcr->dcr) {
205       free_dcr(jcr->dcr);
206       jcr->dcr = NULL;
207    }
208    return;
209 }
210
211
212 /*
213  * Search for device resource that corresponds to 
214  * device name on command line (or default).
215  *       
216  * Returns: NULL on failure
217  *          Device resource pointer on success
218  */
219 static DEVRES *find_device_res(char *device_name, int read_access)
220 {
221    bool found = false;
222    DEVRES *device;
223
224    LockRes();
225    foreach_res(device, R_DEVICE) {
226       if (strcmp(device->device_name, device_name) == 0) {
227          found = true;
228          break;
229       }
230    } 
231    if (!found) {
232       /* Search for name of Device resource rather than archive name */
233       if (device_name[0] == '"') {
234          strcpy(device_name, device_name+1);
235          int len = strlen(device_name);
236          if (len > 0) {
237             device_name[len-1] = 0;   /* zap trailing " */
238          }
239       }
240       foreach_res(device, R_DEVICE) {
241          if (strcmp(device->hdr.name, device_name) == 0) {
242             found = true;
243             break;
244          }
245       } 
246    }
247    UnlockRes();
248    if (!found) {
249       Pmsg2(0, _("Could not find device \"%s\" in config file %s.\n"), device_name,
250             configfile);
251       return NULL;
252    }
253    Pmsg2(0, _("Using device: \"%s\" for %s.\n"), device_name,
254              read_access?"reading":"writing");
255    return device;
256 }
257
258
259 /*
260  * Device got an error, attempt to analyse it
261  */
262 void display_tape_error_status(JCR *jcr, DEVICE *dev)
263 {
264    uint32_t status;
265
266    status = status_dev(dev);
267    Dmsg1(20, "Device status: %x\n", status);
268    if (status & BMT_EOD)
269       Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Data\n"));
270    else if (status & BMT_EOT)
271       Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Tape\n"));
272    else if (status & BMT_EOF)
273       Jmsg(jcr, M_ERROR, 0, _("Unexpected End of File\n"));
274    else if (status & BMT_DR_OPEN)
275       Jmsg(jcr, M_ERROR, 0, _("Tape Door is Open\n"));
276    else if (!(status & BMT_ONLINE))
277       Jmsg(jcr, M_ERROR, 0, _("Unexpected Tape is Off-line\n"));
278 }