-
Notifications
You must be signed in to change notification settings - Fork 20
/
aptmethod.c
1216 lines (1102 loc) · 30.4 KB
/
aptmethod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of "reprepro"
* Copyright (C) 2004,2005,2007,2008,2009,2012 Bernhard R. Link
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <config.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include "error.h"
#include "mprintf.h"
#include "strlist.h"
#include "names.h"
#include "dirs.h"
#include "chunks.h"
#include "checksums.h"
#include "files.h"
#include "uncompression.h"
#include "aptmethod.h"
#include "filecntl.h"
#include "hooks.h"
struct tobedone {
/*@null@*/
struct tobedone *next;
/* must be saved to know where is should be moved to: */
/*@notnull@*/
char *uri;
/*@notnull@*/
char *filename;
/* in case of redirection, store the originally requested uri: */
/*@null@*/
char *original_uri;
/* callback and its data: */
queue_callback *callback;
/*@null@*/void *privdata1, *privdata2;
/* there is no fallback or that was already used */
bool lasttry;
/* how often this was redirected */
unsigned int redirect_count;
};
struct aptmethod {
/*@only@*/ /*@null@*/
struct aptmethod *next;
char *name;
char *baseuri;
/*@null@*/char *fallbackbaseuri;
/*@null@*/char *config;
int mstdin, mstdout;
pid_t child;
enum {
ams_notstarted=0,
ams_waitforcapabilities,
ams_ok,
ams_failed
} status;
/*@null@*/struct tobedone *tobedone;
/*@null@*//*@dependent@*/struct tobedone *lasttobedone;
/*@null@*//*@dependent@*/const struct tobedone *nexttosend;
/* what is currently read: */
/*@null@*/char *inputbuffer;
size_t input_size, alreadyread;
/* What is currently written: */
/*@null@*/char *command;
size_t alreadywritten, output_length;
};
struct aptmethodrun {
struct aptmethod *methods;
};
static void todo_free(/*@only@*/ struct tobedone *todo) {
free(todo->filename);
free(todo->original_uri);
free(todo->uri);
free(todo);
}
static void free_todolist(/*@only@*/ struct tobedone *todo) {
while (todo != NULL) {
struct tobedone *h = todo->next;
todo_free(todo);
todo = h;
}
}
static void aptmethod_free(/*@only@*/struct aptmethod *method) {
if (method == NULL)
return;
free(method->name);
free(method->baseuri);
free(method->config);
free(method->fallbackbaseuri);
free(method->inputbuffer);
free(method->command);
free_todolist(method->tobedone);
free(method);
}
retvalue aptmethod_shutdown(struct aptmethodrun *run) {
retvalue result = RET_OK, r;
struct aptmethod *method, *lastmethod, **method_ptr;
/* first get rid of everything not running: */
method_ptr = &run->methods;
while (*method_ptr != NULL) {
if ((*method_ptr)->child > 0) {
if (verbose > 10)
fprintf(stderr,
"Still waiting for %d\n", (int)(*method_ptr)->child);
method_ptr = &(*method_ptr)->next;
continue;
} else {
/*@only@*/ struct aptmethod *h;
h = (*method_ptr);
*method_ptr = h->next;
h->next = NULL;
aptmethod_free(h);
}
}
/* finally get rid of all the processes: */
for (method = run->methods ; method != NULL ; method = method->next) {
if (method->mstdin >= 0) {
(void)close(method->mstdin);
if (verbose > 30)
fprintf(stderr, "Closing stdin of %d\n",
(int)method->child);
}
method->mstdin = -1;
if (method->mstdout >= 0) {
(void)close(method->mstdout);
if (verbose > 30)
fprintf(stderr, "Closing stdout of %d\n",
(int)method->child);
}
method->mstdout = -1;
}
while (run->methods != NULL || uncompress_running()) {
pid_t pid;int status;
pid = wait(&status);
lastmethod = NULL; method = run->methods;
while (method != NULL) {
if (method->child == pid) {
struct aptmethod *next = method->next;
if (lastmethod != NULL) {
lastmethod->next = next;
} else
run->methods = next;
aptmethod_free(method);
pid = -1;
break;
} else {
lastmethod = method;
method = method->next;
}
}
if (pid > 0) {
r = uncompress_checkpid(pid, status);
RET_UPDATE(result, r);
}
}
free(run);
return result;
}
/******************Initialize the data structures***********************/
retvalue aptmethod_initialize_run(struct aptmethodrun **run) {
struct aptmethodrun *r;
r = zNEW(struct aptmethodrun);
if (FAILEDTOALLOC(r))
return RET_ERROR_OOM;
*run = r;
return RET_OK;
}
retvalue aptmethod_newmethod(struct aptmethodrun *run, const char *uri, const char *fallbackuri, const struct strlist *config, struct aptmethod **m) {
struct aptmethod *method;
const char *p;
method = zNEW(struct aptmethod);
if (FAILEDTOALLOC(method))
return RET_ERROR_OOM;
method->mstdin = -1;
method->mstdout = -1;
method->child = -1;
method->status = ams_notstarted;
p = uri;
while (*p != '\0' && (*p == '_' || *p == '-' || *p == '+' ||
(*p>='a' && *p<='z') || (*p>='A' && *p<='Z') ||
(*p>='0' && *p<='9'))) {
p++;
}
if (*p == '\0') {
fprintf(stderr, "No colon found in method-URI '%s'!\n", uri);
free(method);
return RET_ERROR;
}
if (*p != ':') {
fprintf(stderr,
"Unexpected character '%c' in method-URI '%s'!\n", *p, uri);
free(method);
return RET_ERROR;
}
if (p == uri) {
fprintf(stderr,
"Zero-length name in method-URI '%s'!\n", uri);
free(method);
return RET_ERROR;
}
method->name = strndup(uri, p-uri);
if (FAILEDTOALLOC(method->name)) {
free(method);
return RET_ERROR_OOM;
}
method->baseuri = strdup(uri);
if (FAILEDTOALLOC(method->baseuri)) {
free(method->name);
free(method);
return RET_ERROR_OOM;
}
if (fallbackuri == NULL)
method->fallbackbaseuri = NULL;
else {
method->fallbackbaseuri = strdup(fallbackuri);
if (FAILEDTOALLOC(method->fallbackbaseuri)) {
free(method->baseuri);
free(method->name);
free(method);
return RET_ERROR_OOM;
}
}
#define CONF601 "601 Configuration"
#define CONFITEM "\nConfig-Item: "
if (config->count == 0)
method->config = strdup(CONF601 CONFITEM "Dir=/" "\n\n");
else
method->config = strlist_concat(config,
CONF601 CONFITEM, CONFITEM, "\n\n");
if (FAILEDTOALLOC(method->config)) {
free(method->fallbackbaseuri);
free(method->baseuri);
free(method->name);
free(method);
return RET_ERROR_OOM;
}
method->next = run->methods;
run->methods = method;
*m = method;
return RET_OK;
}
/**************************Fire up a method*****************************/
inline static retvalue aptmethod_startup(struct aptmethod *method) {
pid_t f;
int mstdin[2];
int mstdout[2];
int r;
/* When there is nothing to get, there is no reason to startup
* the method. */
if (method->tobedone == NULL) {
return RET_NOTHING;
}
/* when we are already running, we are already ready...*/
if (method->child > 0) {
return RET_OK;
}
method->status = ams_waitforcapabilities;
r = pipe(mstdin);
if (r < 0) {
int e = errno;
fprintf(stderr, "Error %d creating pipe: %s\n",
e, strerror(e));
return RET_ERRNO(e);
}
r = pipe(mstdout);
if (r < 0) {
int e = errno;
(void)close(mstdin[0]); (void)close(mstdin[1]);
fprintf(stderr, "Error %d in pipe syscall: %s\n",
e, strerror(e));
return RET_ERRNO(e);
}
if (interrupted()) {
(void)close(mstdin[0]);(void)close(mstdin[1]);
(void)close(mstdout[0]);(void)close(mstdout[1]);
return RET_ERROR_INTERRUPTED;
}
f = fork();
if (f < 0) {
int e = errno;
(void)close(mstdin[0]); (void)close(mstdin[1]);
(void)close(mstdout[0]); (void)close(mstdout[1]);
fprintf(stderr, "Error %d forking: %s\n",
e, strerror(e));
return RET_ERRNO(e);
}
if (f == 0) {
char *methodname;
int e;
/* child: */
(void)close(mstdin[1]);
(void)close(mstdout[0]);
if (dup2(mstdin[0], 0) < 0) {
e = errno;
fprintf(stderr, "Error %d while setting stdin: %s\n",
e, strerror(e));
exit(255);
}
if (dup2(mstdout[1], 1) < 0) {
e = errno;
fprintf(stderr, "Error %d while setting stdout: %s\n",
e, strerror(e));
exit(255);
}
closefrom(3);
methodname = calc_dirconcat(global.methoddir, method->name);
if (FAILEDTOALLOC(methodname))
exit(255);
/* not really useful here, unless someone write reprepro
* specific modules (which I hope no one will) */
sethookenvironment(NULL, NULL, NULL, NULL);
/* actually call the method without any arguments: */
(void)execl(methodname, methodname, ENDOFARGUMENTS);
e = errno;
fprintf(stderr, "Error %d while executing '%s': %s\n",
e, methodname, strerror(e));
exit(255);
}
/* the main program continues... */
method->child = f;
if (verbose > 10)
fprintf(stderr,
"Method '%s' started as %d\n", method->baseuri, (int)f);
(void)close(mstdin[0]);
(void)close(mstdout[1]);
markcloseonexec(mstdin[1]);
markcloseonexec(mstdout[0]);
method->mstdin = mstdin[1];
method->mstdout = mstdout[0];
method->inputbuffer = NULL;
method->input_size = 0;
method->alreadyread = 0;
method->command = NULL;
method->output_length = 0;
method->alreadywritten = 0;
return RET_OK;
}
/**************************how to add files*****************************/
static inline void enqueue(struct aptmethod *method, /*@only@*/struct tobedone *todo) {
todo->next = NULL;
if (method->lasttobedone == NULL)
method->nexttosend = method->lasttobedone = method->tobedone = todo;
else {
method->lasttobedone->next = todo;
method->lasttobedone = todo;
if (method->nexttosend == NULL)
method->nexttosend = todo;
}
}
static retvalue enqueuenew(struct aptmethod *method, /*@only@*/char *uri, /*@only@*/char *destfile, queue_callback *callback, void *privdata1, void *privdata2) {
struct tobedone *todo;
if (FAILEDTOALLOC(destfile)) {
free(uri);
return RET_ERROR_OOM;
}
if (FAILEDTOALLOC(uri)) {
free(destfile);
return RET_ERROR_OOM;
}
todo = NEW(struct tobedone);
if (FAILEDTOALLOC(todo)) {
free(uri); free(destfile);
return RET_ERROR_OOM;
}
todo->next = NULL;
todo->uri = uri;
todo->filename = destfile;
todo->original_uri = NULL;
todo->callback = callback;
todo->privdata1 = privdata1;
todo->privdata2 = privdata2;
todo->lasttry = method->fallbackbaseuri == NULL;
todo->redirect_count = 0;
enqueue(method, todo);
return RET_OK;
}
retvalue aptmethod_enqueue(struct aptmethod *method, const char *origfile, /*@only@*/char *destfile, queue_callback *callback, void *privdata1, void *privdata2) {
return enqueuenew(method,
calc_dirconcat(method->baseuri, origfile),
destfile, callback, privdata1, privdata2);
}
retvalue aptmethod_enqueueindex(struct aptmethod *method, const char *suite, const char *origfile, const char *suffix, const char *destfile, const char *downloadsuffix, queue_callback *callback, void *privdata1, void *privdata2) {
return enqueuenew(method,
mprintf("%s/%s/%s%s",
method->baseuri, suite, origfile, suffix),
mprintf("%s%s", destfile, downloadsuffix),
callback, privdata1, privdata2);
}
/*****************what to do with received files************************/
static retvalue requeue_or_fail(struct aptmethod *method, /*@only@*/struct tobedone *todo) {
retvalue r;
if (todo->lasttry) {
if (todo->callback == NULL)
r = RET_ERROR;
else
r = todo->callback(qa_error,
todo->privdata1, todo->privdata2,
todo->uri, NULL, todo->filename,
NULL, method->name);
todo_free(todo);
return r;
} else {
size_t l, old_len, new_len;
char *s;
assert (method->fallbackbaseuri != NULL);
old_len = strlen(method->baseuri);
new_len = strlen(method->fallbackbaseuri);
l = strlen(todo->uri);
s = malloc(l+new_len+1-old_len);
if (FAILEDTOALLOC(s)) {
todo_free(todo);
return RET_ERROR_OOM;
}
memcpy(s, method->fallbackbaseuri, new_len);
strcpy(s+new_len, todo->uri + old_len);
free(todo->uri);
todo->uri = s;
todo->lasttry = true;
todo->redirect_count = 0;
enqueue(method, todo);
return RET_OK;
}
}
/* look which file could not be received and remove it: */
static retvalue urierror(struct aptmethod *method, const char *uri, /*@only@*/char *message) {
struct tobedone *todo, *lasttodo;
lasttodo = NULL; todo = method->tobedone;
while (todo != NULL) {
if (strcmp(todo->uri, uri) == 0) {
/* remove item: */
if (lasttodo == NULL)
method->tobedone = todo->next;
else
lasttodo->next = todo->next;
if (method->nexttosend == todo) {
/* just in case some method received
* files before we request them ;-) */
method->nexttosend = todo->next;
}
if (method->lasttobedone == todo) {
method->lasttobedone = todo->next;
}
fprintf(stderr,
"aptmethod error receiving '%s':\n'%s'\n",
uri, (message != NULL)?message:"");
/* put message in failed items to show it later? */
free(message);
return requeue_or_fail(method, todo);
}
lasttodo = todo;
todo = todo->next;
}
/* huh? If if have not asked for it, how can there be errors? */
fprintf(stderr,
"Method '%s' reported error with unrequested file '%s':\n'%s'!\n",
method->name, uri, message);
free(message);
return RET_ERROR;
}
/* look which file could not be received and readd the new name... */
static retvalue uriredirect(struct aptmethod *method, const char *uri, /*@only@*/char *newuri) {
struct tobedone *todo, *lasttodo;
lasttodo = NULL; todo = method->tobedone;
while (todo != NULL) {
if (strcmp(todo->uri, uri) == 0) {
/* remove item: */
if (lasttodo == NULL)
method->tobedone = todo->next;
else
lasttodo->next = todo->next;
if (method->nexttosend == todo) {
/* just in case some method received
* files before we request them ;-) */
method->nexttosend = todo->next;
}
if (method->lasttobedone == todo) {
method->lasttobedone = todo->next;
}
if (todo->redirect_count < 10) {
if (verbose > 0)
fprintf(stderr,
"aptmethod redirects '%s' to '%s'\n",
uri, newuri);
/* readd with new uri */
if (todo->original_uri != NULL)
free(todo->uri);
else
todo->original_uri = todo->uri;
todo->uri = newuri;
todo->redirect_count++;
enqueue(method, todo);
return RET_OK;
}
fprintf(stderr,
"redirect loop (or too many redirects) detected, original uri is '%s'\n",
todo->original_uri);
/* put message in failed items to show it later? */
free(newuri);
return requeue_or_fail(method, todo);
}
lasttodo = todo;
todo = todo->next;
}
/* huh? If if have not asked for it, how can there be errors? */
fprintf(stderr,
"Method '%s' reported redirect for unrequested file '%s'-> '%s'\n",
method->name, uri, newuri);
free(newuri);
return RET_ERROR;
}
/* look where a received file has to go to: */
static retvalue uridone(struct aptmethod *method, const char *uri, const char *filename, /*@only@*//*@null@*/struct checksums *checksumsfromapt) {
struct tobedone *todo, *lasttodo;
retvalue r;
lasttodo = NULL; todo = method->tobedone;
while (todo != NULL) {
if (strcmp(todo->uri, uri) != 0) {
lasttodo = todo;
todo = todo->next;
continue;
}
r = todo->callback(qa_got,
todo->privdata1, todo->privdata2,
todo->original_uri? todo->original_uri : todo->uri,
filename, todo->filename,
checksumsfromapt, method->name);
checksums_free(checksumsfromapt);
/* remove item: */
if (lasttodo == NULL)
method->tobedone = todo->next;
else
lasttodo->next = todo->next;
if (method->nexttosend == todo) {
/* just in case some method received
* files before we request them ;-) */
method->nexttosend = todo->next;
}
if (method->lasttobedone == todo) {
method->lasttobedone = todo->next;
}
todo_free(todo);
return r;
}
/* huh? */
fprintf(stderr,
"Method '%s' retrieved unexpected file '%s' at '%s'!\n",
method->name, uri, filename);
checksums_free(checksumsfromapt);
return RET_ERROR;
}
/***************************Input and Output****************************/
static retvalue logmessage(const struct aptmethod *method, const char *chunk, const char *type) {
retvalue r;
char *message;
r = chunk_getvalue(chunk, "Message", &message);
if (RET_WAS_ERROR(r))
return r;
if (RET_IS_OK(r)) {
fprintf(stderr, "aptmethod '%s': '%s'\n",
method->baseuri, message);
free(message);
return RET_OK;
}
r = chunk_getvalue(chunk, "URI", &message);
if (RET_WAS_ERROR(r))
return r;
if (RET_IS_OK(r)) {
fprintf(stderr, "aptmethod %s '%s'\n", type, message);
free(message);
return RET_OK;
}
fprintf(stderr, "aptmethod '%s': '%s'\n", method->baseuri, type);
return RET_OK;
}
static inline retvalue gotcapabilities(struct aptmethod *method, const char *chunk) {
retvalue r;
r = chunk_gettruth(chunk, "Single-Instance");
if (RET_WAS_ERROR(r))
return r;
// TODO: what to do with this?
// if (r != RET_NOTHING) {
// fprintf(stderr, "WARNING: Single-instance not yet supported!\n");
// }
r = chunk_gettruth(chunk, "Send-Config");
if (RET_WAS_ERROR(r))
return r;
if (r != RET_NOTHING) {
assert(method->command == NULL);
method->alreadywritten = 0;
method->command = method->config;
method->config = NULL;
method->output_length = strlen(method->command);
if (verbose > 11) {
fprintf(stderr, "Sending config: '%s'\n",
method->command);
}
} else {
free(method->config);
method->config = NULL;
}
method->status = ams_ok;
return RET_OK;
}
static inline retvalue goturidone(struct aptmethod *method, const char *chunk) {
static const char * const method_hash_names[cs_COUNT] =
{ "MD5-Hash", "SHA1-Hash", "SHA256-Hash",
"Size" };
retvalue result, r;
char *uri, *filename;
enum checksumtype type;
char *hashes[cs_COUNT];
struct checksums *checksums = NULL;
//TODO: is it worth the mess to make this in-situ?
r = chunk_getvalue(chunk, "URI", &uri);
if (r == RET_NOTHING) {
fprintf(stderr,
"Missing URI header in uridone received from '%s' method!\n",
method->name);
r = RET_ERROR;
method->status = ams_failed;
}
if (RET_WAS_ERROR(r))
return r;
r = chunk_getvalue(chunk, "Filename", &filename);
if (r == RET_NOTHING) {
char *altfilename;
r = chunk_getvalue(chunk, "Alt-Filename", &altfilename);
if (r == RET_NOTHING) {
fprintf(stderr,
"Missing Filename header in uridone received from '%s' method!\n",
method->name);
r = urierror(method, uri, strdup(
"<no error but missing Filename from apt-method>"));
} else {
r = urierror(method, uri, mprintf(
"<File not there, apt-method suggests '%s' instead>", altfilename));
free(altfilename);
}
free(uri);
return r;
}
if (RET_WAS_ERROR(r)) {
free(uri);
return r;
}
if (verbose >= 1)
fprintf(stderr, "aptmethod got '%s'\n", uri);
result = RET_NOTHING;
for (type = cs_md5sum ; type < cs_COUNT ; type++) {
hashes[type] = NULL;
r = chunk_getvalue(chunk, method_hash_names[type],
&hashes[type]);
RET_UPDATE(result, r);
}
if (RET_IS_OK(result) && hashes[cs_md5sum] == NULL) {
/* the lenny version also has this, better ask for
* in case the old MD5-Hash vanishes in the future */
r = chunk_getvalue(chunk, "MD5Sum-Hash", &hashes[cs_md5sum]);
RET_UPDATE(result, r);
}
if (RET_WAS_ERROR(result)) {
free(uri); free(filename);
for (type = cs_md5sum ; type < cs_COUNT ; type++)
free(hashes[type]);
return result;
}
if (RET_IS_OK(result)) {
/* ignore errors, we can recompute them from the file */
(void)checksums_init(&checksums, hashes);
}
r = uridone(method, uri, filename, checksums);
free(uri);
free(filename);
return r;
}
static inline retvalue goturierror(struct aptmethod *method, const char *chunk) {
retvalue r;
char *uri, *message;
r = chunk_getvalue(chunk, "URI", &uri);
if (r == RET_NOTHING) {
fprintf(stderr,
"Missing URI header in urierror received from '%s' method!\n", method->name);
r = RET_ERROR;
}
if (RET_WAS_ERROR(r))
return r;
r = chunk_getvalue(chunk, "Message", &message);
if (r == RET_NOTHING) {
message = NULL;
}
if (RET_WAS_ERROR(r)) {
free(uri);
return r;
}
r = urierror(method, uri, message);
free(uri);
return r;
}
static inline retvalue gotredirect(struct aptmethod *method, const char *chunk) {
char *uri, *newuri;
retvalue r;
r = chunk_getvalue(chunk, "URI", &uri);
if (r == RET_NOTHING) {
fprintf(stderr,
"Missing URI header in uriredirect received from '%s' method!\n", method->name);
r = RET_ERROR;
}
if (RET_WAS_ERROR(r))
return r;
r = chunk_getvalue(chunk, "New-URI", &newuri);
if (r == RET_NOTHING) {
fprintf(stderr,
"Missing New-URI header in uriredirect received from '%s' method!\n", method->name);
r = RET_ERROR;
}
if (RET_WAS_ERROR(r)) {
free(uri);
return r;
}
r = uriredirect(method, uri, newuri);
free(uri);
return r;
}
static inline retvalue parsereceivedblock(struct aptmethod *method, const char *input) {
const char *p;
retvalue r;
#define OVERLINE {while (*p != '\0' && *p != '\n') p++; if (*p == '\n') p++; }
while (*input == '\n' || *input == '\r')
input++;
if (*input == '\0') {
fprintf(stderr,
"Unexpected number of newlines from '%s' method!\n", method->name);
return RET_NOTHING;
}
p = input;
switch ((*(input+1)=='0')?*input:'\0') {
case '1':
switch (*(input+2)) {
/* 100 Capabilities */
case '0':
OVERLINE;
if (verbose > 14) {
fprintf(stderr, "Got '%s'\n",
input);
}
return gotcapabilities(method, input);
/* 101 Log */
case '1':
if (verbose > 10) {
OVERLINE;
return logmessage(method, p, "101");
}
return RET_OK;
/* 102 Status */
case '2':
if (verbose > 5) {
OVERLINE;
return logmessage(method, p, "102");
}
return RET_OK;
/* 103 Redirect */
case '3':
OVERLINE;
return gotredirect(method, p);
default:
fprintf(stderr,
"Error or unsupported message received: '%s'\n",
input);
return RET_ERROR;
}
case '2':
switch (*(input+2)) {
/* 200 URI Start */
case '0':
if (verbose > 5) {
OVERLINE;
return logmessage(method, p, "start");
}
return RET_OK;
/* 201 URI Done */
case '1':
OVERLINE;
return goturidone(method, p);
default:
fprintf(stderr,
"Error or unsupported message received: '%s'\n",
input);
return RET_ERROR;
}
case '4':
switch (*(input+2)) {
case '0':
OVERLINE;
r = goturierror(method, p);
break;
case '1':
OVERLINE;
(void)logmessage(method, p, "general error");
method->status = ams_failed;
r = RET_ERROR;
break;
default:
fprintf(stderr,
"Error or unsupported message received: '%s'\n",
input);
r = RET_ERROR;
}
/* a failed download is not a error yet, as it might
* be redone from another source later */
return r;
default:
fprintf(stderr,
"Unexpected data from '%s' method: '%s'\n",
method->name, input);
return RET_ERROR;
}
}
static retvalue receivedata(struct aptmethod *method) {
retvalue result;
ssize_t r;
char *p;
int consecutivenewlines;
assert (method->status != ams_ok || method->tobedone != NULL);
if (method->status != ams_waitforcapabilities
&& method->status != ams_ok)
return RET_NOTHING;
/* First look if we have enough room to read.. */
if (method->alreadyread + 1024 >= method->input_size) {
char *newptr;
if (method->input_size >= (size_t)128000) {
fprintf(stderr,
"Ridiculously long answer from method!\n");
method->status = ams_failed;
return RET_ERROR;
}
newptr = realloc(method->inputbuffer, method->alreadyread+1024);
if (FAILEDTOALLOC(newptr)) {
return RET_ERROR_OOM;
}
method->inputbuffer = newptr;
method->input_size = method->alreadyread + 1024;
}
assert (method->inputbuffer != NULL);
/* then read as much as the pipe is able to fill of our buffer */
r = read(method->mstdout, method->inputbuffer + method->alreadyread,
method->input_size - method->alreadyread - 1);
if (r < 0) {
int e = errno;
fprintf(stderr, "Error %d reading pipe from aptmethod: %s\n",
e, strerror(e));
method->status = ams_failed;
return RET_ERRNO(e);
}
method->alreadyread += r;
result = RET_NOTHING;
while(true) {
retvalue res;
r = method->alreadyread;
p = method->inputbuffer;
consecutivenewlines = 0;
while (r > 0) {
if (*p == '\0') {
fprintf(stderr,
"Unexpected Zeroes in method output!\n");
method->status = ams_failed;
return RET_ERROR;
} else if (*p == '\n') {
consecutivenewlines++;
if (consecutivenewlines >= 2)
break;
} else if (*p != '\r') {
consecutivenewlines = 0;
}
p++; r--;
}
if (r <= 0) {
return result;
}
*p ='\0'; p++; r--;
res = parsereceivedblock(method, method->inputbuffer);
if (r > 0)
memmove(method->inputbuffer, p, r);
method->alreadyread = r;
RET_UPDATE(result, res);
}
}
static retvalue senddata(struct aptmethod *method) {
size_t l;
ssize_t r;
if (method->status != ams_ok)
return RET_NOTHING;
if (method->command == NULL) {