iPXE
Data Structures | Functions | Variables
downloader.c File Reference

Image downloader. More...

#include <stdlib.h>
#include <errno.h>
#include <syslog.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/job.h>
#include <ipxe/uaccess.h>
#include <ipxe/umalloc.h>
#include <ipxe/image.h>
#include <ipxe/xferbuf.h>
#include <ipxe/downloader.h>

Go to the source code of this file.

Data Structures

struct  downloader
 A downloader. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void downloader_free (struct refcnt *refcnt)
 Free downloader object. More...
 
static void downloader_finished (struct downloader *downloader, int rc)
 Terminate download. More...
 
static int downloader_progress (struct downloader *downloader, struct job_progress *progress)
 Report progress of download job. More...
 
static int downloader_deliver (struct downloader *downloader, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Handle received data. More...
 
static struct xfer_bufferdownloader_buffer (struct downloader *downloader)
 Get underlying data transfer buffer. More...
 
static int downloader_vredirect (struct downloader *downloader, int type, va_list args)
 Redirect data transfer interface. More...
 
int create_downloader (struct interface *job, struct image *image)
 Instantiate a downloader. More...
 

Variables

static struct interface_operation downloader_xfer_operations []
 Downloader data transfer interface operations. More...
 
static struct interface_descriptor downloader_xfer_desc
 Downloader data transfer interface descriptor. More...
 
static struct interface_operation downloader_job_op []
 Downloader job control interface operations. More...
 
static struct interface_descriptor downloader_job_desc
 Downloader job control interface descriptor. More...
 

Detailed Description

Image downloader.

Definition in file downloader.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ downloader_free()

static void downloader_free ( struct refcnt refcnt)
static

Free downloader object.

Parameters
refcntDownloader reference counter

Definition at line 66 of file downloader.c.

66  {
67  struct downloader *downloader =
68  container_of ( refcnt, struct downloader, refcnt );
69 
71  free ( downloader );
72 }
struct image * image
Image to contain downloaded file.
Definition: downloader.c:56
A reference counter.
Definition: refcnt.h:26
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
A downloader.
Definition: downloader.c:46
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:206

References container_of, free, downloader::image, and image_put().

Referenced by create_downloader().

◆ downloader_finished()

static void downloader_finished ( struct downloader downloader,
int  rc 
)
static

Terminate download.

Parameters
downloaderDownloader
rcReason for termination

Definition at line 80 of file downloader.c.

80  {
81 
82  /* Log download status */
83  if ( rc == 0 ) {
84  syslog ( LOG_NOTICE, "Downloaded \"%s\"\n",
85  downloader->image->name );
86  } else {
87  syslog ( LOG_ERR, "Download of \"%s\" failed: %s\n",
88  downloader->image->name, strerror ( rc ) );
89  }
90 
91  /* Update image length */
93 
94  /* Shut down interfaces */
97 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:273
struct image * image
Image to contain downloaded file.
Definition: downloader.c:56
#define LOG_ERR
Error: error conditions.
Definition: syslog.h:35
struct interface job
Job control interface.
Definition: downloader.c:51
size_t len
Size of data.
Definition: xferbuf.h:23
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
A downloader.
Definition: downloader.c:46
size_t len
Length of raw file image.
Definition: image.h:43
struct interface xfer
Data transfer interface.
Definition: downloader.c:53
struct xfer_buffer buffer
Data transfer buffer.
Definition: downloader.c:58
#define syslog(priority, fmt,...)
Write message to system log.
Definition: syslog.h:93
char * name
Name.
Definition: image.h:34
#define LOG_NOTICE
Notice: normal but significant conditions.
Definition: syslog.h:41

References downloader::buffer, downloader::image, intf_shutdown(), downloader::job, xfer_buffer::len, image::len, LOG_ERR, LOG_NOTICE, image::name, rc, strerror(), syslog, and downloader::xfer.

Referenced by create_downloader(), downloader_deliver(), and downloader_vredirect().

◆ downloader_progress()

static int downloader_progress ( struct downloader downloader,
struct job_progress progress 
)
static

Report progress of download job.

Parameters
downloaderDownloader
progressProgress report to fill in
Return values
ongoing_rcOngoing job status code (if known)

Definition at line 112 of file downloader.c.

113  {
114  int rc;
115 
116  /* Allow data transfer to provide an accurate description */
117  if ( ( rc = job_progress ( &downloader->xfer, progress ) ) != 0 )
118  return rc;
119 
120  /* This is not entirely accurate, since downloaded data may
121  * arrive out of order (e.g. with multicast protocols), but
122  * it's a reasonable first approximation.
123  */
124  if ( ! progress->total ) {
125  progress->completed = downloader->buffer.pos;
126  progress->total = downloader->buffer.len;
127  }
128 
129  return 0;
130 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
size_t pos
Current offset within data.
Definition: xferbuf.h:25
size_t len
Size of data.
Definition: xferbuf.h:23
unsigned long total
Total operation size.
Definition: job.h:30
unsigned long completed
Amount of operation completed so far.
Definition: job.h:23
A downloader.
Definition: downloader.c:46
Job progress.
Definition: job.h:15
struct interface xfer
Data transfer interface.
Definition: downloader.c:53
struct xfer_buffer buffer
Data transfer buffer.
Definition: downloader.c:58

References downloader::buffer, job_progress::completed, xfer_buffer::len, xfer_buffer::pos, rc, job_progress::total, and downloader::xfer.

◆ downloader_deliver()

static int downloader_deliver ( struct downloader downloader,
struct io_buffer iobuf,
struct xfer_metadata meta 
)
static

Handle received data.

Parameters
downloaderDownloader
iobufDatagram I/O buffer
metaData transfer metadata
Return values
rcReturn status code

Definition at line 146 of file downloader.c.

148  {
149  int rc;
150 
151  /* Add data to buffer */
152  if ( ( rc = xferbuf_deliver ( &downloader->buffer, iob_disown ( iobuf ),
153  meta ) ) != 0 )
154  goto err_deliver;
155 
156  return 0;
157 
158  err_deliver:
160  return rc;
161 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int xferbuf_deliver(struct xfer_buffer *xferbuf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Add received data to data transfer buffer.
Definition: xferbuf.c:152
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:208
static void downloader_finished(struct downloader *downloader, int rc)
Terminate download.
Definition: downloader.c:80
int meta(WINDOW *, bool)
A downloader.
Definition: downloader.c:46
struct xfer_buffer buffer
Data transfer buffer.
Definition: downloader.c:58

References downloader::buffer, downloader_finished(), iob_disown, meta(), rc, and xferbuf_deliver().

◆ downloader_buffer()

static struct xfer_buffer* downloader_buffer ( struct downloader downloader)
static

Get underlying data transfer buffer.

Parameters
downloaderDownloader
Return values
xferbufData transfer buffer, or NULL on error

Definition at line 170 of file downloader.c.

170  {
171 
172  /* Provide direct access to underlying data transfer buffer */
173  return &downloader->buffer;
174 }
A downloader.
Definition: downloader.c:46
struct xfer_buffer buffer
Data transfer buffer.
Definition: downloader.c:58

References downloader::buffer.

◆ downloader_vredirect()

static int downloader_vredirect ( struct downloader downloader,
int  type,
va_list  args 
)
static

Redirect data transfer interface.

Parameters
downloaderDownloader
typeNew location type
argsRemaining arguments depend upon location type
Return values
rcReturn status code

Definition at line 184 of file downloader.c.

185  {
186  va_list tmp;
187  struct uri *uri;
188  int rc;
189 
190  /* Intercept redirects to a LOCATION_URI and update the image URI */
191  if ( type == LOCATION_URI ) {
192 
193  /* Extract URI argument */
194  va_copy ( tmp, args );
195  uri = va_arg ( tmp, struct uri * );
196  va_end ( tmp );
197 
198  /* Set image URI */
199  if ( ( rc = image_set_uri ( downloader->image, uri ) ) != 0 )
200  goto err;
201  }
202 
203  /* Redirect to new location */
204  if ( ( rc = xfer_vreopen ( &downloader->xfer, type, args ) ) != 0 )
205  goto err;
206 
207  return 0;
208 
209  err:
211  return rc;
212 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define va_end(ap)
Definition: stdarg.h:9
int image_set_uri(struct image *image, struct uri *uri)
Set image URI.
Definition: image.c:119
struct image * image
Image to contain downloaded file.
Definition: downloader.c:56
uint8_t type
Type.
Definition: ena.h:16
#define va_copy(dest, src)
Definition: stdarg.h:10
static void downloader_finished(struct downloader *downloader, int rc)
Terminate download.
Definition: downloader.c:80
#define va_arg(ap, type)
Definition: stdarg.h:8
A downloader.
Definition: downloader.c:46
uint8_t * tmp
Definition: entropy.h:156
struct interface xfer
Data transfer interface.
Definition: downloader.c:53
int xfer_vreopen(struct interface *intf, int type, va_list args)
Reopen location.
Definition: open.c:225
__builtin_va_list va_list
Definition: stdarg.h:6
A Uniform Resource Identifier.
Definition: uri.h:50
Location is a URI.
Definition: open.h:27

References downloader_finished(), downloader::image, image_set_uri(), LOCATION_URI, rc, tmp, type, va_arg, va_copy, va_end, downloader::xfer, and xfer_vreopen().

◆ create_downloader()

int create_downloader ( struct interface job,
struct image image 
)

Instantiate a downloader.

Parameters
jobJob control interface
imageImage to fill with downloaded file
Return values
rcReturn status code

Instantiates a downloader object to download the content of the specified image from its URI.

Definition at line 258 of file downloader.c.

258  {
259  struct downloader *downloader;
260  int rc;
261 
262  /* Allocate and initialise structure */
263  downloader = zalloc ( sizeof ( *downloader ) );
264  if ( ! downloader )
265  return -ENOMEM;
268  &downloader->refcnt );
270  &downloader->refcnt );
272  xferbuf_umalloc_init ( &downloader->buffer, &image->data );
273 
274  /* Instantiate child objects and attach to our interfaces */
275  if ( ( rc = xfer_open_uri ( &downloader->xfer, image->uri ) ) != 0 )
276  goto err;
277 
278  /* Attach parent interface, mortalise self, and return */
280  ref_put ( &downloader->refcnt );
281  return 0;
282 
283  err:
285  ref_put ( &downloader->refcnt );
286  return rc;
287 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
userptr_t data
Raw file image.
Definition: image.h:41
struct image * image
Image to contain downloaded file.
Definition: downloader.c:56
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
int xfer_open_uri(struct interface *intf, struct uri *uri)
Open URI.
Definition: open.c:66
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:196
static struct interface_descriptor downloader_xfer_desc
Downloader data transfer interface descriptor.
Definition: downloader.c:223
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:102
An executable image.
Definition: image.h:24
struct interface job
Job control interface.
Definition: downloader.c:51
#define ENOMEM
Not enough space.
Definition: errno.h:534
static void downloader_finished(struct downloader *downloader, int rc)
Terminate download.
Definition: downloader.c:80
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
A downloader.
Definition: downloader.c:46
struct uri * uri
URI of image.
Definition: image.h:32
struct interface xfer
Data transfer interface.
Definition: downloader.c:53
struct refcnt refcnt
Reference count for this object.
Definition: downloader.c:48
struct xfer_buffer buffer
Data transfer buffer.
Definition: downloader.c:58
static struct interface_descriptor downloader_job_desc
Downloader job control interface descriptor.
Definition: downloader.c:239
static void downloader_free(struct refcnt *refcnt)
Free downloader object.
Definition: downloader.c:66
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:173
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106

References downloader::buffer, image::data, downloader_finished(), downloader_free(), downloader_job_desc, downloader_xfer_desc, ENOMEM, downloader::image, image_get(), intf_init(), intf_plug_plug(), downloader::job, rc, ref_init, ref_put, downloader::refcnt, image::uri, downloader::xfer, xfer_open_uri(), and zalloc().

Referenced by imgdownload().

Variable Documentation

◆ downloader_xfer_operations

struct interface_operation downloader_xfer_operations[]
static
Initial value:
= {
}
static struct xfer_buffer * downloader_buffer(struct downloader *downloader)
Get underlying data transfer buffer.
Definition: downloader.c:170
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
A data transfer buffer.
Definition: xferbuf.h:19
int xfer_vredirect(struct interface *intf, int type, va_list args)
Send redirection event.
Definition: xfer.c:62
static void downloader_finished(struct downloader *downloader, int rc)
Terminate download.
Definition: downloader.c:80
A downloader.
Definition: downloader.c:46
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193
static int downloader_deliver(struct downloader *downloader, struct io_buffer *iobuf, struct xfer_metadata *meta)
Handle received data.
Definition: downloader.c:146
static int downloader_vredirect(struct downloader *downloader, int type, va_list args)
Redirect data transfer interface.
Definition: downloader.c:184

Downloader data transfer interface operations.

Definition at line 215 of file downloader.c.

◆ downloader_xfer_desc

struct interface_descriptor downloader_xfer_desc
static
Initial value:
=
A downloader.
Definition: downloader.c:46
struct interface xfer
Data transfer interface.
Definition: downloader.c:53
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:65
static struct interface_operation downloader_xfer_operations[]
Downloader data transfer interface operations.
Definition: downloader.c:215

Downloader data transfer interface descriptor.

Definition at line 223 of file downloader.c.

Referenced by create_downloader().

◆ downloader_job_op

struct interface_operation downloader_job_op[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
static void downloader_finished(struct downloader *downloader, int rc)
Terminate download.
Definition: downloader.c:80
A downloader.
Definition: downloader.c:46
Job progress.
Definition: job.h:15
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
static int downloader_progress(struct downloader *downloader, struct job_progress *progress)
Report progress of download job.
Definition: downloader.c:112

Downloader job control interface operations.

Definition at line 233 of file downloader.c.

◆ downloader_job_desc

struct interface_descriptor downloader_job_desc
static
Initial value:
=
struct interface job
Job control interface.
Definition: downloader.c:51
A downloader.
Definition: downloader.c:46
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:65
static struct interface_operation downloader_job_op[]
Downloader job control interface operations.
Definition: downloader.c:233

Downloader job control interface descriptor.

Definition at line 239 of file downloader.c.

Referenced by create_downloader().