curl.c (456af346297ebef86aa097b3609534d34f3d2f75) | curl.c (3ce6a729b5d78b13283ddc6c529811f67519a62d) |
---|---|
1/* 2 * QEMU Block driver for CURL images 3 * 4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights --- 442 unchanged lines hidden (view full) --- 451 curl_multi_check_completion(s); 452 qemu_mutex_unlock(&s->mutex); 453#else 454 abort(); 455#endif 456} 457 458/* Called with s->mutex held. */ | 1/* 2 * QEMU Block driver for CURL images 3 * 4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights --- 442 unchanged lines hidden (view full) --- 451 curl_multi_check_completion(s); 452 qemu_mutex_unlock(&s->mutex); 453#else 454 abort(); 455#endif 456} 457 458/* Called with s->mutex held. */ |
459static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s) | 459static CURLState *curl_find_state(BDRVCURLState *s) |
460{ 461 CURLState *state = NULL; | 460{ 461 CURLState *state = NULL; |
462 int i, j; | 462 int i; |
463 | 463 |
464 do { 465 for (i=0; i<CURL_NUM_STATES; i++) { 466 for (j=0; j<CURL_NUM_ACB; j++) 467 if (s->states[i].acb[j]) 468 continue; 469 if (s->states[i].in_use) 470 continue; 471 | 464 for (i = 0; i < CURL_NUM_STATES; i++) { 465 if (!s->states[i].in_use) { |
472 state = &s->states[i]; 473 state->in_use = 1; 474 break; 475 } | 466 state = &s->states[i]; 467 state->in_use = 1; 468 break; 469 } |
476 if (!state) { 477 qemu_mutex_unlock(&s->mutex); 478 aio_poll(bdrv_get_aio_context(bs), true); 479 qemu_mutex_lock(&s->mutex); 480 } 481 } while(!state); | 470 } 471 return state; 472} |
482 | 473 |
474static int curl_init_state(BDRVCURLState *s, CURLState *state) 475{ |
|
483 if (!state->curl) { 484 state->curl = curl_easy_init(); 485 if (!state->curl) { | 476 if (!state->curl) { 477 state->curl = curl_easy_init(); 478 if (!state->curl) { |
486 return NULL; | 479 return -EIO; |
487 } 488 curl_easy_setopt(state->curl, CURLOPT_URL, s->url); 489 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, 490 (long) s->sslverify); 491 if (s->cookie) { 492 curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie); 493 } 494 curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout); --- 36 unchanged lines hidden (view full) --- 531#ifdef DEBUG_VERBOSE 532 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); 533#endif 534 } 535 536 QLIST_INIT(&state->sockets); 537 state->s = s; 538 | 480 } 481 curl_easy_setopt(state->curl, CURLOPT_URL, s->url); 482 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, 483 (long) s->sslverify); 484 if (s->cookie) { 485 curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie); 486 } 487 curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout); --- 36 unchanged lines hidden (view full) --- 524#ifdef DEBUG_VERBOSE 525 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); 526#endif 527 } 528 529 QLIST_INIT(&state->sockets); 530 state->s = s; 531 |
539 return state; | 532 return 0; |
540} 541 542/* Called with s->mutex held. */ 543static void curl_clean_state(CURLState *s) 544{ 545 int j; 546 for (j = 0; j < CURL_NUM_ACB; j++) { 547 assert(!s->acb[j]); --- 225 unchanged lines hidden (view full) --- 773 curl_global_init(CURL_GLOBAL_ALL); 774 inited = 1; 775 } 776 777 DPRINTF("CURL: Opening %s\n", file); 778 s->aio_context = bdrv_get_aio_context(bs); 779 s->url = g_strdup(file); 780 qemu_mutex_lock(&s->mutex); | 533} 534 535/* Called with s->mutex held. */ 536static void curl_clean_state(CURLState *s) 537{ 538 int j; 539 for (j = 0; j < CURL_NUM_ACB; j++) { 540 assert(!s->acb[j]); --- 225 unchanged lines hidden (view full) --- 766 curl_global_init(CURL_GLOBAL_ALL); 767 inited = 1; 768 } 769 770 DPRINTF("CURL: Opening %s\n", file); 771 s->aio_context = bdrv_get_aio_context(bs); 772 s->url = g_strdup(file); 773 qemu_mutex_lock(&s->mutex); |
781 state = curl_init_state(bs, s); | 774 state = curl_find_state(s); |
782 qemu_mutex_unlock(&s->mutex); | 775 qemu_mutex_unlock(&s->mutex); |
783 if (!state) | 776 if (!state) { |
784 goto out_noclean; | 777 goto out_noclean; |
778 } |
|
785 786 // Get file size 787 | 779 780 // Get file size 781 |
782 if (curl_init_state(s, state) < 0) { 783 goto out; 784 } 785 |
|
788 s->accept_range = false; 789 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); 790 curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, 791 curl_header_cb); 792 curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); 793 if (curl_easy_perform(state->curl)) 794 goto out; 795 if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { --- 78 unchanged lines hidden (view full) --- 874 goto out; 875 case FIND_RET_WAIT: 876 goto out; 877 default: 878 break; 879 } 880 881 // No cache found, so let's start a new request | 786 s->accept_range = false; 787 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); 788 curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, 789 curl_header_cb); 790 curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); 791 if (curl_easy_perform(state->curl)) 792 goto out; 793 if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { --- 78 unchanged lines hidden (view full) --- 872 goto out; 873 case FIND_RET_WAIT: 874 goto out; 875 default: 876 break; 877 } 878 879 // No cache found, so let's start a new request |
882 state = curl_init_state(acb->common.bs, s); 883 if (!state) { | 880 for (;;) { 881 state = curl_find_state(s); 882 if (state) { 883 break; 884 } 885 qemu_mutex_unlock(&s->mutex); 886 aio_poll(bdrv_get_aio_context(bs), true); 887 qemu_mutex_lock(&s->mutex); 888 } 889 890 if (curl_init_state(s, state) < 0) { 891 curl_clean_state(state); |
884 ret = -EIO; 885 goto out; 886 } 887 888 acb->start = 0; 889 acb->end = MIN(acb->nb_sectors * BDRV_SECTOR_SIZE, s->len - start); 890 891 state->buf_off = 0; --- 137 unchanged lines hidden --- | 892 ret = -EIO; 893 goto out; 894 } 895 896 acb->start = 0; 897 acb->end = MIN(acb->nb_sectors * BDRV_SECTOR_SIZE, s->len - start); 898 899 state->buf_off = 0; --- 137 unchanged lines hidden --- |