1 /*
2 BAREOS® - Backup Archiving REcovery Open Sourced
3
4 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
5 Copyright (C) 2011-2016 Planets Communications B.V.
6 Copyright (C) 2013-2016 Bareos GmbH & Co. KG
7
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
11 in the file LICENSE.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
22 */
23 /*
24 * Kern Sibbald, December 2000
25 */
26 /**
27 * @file
28 * BAREOS Catalog Database Delete record interface routines
29 */
30
31 #include "include/bareos.h"
32
33 #if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI
34
35 #include "cats.h"
36 #include "lib/edit.h"
37
38 /* -----------------------------------------------------------------------
39 *
40 * Generic Routines (or almost generic)
41 *
42 * -----------------------------------------------------------------------
43 */
44
45 /**
46 * Delete Pool record, must also delete all associated
47 * Media records.
48 *
49 * Returns: false on error
50 * true on success
51 * PoolId = number of Pools deleted (should be 1)
52 * NumVols = number of Media records deleted
53 */
DeletePoolRecord(JobControlRecord * jcr,PoolDbRecord * pr)54 bool BareosDb::DeletePoolRecord(JobControlRecord *jcr, PoolDbRecord *pr)
55 {
56 bool retval = false;
57 SQL_ROW row;
58 int num_rows;
59 char esc[MAX_ESCAPE_NAME_LENGTH];
60
61 DbLock(this);
62 EscapeString(jcr, esc, pr->Name, strlen(pr->Name));
63 Mmsg(cmd, "SELECT PoolId FROM Pool WHERE Name='%s'", esc);
64 Dmsg1(10, "selectpool: %s\n", cmd);
65
66 pr->PoolId = pr->NumVols = 0;
67
68 if (QUERY_DB(jcr, cmd)) {
69 num_rows = SqlNumRows();
70 if (num_rows == 0) {
71 Mmsg(errmsg, _("No pool record %s exists\n"), pr->Name);
72 SqlFreeResult();
73 goto bail_out;
74 } else if (num_rows != 1) {
75 Mmsg(errmsg, _("Expecting one pool record, got %d\n"), num_rows);
76 SqlFreeResult();
77 goto bail_out;
78 }
79 if ((row = SqlFetchRow()) == NULL) {
80 Mmsg1(errmsg, _("Error fetching row %s\n"), sql_strerror());
81 goto bail_out;
82 }
83 pr->PoolId = str_to_int64(row[0]);
84 SqlFreeResult();
85 }
86
87 /* Delete Media owned by this pool */
88 Mmsg(cmd, "DELETE FROM Media WHERE Media.PoolId = %d", pr->PoolId);
89
90 pr->NumVols = DELETE_DB(jcr, cmd);
91 Dmsg1(200, "Deleted %d Media records\n", pr->NumVols);
92
93 /* Delete Pool */
94 Mmsg(cmd, "DELETE FROM Pool WHERE Pool.PoolId = %d", pr->PoolId);
95 pr->PoolId = DELETE_DB(jcr, cmd);
96 Dmsg1(200, "Deleted %d Pool records\n", pr->PoolId);
97
98 retval = true;
99
100 bail_out:
101 DbUnlock(this);
102 return retval;
103 }
104
105 #define MAX_DEL_LIST_LEN 1000000
106
107 struct s_del_ctx {
108 JobId_t *JobId;
109 int num_ids; /* ids stored */
110 int max_ids; /* size of array */
111 int num_del; /* number deleted */
112 int tot_ids; /* total to process */
113 };
114
115 /**
116 * Called here to make in memory list of JobIds to be
117 * deleted. The in memory list will then be transversed
118 * to issue the SQL DELETE commands. Note, the list
119 * is allowed to get to MAX_DEL_LIST_LEN to limit the
120 * maximum malloc'ed memory.
121 */
DeleteHandler(void * ctx,int num_fields,char ** row)122 static int DeleteHandler(void *ctx, int num_fields, char **row)
123 {
124 struct s_del_ctx *del = (struct s_del_ctx *)ctx;
125
126 if (del->num_ids == MAX_DEL_LIST_LEN) {
127 return 1;
128 }
129 if (del->num_ids == del->max_ids) {
130 del->max_ids = (del->max_ids * 3) / 2;
131 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) *
132 del->max_ids);
133 }
134 del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]);
135 return 0;
136 }
137
138
139 /**
140 * This routine will purge (delete) all records
141 * associated with a particular Volume. It will
142 * not delete the media record itself.
143 * TODO: This function is broken and it doesn't purge
144 * File, BaseFiles, Log, ...
145 * We call it from relabel and delete volume=, both ensure
146 * that the volume is properly purged.
147 */
DoMediaPurge(BareosDb * mdb,MediaDbRecord * mr)148 static int DoMediaPurge(BareosDb *mdb, MediaDbRecord *mr)
149 {
150 int i;
151 char ed1[50];
152 struct s_del_ctx del;
153 PoolMem query(PM_MESSAGE);
154
155 del.num_ids = 0;
156 del.tot_ids = 0;
157 del.num_del = 0;
158 del.max_ids = 0;
159
160 Mmsg(query, "SELECT JobId from JobMedia WHERE MediaId=%d", mr->MediaId);
161
162 del.max_ids = mr->VolJobs;
163 if (del.max_ids < 100) {
164 del.max_ids = 100;
165 } else if (del.max_ids > MAX_DEL_LIST_LEN) {
166 del.max_ids = MAX_DEL_LIST_LEN;
167 }
168 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
169
170 mdb->SqlQuery(query.c_str(), DeleteHandler, (void *)&del);
171
172 for (i = 0; i < del.num_ids; i++) {
173 Dmsg1(400, "Delete JobId=%d\n", del.JobId[i]);
174 Mmsg(query, "DELETE FROM Job WHERE JobId=%s", edit_int64(del.JobId[i], ed1));
175 mdb->SqlQuery(query.c_str());
176
177 Mmsg(query, "DELETE FROM File WHERE JobId=%s", edit_int64(del.JobId[i], ed1));
178 mdb->SqlQuery(query.c_str());
179
180 Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%s", edit_int64(del.JobId[i], ed1));
181 mdb->SqlQuery(query.c_str());
182 }
183
184 free(del.JobId);
185
186 return 1;
187 }
188
189 /**
190 * Delete Media record and all records that are associated with it.
191 * Returns: false on error
192 * true on success
193 */
DeleteMediaRecord(JobControlRecord * jcr,MediaDbRecord * mr)194 bool BareosDb::DeleteMediaRecord(JobControlRecord *jcr, MediaDbRecord *mr)
195 {
196 bool retval = false;
197
198 DbLock(this);
199 if (mr->MediaId == 0 && !GetMediaRecord(jcr, mr)) {
200 goto bail_out;
201 }
202 /* Do purge if not already purged */
203 if (!bstrcmp(mr->VolStatus, "Purged")) {
204 /* Delete associated records */
205 DoMediaPurge(this, mr);
206 }
207
208 Mmsg(cmd, "DELETE FROM Media WHERE MediaId=%d", mr->MediaId);
209 SqlQuery(cmd);
210 retval = true;
211
212 bail_out:
213 DbUnlock(this);
214 return retval;
215 }
216
217 /**
218 * Purge all records associated with a
219 * media record. This does not delete the
220 * media record itself. But the media status
221 * is changed to "Purged".
222 */
PurgeMediaRecord(JobControlRecord * jcr,MediaDbRecord * mr)223 bool BareosDb::PurgeMediaRecord(JobControlRecord *jcr, MediaDbRecord *mr)
224 {
225 bool retval = false;
226
227 DbLock(this);
228 if (mr->MediaId == 0 && !GetMediaRecord(jcr, mr)) {
229 goto bail_out;
230 }
231
232 /*
233 * Delete associated records
234 */
235 DoMediaPurge(this, mr); /* Note, always purge */
236
237 /*
238 * Mark Volume as purged
239 */
240 strcpy(mr->VolStatus, "Purged");
241 if (!UpdateMediaRecord(jcr, mr)) {
242 goto bail_out;
243 }
244
245 retval = true;
246
247 bail_out:
248 DbUnlock(this);
249 return retval;
250 }
251 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES */
252