Skip to content

Commit

Permalink
read-record: add user_data to callback
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsura committed Mar 25, 2024
1 parent d84f4f3 commit 6f18ab1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
4 changes: 2 additions & 2 deletions core/src/stored/ndmp_tape.cc
Expand Up @@ -359,7 +359,7 @@ static inline bool bndmp_read_data_from_block(JobControlRecord* jcr,
} else {
// Read the next block into our buffers.
if (!ReadNextBlockFromDevice(dcr, &rctx->sessrec, NULL,
MountNextReadVolume, &ok)) {
MountNextReadVolume, NULL, &ok)) {
return false;
}

Expand Down Expand Up @@ -687,7 +687,7 @@ extern "C" ndmp9_error bndmp_tape_open(struct ndm_session* sess,

// Read the first block and setup record processing.
if (!ReadNextBlockFromDevice(dcr, &rctx->sessrec, NULL,
MountNextReadVolume, &ok)) {
MountNextReadVolume, NULL, &ok)) {
Jmsg1(jcr, M_FATAL, 0, T_("Read session label failed. ERR=%s\n"),
dcr->dev->bstrerror());
goto bail_out;
Expand Down
42 changes: 33 additions & 9 deletions core/src/stored/read_record.cc
Expand Up @@ -187,8 +187,10 @@ void ReadContextSetRecord(DeviceControlRecord* dcr, READ_CTX* rctx)
bool ReadNextBlockFromDevice(DeviceControlRecord* dcr,
Session_Label* sessrec,
bool RecordCb(DeviceControlRecord* dcr,
DeviceRecord* rec),
DeviceRecord* rec,
void* user_data),
bool mount_cb(DeviceControlRecord* dcr),
void* user_data,
bool* status)
{
JobControlRecord* jcr = dcr->jcr;
Expand All @@ -214,7 +216,7 @@ bool ReadNextBlockFromDevice(DeviceControlRecord* dcr,
trec = new_record();
trec->FileIndex = EOT_LABEL;
trec->File = dcr->dev->file;
*status = RecordCb(dcr, trec);
*status = RecordCb(dcr, trec, user_data);
if (jcr->sd_impl->read_session.mount_next_volume) {
jcr->sd_impl->read_session.mount_next_volume = false;
dcr->dev->ClearEot();
Expand All @@ -232,7 +234,7 @@ bool ReadNextBlockFromDevice(DeviceControlRecord* dcr,
trec = new_record();
ReadRecordFromBlock(dcr, trec);
HandleSessionRecord(dcr->dev, trec, sessrec);
if (RecordCb) { RecordCb(dcr, trec); }
if (RecordCb) { RecordCb(dcr, trec, user_data); }

FreeRecord(trec);
PositionDeviceToFirstFile(jcr, dcr);
Expand Down Expand Up @@ -382,8 +384,11 @@ bool ReadNextRecordFromBlock(DeviceControlRecord* dcr,
* You must not change any values in the DeviceRecord packet
*/
bool ReadRecords(DeviceControlRecord* dcr,
bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec),
bool mount_cb(DeviceControlRecord* dcr))
bool RecordCb(DeviceControlRecord* dcr,
DeviceRecord* rec,
void* user_data),
bool mount_cb(DeviceControlRecord* dcr),
void* user_data)
{
JobControlRecord* jcr = dcr->jcr;
READ_CTX* rctx;
Expand All @@ -402,7 +407,7 @@ bool ReadRecords(DeviceControlRecord* dcr,

// Read the next block into our buffers.
if (!ReadNextBlockFromDevice(dcr, &rctx->sessrec, RecordCb, mount_cb,
&ok)) {
user_data, &ok)) {
break;
}

Expand Down Expand Up @@ -432,7 +437,7 @@ bool ReadRecords(DeviceControlRecord* dcr,
/* Note, we pass *all* labels to the callback routine. If
* he wants to know if they matched the bsr, then he must
* check the match_stat in the record */
ok = RecordCb(dcr, rctx->rec);
ok = RecordCb(dcr, rctx->rec, user_data);
} else {
Dmsg6(debuglevel,
"OK callback. recno=%d state_bits=%s blk=%d SI=%d ST=%d FI=%d\n",
Expand Down Expand Up @@ -463,11 +468,11 @@ bool ReadRecords(DeviceControlRecord* dcr,
auto* brec = dcr->before_rec;
auto* arec = dcr->after_rec;
if (arec) {
ok = RecordCb(dcr, arec);
ok = RecordCb(dcr, arec, user_data);
FreeRecord(arec);
arec = nullptr;
} else {
ok = RecordCb(dcr, dcr->before_rec);
ok = RecordCb(dcr, dcr->before_rec, user_data);
}
dcr->after_rec = arec;
dcr->before_rec = brec;
Expand All @@ -485,4 +490,23 @@ bool ReadRecords(DeviceControlRecord* dcr,
return ok;
}

static bool NoUserData(DeviceControlRecord* dcr,
DeviceRecord* rec,
void* user_data)
{
auto* RecordCb
= reinterpret_cast<bool (*)(DeviceControlRecord* dcr, DeviceRecord* rec)>(
user_data);

return RecordCb(dcr, rec);
}

bool ReadRecords(DeviceControlRecord* dcr,
bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec),
bool mount_cb(DeviceControlRecord* dcr))
{
return ReadRecords(dcr, &NoUserData, mount_cb,
reinterpret_cast<void*>(RecordCb));
}

} /* namespace storagedaemon */
36 changes: 34 additions & 2 deletions core/src/stored/read_record.h
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2019 Bareos GmbH & Co. KG
Copyright (C) 2018-2024 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
Expand Down Expand Up @@ -31,8 +31,10 @@ void ReadContextSetRecord(DeviceControlRecord* dcr, READ_CTX* rctx);
bool ReadNextBlockFromDevice(DeviceControlRecord* dcr,
Session_Label* sessrec,
bool RecordCb(DeviceControlRecord* dcr,
DeviceRecord* rec),
DeviceRecord* rec,
void* user_data),
bool mount_cb(DeviceControlRecord* dcr),
void* user_data,
bool* status);
bool ReadNextRecordFromBlock(DeviceControlRecord* dcr,
READ_CTX* rctx,
Expand All @@ -41,6 +43,36 @@ bool ReadRecords(DeviceControlRecord* dcr,
bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec),
bool mount_cb(DeviceControlRecord* dcr));

bool ReadRecords(DeviceControlRecord* dcr,
bool RecordCb(DeviceControlRecord* dcr,
DeviceRecord* rec,
void* user_data),
bool mount_cb(DeviceControlRecord* dcr),
void* user_data);

template <typename T>
inline bool ReadRecords(DeviceControlRecord* dcr,
bool RecordCb(DeviceControlRecord* dcr,
DeviceRecord* rec,
T* user_data),
bool mount_cb(DeviceControlRecord* dcr),
T* user_data)
{
auto capture = [RecordCb, user_data](DeviceControlRecord* inner_dcr,
DeviceRecord* inner_rec) -> bool {
return RecordCb(inner_dcr, inner_rec, user_data);
};

return ReadRecords(
dcr,
+[](DeviceControlRecord* inner_dcr, DeviceRecord* inner_rec,
void* impl) -> bool {
auto* lambda = reinterpret_cast<decltype(&capture)>(impl);
return (*lambda)(inner_dcr, inner_rec);
},
mount_cb, reinterpret_cast<void*>(&capture));
}

} /* namespace storagedaemon */

#endif // BAREOS_STORED_READ_RECORD_H_

0 comments on commit 6f18ab1

Please sign in to comment.