mirror of
https://github.com/ipxe/ipxe
synced 2025-12-18 02:20:19 +03:00
[image] Add "imgextract" command for extracting archive images
Add the concept of extracting an image from an archive (which could be a single-file archive such as a gzip-compressed file), along with an "imgextract" command to expose this functionality to scripts. Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
104
src/core/archive.c
Normal file
104
src/core/archive.c
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Michael Brown <mbrown@fensystems.co.uk>.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*
|
||||
* You can also choose to distribute this program under the terms of
|
||||
* the Unmodified Binary Distribution Licence (as given in the file
|
||||
* COPYING.UBDL), provided that you have satisfied its requirements.
|
||||
*/
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <ipxe/image.h>
|
||||
|
||||
/** @file
|
||||
*
|
||||
* Archive images
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Extract archive image
|
||||
*
|
||||
* @v image Image
|
||||
* @v name Extracted image name
|
||||
* @v extracted Extracted image to fill in
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
int image_extract ( struct image *image, const char *name,
|
||||
struct image **extracted ) {
|
||||
char *dot;
|
||||
int rc;
|
||||
|
||||
/* Check that this image can be used to extract an archive image */
|
||||
if ( ! ( image->type && image->type->extract ) ) {
|
||||
rc = -ENOTSUP;
|
||||
goto err_unsupported;
|
||||
}
|
||||
|
||||
/* Allocate new image */
|
||||
*extracted = alloc_image ( image->uri );
|
||||
if ( ! *extracted ) {
|
||||
rc = -ENOMEM;
|
||||
goto err_alloc;
|
||||
}
|
||||
|
||||
/* Set image name */
|
||||
if ( ( rc = image_set_name ( *extracted,
|
||||
( name ? name : image->name ) ) ) != 0 ) {
|
||||
goto err_set_name;
|
||||
}
|
||||
|
||||
/* Strip any archive or compression suffix from implicit name */
|
||||
if ( ( ! name ) && ( (*extracted)->name ) &&
|
||||
( ( dot = strrchr ( (*extracted)->name, '.' ) ) != NULL ) ) {
|
||||
*dot = '\0';
|
||||
}
|
||||
|
||||
/* Try extracting archive image */
|
||||
if ( ( rc = image->type->extract ( image, *extracted ) ) != 0 ) {
|
||||
DBGC ( image, "IMAGE %s could not extract image: %s\n",
|
||||
image->name, strerror ( rc ) );
|
||||
goto err_extract;
|
||||
}
|
||||
|
||||
/* Register image */
|
||||
if ( ( rc = register_image ( *extracted ) ) != 0 )
|
||||
goto err_register;
|
||||
|
||||
/* Drop local reference to image */
|
||||
image_put ( *extracted );
|
||||
|
||||
return 0;
|
||||
|
||||
unregister_image ( *extracted );
|
||||
err_register:
|
||||
err_extract:
|
||||
err_set_name:
|
||||
image_put ( *extracted );
|
||||
err_alloc:
|
||||
err_unsupported:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Drag in objects via image_extract() */
|
||||
REQUIRING_SYMBOL ( image_extract );
|
||||
|
||||
/* Drag in archive image formats */
|
||||
REQUIRE_OBJECT ( config_archive );
|
||||
Reference in New Issue
Block a user