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)
79 * If no volume name already given and no bsr, and it is a file,
80 * try getting name from Filename
82 if (!jcr->bsr && jcr->VolumeName[0] == 0) {
83 if (strncmp(jcr->dev_name, "/dev/", 5) != 0) {
84 /* Try stripping file part */
85 p = jcr->dev_name + strlen(jcr->dev_name);
86 while (p >= jcr->dev_name && *p != '/')
89 strcpy(jcr->VolumeName, p+1);
95 if ((device=find_device_res(jcr->dev_name, read_access)) == NULL) {
96 Jmsg2(jcr, M_FATAL, 0, _("Cannot find device %s in config file %s.\n"),
97 jcr->dev_name, configfile);
101 dev = init_dev(NULL, device);
102 if (!dev || !open_device(dev)) {
103 Jmsg1(jcr, M_FATAL, 0, _("Cannot open %s\n"), jcr->dev_name);
106 Dmsg0(90, "Device opened for read.\n");
108 block = new_block(dev);
110 create_vol_list(jcr);
113 if (!acquire_device_for_read(jcr, dev, block)) {
124 * Search for device resource that corresponds to
125 * device name on command line (or default).
127 * Returns: NULL on failure
128 * Device resource pointer on success
130 DEVRES *find_device_res(char *device_name, int read_access)
136 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
137 if (strcmp(device->device_name, device_name) == 0) {
144 Pmsg2(0, _("Could not find device %s in config file %s.\n"), device_name,
148 Pmsg2(0, _("Using device: %s for %s.\n"), device_name,
149 read_access?"reading":"writing");
156 * Called here when freeing JCR so that we can get rid
157 * of "daemon" specific memory allocated.
159 static void my_free_jcr(JCR *jcr)
161 if (jcr->pool_name) {
162 free_pool_memory(jcr->pool_name);
163 jcr->pool_name = NULL;
165 if (jcr->pool_type) {
166 free_pool_memory(jcr->pool_type);
167 jcr->pool_type = NULL;
170 free_pool_memory(jcr->job_name);
171 jcr->job_name = NULL;
173 if (jcr->client_name) {
174 free_pool_memory(jcr->client_name);
175 jcr->client_name = NULL;
177 if (jcr->fileset_name) {
178 free_pool_memory(jcr->fileset_name);
179 jcr->fileset_name = NULL;
181 if (jcr->fileset_md5) {
182 free_pool_memory(jcr->fileset_md5);
183 jcr->fileset_md5 = NULL;
186 free_pool_memory(jcr->dev_name);
187 jcr->dev_name = NULL;
197 * Setup a "daemon" JCR for the various standalone
198 * tools (e.g. bls, bextract, bscan, ...)
200 JCR *setup_jcr(char *name, char *device, BSR *bsr, char *VolumeName)
202 JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr);
203 jcr->VolSessionId = 1;
204 jcr->VolSessionTime = (uint32_t)time(NULL);
207 jcr->pool_name = get_pool_memory(PM_FNAME);
208 strcpy(jcr->pool_name, "Default");
209 jcr->pool_type = get_pool_memory(PM_FNAME);
210 strcpy(jcr->pool_type, "Backup");
211 jcr->job_name = get_pool_memory(PM_FNAME);
212 strcpy(jcr->job_name, "Dummy.Job.Name");
213 jcr->client_name = get_pool_memory(PM_FNAME);
214 strcpy(jcr->client_name, "Dummy.Client.Name");
215 strcpy(jcr->Job, name);
216 jcr->fileset_name = get_pool_memory(PM_FNAME);
217 strcpy(jcr->fileset_name, "Dummy.fileset.name");
218 jcr->fileset_md5 = get_pool_memory(PM_FNAME);
219 strcpy(jcr->fileset_md5, "Dummy.fileset.md5");
221 jcr->JobType = JT_BACKUP;
222 jcr->JobLevel = L_FULL;
223 jcr->JobStatus = JS_Terminated;
224 jcr->dev_name = get_pool_memory(PM_FNAME);
225 pm_strcpy(&jcr->dev_name, device);
226 if (!bsr && VolumeName) {
227 pm_strcpy(&jcr->VolumeName, VolumeName);
229 jcr->where = bstrdup("");
235 * Device got an error, attempt to analyse it
237 void display_tape_error_status(JCR *jcr, DEVICE *dev)
241 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
242 status_dev(dev, &status);
243 Dmsg1(20, "Device status: %x\n", status);
244 if (status & BMT_EOD)
245 Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Data\n"));
246 else if (status & BMT_EOT)
247 Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Tape\n"));
248 else if (status & BMT_EOF)
249 Jmsg(jcr, M_ERROR, 0, _("Unexpected End of File\n"));
250 else if (status & BMT_DR_OPEN)
251 Jmsg(jcr, M_ERROR, 0, _("Tape Door is Open\n"));
252 else if (!(status & BMT_ONLINE))
253 Jmsg(jcr, M_ERROR, 0, _("Unexpected Tape is Off-line\n"));
255 Jmsg(jcr, M_ERROR, 0, _("Read error on Record Header %s: %s\n"), dev_name(dev), strerror(errno));