From ce50e9190bdb770640a53a1a8d612229be65d8e7 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Sat, 3 Oct 2015 12:42:35 -0500 Subject: [PATCH] Add wimlib_get_image_property() and wimlib_set_image_property() --- include/wimlib.h | 63 ++++++++++++++++++++++++++++++++++++++++++++++-- src/xml.c | 29 ++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/include/wimlib.h b/include/wimlib.h index 09a42798..c95e7635 100644 --- a/include/wimlib.h +++ b/include/wimlib.h @@ -259,8 +259,9 @@ * * wimlib_update_image() can add, delete, and rename files in a WIM image. * - * wimlib_set_image_name(), wimlib_set_image_descripton(), and - * wimlib_set_image_flags() can change other image metadata. + * wimlib_set_image_name(), wimlib_set_image_descripton(), + * wimlib_set_image_flags(), and wimlib_set_image_property() can change other + * image metadata. * * wimlib_set_wim_info() can change information about the WIM file itself, such * as the boot index. @@ -3133,6 +3134,33 @@ wimlib_get_image_description(const WIMStruct *wim, int image); extern const wimlib_tchar * wimlib_get_image_name(const WIMStruct *wim, int image); +/** + * @ingroup G_wim_information + * + * Get a per-image property from the WIM's XML document. This is an alternative + * to wimlib_get_image_name() and wimlib_get_image_descripton() which allows + * getting any simple string property. + * + * @param wim + * Pointer to the ::WIMStruct for the WIM. + * @param image + * The 1-based index of the image for which to get the property. + * @param property_name + * The name of the image property, for example "NAME", "DESCRIPTION", or + * "TOTALBYTES". The name can contain forward slashes to indicate a nested + * XML element; for example, "WINDOWS/VERSION/BUILD" indicates the BUILD + * element nested within the VERSION element nested within the WINDOWS + * element. The [ character is reserved for future use. + * + * @return + * The property's value as a wimlib_tchar string, or @c NULL if there is no + * such property. The string may not remain valid after later library + * calls, so the caller should duplicate it if needed. + */ +extern const wimlib_tchar * +wimlib_get_image_property(const WIMStruct *wim, int image, + const wimlib_tchar *property_name); + /** * @ingroup G_general * @@ -3960,6 +3988,37 @@ wimlib_set_image_flags(WIMStruct *wim, int image, const wimlib_tchar *flags); extern int wimlib_set_image_name(WIMStruct *wim, int image, const wimlib_tchar *name); +/** + * @ingroup G_modifying_wims + * + * Add, modify, or remove a per-image property from the WIM's XML document. + * This is an alternative to wimlib_set_image_name(), + * wimlib_set_image_descripton(), and wimlib_set_image_flags() which allows + * manipulating any simple string property. + * + * @param wim + * Pointer to the ::WIMStruct for the WIM. + * @param image + * The 1-based index of the image for which to set the property. + * @param property_name + * The name of the image property in the same format documented for + * wimlib_get_image_property(). + * @param property_value + * If not NULL and not empty, the property is set to this value. + * Otherwise, the property is removed from the XML document. + * + * @return 0 on success; a ::wimlib_error_code value on failure. + * + * @retval ::WIMLIB_ERR_INVALID_IMAGE + * @p image does not exist in @p wim. + * @retval ::WIMLIB_ERR_INVALID_PARAM + * @p property_name has an unsupported format. + */ +extern int +wimlib_set_image_property(WIMStruct *wim, int image, + const wimlib_tchar *property_name, + const wimlib_tchar *property_value); + /** * @ingroup G_general * diff --git a/src/xml.c b/src/xml.c index 60643756..fdc3ed2f 100644 --- a/src/xml.c +++ b/src/xml.c @@ -1380,6 +1380,20 @@ wimlib_get_image_description(const WIMStruct *wim, int image) return get_image_property(wim, image, "DESCRIPTION", NULL); } +WIMLIBAPI const tchar * +wimlib_get_image_property(const WIMStruct *wim, int image, + const tchar *property_name) +{ + const xmlChar *name; + const tchar *value; + + if (tstr_get_utf8(property_name, &name)) + return NULL; + value = get_image_property(wim, image, name, NULL); + tstr_put_utf8(name); + return value; +} + WIMLIBAPI int wimlib_set_image_name(WIMStruct *wim, int image, const tchar *name) { @@ -1400,3 +1414,18 @@ wimlib_set_image_flags(WIMStruct *wim, int image, const tchar *flags) { return set_image_property(wim, image, "FLAGS", flags); } + +WIMLIBAPI int +wimlib_set_image_property(WIMStruct *wim, int image, const tchar *property_name, + const tchar *property_value) +{ + const xmlChar *name; + int ret; + + ret = tstr_get_utf8(property_name, &name); + if (ret) + return ret; + ret = set_image_property(wim, image, name, property_value); + tstr_put_utf8(name); + return ret; +} -- 2.43.0