]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/butil.c
668dbec46fdf286576396ce00f498a28981914c3
[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
36 #ifdef DEBUG
37 char *rec_state_to_str(DEV_RECORD *rec)
38 {
39    static char buf[200]; 
40    buf[0] = 0;
41    if (rec->state & REC_NO_HEADER) {
42       strcat(buf, "Nohdr,");
43    }
44    if (is_partial_record(rec)) {
45       strcat(buf, "partial,");
46    }
47    if (rec->state & REC_BLOCK_EMPTY) {
48       strcat(buf, "empty,");
49    }
50    if (rec->state & REC_NO_MATCH) {
51       strcat(buf, "Nomatch,");
52    }
53    if (rec->state & REC_CONTINUATION) {
54       strcat(buf, "cont,");
55    }
56    if (buf[0]) {
57       buf[strlen(buf)-1] = 0;
58    }
59    return buf;
60 }
61 #endif
62
63
64 /*
65  * Setup device, jcr, and prepare to read
66  */
67 DEVICE *setup_to_read_device(JCR *jcr)
68 {
69    DEVICE *dev;
70    DEV_BLOCK *block;
71    char *p;
72
73    jcr->VolumeName[0] = 0;
74    if (strncmp(jcr->dev_name, "/dev/", 5) != 0) {
75       /* Try stripping file part */
76       p = jcr->dev_name + strlen(jcr->dev_name);
77       while (p >= jcr->dev_name && *p != '/')
78          p--;
79       if (*p == '/') {
80          strcpy(jcr->VolumeName, p+1);
81          *p = 0;
82       }
83    }
84
85    dev = init_dev(NULL, jcr->dev_name);
86    if (!dev) {
87       Emsg1(M_FATAL, 0, "Cannot open %s\n", jcr->dev_name);
88       return NULL;
89    }
90    /* ***FIXME**** init capabilities */
91    if (!open_device(dev)) {
92       Emsg1(M_FATAL, 0, "Cannot open %s\n", jcr->dev_name);
93       return NULL;
94    }
95    Dmsg0(90, "Device opened for read.\n");
96
97    block = new_block(dev);
98
99    create_vol_list(jcr);
100
101    Dmsg1(100, "Volume=%s\n", jcr->VolumeName);
102    if (!acquire_device_for_read(jcr, dev, block)) {
103       Emsg0(M_ERROR, 0, dev->errmsg);
104       return NULL;
105    }
106    free_block(block);
107    return dev;
108 }
109
110
111 static void my_free_jcr(JCR *jcr)
112 {
113    return;
114 }
115
116 JCR *setup_jcr(char *name, char *device, BSR *bsr) 
117 {
118    JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr);
119    jcr->VolSessionId = 1;
120    jcr->VolSessionTime = (uint32_t)time(NULL);
121    jcr->bsr = bsr;
122    strcpy(jcr->Job, name);
123    jcr->dev_name = get_pool_memory(PM_FNAME);
124    strcpy(jcr->dev_name, device);
125    return jcr;
126 }
127
128
129 /*
130  * Device got an error, attempt to analyse it
131  */
132 void display_error_status(DEVICE *dev)
133 {
134    uint32_t status;
135
136    Emsg0(M_ERROR, 0, dev->errmsg);
137    status_dev(dev, &status);
138    Dmsg1(20, "Device status: %x\n", status);
139    if (status & MT_EOD)
140       Emsg0(M_ERROR_TERM, 0, "Unexpected End of Data\n");
141    else if (status & MT_EOT)
142       Emsg0(M_ERROR_TERM, 0, "Unexpected End of Tape\n");
143    else if (status & MT_EOF)
144       Emsg0(M_ERROR_TERM, 0, "Unexpected End of File\n");
145    else if (status & MT_DR_OPEN)
146       Emsg0(M_ERROR_TERM, 0, "Tape Door is Open\n");
147    else if (!(status & MT_ONLINE))
148       Emsg0(M_ERROR_TERM, 0, "Unexpected Tape is Off-line\n");
149    else
150       Emsg2(M_ERROR_TERM, 0, "Read error on Record Header %s: %s\n", dev_name(dev), strerror(errno));
151 }
152
153
154 extern char *getuser(uid_t uid);
155 extern char *getgroup(gid_t gid);
156
157 void print_ls_output(char *fname, char *link, int type, struct stat *statp)
158 {
159    char buf[1000]; 
160    char ec1[30];
161    char *p, *f;
162    int n;
163
164    p = encode_mode(statp->st_mode, buf);
165    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
166    p += n;
167    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
168    p += n;
169    n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
170    p += n;
171    p = encode_time(statp->st_ctime, p);
172    *p++ = ' ';
173    *p++ = ' ';
174    /* Copy file name */
175    for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
176       *p++ = *f++;
177    if (type == FT_LNK) {
178       *p++ = ' ';
179       *p++ = '-';
180       *p++ = '>';
181       *p++ = ' ';
182       /* Copy link name */
183       for (f=link; *f && (p-buf) < (int)sizeof(buf); )
184          *p++ = *f++;
185    }
186    *p++ = '\n';
187    *p = 0;
188    fputs(buf, stdout);
189 }