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)
116 JCR *setup_jcr(char *name, char *device, BSR *bsr)
118 JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr);
119 jcr->VolSessionId = 1;
120 jcr->VolSessionTime = (uint32_t)time(NULL);
122 strcpy(jcr->Job, name);
123 jcr->dev_name = get_pool_memory(PM_FNAME);
124 strcpy(jcr->dev_name, device);
130 * Device got an error, attempt to analyse it
132 void display_error_status(DEVICE *dev)
136 Emsg0(M_ERROR, 0, dev->errmsg);
137 status_dev(dev, &status);
138 Dmsg1(20, "Device status: %x\n", status);
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");
150 Emsg2(M_ERROR_TERM, 0, "Read error on Record Header %s: %s\n", dev_name(dev), strerror(errno));
154 extern char *getuser(uid_t uid);
155 extern char *getgroup(gid_t gid);
157 void print_ls_output(char *fname, char *link, int type, struct stat *statp)
164 p = encode_mode(statp->st_mode, buf);
165 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
167 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
169 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
171 p = encode_time(statp->st_ctime, p);
175 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
177 if (type == FT_LNK) {
183 for (f=link; *f && (p-buf) < (int)sizeof(buf); )