Browse Source

[settings] Add fetch_setting_copy()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
31e60de676
2 changed files with 42 additions and 0 deletions
  1. 40
    0
      src/core/settings.c
  2. 2
    0
      src/include/ipxe/settings.h

+ 40
- 0
src/core/settings.c View File

@@ -676,6 +676,46 @@ int fetch_setting_len ( struct settings *settings, struct setting *setting ) {
676 676
 	return fetch_setting ( settings, setting, NULL, 0 );
677 677
 }
678 678
 
679
+/**
680
+ * Fetch copy of setting
681
+ *
682
+ * @v settings		Settings block, or NULL to search all blocks
683
+ * @v setting		Setting to fetch
684
+ * @v data		Buffer to allocate and fill with setting data
685
+ * @ret len		Length of setting, or negative error
686
+ *
687
+ * The caller is responsible for eventually freeing the allocated
688
+ * buffer.
689
+ *
690
+ * To allow the caller to distinguish between a non-existent setting
691
+ * and an error in allocating memory for the copy, this function will
692
+ * return success (and a NULL buffer pointer) for a non-existent
693
+ * setting.
694
+ */
695
+int fetch_setting_copy ( struct settings *settings, struct setting *setting,
696
+			 void **data ) {
697
+	int len;
698
+	int check_len = 0;
699
+
700
+	/* Avoid returning uninitialised data on error */
701
+	*data = NULL;
702
+
703
+	/* Fetch setting length, and return success if non-existent */
704
+	len = fetch_setting_len ( settings, setting );
705
+	if ( len < 0 )
706
+		return 0;
707
+
708
+	/* Allocate buffer */
709
+	*data = malloc ( len );
710
+	if ( ! *data )
711
+		return -ENOMEM;
712
+
713
+	/* Fetch setting */
714
+	check_len = fetch_setting ( settings, setting, *data, len );
715
+	assert ( check_len == len );
716
+	return len;
717
+}
718
+
679 719
 /**
680 720
  * Fetch value of string setting
681 721
  *

+ 2
- 0
src/include/ipxe/settings.h View File

@@ -240,6 +240,8 @@ extern struct settings * fetch_setting_origin ( struct settings *settings,
240 240
 						struct setting *setting );
241 241
 extern int fetch_setting_len ( struct settings *settings,
242 242
 			       struct setting *setting );
243
+extern int fetch_setting_copy ( struct settings *settings,
244
+				struct setting *setting, void **data );
243 245
 extern int fetch_string_setting ( struct settings *settings,
244 246
 				  struct setting *setting,
245 247
 				  char *data, size_t len );

Loading…
Cancel
Save