-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathsql_get.cc
1908 lines (1729 loc) · 57.8 KB
/
sql_get.cc
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2020 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, March 2000
/**
* @file
* BAREOS Catalog Database Get record interface routines
*
* Note, these routines generally get a record by id or
* by name. If more logic is involved, the routine
* should be in find.c
*/
#include "include/bareos.h"
#if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI
# include "cats.h"
# include "sql.h"
# include "lib/edit.h"
# include "lib/volume_session_info.h"
# include "include/make_unique.h"
/* -----------------------------------------------------------------------
*
* Generic Routines (or almost generic)
*
* -----------------------------------------------------------------------
*/
/* Forward referenced functions */
/**
* Given a full filename (with path), look up the File record
* (with attributes) in the database.
*
* Returns: 0 on failure
* 1 on success with the File record in FileDbRecord
*/
bool BareosDb::GetFileAttributesRecord(JobControlRecord* jcr,
char* filename,
JobDbRecord* jr,
FileDbRecord* fdbr)
{
bool retval;
Dmsg1(100, "db_get_file_attributes_record filename=%s \n", filename);
DbLock(this);
SplitPathAndFile(jcr, filename);
fdbr->PathId = GetPathRecord(jcr);
retval = GetFileRecord(jcr, jr, fdbr);
DbUnlock(this);
return retval;
}
/**
* Get a File record
* Returns: false on failure
* true on success
*
* DO NOT use Jmsg in this routine.
*
* Note in this routine, we do not use Jmsg because it may be
* called to get attributes of a non-existent file, which is
* "normal" if a new file is found during Verify.
*
* The following is a bit of a kludge: because we always backup a
* directory entry, we can end up with two copies of the directory
* in the backup. One is when we encounter the directory and find
* we cannot recurse into it, and the other is when we find an
* explicit mention of the directory. This can also happen if the
* use includes the directory twice. In this case, Verify
* VolumeToCatalog fails because we have two copies in the catalog,
* and only the first one is marked (twice). So, when calling from Verify,
* VolumeToCatalog jr is not NULL and we know jr->FileIndex is the fileindex
* of the version of the directory/file we actually want and do
* a more explicit SQL search.
*/
bool BareosDb::GetFileRecord(JobControlRecord* jcr,
JobDbRecord* jr,
FileDbRecord* fdbr)
{
bool retval = false;
SQL_ROW row;
char ed1[50], ed2[50], ed3[50];
int num_rows;
esc_name = CheckPoolMemorySize(esc_name, 2 * fnl + 2);
EscapeString(jcr, esc_name, fname, fnl);
if (jcr->getJobLevel() == L_VERIFY_DISK_TO_CATALOG) {
Mmsg(cmd,
"SELECT FileId, LStat, MD5, Fhinfo, Fhnode FROM File,Job WHERE "
"File.JobId=Job.JobId AND File.PathId=%s AND "
"File.Name='%s' AND Job.Type='B' AND Job.JobStatus IN ('T','W') AND "
"ClientId=%s ORDER BY StartTime DESC LIMIT 1",
edit_int64(fdbr->PathId, ed1), esc_name,
edit_int64(jr->ClientId, ed3));
} else if (jcr->getJobLevel() == L_VERIFY_VOLUME_TO_CATALOG) {
Mmsg(cmd,
"SELECT FileId, LStat, MD5, Fhinfo, Fhnode FROM File WHERE "
"File.JobId=%s AND File.PathId=%s AND "
"File.Name='%s' AND File.FileIndex=%u",
edit_int64(fdbr->JobId, ed1), edit_int64(fdbr->PathId, ed2), esc_name,
jr->FileIndex);
} else {
Mmsg(cmd,
"SELECT FileId, LStat, MD5, Fhinfo, Fhnode FROM File WHERE "
"File.JobId=%s AND File.PathId=%s AND "
"File.Name='%s'",
edit_int64(fdbr->JobId, ed1), edit_int64(fdbr->PathId, ed2), esc_name);
}
Dmsg3(450, "Get_file_record JobId=%u Filename=%s PathId=%u\n", fdbr->JobId,
esc_name, fdbr->PathId);
Dmsg1(100, "Query=%s\n", cmd);
if (QUERY_DB(jcr, cmd)) {
num_rows = SqlNumRows();
Dmsg1(050, "GetFileRecord num_rows=%d\n", num_rows);
if (num_rows >= 1) {
if ((row = SqlFetchRow()) == NULL) {
Mmsg1(errmsg, _("Error fetching row: %s\n"), sql_strerror());
} else {
fdbr->FileId = (FileId_t)str_to_int64(row[0]);
bstrncpy(fdbr->LStat, row[1], sizeof(fdbr->LStat));
bstrncpy(fdbr->Digest, row[2], sizeof(fdbr->Digest));
retval = true;
if (num_rows > 1) {
Mmsg3(errmsg,
_("GetFileRecord want 1 got rows=%d PathId=%s Filename=%s\n"),
num_rows, edit_int64(fdbr->PathId, ed1), esc_name);
Dmsg1(000, "=== Problem! %s", errmsg);
}
}
} else {
Mmsg2(errmsg, _("File record for PathId=%s Filename=%s not found.\n"),
edit_int64(fdbr->PathId, ed1), esc_name);
}
SqlFreeResult();
} else {
Mmsg(errmsg, _("File record not found in Catalog.\n"));
}
return retval;
}
/**
* Get path record
* Returns: 0 on failure
* PathId on success
*
* DO NOT use Jmsg in this routine (see notes for GetFileRecord)
*/
int BareosDb::GetPathRecord(JobControlRecord* jcr)
{
SQL_ROW row;
DBId_t PathId = 0;
int num_rows;
esc_name = CheckPoolMemorySize(esc_name, 2 * pnl + 2);
EscapeString(jcr, esc_name, path, pnl);
if (cached_path_id != 0 && cached_path_len == pnl
&& bstrcmp(cached_path, path)) {
return cached_path_id;
}
Mmsg(cmd, "SELECT PathId FROM Path WHERE Path='%s'", esc_name);
if (QUERY_DB(jcr, cmd)) {
char ed1[30];
num_rows = SqlNumRows();
if (num_rows > 1) {
Mmsg2(errmsg, _("More than one Path!: %s for path: %s\n"),
edit_uint64(num_rows, ed1), path);
Jmsg(jcr, M_WARNING, 0, "%s", errmsg);
}
/* Even if there are multiple paths, take the first one */
if (num_rows >= 1) {
if ((row = SqlFetchRow()) == NULL) {
Mmsg1(errmsg, _("error fetching row: %s\n"), sql_strerror());
} else {
PathId = str_to_int64(row[0]);
if (PathId <= 0) {
Mmsg2(errmsg, _("Get DB path record %s found bad record: %s\n"), cmd,
edit_int64(PathId, ed1));
PathId = 0;
} else {
if (PathId != cached_path_id) {
cached_path_id = PathId;
cached_path_len = pnl;
PmStrcpy(cached_path, path);
}
}
}
} else {
Mmsg1(errmsg, _("Path record: %s not found.\n"), path);
}
SqlFreeResult();
} else {
Mmsg(errmsg, _("Path record: %s not found in Catalog.\n"), path);
}
return PathId;
}
int BareosDb::GetPathRecord(JobControlRecord* jcr, const char* new_path)
{
PmStrcpy(path, new_path);
pnl = strlen(path);
return GetPathRecord(jcr);
}
/**
* Get Job record for given JobId or Job name
* Returns: false on failure
* true on success
*/
bool BareosDb::GetJobRecord(JobControlRecord* jcr, JobDbRecord* jr)
{
bool retval = false;
SQL_ROW row;
char ed1[50];
char esc[MAX_ESCAPE_NAME_LENGTH];
DbLock(this);
if (jr->JobId == 0) {
EscapeString(jcr, esc, jr->Job, strlen(jr->Job));
Mmsg(cmd,
"SELECT VolSessionId,VolSessionTime,"
"PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus,"
"Type,Level,ClientId,Name,PriorJobId,RealEndTime,JobId,FileSetId,"
"SchedTime,RealEndTime,ReadBytes,HasBase,PurgedFiles "
"FROM Job WHERE Job='%s'",
esc);
} else {
Mmsg(cmd,
"SELECT VolSessionId,VolSessionTime,"
"PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus,"
"Type,Level,ClientId,Name,PriorJobId,RealEndTime,JobId,FileSetId,"
"SchedTime,RealEndTime,ReadBytes,HasBase,PurgedFiles "
"FROM Job WHERE JobId=%s",
edit_int64(jr->JobId, ed1));
}
if (!QUERY_DB(jcr, cmd)) { goto bail_out; }
if ((row = SqlFetchRow()) == NULL) {
Mmsg1(errmsg, _("No Job found for JobId %s\n"), edit_int64(jr->JobId, ed1));
SqlFreeResult();
goto bail_out;
}
jr->VolSessionId = str_to_uint64(row[0]);
jr->VolSessionTime = str_to_uint64(row[1]);
jr->PoolId = str_to_int64(row[2]);
bstrncpy(jr->cStartTime, (row[3] != NULL) ? row[3] : "",
sizeof(jr->cStartTime));
bstrncpy(jr->cEndTime, (row[4] != NULL) ? row[4] : "", sizeof(jr->cEndTime));
jr->JobFiles = str_to_int64(row[5]);
jr->JobBytes = str_to_int64(row[6]);
jr->JobTDate = str_to_int64(row[7]);
bstrncpy(jr->Job, (row[8] != NULL) ? row[8] : "", sizeof(jr->Job));
jr->JobStatus = (row[9] != NULL) ? (int)*row[9] : JS_FatalError;
jr->JobType = (row[10] != NULL) ? (int)*row[10] : JT_BACKUP;
jr->JobLevel = (row[11] != NULL) ? (int)*row[11] : L_NONE;
jr->ClientId = str_to_uint64((row[12] != NULL) ? row[12] : (char*)"");
bstrncpy(jr->Name, (row[13] != NULL) ? row[13] : "", sizeof(jr->Name));
jr->PriorJobId = str_to_uint64((row[14] != NULL) ? row[14] : (char*)"");
bstrncpy(jr->cRealEndTime, (row[15] != NULL) ? row[15] : "",
sizeof(jr->cRealEndTime));
if (jr->JobId == 0) { jr->JobId = str_to_int64(row[16]); }
jr->FileSetId = str_to_int64(row[17]);
bstrncpy(jr->cSchedTime, (row[18] != NULL) ? row[18] : "",
sizeof(jr->cSchedTime));
bstrncpy(jr->cRealEndTime, (row[19] != NULL) ? row[19] : "",
sizeof(jr->cRealEndTime));
jr->ReadBytes = str_to_int64(row[20]);
jr->StartTime = StrToUtime(jr->cStartTime);
jr->SchedTime = StrToUtime(jr->cSchedTime);
jr->EndTime = StrToUtime(jr->cEndTime);
jr->RealEndTime = StrToUtime(jr->cRealEndTime);
jr->HasBase = str_to_int64(row[21]);
jr->PurgedFiles = str_to_int64(row[22]);
SqlFreeResult();
retval = true;
bail_out:
DbUnlock(this);
return retval;
}
/**
* Find VolumeNames for a given JobId
* Returns: 0 on error or no Volumes found
* number of volumes on success
* Volumes are concatenated in VolumeNames
* separated by a vertical bar (|) in the order
* that they were written.
*
* Returns: number of volumes on success
*/
int BareosDb::GetJobVolumeNames(JobControlRecord* jcr,
JobId_t JobId,
POOLMEM*& VolumeNames)
{
SQL_ROW row;
char ed1[50];
int retval = 0;
int i;
int num_rows;
DbLock(this);
// Get one entry per VolumeName, but "sort" by VolIndex
Mmsg(cmd,
"SELECT VolumeName,MAX(VolIndex) FROM JobMedia,Media WHERE "
"JobMedia.JobId=%s AND JobMedia.MediaId=Media.MediaId "
"GROUP BY VolumeName "
"ORDER BY 2 ASC",
edit_int64(JobId, ed1));
Dmsg1(130, "VolNam=%s\n", cmd);
VolumeNames[0] = '\0';
if (QUERY_DB(jcr, cmd)) {
num_rows = SqlNumRows();
Dmsg1(130, "Num rows=%d\n", num_rows);
if (num_rows <= 0) {
Mmsg1(errmsg, _("No volumes found for JobId=%d\n"), JobId);
retval = 0;
} else {
retval = num_rows;
for (i = 0; i < retval; i++) {
if ((row = SqlFetchRow()) == NULL) {
Mmsg2(errmsg, _("Error fetching row %d: ERR=%s\n"), i,
sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
retval = 0;
break;
} else {
if (VolumeNames[0] != '\0') { PmStrcat(VolumeNames, "|"); }
PmStrcat(VolumeNames, row[0]);
}
}
}
SqlFreeResult();
} else {
Mmsg(errmsg, _("No Volume for JobId %d found in Catalog.\n"), JobId);
}
DbUnlock(this);
return retval;
}
/**
* Find Volume parameters for a given JobId
* Returns: 0 on error or no Volumes found
* number of volumes on success
* List of Volumes and start/end file/blocks (malloced structure!)
*
* Returns: number of volumes on success
*/
int BareosDb::GetJobVolumeParameters(JobControlRecord* jcr,
JobId_t JobId,
VolumeParameters** VolParams)
{
SQL_ROW row;
char ed1[50];
int retval = 0;
int i;
VolumeParameters* Vols = NULL;
int num_rows;
DbLock(this);
Mmsg(cmd,
"SELECT VolumeName,MediaType,FirstIndex,LastIndex,StartFile,"
"JobMedia.EndFile,StartBlock,JobMedia.EndBlock,"
"Slot,StorageId,InChanger,"
"JobBytes"
" FROM JobMedia,Media WHERE JobMedia.JobId=%s"
" AND JobMedia.MediaId=Media.MediaId ORDER BY VolIndex,JobMediaId",
edit_int64(JobId, ed1));
Dmsg1(130, "VolNam=%s\n", cmd);
if (QUERY_DB(jcr, cmd)) {
num_rows = SqlNumRows();
Dmsg1(200, "Num rows=%d\n", num_rows);
if (num_rows <= 0) {
Mmsg1(errmsg, _("No volumes found for JobId=%d\n"), JobId);
retval = 0;
} else {
retval = num_rows;
DBId_t* SId = NULL;
if (retval > 0) {
*VolParams = Vols
= (VolumeParameters*)malloc(retval * sizeof(VolumeParameters));
SId = (DBId_t*)malloc(retval * sizeof(DBId_t));
}
for (i = 0; i < retval; i++) {
if ((row = SqlFetchRow()) == NULL) {
Mmsg2(errmsg, _("Error fetching row %d: ERR=%s\n"), i,
sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
retval = 0;
break;
} else {
DBId_t StorageId;
uint32_t StartBlock, EndBlock, StartFile, EndFile;
bstrncpy(Vols[i].VolumeName, row[0], MAX_NAME_LENGTH);
bstrncpy(Vols[i].MediaType, row[1], MAX_NAME_LENGTH);
Vols[i].FirstIndex = str_to_uint64(row[2]);
Vols[i].LastIndex = str_to_uint64(row[3]);
StartFile = str_to_uint64(row[4]);
EndFile = str_to_uint64(row[5]);
StartBlock = str_to_uint64(row[6]);
EndBlock = str_to_uint64(row[7]);
Vols[i].Slot = str_to_uint64(row[8]);
StorageId = str_to_uint64(row[9]);
Vols[i].InChanger = str_to_uint64(row[10]);
Vols[i].JobBytes = str_to_uint64(row[11]);
Vols[i].StartAddr = (((uint64_t)StartFile) << 32) | StartBlock;
Vols[i].EndAddr = (((uint64_t)EndFile) << 32) | EndBlock;
Vols[i].Storage[0] = 0;
SId[i] = StorageId;
}
}
for (i = 0; i < retval; i++) {
if (SId[i] != 0) {
Mmsg(cmd, "SELECT Name from Storage WHERE StorageId=%s",
edit_int64(SId[i], ed1));
if (QUERY_DB(jcr, cmd)) {
if ((row = SqlFetchRow()) && row[0]) {
bstrncpy(Vols[i].Storage, row[0], MAX_NAME_LENGTH);
}
}
}
}
if (SId) { free(SId); }
}
SqlFreeResult();
}
DbUnlock(this);
return retval;
}
/**
* Get the number of pool records
*
* Returns: -1 on failure
* number on success
*/
int BareosDb::GetNumPoolRecords(JobControlRecord* jcr)
{
int retval = 0;
DbLock(this);
Mmsg(cmd, "SELECT count(*) from Pool");
retval = GetSqlRecordMax(jcr);
DbUnlock(this);
return retval;
}
/**
* This function returns a list of all the Pool record ids.
* The caller must free ids if non-NULL.
*
* Returns 0: on failure
* 1: on success
*/
int BareosDb::GetPoolIds(JobControlRecord* jcr, int* num_ids, DBId_t** ids)
{
SQL_ROW row;
int retval = 0;
int i = 0;
DBId_t* id;
DbLock(this);
*ids = NULL;
Mmsg(cmd, "SELECT PoolId FROM Pool");
if (QUERY_DB(jcr, cmd)) {
*num_ids = SqlNumRows();
if (*num_ids > 0) {
id = (DBId_t*)malloc(*num_ids * sizeof(DBId_t));
while ((row = SqlFetchRow()) != NULL) { id[i++] = str_to_uint64(row[0]); }
*ids = id;
}
SqlFreeResult();
retval = 1;
} else {
Mmsg(errmsg, _("Pool id select failed: ERR=%s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
retval = 0;
}
DbUnlock(this);
return retval;
}
/**
* This function returns a list of all the Storage record ids.
* The caller must free ids if non-NULL.
*
* Returns 0: on failure
* 1: on success
*/
int BareosDb::GetStorageIds(JobControlRecord* jcr, int* num_ids, DBId_t* ids[])
{
SQL_ROW row;
int retval = 0;
int i = 0;
DBId_t* id;
DbLock(this);
*ids = NULL;
Mmsg(cmd, "SELECT StorageId FROM Storage");
if (QUERY_DB(jcr, cmd)) {
*num_ids = SqlNumRows();
if (*num_ids > 0) {
id = (DBId_t*)malloc(*num_ids * sizeof(DBId_t));
while ((row = SqlFetchRow()) != NULL) { id[i++] = str_to_uint64(row[0]); }
*ids = id;
}
SqlFreeResult();
retval = 1;
} else {
Mmsg(errmsg, _("Storage id select failed: ERR=%s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
retval = 0;
}
DbUnlock(this);
return retval;
}
/**
* This function returns a list of all the Client record ids.
* The caller must free ids if non-NULL.
*
* Returns false: on failure
* true: on success
*/
bool BareosDb::GetClientIds(JobControlRecord* jcr, int* num_ids, DBId_t* ids[])
{
bool retval = false;
SQL_ROW row;
int i = 0;
DBId_t* id;
DbLock(this);
*ids = NULL;
Mmsg(cmd, "SELECT ClientId FROM Client ORDER BY Name");
if (QUERY_DB(jcr, cmd)) {
*num_ids = SqlNumRows();
if (*num_ids > 0) {
id = (DBId_t*)malloc(*num_ids * sizeof(DBId_t));
while ((row = SqlFetchRow()) != NULL) { id[i++] = str_to_uint64(row[0]); }
*ids = id;
}
SqlFreeResult();
retval = true;
} else {
Mmsg(errmsg, _("Client id select failed: ERR=%s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
}
DbUnlock(this);
return retval;
}
/**
* Get Pool Record
* If the PoolId is non-zero, we get its record,
* otherwise, we search on the PoolName
*
* Returns: false on failure
* true on success
*/
bool BareosDb::GetPoolRecord(JobControlRecord* jcr, PoolDbRecord* pdbr)
{
SQL_ROW row;
bool ok = false;
char ed1[50];
int num_rows;
char esc[MAX_ESCAPE_NAME_LENGTH];
DbLock(this);
if (pdbr->PoolId != 0) { /* find by id */
Mmsg(
cmd,
"SELECT PoolId,Name,NumVols,MaxVols,UseOnce,UseCatalog,AcceptAnyVolume,"
"AutoPrune,Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
"MaxVolBytes,PoolType,LabelType,LabelFormat,RecyclePoolId,"
"ScratchPoolId,"
"ActionOnPurge,MinBlocksize,MaxBlocksize FROM Pool WHERE "
"Pool.PoolId=%s",
edit_int64(pdbr->PoolId, ed1));
} else { /* find by name */
EscapeString(jcr, esc, pdbr->Name, strlen(pdbr->Name));
Mmsg(
cmd,
"SELECT PoolId,Name,NumVols,MaxVols,UseOnce,UseCatalog,AcceptAnyVolume,"
"AutoPrune,Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
"MaxVolBytes,PoolType,LabelType,LabelFormat,RecyclePoolId,"
"ScratchPoolId,"
"ActionOnPurge,MinBlocksize,MaxBlocksize FROM Pool WHERE "
"Pool.Name='%s'",
esc);
}
if (QUERY_DB(jcr, cmd)) {
num_rows = SqlNumRows();
if (num_rows > 1) {
char ed1[30];
Mmsg1(errmsg, _("More than one Pool!: %s\n"), edit_uint64(num_rows, ed1));
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
} else if (num_rows == 1) {
if ((row = SqlFetchRow()) == NULL) {
Mmsg1(errmsg, _("error fetching row: %s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
} else {
pdbr->PoolId = str_to_int64(row[0]);
bstrncpy(pdbr->Name, (row[1] != NULL) ? row[1] : "",
sizeof(pdbr->Name));
pdbr->NumVols = str_to_int64(row[2]);
pdbr->MaxVols = str_to_int64(row[3]);
pdbr->UseOnce = str_to_int64(row[4]);
pdbr->UseCatalog = str_to_int64(row[5]);
pdbr->AcceptAnyVolume = str_to_int64(row[6]);
pdbr->AutoPrune = str_to_int64(row[7]);
pdbr->Recycle = str_to_int64(row[8]);
pdbr->VolRetention = str_to_int64(row[9]);
pdbr->VolUseDuration = str_to_int64(row[10]);
pdbr->MaxVolJobs = str_to_int64(row[11]);
pdbr->MaxVolFiles = str_to_int64(row[12]);
pdbr->MaxVolBytes = str_to_uint64(row[13]);
bstrncpy(pdbr->PoolType, (row[14] != NULL) ? row[14] : "",
sizeof(pdbr->PoolType));
pdbr->LabelType = str_to_int64(row[15]);
bstrncpy(pdbr->LabelFormat, (row[16] != NULL) ? row[16] : "",
sizeof(pdbr->LabelFormat));
pdbr->RecyclePoolId = str_to_int64(row[17]);
pdbr->ScratchPoolId = str_to_int64(row[18]);
pdbr->ActionOnPurge = str_to_int32(row[19]);
pdbr->MinBlocksize = str_to_int32(row[20]);
pdbr->MaxBlocksize = str_to_int32(row[21]);
ok = true;
}
}
SqlFreeResult();
}
if (ok) {
uint32_t NumVols;
Mmsg(cmd, "SELECT count(*) from Media WHERE PoolId=%s",
edit_int64(pdbr->PoolId, ed1));
NumVols = GetSqlRecordMax(jcr);
Dmsg2(400, "Actual NumVols=%d Pool NumVols=%d\n", NumVols, pdbr->NumVols);
if (NumVols != pdbr->NumVols) {
pdbr->NumVols = NumVols;
ok = UpdatePoolRecord(jcr, pdbr);
}
} else {
Mmsg(errmsg, _("Pool record not found in Catalog.\n"));
}
DbUnlock(this);
return ok;
}
/**
* Get Storage Record
* If the StorageId is non-zero, we get its record, otherwise, we search on the
* StorageName
*
* Returns: false on failure
* true on success
*/
bool BareosDb::GetStorageRecord(JobControlRecord* jcr, StorageDbRecord* sdbr)
{
SQL_ROW row;
bool ok = false;
char ed1[50];
int num_rows;
char esc[MAX_ESCAPE_NAME_LENGTH];
DbLock(this);
if (sdbr->StorageId != 0) { /* find by id */
Mmsg(cmd,
"SELECT StorageId,Name,AutoChanger FROM Storage WHERE "
"Storage.StorageId=%s",
edit_int64(sdbr->StorageId, ed1));
} else { /* find by name */
EscapeString(jcr, esc, sdbr->Name, strlen(sdbr->Name));
Mmsg(cmd,
"SELECT StorageId,Name,Autochanger FROM Storage WHERE "
"Storage.Name='%s'",
esc);
}
if (QUERY_DB(jcr, cmd)) {
num_rows = SqlNumRows();
if (num_rows > 1) {
char ed1[30];
Mmsg1(errmsg, _("More than one Storage!: %s\n"),
edit_uint64(num_rows, ed1));
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
} else if (num_rows == 1) {
if ((row = SqlFetchRow()) == NULL) {
Mmsg1(errmsg, _("error fetching row: %s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
} else {
sdbr->StorageId = str_to_int64(row[0]);
bstrncpy(sdbr->Name, (row[1] != NULL) ? row[1] : "",
sizeof(sdbr->Name));
sdbr->AutoChanger = str_to_int64(row[2]);
ok = true;
}
}
SqlFreeResult();
}
DbUnlock(this);
return ok;
}
/**
* Get Client Record
* If the ClientId is non-zero, we get its record, otherwise, we search on the
* Client Name
*
* Returns: false on failure
* true on success
*/
bool BareosDb::GetClientRecord(JobControlRecord* jcr, ClientDbRecord* cdbr)
{
bool retval = false;
SQL_ROW row;
char ed1[50];
int num_rows;
char esc[MAX_ESCAPE_NAME_LENGTH];
DbLock(this);
if (cdbr->ClientId != 0) { /* find by id */
Mmsg(cmd,
"SELECT ClientId,Name,Uname,AutoPrune,FileRetention,JobRetention "
"FROM Client WHERE Client.ClientId=%s",
edit_int64(cdbr->ClientId, ed1));
} else { /* find by name */
EscapeString(jcr, esc, cdbr->Name, strlen(cdbr->Name));
Mmsg(cmd,
"SELECT ClientId,Name,Uname,AutoPrune,FileRetention,JobRetention "
"FROM Client WHERE Client.Name='%s'",
esc);
}
if (QUERY_DB(jcr, cmd)) {
num_rows = SqlNumRows();
if (num_rows > 1) {
Mmsg1(errmsg, _("More than one Client!: %s\n"),
edit_uint64(num_rows, ed1));
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
} else if (num_rows == 1) {
if ((row = SqlFetchRow()) == NULL) {
Mmsg1(errmsg, _("error fetching row: %s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
} else {
cdbr->ClientId = str_to_int64(row[0]);
bstrncpy(cdbr->Name, (row[1] != NULL) ? row[1] : "",
sizeof(cdbr->Name));
bstrncpy(cdbr->Uname, (row[2] != NULL) ? row[2] : "",
sizeof(cdbr->Uname));
cdbr->AutoPrune = str_to_int64(row[3]);
cdbr->FileRetention = str_to_int64(row[4]);
cdbr->JobRetention = str_to_int64(row[5]);
retval = true;
}
} else {
Mmsg(errmsg, _("Client record not found in Catalog.\n"));
}
SqlFreeResult();
} else {
Mmsg(errmsg, _("Client record not found in Catalog.\n"));
}
DbUnlock(this);
return retval;
}
/**
* Get Counter Record
*
* Returns: false on failure
* true on success
*/
bool BareosDb::GetCounterRecord(JobControlRecord* jcr, CounterDbRecord* cr)
{
bool retval = false;
SQL_ROW row;
int num_rows;
char esc[MAX_ESCAPE_NAME_LENGTH];
DbLock(this);
EscapeString(jcr, esc, cr->Counter, strlen(cr->Counter));
FillQuery(SQL_QUERY::select_counter_values, esc);
if (QUERY_DB(jcr, cmd)) {
num_rows = SqlNumRows();
if (num_rows > 1) {
Mmsg1(errmsg, _("More than one Counter!: %d\n"), num_rows);
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
}
if (num_rows >= 1) {
if ((row = SqlFetchRow()) == NULL) {
Mmsg1(errmsg, _("error fetching Counter row: %s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
SqlFreeResult();
goto bail_out;
}
cr->MinValue = str_to_int64(row[0]);
cr->MaxValue = str_to_int64(row[1]);
cr->CurrentValue = str_to_int64(row[2]);
if (row[3]) {
bstrncpy(cr->WrapCounter, row[3], sizeof(cr->WrapCounter));
} else {
cr->WrapCounter[0] = 0;
}
SqlFreeResult();
retval = true;
goto bail_out;
}
SqlFreeResult();
} else {
Mmsg(errmsg, _("Counter record: %s not found in Catalog.\n"), cr->Counter);
}
bail_out:
DbUnlock(this);
return retval;
}
/**
* Get FileSet Record
* If the FileSetId is non-zero, we get its record,
* otherwise, we search on the name
*
* Returns: 0 on failure
* id on success
*/
int BareosDb::GetFilesetRecord(JobControlRecord* jcr, FileSetDbRecord* fsr)
{
SQL_ROW row;
int retval = 0;
char ed1[50];
int num_rows;
char esc[MAX_ESCAPE_NAME_LENGTH];
DbLock(this);
if (fsr->FileSetId != 0) { /* find by id */
Mmsg(cmd,
"SELECT FileSetId,FileSet,MD5,CreateTime FROM FileSet "
"WHERE FileSetId=%s",
edit_int64(fsr->FileSetId, ed1));
} else { /* find by name */
EscapeString(jcr, esc, fsr->FileSet, strlen(fsr->FileSet));
Mmsg(cmd,
"SELECT FileSetId,FileSet,MD5,CreateTime FROM FileSet "
"WHERE FileSet='%s' ORDER BY CreateTime DESC LIMIT 1",
esc);
}
if (QUERY_DB(jcr, cmd)) {
num_rows = SqlNumRows();
if (num_rows > 1) {
char ed1[30];
Mmsg1(errmsg, _("Error got %s FileSets but expected only one!\n"),
edit_uint64(num_rows, ed1));
SqlDataSeek(num_rows - 1);
}
if ((row = SqlFetchRow()) == NULL) {
Mmsg1(errmsg, _("FileSet record \"%s\" not found.\n"), fsr->FileSet);
} else {
fsr->FileSetId = str_to_int64(row[0]);
bstrncpy(fsr->FileSet, (row[1] != NULL) ? row[1] : "",
sizeof(fsr->FileSet));
bstrncpy(fsr->MD5, (row[2] != NULL) ? row[2] : "", sizeof(fsr->MD5));
bstrncpy(fsr->cCreateTime, (row[3] != NULL) ? row[3] : "",
sizeof(fsr->cCreateTime));
retval = fsr->FileSetId;
}
SqlFreeResult();
} else {
Mmsg(errmsg, _("FileSet record not found in Catalog.\n"));
}
DbUnlock(this);
return retval;
}
/**
* Get the number of Media records
*
* Returns: -1 on failure
* number on success
*/
int BareosDb::GetNumMediaRecords(JobControlRecord* jcr)
{
int retval = 0;
DbLock(this);
Mmsg(cmd, "SELECT count(*) from Media");
retval = GetSqlRecordMax(jcr);
DbUnlock(this);
return retval;
}
bool BareosDb::PrepareMediaSqlQuery(JobControlRecord* jcr,
MediaDbRecord* mr,
PoolMem& volumes)
{
bool ok = true;
char ed1[50];
char esc[MAX_NAME_LENGTH * 2 + 1];
PoolMem buf(PM_MESSAGE);
Mmsg(cmd,
"SELECT DISTINCT MediaId FROM Media WHERE Recycle=%d AND Enabled=%d ",
mr->Recycle, mr->Enabled);
if (*mr->MediaType) {
EscapeString(jcr, esc, mr->MediaType, strlen(mr->MediaType));
Mmsg(buf, "AND MediaType='%s' ", esc);
PmStrcat(cmd, buf.c_str());
}
if (mr->StorageId) {
Mmsg(buf, "AND StorageId=%s ", edit_uint64(mr->StorageId, ed1));
PmStrcat(cmd, buf.c_str());
}
if (mr->PoolId) {
Mmsg(buf, "AND PoolId=%s ", edit_uint64(mr->PoolId, ed1));
PmStrcat(cmd, buf.c_str());
}
if (mr->VolBytes) {
Mmsg(buf, "AND VolBytes > %s ", edit_uint64(mr->VolBytes, ed1));
PmStrcat(cmd, buf.c_str());
}
if (*mr->VolStatus) {
EscapeString(jcr, esc, mr->VolStatus, strlen(mr->VolStatus));
Mmsg(buf, "AND VolStatus = '%s' ", esc);
PmStrcat(cmd, buf.c_str());
}
if (volumes.strlen() > 0) {
/* extra list of volumes given */
Mmsg(buf, "AND VolumeName IN (%s) ", volumes.c_str());
PmStrcat(cmd, buf.c_str());
} else if (*mr->VolumeName) {
/* single volume given in media record */
EscapeString(jcr, esc, mr->VolumeName, strlen(mr->VolumeName));
Mmsg(buf, "AND VolumeName = '%s' ", esc);
PmStrcat(cmd, buf.c_str());
}
Dmsg1(100, "query=%s\n", cmd);
return ok;
}
/**
* This function creates a sql query string at cmd to return a list of all the
* Media records for the current Pool, the correct Media Type, Recyle, Enabled,
* StorageId, VolBytes and volumes or VolumeName if specified. Comma separated
* list of volumes takes precedence over VolumeName. The caller must free ids if
* non-NULL.
*/