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