]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/butil.c
Implement first cut DCR in SD
[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    
101    dev = init_dev(NULL, device);
102    jcr->device->dev = dev;
103    if (!dev || !first_open_device(dev)) {
104       Jmsg1(jcr, M_FATAL, 0, _("Cannot open %s\n"), jcr->dev_name);
105       return NULL;
106    }
107    Dmsg0(90, "Device opened for read.\n");
108
109    create_vol_list(jcr);
110
111    if (read_access) {
112       if (!acquire_device_for_read(jcr)) {
113          return NULL;
114       }
115    }
116    return dev;
117 }
118
119
120 /*
121  * Search for device resource that corresponds to 
122  * device name on command line (or default).
123  *       
124  * Returns: NULL on failure
125  *          Device resource pointer on success
126  */
127 DEVRES *find_device_res(char *device_name, int read_access)
128 {
129    int found = 0;
130    DEVRES *device;
131
132    LockRes();
133    for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
134       if (strcmp(device->device_name, device_name) == 0) {
135          found = 1;
136          break;
137       }
138    } 
139    UnlockRes();
140    if (!found) {
141       Pmsg2(0, _("Could not find device %s in config file %s.\n"), device_name,
142             configfile);
143       return NULL;
144    }
145    Pmsg2(0, _("Using device: %s for %s.\n"), device_name,
146              read_access?"reading":"writing");
147    return device;
148 }
149
150
151
152 /*
153  * Called here when freeing JCR so that we can get rid 
154  *  of "daemon" specific memory allocated.
155  */
156 static void my_free_jcr(JCR *jcr)
157 {
158    if (jcr->pool_name) {
159       free_pool_memory(jcr->pool_name);
160       jcr->pool_name = NULL;
161    }
162    if (jcr->pool_type) {
163       free_pool_memory(jcr->pool_type);
164       jcr->pool_type = NULL;
165    }
166    if (jcr->job_name) {
167       free_pool_memory(jcr->job_name);
168       jcr->job_name = NULL;
169    }
170    if (jcr->client_name) {
171       free_pool_memory(jcr->client_name);
172       jcr->client_name = NULL;
173    }
174    if (jcr->fileset_name) {
175       free_pool_memory(jcr->fileset_name);
176       jcr->fileset_name = NULL;
177    }
178    if (jcr->fileset_md5) {
179       free_pool_memory(jcr->fileset_md5);
180       jcr->fileset_md5 = NULL;
181    }
182    if (jcr->dev_name) {
183       free_pool_memory(jcr->dev_name);
184       jcr->dev_name = NULL;
185    }
186    if (jcr->VolList) {
187       free_vol_list(jcr);
188    }  
189    if (jcr->dcr) {
190       free_dcr(jcr->dcr);
191       jcr->dcr = NULL;
192    }
193      
194    return;
195 }
196
197 /*
198  * Setup a "daemon" JCR for the various standalone
199  *  tools (e.g. bls, bextract, bscan, ...)
200  */
201 JCR *setup_jcr(char *name, char *device, BSR *bsr, char *VolumeName)
202 {
203    JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr);
204    jcr->VolSessionId = 1;
205    jcr->VolSessionTime = (uint32_t)time(NULL);
206    jcr->bsr = bsr;
207    jcr->NumVolumes = 0;
208    jcr->pool_name = get_pool_memory(PM_FNAME);
209    strcpy(jcr->pool_name, "Default");
210    jcr->pool_type = get_pool_memory(PM_FNAME);
211    strcpy(jcr->pool_type, "Backup");
212    jcr->job_name = get_pool_memory(PM_FNAME);
213    pm_strcpy(&jcr->job_name, "Dummy.Job.Name");
214    jcr->client_name = get_pool_memory(PM_FNAME);
215    pm_strcpy(&jcr->client_name, "Dummy.Client.Name");
216    bstrncpy(jcr->Job, name, sizeof(jcr->Job));
217    jcr->fileset_name = get_pool_memory(PM_FNAME);
218    pm_strcpy(&jcr->fileset_name, "Dummy.fileset.name");
219    jcr->fileset_md5 = get_pool_memory(PM_FNAME);
220    pm_strcpy(&jcr->fileset_md5, "Dummy.fileset.md5");
221    jcr->JobId = 0;
222    jcr->JobType = JT_CONSOLE;
223    jcr->JobLevel = L_FULL;
224    jcr->JobStatus = JS_Terminated;
225    jcr->dev_name = get_pool_memory(PM_FNAME);
226    pm_strcpy(&jcr->dev_name, device);
227    if (!bsr && VolumeName) {
228       pm_strcpy(&jcr->VolumeName, VolumeName);
229    }
230    jcr->where = bstrdup("");
231    return jcr;
232 }
233
234
235 /*
236  * Device got an error, attempt to analyse it
237  */
238 void display_tape_error_status(JCR *jcr, DEVICE *dev)
239 {
240    uint32_t status;
241
242    status = status_dev(dev);
243    Dmsg1(20, "Device status: %x\n", status);
244    if (status & BMT_EOD)
245       Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Data\n"));
246   else if (status & BMT_EOT)
247       Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Tape\n"));
248    else if (status & BMT_EOF)
249       Jmsg(jcr, M_ERROR, 0, _("Unexpected End of File\n"));
250    else if (status & BMT_DR_OPEN)
251       Jmsg(jcr, M_ERROR, 0, _("Tape Door is Open\n"));
252    else if (!(status & BMT_ONLINE))
253       Jmsg(jcr, M_ERROR, 0, _("Unexpected Tape is Off-line\n"));
254 }