Added minimum transfer size in TransferDataStruct

This commit is contained in:
Fufu Fang 2021-09-01 03:53:19 +01:00
parent 08c1eeba49
commit 95b86825ed
6 changed files with 31 additions and 26 deletions

View File

@ -901,7 +901,7 @@ EXCLUDE_PATTERNS =
# Note that the wildcards are matched against the file with absolute path, so to # Note that the wildcards are matched against the file with absolute path, so to
# exclude all test directories use the pattern */test/* # exclude all test directories use the pattern */test/*
EXCLUDE_SYMBOLS = CALLOC lprintf EXCLUDE_SYMBOLS = CALLOC lprintf FREE
# The EXAMPLE_PATH tag can be used to specify one or more files or directories # The EXAMPLE_PATH tag can be used to specify one or more files or directories
# that contain example code fragments that are included (see the \include # that contain example code fragments that are included (see the \include

View File

@ -113,7 +113,7 @@ static void Link_req_file_stat(Link * this_link)
* *
* It gets freed in curl_multi_perform_once(); * It gets freed in curl_multi_perform_once();
*/ */
TransferStruct *transfer = CALLOC(1, sizeof(TransferStruct)); TransferStatusStruct *transfer = CALLOC(1, sizeof(TransferStatusStruct));
transfer->link = this_link; transfer->link = this_link;
transfer->type = FILESTAT; transfer->type = FILESTAT;
@ -434,12 +434,12 @@ void LinkTable_print(LinkTable * linktbl)
} }
} }
DataStruct Link_to_DataStruct(Link * head_link) TransferDataStruct Link_to_TransferDataStruct(Link * head_link)
{ {
char *url = head_link->f_url; char *url = head_link->f_url;
CURL *curl = Link_to_curl(head_link); CURL *curl = Link_to_curl(head_link);
DataStruct buf; TransferDataStruct buf;
buf.size = 0; buf.size = 0;
buf.data = NULL; buf.data = NULL;
@ -493,7 +493,7 @@ LinkTable *LinkTable_new(const char *url)
/* /*
* start downloading the base URL * start downloading the base URL
*/ */
DataStruct buf = Link_to_DataStruct(linktbl->links[0]); TransferDataStruct buf = Link_to_TransferDataStruct(linktbl->links[0]);
if (buf.size == 0) { if (buf.size == 0) {
LinkTable_free(linktbl); LinkTable_free(linktbl);
return NULL; return NULL;
@ -792,7 +792,7 @@ Link *path_to_Link(const char *path)
} }
long long
path_download(const char *path, char *output_buf, size_t size, path_download(const char *path, char *output_buf, size_t req_size,
off_t offset) off_t offset)
{ {
if (!path) { if (!path) {
@ -805,12 +805,12 @@ path_download(const char *path, char *output_buf, size_t size,
} }
size_t start = offset; size_t start = offset;
size_t end = start + size; size_t end = start + req_size;
char range_str[64]; char range_str[64];
snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end); snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end);
lprintf(debug, "path_download(%s, %s);\n", path, range_str); lprintf(debug, "path_download(%s, %s);\n", path, range_str);
DataStruct buf; TransferDataStruct buf;
buf.size = 0; buf.size = 0;
buf.data = NULL; buf.data = NULL;
@ -818,7 +818,7 @@ path_download(const char *path, char *output_buf, size_t size,
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &buf); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &buf);
curl_easy_setopt(curl, CURLOPT_RANGE, range_str); curl_easy_setopt(curl, CURLOPT_RANGE, range_str);
DataStruct header; TransferDataStruct header;
header.size = 0; header.size = 0;
header.data = NULL; header.data = NULL;
curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *) &header); curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *) &header);
@ -849,18 +849,19 @@ range requests\n");
return -ENOENT; return -ENOENT;
} }
double dl; curl_off_t recv;
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &dl); curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD_T, &recv);
size_t recv = dl; /* The extra 1 byte is probably for '\0' */
if (recv > size) { if (recv - 1 == (long int) req_size) {
recv = size; recv--;
} else {
lprintf(error, "req_size: %lu, recv: %ld\n", req_size, recv);
} }
memmove(output_buf, buf.data, recv); memmove(output_buf, buf.data, recv);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
FREE(buf.data); FREE(buf.data);
lprintf(debug, "recv: %lu bytes\n", recv);
return recv; return recv;
} }

