Skip to content

Commit

Permalink
core: PySavePacketToNative: give feedback on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-at-bareos authored and BareosBot committed Oct 30, 2023
1 parent 92c4f1a commit 105ebd7
Showing 1 changed file with 46 additions and 19 deletions.
65 changes: 46 additions & 19 deletions core/src/plugins/filed/python/module/bareosfd.cc
Expand Up @@ -284,9 +284,13 @@ static inline bool PySavePacketToNative(

plugin_priv_ctx->fname = strdup(fileName_AsUTF8);
sp->fname = plugin_priv_ctx->fname;
} else {
PyErr_SetString(PyExc_TypeError, "fname needs to be of type string \"utf-8\"");
return false;
}
} else {
goto bail_out;
PyErr_SetString(PyExc_RuntimeError, "fname is empty");
return false;
}

// Optional field.
Expand All @@ -297,14 +301,19 @@ static inline bool PySavePacketToNative(
if (plugin_priv_ctx->link) { free(plugin_priv_ctx->link); }
plugin_priv_ctx->link = strdup(PyUnicode_AsUTF8(pSavePkt->link));
sp->link = plugin_priv_ctx->link;
} else {
PyErr_SetString(PyExc_TypeError,
"if given, link needs to be of type string \"utf-8\"");
return false;
}
}

// Handle the stat structure.
if (pSavePkt->statp) {
PyStatPacketToNative((PyStatPacket*)pSavePkt->statp, &sp->statp);
} else {
goto bail_out;
PyErr_SetString(PyExc_RuntimeError, "PyStatPacketToNative() failed");
return false;
}

sp->type = pSavePkt->type;
Expand All @@ -313,16 +322,19 @@ static inline bool PySavePacketToNative(
char* flags;

if (PyByteArray_Size(pSavePkt->flags) != sizeof(sp->flags)) {
goto bail_out;
PyErr_SetString(PyExc_RuntimeError, "PyByteArray_Size(flags) failed");
return false;
}

if ((flags = PyByteArray_AsString(pSavePkt->flags))) {
memcpy(sp->flags, flags, sizeof(sp->flags));
} else {
goto bail_out;
PyErr_SetString(PyExc_RuntimeError,
"PyByteArray_AsString(flags) failed");
return false;
}
} else {
goto bail_out;
PyErr_SetString(PyExc_TypeError, "flags need to be of type bytearray");
return false;
}

// Special code for handling restore objects.
Expand All @@ -331,9 +343,12 @@ static inline bool PySavePacketToNative(
if (pSavePkt->object_len > 0) {
/* As this has to linger as long as the backup is running we save it
* in our plugin context. */
if (pSavePkt->object_name && pSavePkt->object
&& PyUnicode_Check(pSavePkt->object_name)
&& PyByteArray_Check(pSavePkt->object)) {
bool object_name_exists = pSavePkt->object_name;
bool object_name_is_unicode = PyUnicode_Check(pSavePkt->object_name);
bool object_exists = pSavePkt->object;
bool object_is_bytearray = PyByteArray_Check(pSavePkt->object);
if (object_name_exists && object_name_is_unicode && object_exists
&& object_is_bytearray) {
char* buf;

if (plugin_priv_ctx->object_name) {
Expand All @@ -352,13 +367,25 @@ static inline bool PySavePacketToNative(
memcpy(plugin_priv_ctx->object, buf, pSavePkt->object_len);
sp->object = plugin_priv_ctx->object;
} else {
goto bail_out;
return false;
}
} else {
goto bail_out;
std::string err_string{};
if (!object_name_exists) { err_string = "object name missing"; };
if (!object_name_is_unicode) {
err_string = "object name must be unicode type";
};
if (!object_exists) { err_string = "object missing"; };
if (!object_is_bytearray) {
err_string = "object needs to be of type bytearray";
};

PyErr_SetString(PyExc_RuntimeError, err_string.c_str());
return false;
}
} else {
goto bail_out;
PyErr_SetString(PyExc_RuntimeError, "pSavePkt->object_len is <=0");
return false;
}
} else {
sp->no_read = pSavePkt->no_read;
Expand All @@ -374,23 +401,23 @@ static inline bool PySavePacketToNative(
char* flags;

if (PyByteArray_Size(pSavePkt->flags) != sizeof(sp->flags)) {
goto bail_out;
PyErr_SetString(PyExc_RuntimeError, "PyByteArray_Size(flags) failed");
return false;
}

if ((flags = PyByteArray_AsString(pSavePkt->flags))) {
memcpy(sp->flags, flags, sizeof(sp->flags));
} else {
goto bail_out;
PyErr_SetString(PyExc_RuntimeError,
"PyByteArray_AsString(flags) failed");
return false;
}
} else {
goto bail_out;
PyErr_SetString(PyExc_TypeError, "flags need to be of type bytearray");
return false;
}
}

return true;

bail_out:
return false;
}

/**
Expand Down

0 comments on commit 105ebd7

Please sign in to comment.