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