@@ -168,24 +168,22 @@ static int process_remote(CaRemote *rr, ProcessUntil until) {
168
168
}
169
169
}
170
170
171
- static size_t write_index (const void * buffer , size_t size , size_t nmemb , void * userdata ) {
172
- CaRemote * rr = userdata ;
173
- size_t product ;
171
+ static int write_index (CaRemote * rr , ReallocBuffer * buffer ) {
174
172
int r ;
175
173
176
- product = size * nmemb ;
174
+ assert ( rr ) ;
177
175
178
176
r = process_remote (rr , PROCESS_UNTIL_CAN_PUT_INDEX );
179
177
if (r < 0 )
180
- return 0 ;
178
+ return r ;
181
179
182
- r = ca_remote_put_index (rr , buffer , product );
180
+ r = ca_remote_put_index (rr , realloc_buffer_data ( buffer ), realloc_buffer_size ( buffer ) );
183
181
if (r < 0 ) {
184
182
log_error ("Failed to put index: %m" );
185
- return 0 ;
183
+ return r ;
186
184
}
187
185
188
- return product ;
186
+ return 0 ;
189
187
}
190
188
191
189
static int write_index_eof (CaRemote * rr ) {
@@ -240,24 +238,24 @@ static int write_archive_eof(CaRemote *rr) {
240
238
return 0 ;
241
239
}
242
240
243
- static size_t write_chunk (const void * buffer , size_t size , size_t nmemb , void * userdata ) {
244
- ReallocBuffer * chunk_buffer = userdata ;
241
+ static size_t write_buffer (const void * ptr , size_t size , size_t nmemb , void * userdata ) {
242
+ ReallocBuffer * buffer = userdata ;
245
243
size_t product , z ;
246
244
247
245
product = size * nmemb ;
248
246
249
- z = realloc_buffer_size (chunk_buffer ) + product ;
250
- if (z < realloc_buffer_size (chunk_buffer )) {
247
+ z = realloc_buffer_size (buffer ) + product ;
248
+ if (z < realloc_buffer_size (buffer )) {
251
249
log_error ("Overflow" );
252
250
return 0 ;
253
251
}
254
252
255
253
if (z > (CA_PROTOCOL_SIZE_MAX - offsetof(CaProtocolChunk , data ))) {
256
- log_error ("Chunk too large" );
254
+ log_error ("Message too large" );
257
255
return 0 ;
258
256
}
259
257
260
- if (!realloc_buffer_append (chunk_buffer , buffer , product )) {
258
+ if (!realloc_buffer_append (buffer , ptr , product )) {
261
259
log_oom ();
262
260
return 0 ;
263
261
}
@@ -292,13 +290,14 @@ static char *chunk_url(const char *store_url, const CaChunkID *id) {
292
290
static int acquire_file (CaRemote * rr ,
293
291
CURL * curl ,
294
292
const char * url ,
295
- size_t (* callback )(const void * p , size_t size , size_t nmemb , void * userdata )) {
296
-
293
+ size_t (* callback )(const void * p , size_t size , size_t nmemb , void * userdata ),
294
+ void * userdata ) {
297
295
long protocol_status ;
298
296
299
297
assert (curl );
300
298
assert (url );
301
299
assert (callback );
300
+ assert (userdata );
302
301
303
302
if (curl_easy_setopt (curl , CURLOPT_URL , url ) != CURLE_OK ) {
304
303
log_error ("Failed to set CURL URL to: %s" , url );
@@ -310,7 +309,7 @@ static int acquire_file(CaRemote *rr,
310
309
return - EIO ;
311
310
}
312
311
313
- if (curl_easy_setopt (curl , CURLOPT_WRITEDATA , rr ) != CURLE_OK ) {
312
+ if (curl_easy_setopt (curl , CURLOPT_WRITEDATA , userdata ) != CURLE_OK ) {
314
313
log_error ("Failed to set CURL private data." );
315
314
return - EIO ;
316
315
}
@@ -375,7 +374,7 @@ static int run(int argc, char *argv[]) {
375
374
size_t n_stores = 0 , current_store = 0 ;
376
375
CURL * curl = NULL ;
377
376
_cleanup_ (ca_remote_unrefp ) CaRemote * rr = NULL ;
378
- _cleanup_ (realloc_buffer_free ) ReallocBuffer chunk_buffer = {};
377
+ _cleanup_ (realloc_buffer_free ) ReallocBuffer buffer = {};
379
378
_cleanup_free_ char * url_buffer = NULL ;
380
379
long protocol_status ;
381
380
int r ;
@@ -478,7 +477,7 @@ static int run(int argc, char *argv[]) {
478
477
}
479
478
480
479
if (archive_url ) {
481
- r = acquire_file (rr , curl , archive_url , write_archive );
480
+ r = acquire_file (rr , curl , archive_url , write_archive , rr );
482
481
if (r < 0 )
483
482
goto finish ;
484
483
if (r == 0 )
@@ -490,15 +489,21 @@ static int run(int argc, char *argv[]) {
490
489
}
491
490
492
491
if (index_url ) {
493
- r = acquire_file (rr , curl , index_url , write_index );
492
+ r = acquire_file (rr , curl , index_url , write_buffer , & buffer );
494
493
if (r < 0 )
495
494
goto finish ;
496
495
if (r == 0 )
497
496
goto flush ;
498
497
498
+ r = write_index (rr , & buffer );
499
+ if (r < 0 )
500
+ goto finish ;
501
+
499
502
r = write_index_eof (rr );
500
503
if (r < 0 )
501
504
goto finish ;
505
+
506
+ realloc_buffer_empty (& buffer );
502
507
}
503
508
504
509
for (;;) {
@@ -550,13 +555,13 @@ static int run(int argc, char *argv[]) {
550
555
goto finish ;
551
556
}
552
557
553
- if (curl_easy_setopt (curl , CURLOPT_WRITEFUNCTION , write_chunk ) != CURLE_OK ) {
558
+ if (curl_easy_setopt (curl , CURLOPT_WRITEFUNCTION , write_buffer ) != CURLE_OK ) {
554
559
log_error ("Failed to set CURL callback function." );
555
560
r = - EIO ;
556
561
goto finish ;
557
562
}
558
563
559
- if (curl_easy_setopt (curl , CURLOPT_WRITEDATA , & chunk_buffer ) != CURLE_OK ) {
564
+ if (curl_easy_setopt (curl , CURLOPT_WRITEDATA , & buffer ) != CURLE_OK ) {
560
565
log_error ("Failed to set CURL private data." );
561
566
r = - EIO ;
562
567
goto finish ;
@@ -608,7 +613,7 @@ static int run(int argc, char *argv[]) {
608
613
(arg_protocol == ARG_PROTOCOL_FTP && (protocol_status >= 200 && protocol_status <= 299 ))||
609
614
(arg_protocol == ARG_PROTOCOL_SFTP && (protocol_status == 0 ))) {
610
615
611
- r = ca_remote_put_chunk (rr , & id , CA_CHUNK_COMPRESSED , realloc_buffer_data (& chunk_buffer ), realloc_buffer_size (& chunk_buffer ));
616
+ r = ca_remote_put_chunk (rr , & id , CA_CHUNK_COMPRESSED , realloc_buffer_data (& buffer ), realloc_buffer_size (& buffer ));
612
617
if (r < 0 ) {
613
618
log_error_errno (r , "Failed to write chunk: %m" );
614
619
goto finish ;
@@ -625,7 +630,7 @@ static int run(int argc, char *argv[]) {
625
630
}
626
631
}
627
632
628
- realloc_buffer_empty (& chunk_buffer );
633
+ realloc_buffer_empty (& buffer );
629
634
630
635
r = process_remote (rr , PROCESS_UNTIL_WRITTEN );
631
636
if (r == - EPIPE ) {
0 commit comments