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-2019 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) { return 1; }
127   if (del->num_ids == del->max_ids) {
128     del->max_ids = (del->max_ids * 3) / 2;
129     del->JobId = (JobId_t*)realloc(del->JobId, sizeof(JobId_t) * del->max_ids);
130   }
131   del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]);
132   return 0;
133 }
134 
135 
136 /**
137  * This routine will purge (delete) all records
138  * associated with a particular Volume. It will
139  * not delete the media record itself.
140  * TODO: This function is broken and it doesn't purge
141  *       File, BaseFiles, Log, ...
142  *       We call it from relabel and delete volume=, both ensure
143  *       that the volume is properly purged.
144  */
DoMediaPurge(BareosDb * mdb,MediaDbRecord * mr)145 static int DoMediaPurge(BareosDb* mdb, MediaDbRecord* mr)
146 {
147   int i;
148   char ed1[50];
149   struct s_del_ctx del;
150   PoolMem query(PM_MESSAGE);
151 
152   del.num_ids = 0;
153   del.tot_ids = 0;
154   del.num_del = 0;
155   del.max_ids = 0;
156 
157   Mmsg(query, "SELECT JobId from JobMedia WHERE MediaId=%d", mr->MediaId);
158 
159   del.max_ids = mr->VolJobs;
160   if (del.max_ids < 100) {
161     del.max_ids = 100;
162   } else if (del.max_ids > MAX_DEL_LIST_LEN) {
163     del.max_ids = MAX_DEL_LIST_LEN;
164   }
165   del.JobId = (JobId_t*)malloc(sizeof(JobId_t) * del.max_ids);
166 
167   mdb->SqlQuery(query.c_str(), DeleteHandler, (void*)&del);
168 
169   for (i = 0; i < del.num_ids; i++) {
170     Dmsg1(400, "Delete JobId=%d\n", del.JobId[i]);
171     Mmsg(query, "DELETE FROM Job WHERE JobId=%s",
172          edit_int64(del.JobId[i], ed1));
173     mdb->SqlQuery(query.c_str());
174 
175     Mmsg(query, "DELETE FROM File WHERE JobId=%s",
176          edit_int64(del.JobId[i], ed1));
177     mdb->SqlQuery(query.c_str());
178 
179     Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%s",
180          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)) { goto bail_out; }
200   /* Do purge if not already purged */
201   if (!bstrcmp(mr->VolStatus, "Purged")) {
202     /* Delete associated records */
203     DoMediaPurge(this, mr);
204   }
205 
206   Mmsg(cmd, "DELETE FROM Media WHERE MediaId=%d", mr->MediaId);
207   SqlQuery(cmd);
208   retval = true;
209 
210 bail_out:
211   DbUnlock(this);
212   return retval;
213 }
214 
215 /**
216  * Purge all records associated with a
217  * media record. This does not delete the
218  * media record itself. But the media status
219  * is changed to "Purged".
220  */
PurgeMediaRecord(JobControlRecord * jcr,MediaDbRecord * mr)221 bool BareosDb::PurgeMediaRecord(JobControlRecord* jcr, MediaDbRecord* mr)
222 {
223   bool retval = false;
224 
225   DbLock(this);
226   if (mr->MediaId == 0 && !GetMediaRecord(jcr, mr)) { goto bail_out; }
227 
228   /*
229    * Delete associated records
230    */
231   DoMediaPurge(this, mr); /* Note, always purge */
232 
233   /*
234    * Mark Volume as purged
235    */
236   strcpy(mr->VolStatus, "Purged");
237   if (!UpdateMediaRecord(jcr, mr)) { goto bail_out; }
238 
239   retval = true;
240 
241 bail_out:
242   DbUnlock(this);
243   return retval;
244 }
245 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES */
246