diff mbox series

[13/29] curl: add a noipc parameter

Message ID 20211011112156.44192-14-sbabic@denx.de
State Changes Requested
Headers show
Series DELTA Update | expand

Commit Message

Stefano Babic Oct. 11, 2021, 11:21 a.m. UTC
The get_file() function sends data to IPC to install the SWU. To make
channel() more generic, add a parameter to control if the incoming data
must be forwarded to the IPC.

This allows to use get_file() in other contexts, providing an own
callback to handle the stream and the curl callback in channel_curl.c
becomes a proxy that simply forwards the data to a supplied callback as
"dwlwrdata" in the channel_data_t structure.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 corelib/channel_curl.c | 50 +++++++++++++++++++++++-------------------
 include/channel_curl.h |  1 +
 2 files changed, 28 insertions(+), 23 deletions(-)

Comments

Dominique MARTINET Oct. 11, 2021, 11:25 p.m. UTC | #1
Stefano Babic wrote on Mon, Oct 11, 2021 at 01:21:40PM +0200:
> The get_file() function sends data to IPC to install the SWU. To make
> channel() more generic, add a parameter to control if the incoming data
> must be forwarded to the IPC.
> 
> This allows to use get_file() in other contexts, providing an own
> callback to handle the stream and the curl callback in channel_curl.c
> becomes a proxy that simply forwards the data to a supplied callback as
> "dwlwrdata" in the channel_data_t structure.
> 
> Signed-off-by: Stefano Babic <sbabic@denx.de>
> ---
>  corelib/channel_curl.c | 50 +++++++++++++++++++++++-------------------
>  include/channel_curl.h |  1 +
>  2 files changed, 28 insertions(+), 23 deletions(-)
> 
> diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
> index 0636efc..be553f0 100644
> --- a/corelib/channel_curl.c
> +++ b/corelib/channel_curl.c
> @@ -1305,7 +1309,7 @@ cleanup_file:
>  	 *      so use close() here directly to issue an error in case.
>  	 *      Also, for a given file handle, calling ipc_end() would make
>  	 *      no semantic sense. */
> -	if (close(file_handle) != 0) {
> +	if (file_handle > 0 && close(file_handle) != 0) {

should be >= 0 as 0 is a valid fd number
Stefano Babic Oct. 12, 2021, 8:49 a.m. UTC | #2
On 12.10.21 01:25, Dominique MARTINET wrote:
> Stefano Babic wrote on Mon, Oct 11, 2021 at 01:21:40PM +0200:
>> The get_file() function sends data to IPC to install the SWU. To make
>> channel() more generic, add a parameter to control if the incoming data
>> must be forwarded to the IPC.
>>
>> This allows to use get_file() in other contexts, providing an own
>> callback to handle the stream and the curl callback in channel_curl.c
>> becomes a proxy that simply forwards the data to a supplied callback as
>> "dwlwrdata" in the channel_data_t structure.
>>
>> Signed-off-by: Stefano Babic <sbabic@denx.de>
>> ---
>>   corelib/channel_curl.c | 50 +++++++++++++++++++++++-------------------
>>   include/channel_curl.h |  1 +
>>   2 files changed, 28 insertions(+), 23 deletions(-)
>>
>> diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
>> index 0636efc..be553f0 100644
>> --- a/corelib/channel_curl.c
>> +++ b/corelib/channel_curl.c
>> @@ -1305,7 +1309,7 @@ cleanup_file:
>>   	 *      so use close() here directly to issue an error in case.
>>   	 *      Also, for a given file handle, calling ipc_end() would make
>>   	 *      no semantic sense. */
>> -	if (close(file_handle) != 0) {
>> +	if (file_handle > 0 && close(file_handle) != 0) {
> 
> should be >= 0 as 0 is a valid fd number
> 

Thanks, I will fix in V2.

Regards,
Stefano
diff mbox series

Patch

diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
index 0636efc..be553f0 100644
--- a/corelib/channel_curl.c
+++ b/corelib/channel_curl.c
@@ -184,7 +184,8 @@  size_t channel_callback_ipc(void *streamdata, size_t size, size_t nmemb,
 		}
 	}
 
-	if (ipc_send_data(data->output, streamdata, (int)(size * nmemb)) <
+	if (!data->channel_data->noipc &&
+		ipc_send_data(data->output, streamdata, (int)(size * nmemb)) <
 	    0) {
 		ERROR("Writing into SWUpdate IPC stream failed.");
 		result_channel_callback_ipc = CHANNEL_EIO;
@@ -1083,7 +1084,8 @@  channel_op_res_t channel_put(channel_t *this, void *data)
 channel_op_res_t channel_get_file(channel_t *this, void *data)
 {
 	channel_curl_t *channel_curl = this->priv;
-	int file_handle;
+	int file_handle = -1;
+	struct swupdate_request req;
 	assert(data != NULL);
 	assert(channel_curl->handle != NULL);
 
@@ -1143,30 +1145,32 @@  channel_op_res_t channel_get_file(channel_t *this, void *data)
 		goto cleanup_header;
 	}
 
-	struct swupdate_request req;
-	swupdate_prepare_req(&req);
-	req.dry_run = channel_data->dry_run;
-	req.source = channel_data->source;
-	if (channel_data->info) {
-		strncpy(req.info, channel_data->info,
-			  sizeof(req.info) - 1 );
-	}
-	for (int retries = 3; retries >= 0; retries--) {
-		file_handle = ipc_inst_start_ext( &req, sizeof(struct swupdate_request));
-		if (file_handle > 0)
-			break;
-		sleep(1);
-	}
-	if (file_handle < 0) {
-		ERROR("Cannot open SWUpdate IPC stream: %s", strerror(errno));
-		result = CHANNEL_EIO;
-		goto cleanup_header;
-	}
-
 	write_callback_t wrdata;
 	wrdata.channel_data = channel_data;
+	if (!channel_data->noipc) {
+		swupdate_prepare_req(&req);
+		req.dry_run = channel_data->dry_run;
+		req.source = channel_data->source;
+		if (channel_data->info) {
+			strncpy(req.info, channel_data->info,
+				sizeof(req.info) - 1 );
+		}
+		for (int retries = 3; retries >= 0; retries--) {
+			file_handle = ipc_inst_start_ext( &req, sizeof(struct swupdate_request));
+			if (file_handle > 0)
+				break;
+			sleep(1);
+		}
+		if (file_handle < 0) {
+			ERROR("Cannot open SWUpdate IPC stream: %s", strerror(errno));
+			result = CHANNEL_EIO;
+			goto cleanup_header;
+		}
+	}
+
 	wrdata.output = file_handle;
 	result_channel_callback_ipc = CHANNEL_OK;
+
 	if ((curl_easy_setopt(channel_curl->handle, CURLOPT_WRITEFUNCTION,
 			      channel_callback_ipc) != CURLE_OK) ||
 	    (curl_easy_setopt(channel_curl->handle, CURLOPT_WRITEDATA,
@@ -1305,7 +1309,7 @@  cleanup_file:
 	 *      so use close() here directly to issue an error in case.
 	 *      Also, for a given file handle, calling ipc_end() would make
 	 *      no semantic sense. */
-	if (close(file_handle) != 0) {
+	if (file_handle > 0 && close(file_handle) != 0) {
 		ERROR("Channel error while closing download target handle: '%s'",
 		      strerror(errno));
 	}
diff --git a/include/channel_curl.h b/include/channel_curl.h
index 49d5242..fe68a99 100644
--- a/include/channel_curl.h
+++ b/include/channel_curl.h
@@ -66,6 +66,7 @@  typedef struct {
 	bool usessl;
 	bool strictssl;
 	bool nocheckanswer;
+	bool noipc;	/* do not send to SWUpdate IPC if set */
 	long http_response_code;
 	bool nofollow;
 	size_t (*dwlwrdata)(char *streamdata, size_t size, size_t nmemb,