]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/butil.c
Fix FileSet MD5 in JCR
[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    jcr->VolumeName[0] = 0;
79    if (strncmp(jcr->dev_name, "/dev/", 5) != 0) {
80       /* Try stripping file part */
81       p = jcr->dev_name + strlen(jcr->dev_name);
82       while (p >= jcr->dev_name && *p != '/')
83          p--;
84       if (*p == '/') {
85          strcpy(jcr->VolumeName, p+1);
86          *p = 0;
87       }
88    }
89
90    if ((device=find_device_res(jcr->dev_name, read_access)) == NULL) {
91       Emsg2(M_FATAL, 0, _("Cannot find device %s in config file %s.\n"), 
92            jcr->dev_name, configfile);
93       return NULL;
94    }
95    
96    dev = init_dev(NULL, device);
97    if (!dev || !open_device(dev)) {
98       Emsg1(M_FATAL, 0, _("Cannot open %s\n"), jcr->dev_name);
99       return NULL;
100    }
101    Dmsg0(90, "Device opened for read.\n");
102
103    block = new_block(dev);
104
105    create_vol_list(jcr);
106
107    if (read_access) {
108       if (!acquire_device_for_read(jcr, dev, block)) {
109          Emsg0(M_ERROR, 0, dev->errmsg);
110          free_block(block);
111          return NULL;
112       }
113    }
114    free_block(block);
115    return dev;
116 }
117
118
119 /*
120  * Search for device resource that corresponds to 
121  * device name on command line (or default).
122  *       
123  * Returns: NULL on failure
124  *          Device resource pointer on success
125  */
126 DEVRES *find_device_res(char *device_name, int read_access)
127 {
128    int found = 0;
129    DEVRES *device;
130
131    LockRes();
132    for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
133       if (strcmp(device->device_name, device_name) == 0) {
134          found = 1;
135          break;
136       }
137    } 
138    UnlockRes();
139    if (!found) {
140       Pmsg2(0, _("Could not find device %s in config file %s.\n"), device_name,
141             configfile);
142       return NULL;
143    }
144    Pmsg2(0, _("Using device: %s for %s.\n"), device_name,
145              read_access?"reading":"writing");
146    return device;
147 }
148
149
150
151 /*
152  * Called here when freeing JCR so that we can get rid 
153  *  of "daemon" specific memory allocated.
154  */
155 static void my_free_jcr(JCR *jcr)
156 {
157    if (jcr->pool_name) {
158       free_pool_memory(jcr->pool_name);
159       jcr->pool_name = NULL;
160    }
161    if (jcr->pool_type) {
162       free_pool_memory(jcr->pool_type);
163       jcr->pool_type = NULL;
164    }
165    if (jcr->job_name) {
166       free_pool_memory(jcr->job_name);
167       jcr->job_name = NULL;
168    }
169    if (jcr->client_name) {
170       free_pool_memory(jcr->client_name);
171       jcr->client_name = NULL;
172    }
173    if (jcr->fileset_name) {
174       free_pool_memory(jcr->fileset_name);
175       jcr->fileset_name = NULL;
176    }
177    if (jcr->dev_name) {
178       free_pool_memory(jcr->dev_name);
179       jcr->dev_name = NULL;
180    }
181    if (jcr->VolList) {
182       free_vol_list(jcr);
183    }  
184      
185    return;
186 }
187
188 /*
189  * Setup a "daemon" JCR for the various standalone
190  *  tools (e.g. bls, bextract, bscan, ...)
191  */
192 JCR *setup_jcr(char *name, char *device, BSR *bsr) 
193 {
194    JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr);
195    jcr->VolSessionId = 1;
196    jcr->VolSessionTime = (uint32_t)time(NULL);
197    jcr->bsr = bsr;
198    jcr->NumVolumes = 1;
199    jcr->pool_name = get_pool_memory(PM_FNAME);
200    strcpy(jcr->pool_name, "Default");
201    jcr->pool_type = get_pool_memory(PM_FNAME);
202    strcpy(jcr->pool_type, "Backup");
203    jcr->job_name = get_pool_memory(PM_FNAME);
204    strcpy(jcr->job_name, "Dummy.Job.Name");
205    jcr->client_name = get_pool_memory(PM_FNAME);
206    strcpy(jcr->client_name, "Dummy.Client.Name");
207    strcpy(jcr->Job, name);
208    jcr->fileset_name = get_pool_memory(PM_FNAME);
209    strcpy(jcr->fileset_name, "Dummy.fileset.name");
210    jcr->fileset_md5 = get_pool_memory(PM_FNAME);
211    strcpy(jcr->fileset_md5, "Dummy.fileset.md5");
212    jcr->JobId = 1;
213    jcr->JobType = JT_BACKUP;
214    jcr->JobLevel = L_FULL;
215    jcr->JobStatus = JS_Terminated;
216    jcr->dev_name = get_pool_memory(PM_FNAME);
217    pm_strcpy(&jcr->dev_name, device);
218    return jcr;
219 }
220
221
222 /*
223  * Device got an error, attempt to analyse it
224  */
225 void display_error_status(DEVICE *dev)
226 {
227    uint32_t status;
228
229    Emsg0(M_ERROR, 0, dev->errmsg);
230    status_dev(dev, &status);
231    Dmsg1(20, "Device status: %x\n", status);
232    if (status & MT_EOD)
233       Emsg0(M_ERROR_TERM, 0, _("Unexpected End of Data\n"));
234    else if (status & MT_EOT)
235       Emsg0(M_ERROR_TERM, 0, _("Unexpected End of Tape\n"));
236    else if (status & MT_EOF)
237       Emsg0(M_ERROR_TERM, 0, _("Unexpected End of File\n"));
238    else if (status & MT_DR_OPEN)
239       Emsg0(M_ERROR_TERM, 0, _("Tape Door is Open\n"));
240    else if (!(status & MT_ONLINE))
241       Emsg0(M_ERROR_TERM, 0, _("Unexpected Tape is Off-line\n"));
242    else
243       Emsg2(M_ERROR_TERM, 0, _("Read error on Record Header %s: %s\n"), dev_name(dev), strerror(errno));
244 }
245
246
247 extern char *getuser(uid_t uid);
248 extern char *getgroup(gid_t gid);
249
250 void print_ls_output(char *fname, char *link, int type, struct stat *statp)
251 {
252    char buf[1000]; 
253    char ec1[30];
254    char *p, *f;
255    int n;
256
257    p = encode_mode(statp->st_mode, buf);
258    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
259    p += n;
260    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
261    p += n;
262    n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
263    p += n;
264    p = encode_time(statp->st_ctime, p);
265    *p++ = ' ';
266    *p++ = ' ';
267    /* Copy file name */
268    for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
269       *p++ = *f++;
270    if (type == FT_LNK) {
271       *p++ = ' ';
272       *p++ = '-';
273       *p++ = '>';
274       *p++ = ' ';
275       /* Copy link name */
276       for (f=link; *f && (p-buf) < (int)sizeof(buf); )
277          *p++ = *f++;
278    }
279    *p++ = '\n';
280    *p = 0;
281    fputs(buf, stdout);
282 }