1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-06 05:01:48 +02:00

C api: nix_export_std_string -> nix_observe_string

This commit is contained in:
José Luis Lafuente 2024-03-27 17:50:36 +01:00
parent 940ff6535c
commit d96b52bd8b
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
8 changed files with 92 additions and 76 deletions

View file

@ -63,16 +63,17 @@ const char * nix_version_get()
}
// Implementations
nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n)
nix_err nix_setting_get(nix_c_context * context, const char * key, void * callback, void * user_data)
{
if (context)
context->last_err_code = NIX_OK;
try {
std::map<std::string, nix::AbstractConfig::SettingInfo> settings;
nix::globalConfig.getSettings(settings);
if (settings.contains(key))
return nix_export_std_string(context, settings[key].value, value, n);
else {
if (settings.contains(key)) {
return call_nix_observe_string(settings[key].value, callback, user_data);
} else {
return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found");
}
}
@ -114,24 +115,24 @@ const char * nix_err_msg(nix_c_context * context, const nix_c_context * read_con
return nullptr;
}
nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n)
nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data)
{
if (context)
context->last_err_code = NIX_OK;
if (read_context->last_err_code != NIX_ERR_NIX_ERROR) {
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error");
}
return nix_export_std_string(context, read_context->name, value, n);
return call_nix_observe_string(read_context->name, callback, user_data);
}
nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n)
nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data)
{
if (context)
context->last_err_code = NIX_OK;
if (read_context->last_err_code != NIX_ERR_NIX_ERROR) {
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error");
}
return nix_export_std_string(context, read_context->info->msg.str(), value, n);
return call_nix_observe_string(read_context->info->msg.str(), callback, user_data);
}
nix_err nix_err_code(const nix_c_context * read_context)
@ -140,12 +141,8 @@ nix_err nix_err_code(const nix_c_context * read_context)
}
// internal
nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n)
nix_err call_nix_observe_string(const std::string str, void * callback, void * user_data)
{
size_t i = str.copy(dest, n - 1);
dest[i] = 0;
if (i == n - 1) {
return nix_set_err_msg(context, NIX_ERR_OVERFLOW, "Provided buffer too short");
} else
return NIX_OK;
((nix_observe_string) callback)(str.c_str(), str.size(), user_data);
return NIX_OK;
}

View file

@ -119,6 +119,15 @@ typedef int nix_err;
*/
typedef struct nix_c_context nix_c_context;
/**
* @brief Called to get the value of a string owned by Nix.
*
* @param[in] start the string to copy.
* @param[in] n the string length.
* @param[in] user_data optional, arbitrary data, passed to the nix_observe_string when it's called.
*/
typedef void (*nix_observe_string)(const char * start, unsigned int n, void * user_data);
// Function prototypes
/**
@ -160,14 +169,13 @@ nix_err nix_libutil_init(nix_c_context * context);
*
* @param[out] context optional, Stores error information
* @param[in] key The key of the setting to retrieve.
* @param[out] value A pointer to a buffer where the value of the setting will
* be stored.
* @param[in] n The size of the buffer pointed to by value.
* @return NIX_ERR_KEY if the setting is unknown, NIX_ERR_OVERFLOW if the
* provided buffer is too short, or NIX_OK if the setting was retrieved
* @param[in] callback Called with the setting value.
* @param[in] user_data optional, arbitrary data, passed to the callback when it's called.
* @see nix_observe_string
* @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was retrieved
* successfully.
*/
nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n);
nix_err nix_setting_get(nix_c_context * context, const char * key, void * callback, void * user_data);
/**
* @brief Sets a setting in the nix global configuration.
@ -227,12 +235,14 @@ const char * nix_err_msg(nix_c_context * context, const nix_c_context * ctx, uns
*
* @param[out] context optional, the context to store errors in if this function
* fails
* @param[in] read_context the context to retrieve the error message from
* @param[out] value The allocated area to write the error string to.
* @param[in] n Maximum size of the returned string.
* @param[in] read_context the context to retrieve the error message from.
* @param[in] callback Called with the error message.
* @param[in] user_data optional, arbitrary data, passed to the callback when it's called.
* @see nix_observe_string
* @return NIX_OK if there were no errors, an error code otherwise.
*/
nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n);
nix_err
nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data);
/**
* @brief Retrieves the error name from a context.
@ -245,11 +255,12 @@ nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_con
* @param context optional, the context to store errors in if this function
* fails
* @param[in] read_context the context to retrieve the error message from
* @param[out] value The allocated area to write the error string to.
* @param[in] n Maximum size of the returned string.
* @param[in] callback Called with the error name.
* @param[in] user_data optional, arbitrary data, passed to the callback when it's called.
* @see nix_observe_string
* @return NIX_OK if there were no errors, an error code otherwise.
*/
nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n);
nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data);
/**
* @brief Retrieves the most recent error code from a nix_c_context

View file

@ -20,16 +20,16 @@ nix_err nix_context_error(nix_c_context * context);
/**
* Internal use only.
*
* Export a std::string across the C api boundary
* Helper to invoke nix_observe_string
* @param context optional, the context to store errors in if this function
* fails
* @param str The string to export
* @param value The allocated area to write the string to.
* @param n Maximum size of the returned string.
* @return NIX_OK if there were no errors, NIX_ERR_OVERFLOW if the string length
* exceeds `n`.
* @param str The string to observe
* @param callback Called with the observed string.
* @param user_data optional, arbitrary data, passed to the callback when it's called.
* @return NIX_OK if there were no errors.
* @see nix_observe_string
*/
nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n);
nix_err call_nix_observe_string(const std::string str, void * callback, void * user_data);
#define NIXC_CATCH_ERRS \
catch (...) \