summaryrefslogtreecommitdiffstats
path: root/src/core/image.c
diff options
context:
space:
mode:
authorMichael Brown2012-03-24 02:16:37 +0100
committerMichael Brown2012-03-25 00:12:04 +0100
commit1c127a696215bd75917c3ba836c2db11636b3ffb (patch)
treebd883060a15bfae71aef090481f9ea67d4fb43bc /src/core/image.c
parent[build] Fix compilation under Cygwin (diff)
downloadipxe-1c127a696215bd75917c3ba836c2db11636b3ffb.tar.gz
ipxe-1c127a696215bd75917c3ba836c2db11636b3ffb.tar.xz
ipxe-1c127a696215bd75917c3ba836c2db11636b3ffb.zip
[image] Simplify image management commands and internal API
Remove the name, cmdline, and action parameters from imgdownload() and imgdownload_string(). These functions now simply download and return an image. Add the function imgacquire(), which will interpret a "name or URI string" parameter and return either an existing image or a newly downloaded image. Use imgacquire() to merge similar image-management commands that currently differ only by whether they take the name of an existing image or the URI of a new image to download. For example, "chain" and "imgexec" can now be merged. Extend imgstat and imgfree commands to take an optional list of images. Remove the arbitrary restriction on the length of image names. Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/image.c')
-rw-r--r--src/core/image.c61
1 files changed, 42 insertions, 19 deletions
diff --git a/src/core/image.c b/src/core/image.c
index ae09a072..b02b8936 100644
--- a/src/core/image.c
+++ b/src/core/image.c
@@ -66,6 +66,7 @@ static int require_trusted_images_permanent = 0;
static void free_image ( struct refcnt *refcnt ) {
struct image *image = container_of ( refcnt, struct image, refcnt );
+ free ( image->name );
free ( image->cmdline );
uri_put ( image->uri );
ufree ( image->data );
@@ -77,37 +78,56 @@ static void free_image ( struct refcnt *refcnt ) {
/**
* Allocate executable image
*
+ * @v uri URI, or NULL
* @ret image Executable image
*/
-struct image * alloc_image ( void ) {
+struct image * alloc_image ( struct uri *uri ) {
+ const char *name;
struct image *image;
+ int rc;
+ /* Allocate image */
image = zalloc ( sizeof ( *image ) );
- if ( image ) {
- ref_init ( &image->refcnt, free_image );
+ if ( ! image )
+ goto err_alloc;
+
+ /* Initialise image */
+ ref_init ( &image->refcnt, free_image );
+ if ( uri ) {
+ image->uri = uri_get ( uri );
+ name = basename ( ( char * ) uri->path );
+ if ( ( rc = image_set_name ( image, name ) ) != 0 )
+ goto err_set_name;
}
+
return image;
+
+ err_set_name:
+ image_put ( image );
+ err_alloc:
+ return NULL;
}
/**
- * Set image URI
+ * Set image name
*
* @v image Image
- * @v URI New image URI
- *
- * If no name is set, the name will be updated to the base name of the
- * URI path (if any).
+ * @v name New image name
+ * @ret rc Return status code
*/
-void image_set_uri ( struct image *image, struct uri *uri ) {
- const char *path = uri->path;
+int image_set_name ( struct image *image, const char *name ) {
+ char *name_copy;
- /* Replace URI reference */
- uri_put ( image->uri );
- image->uri = uri_get ( uri );
+ /* Duplicate name */
+ name_copy = strdup ( name );
+ if ( ! name_copy )
+ return -ENOMEM;
+
+ /* Replace existing name */
+ free ( image->name );
+ image->name = name_copy;
- /* Set name if none already specified */
- if ( path && ( ! image->name[0] ) )
- image_set_name ( image, basename ( ( char * ) path ) );
+ return 0;
}
/**
@@ -137,11 +157,14 @@ int image_set_cmdline ( struct image *image, const char *cmdline ) {
*/
int register_image ( struct image *image ) {
static unsigned int imgindex = 0;
+ char name[8]; /* "imgXXXX" */
+ int rc;
/* Create image name if it doesn't already have one */
- if ( ! image->name[0] ) {
- snprintf ( image->name, sizeof ( image->name ), "img%d",
- imgindex++ );
+ if ( ! image->name ) {
+ snprintf ( name, sizeof ( name ), "img%d", imgindex++ );
+ if ( ( rc = image_set_name ( image, name ) ) != 0 )
+ return rc;
}
/* Avoid ending up with multiple "selected" images on