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

read.cc « stored « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de0688d7866ff9e2ba1c9c0eeba86f9d2f13f43d (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
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2000-2011 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, November MM
/**
 * @file
 * Read code for Storage daemon
 */

#include "include/bareos.h"
#include "stored/stored.h"
#include "stored/acquire.h"
#include "stored/bsr.h"
#include "stored/device_control_record.h"
#include "stored/stored_jcr_impl.h"
#include "stored/mount.h"
#include "stored/read_record.h"
#include "lib/bnet.h"
#include "lib/bsock.h"
#include "include/jcr.h"

namespace storagedaemon {

/* Forward referenced subroutines */
static bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec);

/* Responses sent to the File daemon */
static char OK_data[] = "3000 OK data\n";
static char FD_error[] = "3000 error\n";
static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";

/**
 * Read Data and send to File Daemon
 *
 * Returns: false on failure
 *          true  on success
 */
bool DoReadData(JobControlRecord* jcr)
{
  BareosSocket* fd = jcr->file_bsock;
  DeviceControlRecord* dcr = jcr->sd_impl->read_dcr;
  bool ok = true;

  Dmsg0(20, "Start read data.\n");

  if (!BnetSetBufferSize(fd, dcr->device_resource->max_network_buffer_size,
                         BNET_SETBUF_WRITE)) {
    return false;
  }

  if (jcr->sd_impl->NumReadVolumes == 0) {
    Jmsg(jcr, M_FATAL, 0, _("No Volume names found for restore.\n"));
    fd->fsend(FD_error);
    return false;
  }

  Dmsg2(200, "Found %d volumes names to restore. First=%s\n",
        jcr->sd_impl->NumReadVolumes, jcr->sd_impl->VolList->VolumeName);

  // Ready device for reading
  if (!AcquireDeviceForRead(dcr)) {
    fd->fsend(FD_error);
    return false;
  }

  // Let any SD plugin know now its time to setup the record translation infra.
  if (GeneratePluginEvent(jcr, bSdEventSetupRecordTranslation, dcr) != bRC_OK) {
    jcr->setJobStatusWithPriorityCheck(JS_ErrorTerminated);
    return false;
  }

  // Tell File daemon we will send data
  fd->fsend(OK_data);
  jcr->sendJobStatus(JS_Running);
  ok = ReadRecords(dcr, RecordCb, MountNextReadVolume);

  // Send end of data to FD
  fd->signal(BNET_EOD);

  if (!ReleaseDevice(jcr->sd_impl->read_dcr)) { ok = false; }

  Dmsg0(30, "Done reading.\n");
  return ok;
}

/**
 * Called here for each record from ReadRecords()
 *
 * Returns: true if OK
 *          false if error
 */
static bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec)
{
  JobControlRecord* jcr = dcr->jcr;
  BareosSocket* fd = jcr->file_bsock;
  bool ok = true;
  POOLMEM* save_msg;
  char ec1[50], ec2[50];

  if (rec->FileIndex < 0) { return true; }

  Dmsg5(400, "Send to FD: SessId=%u SessTim=%u FI=%s Strm=%s, len=%d\n",
        rec->VolSessionId, rec->VolSessionTime,
        FI_to_ascii(ec1, rec->FileIndex),
        stream_to_ascii(ec2, rec->Stream, rec->FileIndex), rec->data_len);

  // Send record header to File daemon
  if (!fd->fsend(rec_header, rec->VolSessionId, rec->VolSessionTime,
                 rec->FileIndex, rec->Stream, rec->data_len)) {
    Pmsg1(000, _(">filed: Error Hdr=%s"), fd->msg);
    Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
          fd->bstrerror());
    return false;
  } else {
    Dmsg1(400, ">filed: Hdr=%s\n", fd->msg);
  }

  // Send data record to File daemon
  save_msg = fd->msg;  /* save fd message pointer */
  fd->msg = rec->data; /* pass data directly to the FD */
  fd->message_length = rec->data_len;

  Dmsg1(400, ">filed: send %d bytes data.\n", fd->message_length);
  if (!fd->send()) {
    Pmsg1(000, _("Error sending to FD. ERR=%s\n"), fd->bstrerror());
    Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
          fd->bstrerror());

    ok = false;
  }
  fd->msg = save_msg; /* restore fd message pointer */

  return ok;
}

} /* namespace storagedaemon */