Changeset 9621c7d in mainline for uspace/app/download/main.c


Ignore:
Timestamp:
2017-08-24T16:43:24Z (7 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ac415d50
Parents:
258d77e
Message:

Add simple coastline package installer. Add downloader option to save output to file.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/download/main.c

    r258d77e r9621c7d  
    5757static void syntax_print(void)
    5858{
    59         fprintf(stderr, "Usage: download <url>\n");
    60         fprintf(stderr, "  This will write the data to stdout, so you may want\n");
     59        fprintf(stderr, "Usage: download [-o <outfile>] <url>\n");
     60        fprintf(stderr, "  Without -o, data will be written to stdout, so you may want\n");
    6161        fprintf(stderr, "  to redirect the output, e.g.\n");
    6262        fprintf(stderr, "\n");
     
    6666int main(int argc, char *argv[])
    6767{
    68         if (argc != 2) {
     68        int i;
     69        char *ofname = NULL;
     70        FILE *ofile = NULL;
     71        size_t buf_size = 4096;
     72        void *buf = NULL;
     73        uri_t *uri = NULL;
     74        int rc;
     75
     76        if (argc < 2) {
    6977                syntax_print();
    70                 return 2;
    71         }
    72        
    73         uri_t *uri = uri_parse(argv[1]);
     78                rc = EINVAL;
     79                goto error;
     80        }
     81       
     82        i = 1;
     83       
     84        if (str_cmp(argv[i], "-o") == 0) {
     85                ++i;
     86                if (argc < i + 1) {
     87                        syntax_print();
     88                        rc = EINVAL;
     89                        goto error;
     90                }
     91               
     92                ofname = argv[i++];
     93                ofile = fopen(ofname, "wb");
     94                if (ofile == NULL) {
     95                        fprintf(stderr, "Error creating '%s'.\n", ofname);
     96                        rc = EINVAL;
     97                        goto error;
     98                }
     99        }
     100       
     101        if (argc != i + 1) {
     102                syntax_print();
     103                rc = EINVAL;
     104                goto error;
     105        }
     106       
     107        uri = uri_parse(argv[i]);
    74108        if (uri == NULL) {
    75109                fprintf(stderr, "Failed parsing URI\n");
    76                 return 2;
     110                rc = EINVAL;
     111                goto error;
    77112        }
    78113       
    79114        if (!uri_validate(uri)) {
    80115                fprintf(stderr, "The URI is invalid\n");
    81                 return 2;
     116                rc = EINVAL;
     117                goto error;
    82118        }
    83119       
     
    86122        if (str_cmp(uri->scheme, "http") != 0) {
    87123                fprintf(stderr, "Only http scheme is supported at the moment\n");
    88                 return 2;
     124                rc = EINVAL;
     125                goto error;
    89126        }
    90127       
    91128        if (uri->host == NULL) {
    92129                fprintf(stderr, "host not set\n");
    93                 return 2;
     130                rc = EINVAL;
     131                goto error;
    94132        }
    95133       
    96134        uint16_t port = 80;
    97135        if (uri->port != NULL) {
    98                 int rc = str_uint16_t(uri->port, NULL, 10, true, &port);
     136                rc = str_uint16_t(uri->port, NULL, 10, true, &port);
    99137                if (rc != EOK) {
    100138                        fprintf(stderr, "Invalid port number: %s\n", uri->port);
    101                         return 2;
     139                        rc = EINVAL;
     140                        goto error;
    102141                }
    103142        }
     
    111150                if (server_path == NULL) {
    112151                        fprintf(stderr, "Failed allocating path\n");
    113                         uri_destroy(uri);
    114                         return 3;
    115                 }
    116         }
    117         else {
    118                 int rc = asprintf(&server_path, "%s?%s", path, uri->query);
     152                        rc = ENOMEM;
     153                        goto error;
     154                }
     155        } else {
     156                rc = asprintf(&server_path, "%s?%s", path, uri->query);
    119157                if (rc < 0) {
    120158                        fprintf(stderr, "Failed allocating path\n");
    121                         uri_destroy(uri);
    122                         return 3;
     159                        rc = ENOMEM;
     160                        goto error;
    123161                }
    124162        }
     
    128166        if (req == NULL) {
    129167                fprintf(stderr, "Failed creating request\n");
    130                 uri_destroy(uri);
    131                 return 3;
    132         }
    133        
    134         int rc = http_headers_append(&req->headers, "Host", uri->host);
     168                rc = ENOMEM;
     169                goto error;
     170        }
     171       
     172        rc = http_headers_append(&req->headers, "Host", uri->host);
    135173        if (rc != EOK) {
    136174                fprintf(stderr, "Failed setting Host header: %s\n", str_error(rc));
    137                 uri_destroy(uri);
    138                 return rc;
     175                goto error;
    139176        }
    140177       
     
    142179        if (rc != EOK) {
    143180                fprintf(stderr, "Failed creating User-Agent header: %s\n", str_error(rc));
    144                 uri_destroy(uri);
    145                 return rc;
     181                goto error;
    146182        }
    147183       
    148184        http_t *http = http_create(uri->host, port);
    149185        if (http == NULL) {
    150                 uri_destroy(uri);
    151186                fprintf(stderr, "Failed creating HTTP object\n");
    152                 return 3;
     187                rc = ENOMEM;
     188                goto error;
    153189        }
    154190       
     
    156192        if (rc != EOK) {
    157193                fprintf(stderr, "Failed connecting: %s\n", str_error(rc));
    158                 uri_destroy(uri);
    159                 return rc;
     194                rc = EIO;
     195                goto error;
    160196        }
    161197       
     
    163199        if (rc != EOK) {
    164200                fprintf(stderr, "Failed sending request: %s\n", str_error(rc));
    165                 uri_destroy(uri);
    166                 return rc;
     201                rc = EIO;
     202                goto error;
    167203        }
    168204       
     
    172208        if (rc != EOK) {
    173209                fprintf(stderr, "Failed receiving response: %s\n", str_error(rc));
    174                 uri_destroy(uri);
    175                 return rc;
     210                rc = EIO;
     211                goto error;
    176212        }
    177213       
     
    179215                fprintf(stderr, "Server returned status %d %s\n", response->status,
    180216                    response->message);
    181         }
    182         else {
    183                 size_t buf_size = 4096;
    184                 void *buf = malloc(buf_size);
     217        } else {
     218                buf = malloc(buf_size);
    185219                if (buf == NULL) {
    186220                        fprintf(stderr, "Failed allocating buffer\n)");
    187                         uri_destroy(uri);
    188                         return ENOMEM;
     221                        rc = ENOMEM;
     222                        goto error;
    189223                }
    190224               
    191225                int body_size;
    192226                while ((body_size = recv_buffer(&http->recv_buffer, buf, buf_size)) > 0) {
    193                         fwrite(buf, 1, body_size, stdout);
     227                        fwrite(buf, 1, body_size, ofile != NULL ? ofile : stdout);
    194228                }
    195229               
     
    199233        }
    200234       
     235        free(buf);
    201236        uri_destroy(uri);
     237        if (fclose(ofile) != 0) {
     238                printf("Error writing '%s'.\n", ofname);
     239                return EIO;
     240        }
     241
    202242        return EOK;
     243error:
     244        free(buf);
     245        if (uri != NULL)
     246                uri_destroy(uri);
     247        if (ofile != NULL)
     248                fclose(ofile);
     249        return rc;
    203250}
    204251
Note: See TracChangeset for help on using the changeset viewer.