Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[settings] Add fetch_setting_copy()
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Apr 19, 2012
1 parent 62eb229 commit 31e60de
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/core/settings.c
Expand Up @@ -676,6 +676,46 @@ int fetch_setting_len ( struct settings *settings, struct setting *setting ) {
return fetch_setting ( settings, setting, NULL, 0 );
}

/**
* Fetch copy of setting
*
* @v settings Settings block, or NULL to search all blocks
* @v setting Setting to fetch
* @v data Buffer to allocate and fill with setting data
* @ret len Length of setting, or negative error
*
* The caller is responsible for eventually freeing the allocated
* buffer.
*
* To allow the caller to distinguish between a non-existent setting
* and an error in allocating memory for the copy, this function will
* return success (and a NULL buffer pointer) for a non-existent
* setting.
*/
int fetch_setting_copy ( struct settings *settings, struct setting *setting,
void **data ) {
int len;
int check_len = 0;

/* Avoid returning uninitialised data on error */
*data = NULL;

/* Fetch setting length, and return success if non-existent */
len = fetch_setting_len ( settings, setting );
if ( len < 0 )
return 0;

/* Allocate buffer */
*data = malloc ( len );
if ( ! *data )
return -ENOMEM;

/* Fetch setting */
check_len = fetch_setting ( settings, setting, *data, len );
assert ( check_len == len );
return len;
}

/**
* Fetch value of string setting
*
Expand Down
2 changes: 2 additions & 0 deletions src/include/ipxe/settings.h
Expand Up @@ -240,6 +240,8 @@ extern struct settings * fetch_setting_origin ( struct settings *settings,
struct setting *setting );
extern int fetch_setting_len ( struct settings *settings,
struct setting *setting );
extern int fetch_setting_copy ( struct settings *settings,
struct setting *setting, void **data );
extern int fetch_string_setting ( struct settings *settings,
struct setting *setting,
char *data, size_t len );
Expand Down

0 comments on commit 31e60de

Please sign in to comment.