1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2002-2010 Free Software Foundation Europe e.V.
5    Copyright (C) 2013-2018 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Kern Sibbald, July 2002
24  */
25 /**
26  * @file
27  * Bootstrap Record header file
28  *
29  * BootStrapRecord (bootstrap record) handling routines split from ua_restore.c
30  * July 2003 Bootstrap send handling routines split from restore.c July 2012
31  */
32 
33 #ifndef BAREOS_DIRD_BSR_H_
34 #define BAREOS_DIRD_BSR_H_
35 
36 #include "dird/ua.h"
37 
38 struct VolumeParameters;
39 
40 namespace directordaemon {
41 
42 /**
43  * FileIndex entry in restore bootstrap record
44  */
45 class RestoreBootstrapRecordFileIndex {
46  private:
47   std::vector<int32_t> fileIds_;
48   bool allFiles_ = false;
49   bool sorted_ = false;
50   void Sort();
51 
52  public:
53   void Add(int32_t findex);
54   void AddAll();
55   std::vector<std::pair<int32_t, int32_t>> GetRanges();
Empty()56   bool Empty() const { return !allFiles_ && fileIds_.empty(); }
57 };
58 
59 /**
60  * Restore bootstrap record -- not the real one, but useful here
61  *  The restore bsr is a chain of BootStrapRecord records (linked by next).
62  *  Each BootStrapRecord represents a single JobId, and within it, it
63  *    contains a linked list of file indexes for that JobId.
64  *    The AddVolumeInformationToBsr() routine, will then add all the volumes
65  *    on which the Job is stored to the BootStrapRecord.
66  */
67 struct RestoreBootstrapRecord {
68   std::unique_ptr<RestoreBootstrapRecord> next; /**< next JobId */
69   JobId_t JobId = 0;                            /**< JobId this bsr */
70   uint32_t VolSessionId = 0;
71   uint32_t VolSessionTime = 0;
72   int VolCount = 0;                      /**< Volume parameter count */
73   VolumeParameters* VolParams = nullptr; /**< Volume, start/end file/blocks */
74   std::unique_ptr<RestoreBootstrapRecordFileIndex>
75       fi;                    /**< File indexes this JobId */
76   char* fileregex = nullptr; /**< Only restore files matching regex */
77 
78   RestoreBootstrapRecord();
79   RestoreBootstrapRecord(JobId_t t_JobId);
80   virtual ~RestoreBootstrapRecord();
81   RestoreBootstrapRecord(const RestoreBootstrapRecord&) = delete;
82   RestoreBootstrapRecord& operator=(const RestoreBootstrapRecord&) = delete;
83   RestoreBootstrapRecord(RestoreBootstrapRecord&&) = delete;
84   RestoreBootstrapRecord& operator=(RestoreBootstrapRecord&&) = delete;
85 };
86 
87 class UaContext;
88 
89 /**
90  * Open bootstrap file.
91  */
92 struct bootstrap_info {
93   FILE* bs;
94   UaContext* ua;
95   char storage[MAX_NAME_LENGTH + 1];
96 };
97 
98 bool AddVolumeInformationToBsr(UaContext* ua, RestoreBootstrapRecord* bsr);
99 uint32_t WriteBsrFile(UaContext* ua, RestoreContext& rx);
100 void DisplayBsrInfo(UaContext* ua, RestoreContext& rx);
101 uint32_t WriteBsr(UaContext* ua, RestoreContext& rx, std::string& buffer);
102 void AddFindex(RestoreBootstrapRecord* bsr, uint32_t JobId, int32_t findex);
103 void AddFindexAll(RestoreBootstrapRecord* bsr, uint32_t JobId);
104 RestoreBootstrapRecordFileIndex* new_findex();
105 void MakeUniqueRestoreFilename(UaContext* ua, POOLMEM*& fname);
106 void PrintBsr(UaContext* ua, RestoreContext& rx);
107 bool OpenBootstrapFile(JobControlRecord* jcr, bootstrap_info& info);
108 bool SendBootstrapFile(JobControlRecord* jcr,
109                        BareosSocket* sock,
110                        bootstrap_info& info);
111 void CloseBootstrapFile(bootstrap_info& info);
112 uint32_t write_findex(RestoreBootstrapRecordFileIndex* fi,
113                       int32_t FirstIndex,
114                       int32_t LastIndex,
115                       std::string& buffer);
116 
117 } /* namespace directordaemon */
118 #endif  // BAREOS_DIRD_BSR_H_
119