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,
35 /* Imported variables -- eliminate some day */
36 extern char *configfile;
39 char *rec_state_to_str(DEV_RECORD *rec)
43 if (rec->state & REC_NO_HEADER) {
44 strcat(buf, "Nohdr,");
46 if (is_partial_record(rec)) {
47 strcat(buf, "partial,");
49 if (rec->state & REC_BLOCK_EMPTY) {
50 strcat(buf, "empty,");
52 if (rec->state & REC_NO_MATCH) {
53 strcat(buf, "Nomatch,");
55 if (rec->state & REC_CONTINUATION) {
59 buf[strlen(buf)-1] = 0;
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.
71 DEVICE *setup_to_access_device(JCR *jcr, int read_access)
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 != '/')
85 strcpy(jcr->VolumeName, p+1);
90 if ((device=find_device_res(jcr->dev_name, read_access)) == NULL) {
91 Jmsg2(jcr, M_FATAL, 0, _("Cannot find device %s in config file %s.\n"),
92 jcr->dev_name, configfile);
96 dev = init_dev(NULL, device);
97 if (!dev || !open_device(dev)) {
98 Jmsg1(jcr, M_FATAL, 0, _("Cannot open %s\n"), jcr->dev_name);
101 Dmsg0(90, "Device opened for read.\n");
103 block = new_block(dev);
105 create_vol_list(jcr);
108 if (!acquire_device_for_read(jcr, dev, block)) {
119 * Search for device resource that corresponds to
120 * device name on command line (or default).
122 * Returns: NULL on failure
123 * Device resource pointer on success
125 DEVRES *find_device_res(char *device_name, int read_access)
131 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
132 if (strcmp(device->device_name, device_name) == 0) {
139 Pmsg2(0, _("Could not find device %s in config file %s.\n"), device_name,
143 Pmsg2(0, _("Using device: %s for %s.\n"), device_name,
144 read_access?"reading":"writing");
151 * Called here when freeing JCR so that we can get rid
152 * of "daemon" specific memory allocated.
154 static void my_free_jcr(JCR *jcr)
156 if (jcr->pool_name) {
157 free_pool_memory(jcr->pool_name);
158 jcr->pool_name = NULL;
160 if (jcr->pool_type) {
161 free_pool_memory(jcr->pool_type);
162 jcr->pool_type = NULL;
165 free_pool_memory(jcr->job_name);
166 jcr->job_name = NULL;
168 if (jcr->client_name) {
169 free_pool_memory(jcr->client_name);
170 jcr->client_name = NULL;
172 if (jcr->fileset_name) {
173 free_pool_memory(jcr->fileset_name);
174 jcr->fileset_name = NULL;
176 if (jcr->fileset_md5) {
177 free_pool_memory(jcr->fileset_md5);
178 jcr->fileset_md5 = NULL;
181 free_pool_memory(jcr->dev_name);
182 jcr->dev_name = NULL;
192 * Setup a "daemon" JCR for the various standalone
193 * tools (e.g. bls, bextract, bscan, ...)
195 JCR *setup_jcr(char *name, char *device, BSR *bsr)
197 JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr);
198 jcr->VolSessionId = 1;
199 jcr->VolSessionTime = (uint32_t)time(NULL);
202 jcr->pool_name = get_pool_memory(PM_FNAME);
203 strcpy(jcr->pool_name, "Default");
204 jcr->pool_type = get_pool_memory(PM_FNAME);
205 strcpy(jcr->pool_type, "Backup");
206 jcr->job_name = get_pool_memory(PM_FNAME);
207 strcpy(jcr->job_name, "Dummy.Job.Name");
208 jcr->client_name = get_pool_memory(PM_FNAME);
209 strcpy(jcr->client_name, "Dummy.Client.Name");
210 strcpy(jcr->Job, name);
211 jcr->fileset_name = get_pool_memory(PM_FNAME);
212 strcpy(jcr->fileset_name, "Dummy.fileset.name");
213 jcr->fileset_md5 = get_pool_memory(PM_FNAME);
214 strcpy(jcr->fileset_md5, "Dummy.fileset.md5");
216 jcr->JobType = JT_BACKUP;
217 jcr->JobLevel = L_FULL;
218 jcr->JobStatus = JS_Terminated;
219 jcr->dev_name = get_pool_memory(PM_FNAME);
220 pm_strcpy(&jcr->dev_name, device);
226 * Device got an error, attempt to analyse it
228 void display_error_status(DEVICE *dev)
232 Emsg0(M_ERROR, 0, dev->errmsg);
233 status_dev(dev, &status);
234 Dmsg1(20, "Device status: %x\n", status);
236 Emsg0(M_ERROR_TERM, 0, _("Unexpected End of Data\n"));
237 else if (status & MT_EOT)
238 Emsg0(M_ERROR_TERM, 0, _("Unexpected End of Tape\n"));
239 else if (status & MT_EOF)
240 Emsg0(M_ERROR_TERM, 0, _("Unexpected End of File\n"));
241 else if (status & MT_DR_OPEN)
242 Emsg0(M_ERROR_TERM, 0, _("Tape Door is Open\n"));
243 else if (!(status & MT_ONLINE))
244 Emsg0(M_ERROR_TERM, 0, _("Unexpected Tape is Off-line\n"));
246 Emsg2(M_ERROR_TERM, 0, _("Read error on Record Header %s: %s\n"), dev_name(dev), strerror(errno));
250 extern char *getuser(uid_t uid);
251 extern char *getgroup(gid_t gid);
253 void print_ls_output(char *fname, char *link, int type, struct stat *statp)
260 p = encode_mode(statp->st_mode, buf);
261 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
263 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
265 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
267 p = encode_time(statp->st_ctime, p);
271 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
273 if (type == FT_LNK) {
279 for (f=link; *f && (p-buf) < (int)sizeof(buf); )