]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/butil.c
Add Peter Eriksson's const code + turn bsscanf code
[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  *  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    char *p;
75    DEVRES *device;
76
77    /*
78     * If no volume name already given and no bsr, and it is a file,
79     * try getting name from Filename  
80     */
81    if (!jcr->bsr && jcr->VolumeName[0] == 0) {
82       if (strncmp(jcr->dev_name, "/dev/", 5) != 0) {
83          /* Try stripping file part */
84          p = jcr->dev_name + strlen(jcr->dev_name);
85          while (p >= jcr->dev_name && *p != '/')
86             p--;
87          if (*p == '/') {
88             pm_strcpy(&jcr->VolumeName, p+1);
89             *p = 0;
90          }
91       }
92    }
93
94    if ((device=find_device_res(jcr->dev_name, read_access)) == NULL) {
95       Jmsg2(jcr, M_FATAL, 0, _("Cannot find device \"%s\" in config file %s.\n"), 
96            jcr->dev_name, configfile);
97       return NULL;
98    }
99    jcr->device = device;
100    pm_strcpy(&jcr->dev_name, device->device_name);
101    
102    dev = init_dev(NULL, device);
103    if (!dev) {
104       Jmsg1(jcr, M_FATAL, 0, _("Cannot init device %s\n"), jcr->dev_name);
105       return NULL;
106    }
107    jcr->device->dev = dev;
108    new_dcr(jcr, dev);        
109    if (!dev || !first_open_device(dev)) {
110       Jmsg1(jcr, M_FATAL, 0, _("Cannot open %s\n"), jcr->dev_name);
111       return NULL;
112    }
113    Dmsg0(90, "Device opened for read.\n");
114
115    create_vol_list(jcr);
116
117    if (read_access) {
118       if (!acquire_device_for_read(jcr)) {
119          return NULL;
120       }
121    }
122    return dev;
123 }
124
125
126 /*
127  * Search for device resource that corresponds to 
128  * device name on command line (or default).
129  *       
130  * Returns: NULL on failure
131  *          Device resource pointer on success
132  */
133 DEVRES *find_device_res(char *device_name, int read_access)
134 {
135    bool found = false;
136    DEVRES *device;
137
138    LockRes();
139    foreach_res(device, R_DEVICE) {
140       if (strcmp(device->device_name, device_name) == 0) {
141          found = true;
142          break;
143       }
144    } 
145    if (!found) {
146       /* Search for name of Device resource rather than archive name */
147       if (device_name[0] == '"') {
148          strcpy(device_name, device_name+1);
149          int len = strlen(device_name);
150          if (len > 0) {
151             device_name[len-1] = 0;
152          }
153       }
154       foreach_res(device, R_DEVICE) {
155          if (strcmp(device->hdr.name, device_name) == 0) {
156             found = true;
157             break;
158          }
159       } 
160    }
161    UnlockRes();
162    if (!found) {
163       Pmsg2(0, _("Could not find device \"%s\" in config file %s.\n"), device_name,
164             configfile);
165       return NULL;
166    }
167    Pmsg2(0, _("Using device: \"%s\" for %s.\n"), device_name,
168              read_access?"reading":"writing");
169    return device;
170 }
171
172
173
174 /*
175  * Called here when freeing JCR so that we can get rid 
176  *  of "daemon" specific memory allocated.
177  */
178 static void my_free_jcr(JCR *jcr)
179 {
180    if (jcr->pool_name) {
181       free_pool_memory(jcr->pool_name);
182       jcr->pool_name = NULL;
183    }
184    if (jcr->pool_type) {
185       free_pool_memory(jcr->pool_type);
186       jcr->pool_type = NULL;
187    }
188    if (jcr->job_name) {
189       free_pool_memory(jcr->job_name);
190       jcr->job_name = NULL;
191    }
192    if (jcr->client_name) {
193       free_pool_memory(jcr->client_name);
194       jcr->client_name = NULL;
195    }
196    if (jcr->fileset_name) {
197       free_pool_memory(jcr->fileset_name);
198       jcr->fileset_name = NULL;
199    }
200    if (jcr->fileset_md5) {
201       free_pool_memory(jcr->fileset_md5);
202       jcr->fileset_md5 = NULL;
203    }
204    if (jcr->dev_name) {
205       free_pool_memory(jcr->dev_name);
206       jcr->dev_name = NULL;
207    }
208    if (jcr->VolList) {
209       free_vol_list(jcr);
210    }  
211    if (jcr->dcr) {
212       free_dcr(jcr->dcr);
213       jcr->dcr = NULL;
214    }
215      
216    return;
217 }
218
219 /*
220  * Setup a "daemon" JCR for the various standalone
221  *  tools (e.g. bls, bextract, bscan, ...)
222  */
223 JCR *setup_jcr(const char *name, const char *device, BSR *bsr, const char *VolumeName)
224 {
225    JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr);
226    jcr->VolSessionId = 1;
227    jcr->VolSessionTime = (uint32_t)time(NULL);
228    jcr->bsr = bsr;
229    jcr->NumVolumes = 0;
230    jcr->pool_name = get_pool_memory(PM_FNAME);
231    strcpy(jcr->pool_name, "Default");
232    jcr->pool_type = get_pool_memory(PM_FNAME);
233    strcpy(jcr->pool_type, "Backup");
234    jcr->job_name = get_pool_memory(PM_FNAME);
235    pm_strcpy(&jcr->job_name, "Dummy.Job.Name");
236    jcr->client_name = get_pool_memory(PM_FNAME);
237    pm_strcpy(&jcr->client_name, "Dummy.Client.Name");
238    bstrncpy(jcr->Job, name, sizeof(jcr->Job));
239    jcr->fileset_name = get_pool_memory(PM_FNAME);
240    pm_strcpy(&jcr->fileset_name, "Dummy.fileset.name");
241    jcr->fileset_md5 = get_pool_memory(PM_FNAME);
242    pm_strcpy(&jcr->fileset_md5, "Dummy.fileset.md5");
243    jcr->JobId = 0;
244    jcr->JobType = JT_CONSOLE;
245    jcr->JobLevel = L_FULL;
246    jcr->JobStatus = JS_Terminated;
247    jcr->dev_name = get_pool_memory(PM_FNAME);
248    pm_strcpy(&jcr->dev_name, device);
249    if (!bsr && VolumeName) {
250       pm_strcpy(&jcr->VolumeName, VolumeName);
251    }
252    jcr->where = bstrdup("");
253    return jcr;
254 }
255
256
257 /*
258  * Device got an error, attempt to analyse it
259  */
260 void display_tape_error_status(JCR *jcr, DEVICE *dev)
261 {
262    uint32_t status;
263
264    status = status_dev(dev);
265    Dmsg1(20, "Device status: %x\n", status);
266    if (status & BMT_EOD)
267       Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Data\n"));
268   else if (status & BMT_EOT)
269       Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Tape\n"));
270    else if (status & BMT_EOF)
271       Jmsg(jcr, M_ERROR, 0, _("Unexpected End of File\n"));
272    else if (status & BMT_DR_OPEN)
273       Jmsg(jcr, M_ERROR, 0, _("Tape Door is Open\n"));
274    else if (!(status & BMT_ONLINE))
275       Jmsg(jcr, M_ERROR, 0, _("Unexpected Tape is Off-line\n"));
276 }