Skip to content

Commit

Permalink
Fix: misunderstanding of maxbytes and read return
Browse files Browse the repository at this point in the history
  • Loading branch information
mmege committed Mar 8, 2024
1 parent 451b2cf commit 9e1f49e
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions handlers/delta_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "swupdate_image.h"

#define DEFAULT_MAX_RANGES 150 /* Apache has default = 200 */
#define BUFF_SIZE 16384

const char *handlername = "delta";
void delta_handler(void);
Expand Down Expand Up @@ -470,32 +471,54 @@ static void zck_log_toswupdate(const char *function, zck_log_type lt,

/*
* Create a zck Index from a file
*
* If maxbytes has been set, it acts as a limit for the input data.
* If not (i.e. maxbytes==0), all the file/dev available data is used.
*/
static bool create_zckindex(zckCtx *zck, int fd, size_t maxbytes)
{
const size_t bufsize = 16384;
char *buf = malloc(bufsize);
ssize_t n;
int ret;
ssize_t n = 0;
size_t count = 0;
size_t buffsize = BUFF_SIZE;
bool rstatus = true;
char *buff = NULL;

if (!buf) {
if (!(buff = malloc(BUFF_SIZE))) {
ERROR("OOM creating temporary buffer");
return false;
}
while ((n = read(fd, buf, bufsize)) > 0) {
ret = zck_write(zck, buf, n);
if (ret < 0) {
ERROR("ZCK returns %s", zck_get_error(zck));
free(buf);
return false;

do {
if((n = read(fd, buff, buffsize)) < 0) {
ERROR("Error occurred while reading data : %s", strerror(zck));
rstatus = false;
break;
}
if (maxbytes && n > maxbytes)

if (zck_write(zck, buff, n) < 0) {
ERROR("ZCK returns %s", zck_get_error(zck));
rstatus = false;
break;
}
}

free(buf);
if(maxbytes) {
/* Keep count only if maxbytes has been set and it's significant*/
count += n;

return true;
/* Stop if limit is reached*/
if (count >= maxbytes) {
break;
}

/* Be sure read up to maxbytes limit next time */
if (BUFF_SIZE > (maxbytes - count)) {
buffsize = maxbytes - count;
}
}
} while (!n); // Keep reading until the end of file

free(buff);
return rstatus;
}

/*
Expand Down

0 comments on commit 9e1f49e

Please sign in to comment.