/
sql_create.c
1496 lines (1309 loc) · 41.8 KB
/
sql_create.c
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-2017 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 Create record interface routines
*/
#include "bareos.h"
static const int dbglevel = 100;
#if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI
#include "cats.h"
/* -----------------------------------------------------------------------
*
* Generic Routines (or almost generic)
*
* -----------------------------------------------------------------------
*/
/**
* Forward referenced subroutines
*/
/**
* Create a new record for the Job
* Returns: false on failure
* true on success
*/
bool B_DB::create_job_record(JCR *jcr, JOB_DBR *jr)
{
bool retval = false;;
POOL_MEM buf;
char dt[MAX_TIME_LENGTH];
time_t stime;
int len;
utime_t JobTDate;
char ed1[30], ed2[30];
char esc_ujobname[MAX_ESCAPE_NAME_LENGTH];
char esc_jobname[MAX_ESCAPE_NAME_LENGTH];
db_lock(this);
stime = jr->SchedTime;
ASSERT(stime != 0);
bstrutime(dt, sizeof(dt), stime);
JobTDate = (utime_t)stime;
len = strlen(jcr->comment); /* TODO: use jr instead of jcr to get comment */
buf.check_size(len * 2 + 1);
escape_string(jcr, buf.c_str(), jcr->comment, len);
escape_string(jcr, esc_ujobname, jr->Job, strlen(jr->Job));
escape_string(jcr, esc_jobname, jr->Name, strlen(jr->Name));
/*
* Must create it
*/
Mmsg(cmd,
"INSERT INTO Job (Job,Name,Type,Level,JobStatus,SchedTime,JobTDate,"
"ClientId,Comment) "
"VALUES ('%s','%s','%c','%c','%c','%s',%s,%s,'%s')",
esc_ujobname, esc_jobname, (char)(jr->JobType), (char)(jr->JobLevel),
(char)(jr->JobStatus), dt, edit_uint64(JobTDate, ed1),
edit_int64(jr->ClientId, ed2), buf.c_str());
jr->JobId = sql_insert_autokey_record(cmd, NT_("Job"));
if (jr->JobId == 0) {
Mmsg2(errmsg, _("Create DB Job record %s failed. ERR=%s\n"), cmd, sql_strerror());
} else {
retval = true;
}
db_unlock(this);
return retval;
}
/**
* Create a JobMedia record for medium used this job
* Returns: false on failure
* true on success
*/
bool B_DB::create_jobmedia_record(JCR *jcr, JOBMEDIA_DBR *jm)
{
bool retval = false;
int count;
char ed1[50], ed2[50];
db_lock(this);
/*
* Now get count for VolIndex
*/
Mmsg(cmd,
"SELECT count(*) from JobMedia WHERE JobId=%s",
edit_int64(jm->JobId, ed1));
count = get_sql_record_max(jcr);
if (count < 0) {
count = 0;
}
count++;
Mmsg(cmd,
"INSERT INTO JobMedia (JobId,MediaId,FirstIndex,LastIndex,"
"StartFile,EndFile,StartBlock,EndBlock,VolIndex) "
"VALUES (%s,%s,%u,%u,%u,%u,%u,%u,%u)",
edit_int64(jm->JobId, ed1),
edit_int64(jm->MediaId, ed2),
jm->FirstIndex, jm->LastIndex,
jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock,count);
Dmsg0(300, cmd);
if (!INSERT_DB(jcr, cmd)) {
Mmsg2(errmsg, _("Create JobMedia record %s failed: ERR=%s\n"), cmd, sql_strerror());
} else {
/*
* Worked, now update the Media record with the EndFile and EndBlock
*/
Mmsg(cmd,
"UPDATE Media SET EndFile=%u, EndBlock=%u WHERE MediaId=%u",
jm->EndFile, jm->EndBlock, jm->MediaId);
if (!UPDATE_DB(jcr, cmd)) {
Mmsg2(errmsg, _("Update Media record %s failed: ERR=%s\n"), cmd, sql_strerror());
} else {
retval = true;
}
}
db_unlock(this);
Dmsg0(300, "Return from JobMedia\n");
return retval;
}
/**
* Create Unique Pool record
* Returns: false on failure
* true on success
*/
bool B_DB::create_pool_record(JCR *jcr, POOL_DBR *pr)
{
bool retval = false;
char ed1[30], ed2[30], ed3[50], ed4[50], ed5[50];
char esc_poolname[MAX_ESCAPE_NAME_LENGTH];
char esc_lf[MAX_ESCAPE_NAME_LENGTH];
int num_rows;
Dmsg0(200, "In create pool\n");
db_lock(this);
escape_string(jcr, esc_poolname, pr->Name, strlen(pr->Name));
escape_string(jcr, esc_lf, pr->LabelFormat, strlen(pr->LabelFormat));
Mmsg(cmd, "SELECT PoolId,Name FROM Pool WHERE Name='%s'", esc_poolname);
Dmsg1(200, "selectpool: %s\n", cmd);
if (QUERY_DB(jcr, cmd)) {
num_rows = sql_num_rows();
if (num_rows > 0) {
Mmsg1(errmsg, _("pool record %s already exists\n"), pr->Name);
sql_free_result();
goto bail_out;
}
sql_free_result();
}
/*
* Must create it
*/
Mmsg(cmd,
"INSERT INTO Pool (Name,NumVols,MaxVols,UseOnce,UseCatalog,"
"AcceptAnyVolume,AutoPrune,Recycle,VolRetention,VolUseDuration,"
"MaxVolJobs,MaxVolFiles,MaxVolBytes,PoolType,LabelType,LabelFormat,"
"RecyclePoolId,ScratchPoolId,ActionOnPurge,MinBlocksize,MaxBlocksize) "
"VALUES ('%s',%u,%u,%d,%d,%d,%d,%d,%s,%s,%u,%u,%s,'%s',%d,'%s',%s,%s,%d,%d,%d)",
esc_poolname,
pr->NumVols, pr->MaxVols,
pr->UseOnce, pr->UseCatalog,
pr->AcceptAnyVolume,
pr->AutoPrune, pr->Recycle,
edit_uint64(pr->VolRetention, ed1),
edit_uint64(pr->VolUseDuration, ed2),
pr->MaxVolJobs, pr->MaxVolFiles,
edit_uint64(pr->MaxVolBytes, ed3),
pr->PoolType, pr->LabelType, esc_lf,
edit_int64(pr->RecyclePoolId,ed4),
edit_int64(pr->ScratchPoolId,ed5),
pr->ActionOnPurge,
pr->MinBlocksize,
pr->MaxBlocksize);
Dmsg1(200, "Create Pool: %s\n", cmd);
pr->PoolId = sql_insert_autokey_record(cmd, NT_("Pool"));
if (pr->PoolId == 0) {
Mmsg2(errmsg, _("Create db Pool record %s failed: ERR=%s\n"), cmd, sql_strerror());
} else {
retval = true;
}
bail_out:
db_unlock(this);
Dmsg0(500, "Create Pool: done\n");
return retval;
}
/**
* Create Unique Device record
* Returns: false on failure
* true on success
*/
bool B_DB::create_device_record(JCR *jcr, DEVICE_DBR *dr)
{
bool retval = false;
SQL_ROW row;
char ed1[30], ed2[30];
char esc[MAX_ESCAPE_NAME_LENGTH];
int num_rows;
Dmsg0(200, "In create Device\n");
db_lock(this);
escape_string(jcr, esc, dr->Name, strlen(dr->Name));
Mmsg(cmd,
"SELECT DeviceId,Name FROM Device WHERE Name='%s' AND StorageId = %s",
esc, edit_int64(dr->StorageId, ed1));
Dmsg1(200, "selectdevice: %s\n", cmd);
if (QUERY_DB(jcr, cmd)) {
num_rows = sql_num_rows();
/*
* If more than one, report error, but return first row
*/
if (num_rows > 1) {
Mmsg1(errmsg, _("More than one Device!: %d\n"), num_rows);
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
}
if (num_rows >= 1) {
if ((row = sql_fetch_row()) == NULL) {
Mmsg1(errmsg, _("error fetching Device row: %s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
sql_free_result();
goto bail_out;
}
dr->DeviceId = str_to_int64(row[0]);
if (row[1]) {
bstrncpy(dr->Name, row[1], sizeof(dr->Name));
} else {
dr->Name[0] = 0; /* no name */
}
sql_free_result();
retval = true;
goto bail_out;
}
sql_free_result();
}
/*
* Must create it
*/
Mmsg(cmd,
"INSERT INTO Device (Name,MediaTypeId,StorageId) VALUES ('%s',%s,%s)",
esc,
edit_uint64(dr->MediaTypeId, ed1),
edit_int64(dr->StorageId, ed2));
Dmsg1(200, "Create Device: %s\n", cmd);
dr->DeviceId = sql_insert_autokey_record(cmd, NT_("Device"));
if (dr->DeviceId == 0) {
Mmsg2(errmsg, _("Create db Device record %s failed: ERR=%s\n"), cmd, sql_strerror());
} else {
retval = true;
}
bail_out:
db_unlock(this);
return retval;
}
/**
* Create a Unique record for Storage -- no duplicates
* Returns: false on failure
* true on success with id in sr->StorageId
*/
bool B_DB::create_storage_record(JCR *jcr, STORAGE_DBR *sr)
{
SQL_ROW row;
bool retval = false;
int num_rows;
char esc[MAX_ESCAPE_NAME_LENGTH];
db_lock(this);
escape_string(jcr, esc, sr->Name, strlen(sr->Name));
Mmsg(cmd, "SELECT StorageId,AutoChanger FROM Storage WHERE Name='%s'", esc);
sr->StorageId = 0;
sr->created = false;
/*
* Check if it already exists
*/
if (QUERY_DB(jcr, cmd)) {
num_rows = sql_num_rows();
/*
* If more than one, report error, but return first row
*/
if (num_rows > 1) {
Mmsg1(errmsg, _("More than one Storage record!: %d\n"), num_rows);
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
}
if (num_rows >= 1) {
if ((row = sql_fetch_row()) == NULL) {
Mmsg1(errmsg, _("error fetching Storage row: %s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
sql_free_result();
goto bail_out;
}
sr->StorageId = str_to_int64(row[0]);
sr->AutoChanger = atoi(row[1]); /* bool */
sql_free_result();
retval = true;
goto bail_out;
}
sql_free_result();
}
/*
* Must create it
*/
Mmsg(cmd,
"INSERT INTO Storage (Name,AutoChanger)"
" VALUES ('%s',%d)",
esc, sr->AutoChanger);
sr->StorageId = sql_insert_autokey_record(cmd, NT_("Storage"));
if (sr->StorageId == 0) {
Mmsg2(errmsg, _("Create DB Storage record %s failed. ERR=%s\n"), cmd, sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
} else {
sr->created = true;
retval = true;
}
bail_out:
db_unlock(this);
return retval;
}
/**
* Create Unique MediaType record
* Returns: false on failure
* true on success
*/
bool B_DB::create_mediatype_record(JCR *jcr, MEDIATYPE_DBR *mr)
{
bool retval = false;
int num_rows;
char esc[MAX_ESCAPE_NAME_LENGTH];
Dmsg0(200, "In create mediatype\n");
db_lock(this);
escape_string(jcr, esc, mr->MediaType, strlen(mr->MediaType));
Mmsg(cmd, "SELECT MediaTypeId,MediaType FROM MediaType WHERE MediaType='%s'", esc);
Dmsg1(200, "selectmediatype: %s\n", cmd);
if (QUERY_DB(jcr, cmd)) {
num_rows = sql_num_rows();
if (num_rows > 0) {
Mmsg1(errmsg, _("mediatype record %s already exists\n"), mr->MediaType);
sql_free_result();
goto bail_out;
}
sql_free_result();
}
/*
* Must create it
*/
Mmsg(cmd,
"INSERT INTO MediaType (MediaType,ReadOnly) "
"VALUES ('%s',%d)",
mr->MediaType,
mr->ReadOnly);
Dmsg1(200, "Create mediatype: %s\n", cmd);
mr->MediaTypeId = sql_insert_autokey_record(cmd, NT_("MediaType"));
if (mr->MediaTypeId == 0) {
Mmsg2(errmsg, _("Create db mediatype record %s failed: ERR=%s\n"), cmd, sql_strerror());
goto bail_out;
} else {
retval = true;
}
bail_out:
db_unlock(this);
return retval;
}
/**
* Create Media record. VolumeName and non-zero Slot must be unique
* Returns: false on failure
* true on success with id in mr->MediaId
*/
bool B_DB::create_media_record(JCR *jcr, MEDIA_DBR *mr)
{
bool retval = false;
char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50], ed6[50], ed7[50], ed8[50];
char ed9[50], ed10[50], ed11[50], ed12[50];
int num_rows;
char esc_medianame[MAX_ESCAPE_NAME_LENGTH];
char esc_mtype[MAX_ESCAPE_NAME_LENGTH];
char esc_status[MAX_ESCAPE_NAME_LENGTH];
db_lock(this);
escape_string(jcr, esc_medianame, mr->VolumeName, strlen(mr->VolumeName));
escape_string(jcr, esc_mtype, mr->MediaType, strlen(mr->MediaType));
escape_string(jcr, esc_status, mr->VolStatus, strlen(mr->VolStatus));
Mmsg(cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'", esc_medianame);
Dmsg1(500, "selectpool: %s\n", cmd);
if (QUERY_DB(jcr, cmd)) {
num_rows = sql_num_rows();
if (num_rows > 0) {
Mmsg1(errmsg, _("Volume \"%s\" already exists.\n"), mr->VolumeName);
sql_free_result();
goto bail_out;
}
sql_free_result();
}
/*
* Must create it
*/
Mmsg(cmd,
"INSERT INTO Media (VolumeName,MediaType,MediaTypeId,PoolId,MaxVolBytes,"
"VolCapacityBytes,Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
"VolStatus,Slot,VolBytes,InChanger,VolReadTime,VolWriteTime,"
"EndFile,EndBlock,LabelType,StorageId,DeviceId,LocationId,"
"ScratchPoolId,RecyclePoolId,Enabled,ActionOnPurge,EncryptionKey,"
"MinBlocksize,MaxBlocksize) "
"VALUES ('%s','%s',0,%u,%s,%s,%d,%s,%s,%u,%u,'%s',%d,%s,%d,%s,%s,0,0,%d,%s,"
"%s,%s,%s,%s,%d,%d,'%s',%d,%d)",
esc_medianame,
esc_mtype, mr->PoolId,
edit_uint64(mr->MaxVolBytes,ed1),
edit_uint64(mr->VolCapacityBytes, ed2),
mr->Recycle,
edit_uint64(mr->VolRetention, ed3),
edit_uint64(mr->VolUseDuration, ed4),
mr->MaxVolJobs,
mr->MaxVolFiles,
esc_status,
mr->Slot,
edit_uint64(mr->VolBytes, ed5),
mr->InChanger,
edit_int64(mr->VolReadTime, ed6),
edit_int64(mr->VolWriteTime, ed7),
mr->LabelType,
edit_int64(mr->StorageId, ed8),
edit_int64(mr->DeviceId, ed9),
edit_int64(mr->LocationId, ed10),
edit_int64(mr->ScratchPoolId, ed11),
edit_int64(mr->RecyclePoolId, ed12),
mr->Enabled, mr->ActionOnPurge,
mr->EncrKey, mr->MinBlocksize,
mr->MaxBlocksize);
Dmsg1(500, "Create Volume: %s\n", cmd);
mr->MediaId = sql_insert_autokey_record(cmd, NT_("Media"));
if (mr->MediaId == 0) {
Mmsg2(errmsg, _("Create DB Media record %s failed. ERR=%s\n"), cmd, sql_strerror());
} else {
retval = true;
if (mr->set_label_date) {
char dt[MAX_TIME_LENGTH];
if (mr->LabelDate == 0) {
mr->LabelDate = time(NULL);
}
bstrutime(dt, sizeof(dt), mr->LabelDate);
Mmsg(cmd, "UPDATE Media SET LabelDate='%s' "
"WHERE MediaId=%d", dt, mr->MediaId);
retval = UPDATE_DB(jcr, cmd);
}
/*
* Make sure that if InChanger is non-zero any other identical slot
* has InChanger zero.
*/
make_inchanger_unique(jcr, mr);
}
bail_out:
db_unlock(this);
return retval;
}
/**
* Create a Unique record for the client -- no duplicates
* Returns: false on failure
* true on success with id in cr->ClientId
*/
bool B_DB::create_client_record(JCR *jcr, CLIENT_DBR *cr)
{
bool retval = false;
SQL_ROW row;
char ed1[50], ed2[50];
int num_rows;
char esc_clientname[MAX_ESCAPE_NAME_LENGTH];
char esc_uname[MAX_ESCAPE_NAME_LENGTH];
db_lock(this);
escape_string(jcr, esc_clientname, cr->Name, strlen(cr->Name));
escape_string(jcr, esc_uname, cr->Uname, strlen(cr->Uname));
Mmsg(cmd, "SELECT ClientId,Uname FROM Client WHERE Name='%s'", esc_clientname);
cr->ClientId = 0;
if (QUERY_DB(jcr, cmd)) {
num_rows = sql_num_rows();
/*
* If more than one, report error, but return first row
*/
if (num_rows > 1) {
Mmsg1(errmsg, _("More than one Client!: %d\n"), num_rows);
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
}
if (num_rows >= 1) {
if ((row = sql_fetch_row()) == NULL) {
Mmsg1(errmsg, _("error fetching Client row: %s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
sql_free_result();
goto bail_out;
}
cr->ClientId = str_to_int64(row[0]);
if (row[1]) {
bstrncpy(cr->Uname, row[1], sizeof(cr->Uname));
} else {
cr->Uname[0] = 0; /* no name */
}
sql_free_result();
retval = true;
goto bail_out;
}
sql_free_result();
}
/*
* Must create it
*/
Mmsg(cmd,
"INSERT INTO Client (Name,Uname,AutoPrune,"
"FileRetention,JobRetention) VALUES "
"('%s','%s',%d,%s,%s)",
esc_clientname, esc_uname, cr->AutoPrune,
edit_uint64(cr->FileRetention, ed1),
edit_uint64(cr->JobRetention, ed2));
cr->ClientId = sql_insert_autokey_record(cmd, NT_("Client"));
if (cr->ClientId == 0) {
Mmsg2(errmsg, _("Create DB Client record %s failed. ERR=%s\n"), cmd, sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
} else {
retval = true;
}
bail_out:
db_unlock(this);
return retval;
}
/**
* Create a Unique record for the Path -- no duplicates
* Returns: false on failure
* true on success with id in cr->ClientId
*/
bool B_DB::create_path_record(JCR *jcr, ATTR_DBR *ar)
{
bool retval = false;
SQL_ROW row;
int num_rows;
errmsg[0] = 0;
esc_name = check_pool_memory_size(esc_name, 2 * pnl + 2);
escape_string(jcr, esc_name, path, pnl);
if (cached_path_id != 0 &&
cached_path_len == pnl &&
bstrcmp(cached_path, path)) {
ar->PathId = cached_path_id;
return true;
}
Mmsg(cmd, "SELECT PathId FROM Path WHERE Path='%s'", esc_name);
if (QUERY_DB(jcr, cmd)) {
num_rows = sql_num_rows();
if (num_rows > 1) {
char ed1[30];
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 = sql_fetch_row()) == NULL) {
Mmsg1(errmsg, _("error fetching row: %s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
sql_free_result();
ar->PathId = 0;
ASSERT(ar->PathId);
goto bail_out;
}
ar->PathId = str_to_int64(row[0]);
sql_free_result();
/*
* Cache path
*/
if (ar->PathId != cached_path_id) {
cached_path_id = ar->PathId;
cached_path_len = pnl;
pm_strcpy(cached_path, path);
}
ASSERT(ar->PathId);
retval = true;
goto bail_out;
}
sql_free_result();
}
Mmsg(cmd, "INSERT INTO Path (Path) VALUES ('%s')", esc_name);
ar->PathId = sql_insert_autokey_record(cmd, NT_("Path"));
if (ar->PathId == 0) {
Mmsg2(errmsg, _("Create db Path record %s failed. ERR=%s\n"), cmd, sql_strerror());
Jmsg(jcr, M_FATAL, 0, "%s", errmsg);
ar->PathId = 0;
goto bail_out;
}
/*
* Cache path
*/
if (ar->PathId != cached_path_id) {
cached_path_id = ar->PathId;
cached_path_len = pnl;
pm_strcpy(cached_path, path);
}
retval = true;
bail_out:
return retval;
}
/**
* Create a Unique record for the counter -- no duplicates
* Returns: false on failure
* true on success with counter filled in
*/
bool B_DB::create_counter_record(JCR *jcr, COUNTER_DBR *cr)
{
bool retval = false;
char esc[MAX_ESCAPE_NAME_LENGTH];
COUNTER_DBR mcr;
db_lock(this);
memset(&mcr, 0, sizeof(mcr));
bstrncpy(mcr.Counter, cr->Counter, sizeof(mcr.Counter));
if (get_counter_record(jcr, &mcr)) {
memcpy(cr, &mcr, sizeof(COUNTER_DBR));
retval = true;
goto bail_out;
}
escape_string(jcr, esc, cr->Counter, strlen(cr->Counter));
/*
* Must create it
*/
fill_query(SQL_QUERY_insert_counter_values, esc, cr->MinValue, cr->MaxValue, cr->CurrentValue, cr->WrapCounter);
if (!INSERT_DB(jcr, cmd)) {
Mmsg2(errmsg, _("Create DB Counters record %s failed. ERR=%s\n"), cmd, sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
} else {
retval = true;
}
bail_out:
db_unlock(this);
return retval;
}
/**
* Create a FileSet record. This record is unique in the
* name and the MD5 signature of the include/exclude sets.
* Returns: false on failure
* true on success with FileSetId in record
*/
bool B_DB::create_fileset_record(JCR *jcr, FILESET_DBR *fsr)
{
bool retval = false;
SQL_ROW row;
int num_rows, len;
char esc_fs[MAX_ESCAPE_NAME_LENGTH];
char esc_md5[MAX_ESCAPE_NAME_LENGTH];
db_lock(this);
fsr->created = false;
escape_string(jcr, esc_fs, fsr->FileSet, strlen(fsr->FileSet));
escape_string(jcr, esc_md5, fsr->MD5, strlen(fsr->MD5));
Mmsg(cmd,
"SELECT FileSetId,CreateTime FROM FileSet WHERE "
"FileSet='%s' AND MD5='%s'",
esc_fs, esc_md5);
fsr->FileSetId = 0;
if (QUERY_DB(jcr, cmd)) {
num_rows = sql_num_rows();
if (num_rows > 1) {
Mmsg1(errmsg, _("More than one FileSet!: %d\n"), num_rows);
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
}
if (num_rows >= 1) {
if ((row = sql_fetch_row()) == NULL) {
Mmsg1(errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
sql_free_result();
goto bail_out;
}
fsr->FileSetId = str_to_int64(row[0]);
if (row[1] == NULL) {
fsr->cCreateTime[0] = 0;
} else {
bstrncpy(fsr->cCreateTime, row[1], sizeof(fsr->cCreateTime));
}
sql_free_result();
retval = true;
goto bail_out;
}
sql_free_result();
}
/*
* Must create it
*/
if (fsr->CreateTime == 0 && fsr->cCreateTime[0] == 0) {
fsr->CreateTime = time(NULL);
}
bstrutime(fsr->cCreateTime, sizeof(fsr->cCreateTime), fsr->CreateTime);
if (fsr->FileSetText) {
POOL_MEM esc_filesettext(PM_MESSAGE);
len = strlen(fsr->FileSetText);
esc_filesettext.check_size(len * 2 + 1);
escape_string(jcr, esc_filesettext.c_str(), fsr->FileSetText, len);
Mmsg(cmd,
"INSERT INTO FileSet (FileSet,MD5,CreateTime,FileSetText) "
"VALUES ('%s','%s','%s','%s')",
esc_fs, esc_md5, fsr->cCreateTime, esc_filesettext.c_str());
} else {
Mmsg(cmd,
"INSERT INTO FileSet (FileSet,MD5,CreateTime) "
"VALUES ('%s','%s','%s')",
esc_fs, esc_md5, fsr->cCreateTime);
}
fsr->FileSetId = sql_insert_autokey_record(cmd, NT_("FileSet"));
if (fsr->FileSetId == 0) {
Mmsg2(errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"), cmd, sql_strerror());
Jmsg(jcr, M_ERROR, 0, "%s", errmsg);
goto bail_out;
} else {
fsr->created = true;
retval = true;
}
bail_out:
db_unlock(this);
return retval;
}
/**
* All sql_batch_* functions are used to do bulk batch insert in File/Filename/Path
* tables.
*
* To sum up :
* - bulk load a temp table
* - insert missing filenames into filename with a single query (lock filenames
* - table before that to avoid possible duplicate inserts with concurrent update)
* - insert missing paths into path with another single query
* - then insert the join between the temp, filename and path tables into file.
*
* Returns: false on failure
* true on success
*/
bool B_DB::write_batch_file_records(JCR *jcr)
{
bool retval = false;
int JobStatus = jcr->JobStatus;
if (job_canceled(jcr)) {
goto bail_out;
}
Dmsg1(50,"db_create_file_record changes=%u\n", changes);
jcr->JobStatus = JS_AttrInserting;
if (!sql_batch_end(jcr, NULL)) {
Jmsg1(jcr, M_FATAL, 0, "Batch end %s\n", errmsg);
goto bail_out;
}
if (job_canceled(jcr)) {
goto bail_out;
}
/*
* We have to lock tables
*/
if (!sql_query(SQL_QUERY_batch_lock_path_query)) {
Jmsg1(jcr, M_FATAL, 0, "Lock Path table %s\n", errmsg);
goto bail_out;
}
if (!sql_query(SQL_QUERY_batch_fill_path_query)) {
Jmsg1(jcr, M_FATAL, 0, "Fill Path table %s\n",errmsg);
sql_query(SQL_QUERY_batch_unlock_tables_query);
goto bail_out;
}
if (!sql_query(SQL_QUERY_batch_unlock_tables_query)) {
Jmsg1(jcr, M_FATAL, 0, "Unlock Path table %s\n", errmsg);
goto bail_out;
}
#if 0
/*
* We have to lock tables
*/
<<<<<<< HEAD
if (!sql_query(SQL_QUERY_batch_lock_filename_query)) {
Jmsg1(jcr, M_FATAL, 0, "Lock Filename table %s\n", errmsg);
=======
/*
if (!db_sql_query(jcr->db_batch, batch_lock_filename_query[db_get_type_index(jcr->db_batch)])) {
Jmsg1(jcr, M_FATAL, 0, "Lock Filename table %s\n", jcr->db_batch->errmsg);
>>>>>>> bareos-16.2-db2170
goto bail_out;
*/
}
#endif
if (!sql_query(SQL_QUERY_batch_fill_filename_query)) {
Jmsg1(jcr,M_FATAL,0,"Fill Filename table %s\n", errmsg);
sql_query(SQL_QUERY_batch_unlock_tables_query);
goto bail_out;
}
if (!sql_query(SQL_QUERY_batch_unlock_tables_query)) {
Jmsg1(jcr, M_FATAL, 0, "Unlock Filename table %s\n", errmsg);
goto bail_out;
}
if (!sql_query( "INSERT INTO File (FileIndex, JobId, PathId, Name, LStat, MD5, DeltaSeq) "
"SELECT batch.FileIndex, batch.JobId, Path.PathId, "
"batch.Name,batch.LStat, batch.MD5, batch.DeltaSeq "
"FROM batch "
"JOIN Path ON (batch.Path = Path.Path) ")) {
Jmsg1(jcr, M_FATAL, 0, "Fill File table %s\n", errmsg);
goto bail_out;
}
jcr->JobStatus = JobStatus; /* reset entry status */
retval = true;
bail_out:
sql_query("DROP TABLE batch");
jcr->batch_started = false;
changes = 0;
return retval;
}
/**
* Create File record in B_DB
*
* In order to reduce database size, we store the File attributes,
* the FileName, and the Path separately. In principle, there
* is a single FileName record and a single Path record, no matter
* how many times it occurs. This is this subroutine, we separate
* the file and the path and fill temporary tables with this three records.
*
* Note: all routines that call this expect to be able to call
* db_strerror(mdb) to get the error message, so the error message
* MUST be edited into mdb->errmsg before returning an error status.
*
* Returns: false on failure
* true on success
*/
bool B_DB::create_batch_file_attributes_record(JCR *jcr, ATTR_DBR *ar)
{
ASSERT(ar->FileType != FT_BASE);
Dmsg1(dbglevel, "Fname=%s\n", ar->fname);
Dmsg0(dbglevel, "put_file_into_catalog\n");
if (jcr->batch_started && jcr->db_batch->changes > BATCH_FLUSH) {
jcr->db_batch->write_batch_file_records(jcr);
}
/*
* Open the dedicated connection
*/
if (!jcr->batch_started) {
if (!open_batch_connection(jcr)) {
return false; /* error already printed */
}
if (!jcr->db_batch->sql_batch_start(jcr)) {
Mmsg1(errmsg, "Can't start batch mode: ERR=%s", jcr->db_batch->strerror());
Jmsg(jcr, M_FATAL, 0, "%s", errmsg);
return false;
}
jcr->batch_started = true;
}
jcr->db_batch->split_path_and_file(jcr, ar->fname);
return jcr->db_batch->sql_batch_insert(jcr, ar);
}
/**
* Create File record in B_DB
*
* In order to reduce database size, we store the File attributes,
* the FileName, and the Path separately. In principle, there
* is a single FileName record and a single Path record, no matter
* how many times it occurs. This is this subroutine, we separate
* the file and the path and create three database records.
*
* Returns: false on failure
* true on success
*/
bool B_DB::create_file_attributes_record(JCR *jcr, ATTR_DBR *ar)
{
bool retval = false;
db_lock(this);
Dmsg1(dbglevel, "Fname=%s\n", ar->fname);
Dmsg0(dbglevel, "put_file_into_catalog\n");
split_path_and_file(jcr, ar->fname);
if (!create_path_record(jcr, ar)) {
goto bail_out;
}
Dmsg1(dbglevel, "create_path_record: %s\n", esc_name);
/* Now create master File record */
if (!create_file_record(jcr, ar)) {
goto bail_out;
}
Dmsg0(dbglevel, "create_file_record OK\n");
Dmsg2(dbglevel, "CreateAttributes Path=%s File=%s\n", path, fname);
retval = true;
bail_out:
db_unlock(this);
return retval;
}
/**
* This is the master File entry containing the attributes.
* The filename and path records have already been created.
* Returns: false on failure
* true on success with fileid filled in
*/
bool B_DB::create_file_record(JCR *jcr, ATTR_DBR *ar)
{
bool retval = false;
static const char *no_digest = "0";
const char *digest;