3 * Utility routines for "tool" programs such as bscan, bls,
4 * bextract, ... Some routines also used by Bacula.
8 * Normally nothing in this file is called by the Storage
9 * daemon because we interact more directly with the user
15 Copyright (C) 2000-2005 Kern Sibbald
17 This program is free software; you can redistribute it and/or
18 modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation; either version 2 of
20 the License, or (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public
28 License along with this program; if not, write to the Free
29 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 /* Forward referenced functions */
38 static DCR *setup_to_access_device(JCR *jcr, char *dev_name, const char *VolumeName, int mode);
39 static DEVRES *find_device_res(char *device_name, int mode);
40 static void my_free_jcr(JCR *jcr);
42 /* Imported variables -- eliminate some day */
43 extern char *configfile;
46 char *rec_state_to_str(DEV_RECORD *rec)
50 if (rec->state & REC_NO_HEADER) {
51 strcat(buf, "Nohdr,");
53 if (is_partial_record(rec)) {
54 strcat(buf, "partial,");
56 if (rec->state & REC_BLOCK_EMPTY) {
57 strcat(buf, "empty,");
59 if (rec->state & REC_NO_MATCH) {
60 strcat(buf, "Nomatch,");
62 if (rec->state & REC_CONTINUATION) {
66 buf[strlen(buf)-1] = 0;
73 * Setup a "daemon" JCR for the various standalone
74 * tools (e.g. bls, bextract, bscan, ...)
76 JCR *setup_jcr(const char *name, char *dev_name, BSR *bsr,
77 const char *VolumeName, int mode)
80 JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr);
82 jcr->VolSessionId = 1;
83 jcr->VolSessionTime = (uint32_t)time(NULL);
86 jcr->JobType = JT_CONSOLE;
87 jcr->JobLevel = L_FULL;
88 jcr->JobStatus = JS_Terminated;
89 jcr->where = bstrdup("");
90 jcr->job_name = get_pool_memory(PM_FNAME);
91 pm_strcpy(jcr->job_name, "Dummy.Job.Name");
92 jcr->client_name = get_pool_memory(PM_FNAME);
93 pm_strcpy(jcr->client_name, "Dummy.Client.Name");
94 bstrncpy(jcr->Job, name, sizeof(jcr->Job));
95 jcr->fileset_name = get_pool_memory(PM_FNAME);
96 pm_strcpy(jcr->fileset_name, "Dummy.fileset.name");
97 jcr->fileset_md5 = get_pool_memory(PM_FNAME);
98 pm_strcpy(jcr->fileset_md5, "Dummy.fileset.md5");
100 dcr = setup_to_access_device(jcr, dev_name, VolumeName, mode);
104 if (!bsr && VolumeName) {
105 bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
107 bstrncpy(dcr->pool_name, "Default", sizeof(dcr->pool_name));
108 bstrncpy(dcr->pool_type, "Backup", sizeof(dcr->pool_type));
113 * Setup device, jcr, and prepare to access device.
114 * If the caller wants read access, acquire the device, otherwise,
115 * the caller will do it.
117 static DCR *setup_to_access_device(JCR *jcr, char *dev_name, const char *VolumeName, int mode)
123 char VolName[MAX_NAME_LENGTH];
126 * If no volume name already given and no bsr, and it is a file,
127 * try getting name from Filename
130 bstrncpy(VolName, VolumeName, sizeof(VolName));
134 if (!jcr->bsr && VolName[0] == 0) {
135 if (strncmp(dev_name, "/dev/", 5) != 0) {
136 /* Try stripping file part */
137 p = dev_name + strlen(dev_name);
139 while (p >= dev_name && *p != '/')
142 bstrncpy(VolName, p+1, sizeof(VolName));
148 if ((device=find_device_res(dev_name, mode)) == NULL) {
149 Jmsg2(jcr, M_FATAL, 0, _("Cannot find device \"%s\" in config file %s.\n"),
150 dev_name, configfile);
154 dev = init_dev(NULL, device);
156 Jmsg1(jcr, M_FATAL, 0, _("Cannot init device %s\n"), dev_name);
160 dcr = new_dcr(jcr, dev);
162 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
164 bstrncpy(dcr->dev_name, device->device_name, sizeof(dcr->dev_name));
165 if (!first_open_device(dev)) {
166 Jmsg1(jcr, M_FATAL, 0, _("Cannot open %s\n"), dcr->dev_name);
169 Dmsg0(90, "Device opened for read.\n");
171 create_vol_list(jcr);
173 if (mode) { /* read only access? */
174 if (!acquire_device_for_read(jcr, dev)) {
183 * Called here when freeing JCR so that we can get rid
184 * of "daemon" specific memory allocated.
186 static void my_free_jcr(JCR *jcr)
189 free_pool_memory(jcr->job_name);
190 jcr->job_name = NULL;
192 if (jcr->client_name) {
193 free_pool_memory(jcr->client_name);
194 jcr->client_name = NULL;
196 if (jcr->fileset_name) {
197 free_pool_memory(jcr->fileset_name);
198 jcr->fileset_name = NULL;
200 if (jcr->fileset_md5) {
201 free_pool_memory(jcr->fileset_md5);
202 jcr->fileset_md5 = NULL;
216 * Search for device resource that corresponds to
217 * device name on command line (or default).
219 * Returns: NULL on failure
220 * Device resource pointer on success
222 static DEVRES *find_device_res(char *device_name, int read_access)
228 foreach_res(device, R_DEVICE) {
229 if (strcmp(device->device_name, device_name) == 0) {
235 /* Search for name of Device resource rather than archive name */
236 if (device_name[0] == '"') {
237 int len = strlen(device_name);
238 bstrncpy(device_name, device_name+1, len+1);
241 device_name[len-1] = 0; /* zap trailing " */
244 foreach_res(device, R_DEVICE) {
245 if (strcmp(device->hdr.name, device_name) == 0) {
253 Pmsg2(0, _("Could not find device \"%s\" in config file %s.\n"), device_name,
257 Pmsg2(0, _("Using device: \"%s\" for %s.\n"), device_name,
258 read_access?"reading":"writing");
264 * Device got an error, attempt to analyse it
266 void display_tape_error_status(JCR *jcr, DEVICE *dev)
270 status = status_dev(dev);
271 Dmsg1(20, "Device status: %x\n", status);
272 if (status & BMT_EOD)
273 Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Data\n"));
274 else if (status & BMT_EOT)
275 Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Tape\n"));
276 else if (status & BMT_EOF)
277 Jmsg(jcr, M_ERROR, 0, _("Unexpected End of File\n"));
278 else if (status & BMT_DR_OPEN)
279 Jmsg(jcr, M_ERROR, 0, _("Tape Door is Open\n"));
280 else if (!(status & BMT_ONLINE))
281 Jmsg(jcr, M_ERROR, 0, _("Unexpected Tape is Off-line\n"));