Welcome to mirror list, hosted at ThFree Co, Russian Federation.

butil.cc « stored « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: feb6215e66a2d770717a14fc373ec5a8b632254d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
   Copyright (C) 2011-2012 Planets Communications B.V.
   Copyright (C) 2013-2022 Bareos GmbH & Co. KG

   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version three of the GNU Affero General Public
   License as published by the Free Software Foundation and included
   in the file LICENSE.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/
// Kern Sibbald, MM
/**
 * @file
 * Utility routines for "tool" programs such as bscan, bls,
 * bextract, ...  Some routines also used by Bareos.
 *
 * Normally nothing in this file is called by the Storage
 * daemon because we interact more directly with the user
 * i.e. printf, ...
 */

#include "include/bareos.h"
#include "stored/stored.h"
#include "stored/stored_globals.h"
#include "stored/acquire.h"
#include "stored/autochanger.h"
#include "stored/device.h"
#include "stored/device_control_record.h"
#include "stored/bsr.h"
#include "stored/jcr_private.h"
#include "lib/parse_bsr.h"
#include "lib/parse_conf.h"
#include "include/jcr.h"

namespace storagedaemon {

/* Forward referenced functions */
static bool setup_to_access_device(DeviceControlRecord* dcr,
                                   JobControlRecord* jcr,
                                   char* dev_name,
                                   const std::string& VolumeName,
                                   bool readonly);
static DeviceResource* find_device_res(char* archive_device_string,
                                       bool readonly);
static void MyFreeJcr(JobControlRecord* jcr);


JobControlRecord* SetupDummyJcr(const char* name,
                                BootStrapRecord* bsr,
                                DirectorResource* director)
{
  JobControlRecord* jcr = new_jcr(MyFreeJcr);
  jcr->impl = new JobControlRecordPrivate;

  jcr->impl->read_session.bsr = bsr;
  jcr->impl->director = director;
  jcr->VolSessionId = 1;
  jcr->VolSessionTime = (uint32_t)time(NULL);
  jcr->impl->NumReadVolumes = 0;
  jcr->impl->NumWriteVolumes = 0;
  jcr->JobId = 0;
  jcr->setJobType(JT_CONSOLE);
  jcr->setJobLevel(L_FULL);
  jcr->JobStatus = JS_Terminated;
  jcr->where = strdup("");
  jcr->impl->job_name = GetPoolMemory(PM_FNAME);
  PmStrcpy(jcr->impl->job_name, "Dummy.Job.Name");
  jcr->client_name = GetPoolMemory(PM_FNAME);
  PmStrcpy(jcr->client_name, "Dummy.Client.Name");
  bstrncpy(jcr->Job, name, sizeof(jcr->Job));
  jcr->impl->fileset_name = GetPoolMemory(PM_FNAME);
  PmStrcpy(jcr->impl->fileset_name, "Dummy.fileset.name");
  jcr->impl->fileset_md5 = GetPoolMemory(PM_FNAME);
  PmStrcpy(jcr->impl->fileset_md5, "Dummy.fileset.md5");

  NewPlugins(jcr); /* instantiate plugins */

  return jcr;
}


/**
 * Setup a "daemon" JobControlRecord for the various standalone tools (e.g. bls,
 * bextract, bscan, ...)
 */
JobControlRecord* SetupJcr(const char* name,
                           char* dev_name,
                           BootStrapRecord* bsr,
                           DirectorResource* director,
                           DeviceControlRecord* dcr,
                           const std::string& VolumeName,
                           bool readonly)
{
  JobControlRecord* jcr = SetupDummyJcr(name, bsr, director);

  InitAutochangers();
  CreateVolumeLists();

  if (!setup_to_access_device(dcr, jcr, dev_name, VolumeName, readonly)) {
    return NULL;
  }

  if (!bsr && !VolumeName.empty()) {
    bstrncpy(dcr->VolumeName, VolumeName.c_str(), sizeof(dcr->VolumeName));
  }

  bstrncpy(dcr->pool_name, "Default", sizeof(dcr->pool_name));
  bstrncpy(dcr->pool_type, "Backup", sizeof(dcr->pool_type));

  return jcr;
}

/**
 * Setup device, jcr, and prepare to access device.
 *   If the caller wants read access, acquire the device, otherwise,
 *     the caller will do it.
 */
static bool setup_to_access_device(DeviceControlRecord* dcr,
                                   JobControlRecord* jcr,
                                   char* dev_name,
                                   const std::string& VolumeName,
                                   bool readonly)
{
  Device* dev;
  char* p;
  DeviceResource* device_resource;
  char VolName[MAX_NAME_LENGTH];

  InitReservationsLock();

  /*
   * If no volume name already given and no bsr, and it is a file,
   * try getting name from Filename
   */
  if (!VolumeName.empty()) {
    bstrncpy(VolName, VolumeName.c_str(), sizeof(VolName));
    if (VolumeName.size() >= MAX_NAME_LENGTH) {
      Jmsg0(jcr, M_ERROR, 0,
            _("Volume name or names is too long. Please use a .bsr file.\n"));
    }
  } else {
    VolName[0] = 0;
  }
  if (!jcr->impl->read_session.bsr && VolName[0] == 0) {
    if (!bstrncmp(dev_name, "/dev/", 5)) {
      /* Try stripping file part */
      p = dev_name + strlen(dev_name);

      while (p >= dev_name && !IsPathSeparator(*p)) p--;
      if (IsPathSeparator(*p)) {
        bstrncpy(VolName, p + 1, sizeof(VolName));
        *p = 0;
      }
    }
  }

  if ((device_resource = find_device_res(dev_name, readonly)) == NULL) {
    Jmsg2(jcr, M_FATAL, 0, _("Cannot find device \"%s\" in config file %s.\n"),
          dev_name, configfile);
    return false;
  }

  dev = FactoryCreateDevice(jcr, device_resource);
  if (!dev) {
    Jmsg1(jcr, M_FATAL, 0, _("Cannot init device %s\n"), dev_name);
    return false;
  }
  device_resource->dev = dev;
  jcr->impl->dcr = dcr;
  SetupNewDcrDevice(jcr, dcr, dev, NULL);
  if (!readonly) { dcr->SetWillWrite(); }

  if (VolName[0]) {
    bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
  }
  bstrncpy(dcr->dev_name, device_resource->archive_device_string,
           sizeof(dcr->dev_name));

  CreateRestoreVolumeList(jcr);

  if (readonly) { /* read only access? */
    Dmsg0(100, "Acquire device for read\n");
    if (!AcquireDeviceForRead(dcr)) { return false; }
    jcr->impl->read_dcr = dcr;
  } else {
    if (!FirstOpenDevice(dcr)) {
      Jmsg1(jcr, M_FATAL, 0, _("Cannot open %s\n"), dev->print_name());
      return false;
    }
  }

  return true;
}

/**
 * Called here when freeing JobControlRecord so that we can get rid
 *  of "daemon" specific memory allocated.
 */
static void MyFreeJcr(JobControlRecord* jcr)
{
  if (jcr->impl->job_name) {
    FreePoolMemory(jcr->impl->job_name);
    jcr->impl->job_name = NULL;
  }

  if (jcr->client_name) {
    FreePoolMemory(jcr->client_name);
    jcr->client_name = NULL;
  }

  if (jcr->impl->fileset_name) {
    FreePoolMemory(jcr->impl->fileset_name);
    jcr->impl->fileset_name = NULL;
  }

  if (jcr->impl->fileset_md5) {
    FreePoolMemory(jcr->impl->fileset_md5);
    jcr->impl->fileset_md5 = NULL;
  }

  if (jcr->comment) {
    FreePoolMemory(jcr->comment);
    jcr->comment = NULL;
  }

  if (jcr->impl->VolList) { FreeRestoreVolumeList(jcr); }

  if (jcr->impl->dcr) {
    FreeDeviceControlRecord(jcr->impl->dcr);
    jcr->impl->dcr = NULL;
  }

  if (jcr->impl) {
    delete jcr->impl;
    jcr->impl = nullptr;
  }

  return;
}

/**
 * Search for device resource that corresponds to
 * device name on command line (or default).
 *
 * Returns: NULL on failure
 *          Device resource pointer on success
 */
static DeviceResource* find_device_res(char* archive_device_string,
                                       bool readonly)
{
  bool found = false;
  DeviceResource* device_resource;

  Dmsg0(900, "Enter find_device_res\n");
  LockRes(my_config);
  foreach_res (device_resource, R_DEVICE) {
    Dmsg2(900, "Compare %s and %s\n", device_resource->archive_device_string,
          archive_device_string);
    if (bstrcmp(device_resource->archive_device_string,
                archive_device_string)) {
      found = true;
      break;
    }
  }

  if (!found) {
    /* Search for name of Device resource rather than archive name */
    if (archive_device_string[0] == '"') {
      int len = strlen(archive_device_string);
      bstrncpy(archive_device_string, archive_device_string + 1, len + 1);
      len--;
      if (len > 0) { archive_device_string[len - 1] = 0; /* zap trailing " */ }
    }
    foreach_res (device_resource, R_DEVICE) {
      Dmsg2(900, "Compare %s and %s\n", device_resource->resource_name_,
            archive_device_string);
      if (bstrcmp(device_resource->resource_name_, archive_device_string)) {
        found = true;
        break;
      }
    }
  }
  UnlockRes(my_config);

  if (!found) {
    Pmsg2(0, _("Could not find device \"%s\" in config file %s.\n"),
          archive_device_string, configfile);
    return NULL;
  }

  if (readonly) {
    Pmsg1(0, _("Using device: \"%s\" for reading.\n"), archive_device_string);
  } else {
    Pmsg1(0, _("Using device: \"%s\" for writing.\n"), archive_device_string);
  }

  return device_resource;
}

// Device got an error, attempt to analyse it
void DisplayTapeErrorStatus(JobControlRecord* jcr, Device* dev)
{
  char* status;

  status = dev->StatusDev();

  if (BitIsSet(BMT_EOD, status))
    Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Data\n"));
  else if (BitIsSet(BMT_EOT, status))
    Jmsg(jcr, M_ERROR, 0, _("Unexpected End of Tape\n"));
  else if (BitIsSet(BMT_EOF, status))
    Jmsg(jcr, M_ERROR, 0, _("Unexpected End of File\n"));
  else if (BitIsSet(BMT_DR_OPEN, status))
    Jmsg(jcr, M_ERROR, 0, _("Tape Door is Open\n"));
  else if (!BitIsSet(BMT_ONLINE, status))
    Jmsg(jcr, M_ERROR, 0, _("Unexpected Tape is Off-line\n"));

  free(status);
}

} /* namespace storagedaemon */