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.
static void downloader_finished (struct downloader *downloader, int rc)
 Terminate download.
static int downloader_progress (struct downloader *downloader, struct job_progress *progress)
 Report progress of download job.
static int downloader_deliver (struct downloader *downloader, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Handle received data.
static struct xfer_bufferdownloader_buffer (struct downloader *downloader)
 Get underlying data transfer buffer.
static int downloader_vredirect (struct downloader *downloader, int type, va_list args)
 Redirect data transfer interface.
int create_downloader (struct interface *job, struct image *image)
 Instantiate a downloader.

Variables

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

Detailed Description

Image downloader.

Definition in file downloader.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static void downloader_free ( struct refcnt refcnt) [static]

Free downloader object.

Parameters:
refcntDownloader reference counter

Definition at line 66 of file downloader.c.

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

Referenced by create_downloader().

                                                      {
        struct downloader *downloader =
                container_of ( refcnt, struct downloader, refcnt );

        image_put ( downloader->image );
        free ( downloader );
}
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.

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

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

                                                                          {

        /* Log download status */
        if ( rc == 0 ) {
                syslog ( LOG_NOTICE, "Downloaded \"%s\"\n",
                         downloader->image->name );
        } else {
                syslog ( LOG_ERR, "Download of \"%s\" failed: %s\n",
                         downloader->image->name, strerror ( rc ) );
        }

        /* Update image length */
        downloader->image->len = downloader->buffer.len;

        /* Shut down interfaces */
        intf_shutdown ( &downloader->xfer, rc );
        intf_shutdown ( &downloader->job, rc );
}
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.

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

                                                                 {
        int rc;

        /* Allow data transfer to provide an accurate description */
        if ( ( rc = job_progress ( &downloader->xfer, progress ) ) != 0 )
                return rc;

        /* This is not entirely accurate, since downloaded data may
         * arrive out of order (e.g. with multicast protocols), but
         * it's a reasonable first approximation.
         */
        if ( ! progress->total ) {
                progress->completed = downloader->buffer.pos;
                progress->total = downloader->buffer.len;
        }

        return 0;
}
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.

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

                                                             {
        int rc;

        /* Add data to buffer */
        if ( ( rc = xferbuf_deliver ( &downloader->buffer, iob_disown ( iobuf ),
                                      meta ) ) != 0 )
                goto err_deliver;

        return 0;

 err_deliver:
        downloader_finished ( downloader, rc );
        return rc;
}
static struct xfer_buffer* downloader_buffer ( struct downloader downloader) [static, read]

Get underlying data transfer buffer.

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

Definition at line 170 of file downloader.c.

References downloader::buffer.

                                                    {

        /* Provide direct access to underlying data transfer buffer */
        return &downloader->buffer;
}
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.

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

                                                 {
        va_list tmp;
        struct uri *uri;
        int rc;

        /* Intercept redirects to a LOCATION_URI and update the image URI */
        if ( type == LOCATION_URI ) {

                /* Extract URI argument */
                va_copy ( tmp, args );
                uri = va_arg ( tmp, struct uri * );
                va_end ( tmp );

                /* Set image URI */
                if ( ( rc = image_set_uri ( downloader->image, uri ) ) != 0 )
                        goto err;
        }

        /* Redirect to new location */
        if ( ( rc = xfer_vreopen ( &downloader->xfer, type, args ) ) != 0 )
                goto err;

        return 0;

 err:
        downloader_finished ( downloader, rc );
        return rc;
}
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.

References downloader::buffer, image::data, downloader_finished(), downloader_free(), 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().

                                                                     {
        struct downloader *downloader;
        int rc;

        /* Allocate and initialise structure */
        downloader = zalloc ( sizeof ( *downloader ) );
        if ( ! downloader )
                return -ENOMEM;
        ref_init ( &downloader->refcnt, downloader_free );
        intf_init ( &downloader->job, &downloader_job_desc,
                    &downloader->refcnt );
        intf_init ( &downloader->xfer, &downloader_xfer_desc,
                    &downloader->refcnt );
        downloader->image = image_get ( image );
        xferbuf_umalloc_init ( &downloader->buffer, &image->data );

        /* Instantiate child objects and attach to our interfaces */
        if ( ( rc = xfer_open_uri ( &downloader->xfer, image->uri ) ) != 0 )
                goto err;

        /* Attach parent interface, mortalise self, and return */
        intf_plug_plug ( &downloader->job, job );
        ref_put ( &downloader->refcnt );
        return 0;

 err:
        downloader_finished ( downloader, rc );
        ref_put ( &downloader->refcnt );
        return rc;
}

Variable Documentation

Initial value:

Downloader data transfer interface operations.

Definition at line 215 of file downloader.c.

Initial value:

Downloader data transfer interface descriptor.

Definition at line 223 of file downloader.c.

Initial value:

Downloader job control interface operations.

Definition at line 233 of file downloader.c.

Initial value:

Downloader job control interface descriptor.

Definition at line 239 of file downloader.c.