iPXE
image_archive_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <getopt.h>
27 #include <ipxe/command.h>
28 #include <ipxe/parseopt.h>
29 #include <usr/imgmgmt.h>
30 #include <usr/imgarchive.h>
31 
32 /** @file
33  *
34  * Archive image commands
35  *
36  */
37 
38 /** "imgextract" options */
40  /** Image name */
41  char *name;
42  /** Keep original image */
43  int keep;
44  /** Download timeout */
45  unsigned long timeout;
46 };
47 
48 /** "imgextract" option list */
50  OPTION_DESC ( "name", 'n', required_argument,
52  OPTION_DESC ( "keep", 'k', no_argument,
53  struct imgextract_options, keep, parse_flag ),
54  OPTION_DESC ( "timeout", 't', required_argument,
56 };
57 
58 /** "imgextract" command descriptor */
61 
62 /**
63  * The "imgextract" command
64  *
65  * @v argc Argument count
66  * @v argv Argument list
67  * @ret rc Return status code
68  */
69 static int imgextract_exec ( int argc, char **argv ) {
70  struct imgextract_options opts;
71  struct image *image;
72  int rc;
73 
74  /* Parse options */
75  if ( ( rc = parse_options ( argc, argv, &imgextract_cmd,
76  &opts ) ) != 0 )
77  goto err_parse;
78 
79  /* Acquire image */
80  if ( ( rc = imgacquire ( argv[optind], opts.timeout, &image ) ) != 0 )
81  goto err_acquire;
82 
83  /* Extract archive image */
84  if ( ( rc = imgextract ( image, opts.name ) ) != 0 )
85  goto err_extract;
86 
87  /* Success */
88  rc = 0;
89 
90  err_extract:
91  /* Discard original image unless --keep was specified */
92  if ( ! opts.keep )
94  err_acquire:
95  err_parse:
96  return rc;
97 }
98 
99 /** Archive image commands */
100 struct command image_archive_commands[] __command = {
101  {
102  .name = "imgextract",
103  .exec = imgextract_exec,
104  },
105 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
int optind
Current option index.
Definition: getopt.c:51
A command-line command.
Definition: command.h:9
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:114
struct command image_archive_commands [] __command
Archive image commands.
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
An executable image.
Definition: image.h:24
A command descriptor.
Definition: parseopt.h:77
char * name
Image name.
"imgextract" options
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned long timeout
Download timeout.
int keep
Keep original image.
Parse command-line options.
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
static struct option_descriptor imgextract_opts[]
"imgextract" option list
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
Command line option parsing.
int imgextract(struct image *image, const char *name)
Extract archive image.
Definition: imgarchive.c:43
Option does not take an argument.
Definition: getopt.h:16
static struct command_descriptor imgextract_cmd
"imgextract" command descriptor
Archive image management.
static union @437 opts
"cert<xxx>" option list
const char * name
Name of the command.
Definition: command.h:11
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:303
Image management.
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18
A command-line option descriptor.
Definition: parseopt.h:23
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
static int imgextract_exec(int argc, char **argv)
The "imgextract" command.
void timeout(int)
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:141