View File

@ -23,11 +23,15 @@ typedef enum {
LINK_UNINITIALISED_FILE = 'U' LINK_UNINITIALISED_FILE = 'U'
} LinkType; } LinkType;
/** \brief for storing downloaded data in memory */ /** \brief For storing transfer data */
typedef struct { typedef struct {
/** \brief The array to store the data */
char *data; char *data;
/** \brief The size of the array */
size_t size; size_t size;
} DataStruct; /** \brief The minium requested size */
size_t min_req_size;
} TransferDataStruct;
/** \brief specify the type of data transfer */ /** \brief specify the type of data transfer */
typedef enum { typedef enum {
@ -35,12 +39,12 @@ typedef enum {
DATA = 'd' DATA = 'd'
} TransferType; } TransferType;
/** \brief for storing the link being transferred, and metadata */ /** \brief For storing transfer status and metadata */
typedef struct { typedef struct {
TransferType type; TransferType type;
int transferring; int transferring;
Link *link; Link *link;
} TransferStruct; } TransferStatusStruct;
/** /**
* \brief link table type * \brief link table type
@ -145,9 +149,9 @@ LinkTable *LinkTable_disk_open(const char *dirn);
/** /**
* \brief Download a link's content to the memory * \brief Download a link's content to the memory
* \warning You MUST free the memory field in DataStruct after use! * \warning You MUST free the memory field in TransferDataStruct after use!
*/ */
DataStruct Link_to_DataStruct(Link * head_link); TransferDataStruct Link_to_TransferDataStruct(Link * head_link);
/** /**
* \brief Allocate a LinkTable * \brief Allocate a LinkTable

View File

@ -18,7 +18,7 @@ typedef enum {
/** /**
* \brief The default log level * \brief The default log level
*/ */
#define DEFAULT_LOG_LEVEL fatal | error | warning | info #define DEFAULT_LOG_LEVEL fatal | error | warning | info | debug
/** /**
* \brief Get the log level from the environment. * \brief Get the log level from the environment.

View File

@ -117,7 +117,7 @@ curl_process_msgs(CURLMsg * curl_msg, int n_running_curl, int n_mesgs)
(void) n_mesgs; (void) n_mesgs;
static volatile int slept = 0; static volatile int slept = 0;
if (curl_msg->msg == CURLMSG_DONE) { if (curl_msg->msg == CURLMSG_DONE) {
TransferStruct *transfer; TransferStatusStruct *transfer;
CURL *curl = curl_msg->easy_handle; CURL *curl = curl_msg->easy_handle;
curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE, curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE,
&transfer); &transfer);
@ -311,7 +311,7 @@ void transfer_blocking(CURL * curl)
* We don't need to malloc here, as the transfer is finished before * We don't need to malloc here, as the transfer is finished before
* the variable gets popped from the stack * the variable gets popped from the stack
*/ */
volatile TransferStruct transfer; volatile TransferStatusStruct transfer;
transfer.type = DATA; transfer.type = DATA;
transfer.transferring = 1; transfer.transferring = 1;
curl_easy_setopt(curl, CURLOPT_PRIVATE, &transfer); curl_easy_setopt(curl, CURLOPT_PRIVATE, &transfer);
@ -357,7 +357,7 @@ write_memory_callback(void *contents, size_t size, size_t nmemb,
void *userp) void *userp)
{ {
size_t realsize = size * nmemb; size_t realsize = size * nmemb;
DataStruct *mem = (DataStruct *) userp; TransferDataStruct *mem = (TransferDataStruct *) userp;
mem->data = realloc(mem->data, mem->size + realsize + 1); mem->data = realloc(mem->data, mem->size + realsize + 1);
if (!mem->data) { if (!mem->data) {

View File

@ -332,7 +332,7 @@ static LinkTable *sonic_url_to_LinkTable(const char *url,
/* /*
* start downloading the base URL * start downloading the base URL
*/ */
DataStruct xml = Link_to_DataStruct(linktbl->links[0]); TransferDataStruct xml = Link_to_TransferDataStruct(linktbl->links[0]);
if (xml.size == 0) { if (xml.size == 0) {
LinkTable_free(linktbl); LinkTable_free(linktbl);
return NULL; return NULL;