3 * Utility routines for "tool" programs such as bscan, bls,
6 * Normally nothing in this file is called by the Storage
7 * daemon because we interact more directly with the user
13 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
37 char *rec_state_to_str(DEV_RECORD *rec)
41 if (rec->state & REC_NO_HEADER) {
42 strcat(buf, "Nohdr,");
44 if (is_partial_record(rec)) {
45 strcat(buf, "partial,");
47 if (rec->state & REC_BLOCK_EMPTY) {
48 strcat(buf, "empty,");
50 if (rec->state & REC_NO_MATCH) {
51 strcat(buf, "Nomatch,");
53 if (rec->state & REC_CONTINUATION) {
57 buf[strlen(buf)-1] = 0;
65 * Setup device, jcr, and prepare to read
67 DEVICE *setup_to_read_device(JCR *jcr)
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 != '/')
80 strcpy(jcr->VolumeName, p+1);
85 dev = init_dev(NULL, jcr->dev_name);
87 Emsg1(M_FATAL, 0, "Cannot open %s\n", jcr->dev_name);
90 /* ***FIXME**** init capabilities */
91 if (!open_device(dev)) {
92 Emsg1(M_FATAL, 0, "Cannot open %s\n", jcr->dev_name);
95 Dmsg0(90, "Device opened for read.\n");
97 block = new_block(dev);
101 Dmsg1(100, "Volume=%s\n", jcr->VolumeName);
102 if (!acquire_device_for_read(jcr, dev, block)) {
103 Emsg0(M_ERROR, 0, dev->errmsg);
111 static void my_free_jcr(JCR *jcr)
117 * Setup a "daemon" JCR for the various standalone
118 * tools (e.g. bls, bextract, bscan, ...)
120 JCR *setup_jcr(char *name, char *device, BSR *bsr)
122 JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr);
123 jcr->VolSessionId = 1;
124 jcr->VolSessionTime = (uint32_t)time(NULL);
126 strcpy(jcr->Job, name);
127 jcr->dev_name = get_pool_memory(PM_FNAME);
128 strcpy(jcr->dev_name, device);
134 * Device got an error, attempt to analyse it
136 void display_error_status(DEVICE *dev)
140 Emsg0(M_ERROR, 0, dev->errmsg);
141 status_dev(dev, &status);
142 Dmsg1(20, "Device status: %x\n", status);
144 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Data\n");
145 else if (status & MT_EOT)
146 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Tape\n");
147 else if (status & MT_EOF)
148 Emsg0(M_ERROR_TERM, 0, "Unexpected End of File\n");
149 else if (status & MT_DR_OPEN)
150 Emsg0(M_ERROR_TERM, 0, "Tape Door is Open\n");
151 else if (!(status & MT_ONLINE))
152 Emsg0(M_ERROR_TERM, 0, "Unexpected Tape is Off-line\n");
154 Emsg2(M_ERROR_TERM, 0, "Read error on Record Header %s: %s\n", dev_name(dev), strerror(errno));
158 extern char *getuser(uid_t uid);
159 extern char *getgroup(gid_t gid);
161 void print_ls_output(char *fname, char *link, int type, struct stat *statp)
168 p = encode_mode(statp->st_mode, buf);
169 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
171 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
173 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
175 p = encode_time(statp->st_ctime, p);
179 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
181 if (type == FT_LNK) {
187 for (f=link; *f && (p-buf) < (int)sizeof(buf); )