]>
Commit | Line | Data |
---|---|---|
93bf083d AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
cdcc6d34 | 3 | // $Id: acquire-method.cc,v 1.16 1999/01/27 02:48:52 jgg Exp $ |
93bf083d AL |
4 | /* ###################################################################### |
5 | ||
6 | Acquire Method | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Include Files /*{{{*/ | |
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "apt-pkg/acquire-method.h" | |
13 | #endif | |
14 | #include <apt-pkg/acquire-method.h> | |
15 | #include <apt-pkg/error.h> | |
16 | #include <apt-pkg/configuration.h> | |
cdcc6d34 | 17 | #include <apt-pkg/strutl.h> |
93bf083d AL |
18 | #include <apt-pkg/fileutl.h> |
19 | ||
20 | #include <stdio.h> | |
21 | /*}}}*/ | |
22 | ||
23 | // AcqMethod::pkgAcqMethod - Constructor /*{{{*/ | |
24 | // --------------------------------------------------------------------- | |
25 | /* This constructs the initialization text */ | |
26 | pkgAcqMethod::pkgAcqMethod(const char *Ver,unsigned long Flags) | |
27 | { | |
28 | char S[300] = ""; | |
29 | char *End = S; | |
30 | strcat(End,"100 Capabilities\n"); | |
31 | sprintf(End+strlen(End),"Version: %s\n",Ver); | |
32 | ||
33 | if ((Flags & SingleInstance) == SingleInstance) | |
34 | strcat(End,"Single-Instance: true\n"); | |
35 | ||
93bf083d AL |
36 | if ((Flags & Pipeline) == Pipeline) |
37 | strcat(End,"Pipeline: true\n"); | |
38 | ||
39 | if ((Flags & SendConfig) == SendConfig) | |
40 | strcat(End,"Send-Config: true\n"); | |
e331f6ed AL |
41 | |
42 | if ((Flags & LocalOnly) == LocalOnly) | |
43 | strcat(End,"Local-Only: true\n"); | |
93bf083d AL |
44 | strcat(End,"\n"); |
45 | ||
46 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
47 | exit(100); | |
be4401bf AL |
48 | |
49 | SetNonBlock(STDIN_FILENO,true); | |
5cb5d8dc | 50 | |
92e889c8 | 51 | Queue = 0; |
5cb5d8dc | 52 | QueueBack = 0; |
93bf083d AL |
53 | } |
54 | /*}}}*/ | |
55 | // AcqMethod::Fail - A fetch has failed /*{{{*/ | |
56 | // --------------------------------------------------------------------- | |
57 | /* */ | |
a72ace20 | 58 | void pkgAcqMethod::Fail(bool Transient) |
93bf083d AL |
59 | { |
60 | string Err = "Undetermined Error"; | |
61 | if (_error->empty() == false) | |
62 | _error->PopMessage(Err); | |
63 | _error->Discard(); | |
a72ace20 | 64 | Fail(Err,Transient); |
93bf083d AL |
65 | } |
66 | /*}}}*/ | |
67 | // AcqMethod::Fail - A fetch has failed /*{{{*/ | |
68 | // --------------------------------------------------------------------- | |
69 | /* */ | |
a72ace20 | 70 | void pkgAcqMethod::Fail(string Err,bool Transient) |
93bf083d AL |
71 | { |
72 | char S[1024]; | |
be4401bf AL |
73 | if (Queue != 0) |
74 | { | |
75 | snprintf(S,sizeof(S),"400 URI Failure\nURI: %s\n" | |
a72ace20 AL |
76 | "Message: %s\n",Queue->Uri.c_str(),Err.c_str()); |
77 | ||
be4401bf AL |
78 | // Dequeue |
79 | FetchItem *Tmp = Queue; | |
80 | Queue = Queue->Next; | |
81 | delete Tmp; | |
5cb5d8dc AL |
82 | if (Tmp == QueueBack) |
83 | QueueBack = Queue; | |
be4401bf AL |
84 | } |
85 | else | |
86 | snprintf(S,sizeof(S),"400 URI Failure\nURI: <UNKNOWN>\n" | |
a72ace20 | 87 | "Message: %s\n",Err.c_str()); |
be4401bf | 88 | |
a72ace20 AL |
89 | // Set the transient flag |
90 | if (Transient == true) | |
91 | strcat(S,"Transient-Failure: true\n\n"); | |
92 | else | |
93 | strcat(S,"\n"); | |
94 | ||
93bf083d AL |
95 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) |
96 | exit(100); | |
97 | } | |
98 | /*}}}*/ | |
99 | // AcqMethod::URIStart - Indicate a download is starting /*{{{*/ | |
100 | // --------------------------------------------------------------------- | |
101 | /* */ | |
be4401bf | 102 | void pkgAcqMethod::URIStart(FetchResult &Res) |
93bf083d | 103 | { |
be4401bf AL |
104 | if (Queue == 0) |
105 | abort(); | |
106 | ||
93bf083d AL |
107 | char S[1024] = ""; |
108 | char *End = S; | |
109 | ||
be4401bf | 110 | End += snprintf(S,sizeof(S),"200 URI Start\nURI: %s\n",Queue->Uri.c_str()); |
93bf083d AL |
111 | if (Res.Size != 0) |
112 | End += snprintf(End,sizeof(S) - (End - S),"Size: %u\n",Res.Size); | |
113 | ||
114 | if (Res.LastModified != 0) | |
115 | End += snprintf(End,sizeof(S) - (End - S),"Last-Modified: %s\n", | |
116 | TimeRFC1123(Res.LastModified).c_str()); | |
117 | ||
be4401bf | 118 | if (Res.ResumePoint != 0) |
93bf083d | 119 | End += snprintf(End,sizeof(S) - (End - S),"Resume-Point: %u\n", |
be4401bf | 120 | Res.ResumePoint); |
93bf083d AL |
121 | |
122 | strcat(End,"\n"); | |
123 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
124 | exit(100); | |
125 | } | |
126 | /*}}}*/ | |
127 | // AcqMethod::URIDone - A URI is finished /*{{{*/ | |
128 | // --------------------------------------------------------------------- | |
129 | /* */ | |
130 | void pkgAcqMethod::URIDone(FetchResult &Res, FetchResult *Alt) | |
131 | { | |
be4401bf AL |
132 | if (Queue == 0) |
133 | abort(); | |
134 | ||
93bf083d AL |
135 | char S[1024] = ""; |
136 | char *End = S; | |
137 | ||
be4401bf | 138 | End += snprintf(S,sizeof(S),"201 URI Done\nURI: %s\n",Queue->Uri.c_str()); |
93bf083d AL |
139 | |
140 | if (Res.Filename.empty() == false) | |
141 | End += snprintf(End,sizeof(S) - (End - S),"Filename: %s\n",Res.Filename.c_str()); | |
142 | ||
143 | if (Res.Size != 0) | |
144 | End += snprintf(End,sizeof(S) - (End - S),"Size: %u\n",Res.Size); | |
145 | ||
146 | if (Res.LastModified != 0) | |
147 | End += snprintf(End,sizeof(S) - (End - S),"Last-Modified: %s\n", | |
148 | TimeRFC1123(Res.LastModified).c_str()); | |
149 | ||
150 | if (Res.MD5Sum.empty() == false) | |
fd9bd3dc | 151 | End += snprintf(End,sizeof(S) - (End - S),"MD5-Hash: %s\n",Res.MD5Sum.c_str()); |
93bf083d | 152 | |
b98f2859 AL |
153 | if (Res.ResumePoint != 0) |
154 | End += snprintf(End,sizeof(S) - (End - S),"Resume-Point: %u\n", | |
155 | Res.ResumePoint); | |
156 | ||
93bf083d AL |
157 | if (Res.IMSHit == true) |
158 | strcat(End,"IMS-Hit: true\n"); | |
159 | End = S + strlen(S); | |
160 | ||
161 | if (Alt != 0) | |
162 | { | |
163 | if (Alt->Filename.empty() == false) | |
164 | End += snprintf(End,sizeof(S) - (End - S),"Alt-Filename: %s\n",Alt->Filename.c_str()); | |
165 | ||
166 | if (Alt->Size != 0) | |
167 | End += snprintf(End,sizeof(S) - (End - S),"Alt-Size: %u\n",Alt->Size); | |
168 | ||
169 | if (Alt->LastModified != 0) | |
170 | End += snprintf(End,sizeof(S) - (End - S),"Alt-Last-Modified: %s\n", | |
171 | TimeRFC1123(Alt->LastModified).c_str()); | |
172 | ||
173 | if (Alt->MD5Sum.empty() == false) | |
fd9bd3dc | 174 | End += snprintf(End,sizeof(S) - (End - S),"Alt-MD5-Hash: %s\n", |
93bf083d AL |
175 | Alt->MD5Sum.c_str()); |
176 | ||
177 | if (Alt->IMSHit == true) | |
178 | strcat(End,"Alt-IMS-Hit: true\n"); | |
179 | } | |
180 | ||
181 | strcat(End,"\n"); | |
182 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
183 | exit(100); | |
be4401bf AL |
184 | |
185 | // Dequeue | |
186 | FetchItem *Tmp = Queue; | |
187 | Queue = Queue->Next; | |
188 | delete Tmp; | |
5cb5d8dc AL |
189 | if (Tmp == QueueBack) |
190 | QueueBack = Queue; | |
93bf083d AL |
191 | } |
192 | /*}}}*/ | |
f46e7681 AL |
193 | // AcqMethod::MediaFail - Syncronous request for new media /*{{{*/ |
194 | // --------------------------------------------------------------------- | |
195 | /* This sends a 403 Media Failure message to the APT and waits for it | |
196 | to be ackd */ | |
018f1533 | 197 | bool pkgAcqMethod::MediaFail(string Required,string Drive) |
f46e7681 AL |
198 | { |
199 | char S[1024]; | |
200 | snprintf(S,sizeof(S),"403 Media Failure\nMedia: %s\nDrive: %s\n\n", | |
201 | Required.c_str(),Drive.c_str()); | |
202 | ||
203 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
204 | exit(100); | |
205 | ||
206 | vector<string> MyMessages; | |
207 | ||
208 | /* Here we read messages until we find a 603, each non 603 message is | |
209 | appended to the main message list for later processing */ | |
210 | while (1) | |
211 | { | |
212 | if (WaitFd(STDIN_FILENO) == false) | |
76d97c26 | 213 | return false; |
f46e7681 AL |
214 | |
215 | if (ReadMessages(STDIN_FILENO,MyMessages) == false) | |
76d97c26 AL |
216 | return false; |
217 | ||
f46e7681 AL |
218 | string Message = MyMessages.front(); |
219 | MyMessages.erase(MyMessages.begin()); | |
220 | ||
221 | // Fetch the message number | |
222 | char *End; | |
223 | int Number = strtol(Message.c_str(),&End,10); | |
224 | if (End == Message.c_str()) | |
225 | { | |
226 | cerr << "Malformed message!" << endl; | |
227 | exit(100); | |
228 | } | |
229 | ||
230 | // Change ack | |
231 | if (Number == 603) | |
232 | { | |
76d97c26 | 233 | while (MyMessages.empty() == false) |
f46e7681 AL |
234 | { |
235 | Messages.push_back(MyMessages.front()); | |
236 | MyMessages.erase(MyMessages.begin()); | |
237 | } | |
542ec555 | 238 | |
76d97c26 | 239 | return !StringToBool(LookupTag(Message,"Fail"),false); |
f46e7681 AL |
240 | } |
241 | ||
242 | Messages.push_back(Message); | |
243 | } | |
244 | } | |
245 | /*}}}*/ | |
93bf083d AL |
246 | // AcqMethod::Configuration - Handle the configuration message /*{{{*/ |
247 | // --------------------------------------------------------------------- | |
248 | /* This parses each configuration entry and puts it into the _config | |
249 | Configuration class. */ | |
250 | bool pkgAcqMethod::Configuration(string Message) | |
251 | { | |
252 | ::Configuration &Cnf = *_config; | |
253 | ||
254 | const char *I = Message.begin(); | |
255 | ||
256 | unsigned int Length = strlen("Config-Item"); | |
257 | for (; I + Length < Message.end(); I++) | |
258 | { | |
259 | // Not a config item | |
260 | if (I[Length] != ':' || stringcasecmp(I,I+Length,"Config-Item") != 0) | |
261 | continue; | |
262 | ||
263 | I += Length + 1; | |
264 | ||
265 | for (; I < Message.end() && *I == ' '; I++); | |
266 | const char *Equals = I; | |
267 | for (; Equals < Message.end() && *Equals != '='; Equals++); | |
268 | const char *End = Equals; | |
269 | for (; End < Message.end() && *End != '\n'; End++); | |
270 | if (End == Equals) | |
271 | return false; | |
272 | ||
273 | Cnf.Set(string(I,Equals-I),string(Equals+1,End-Equals-1)); | |
274 | I = End; | |
275 | } | |
276 | ||
277 | return true; | |
278 | } | |
279 | /*}}}*/ | |
280 | // AcqMethod::Run - Run the message engine /*{{{*/ | |
281 | // --------------------------------------------------------------------- | |
282 | /* */ | |
be4401bf | 283 | int pkgAcqMethod::Run(bool Single) |
93bf083d | 284 | { |
93bf083d AL |
285 | while (1) |
286 | { | |
be4401bf | 287 | // Block if the message queue is empty |
93bf083d | 288 | if (Messages.empty() == true) |
be4401bf AL |
289 | { |
290 | if (Single == false) | |
291 | if (WaitFd(STDIN_FILENO) == false) | |
292 | return 0; | |
be4401bf | 293 | |
92e889c8 AL |
294 | if (ReadMessages(STDIN_FILENO,Messages) == false) |
295 | return 0; | |
296 | } | |
297 | ||
be4401bf AL |
298 | // Single mode exits if the message queue is empty |
299 | if (Single == true && Messages.empty() == true) | |
300 | return 0; | |
301 | ||
93bf083d AL |
302 | string Message = Messages.front(); |
303 | Messages.erase(Messages.begin()); | |
304 | ||
305 | // Fetch the message number | |
306 | char *End; | |
307 | int Number = strtol(Message.c_str(),&End,10); | |
308 | if (End == Message.c_str()) | |
309 | { | |
310 | cerr << "Malformed message!" << endl; | |
311 | return 100; | |
312 | } | |
313 | ||
314 | switch (Number) | |
315 | { | |
316 | case 601: | |
317 | if (Configuration(Message) == false) | |
318 | return 100; | |
319 | break; | |
320 | ||
321 | case 600: | |
be4401bf AL |
322 | { |
323 | FetchItem *Tmp = new FetchItem; | |
324 | ||
325 | Tmp->Uri = LookupTag(Message,"URI"); | |
326 | Tmp->DestFile = LookupTag(Message,"FileName"); | |
b98f2859 AL |
327 | if (StrToTime(LookupTag(Message,"Last-Modified"),Tmp->LastModified) == false) |
328 | Tmp->LastModified = 0; | |
a72ace20 | 329 | Tmp->IndexFile = StringToBool(LookupTag(Message,"Index-File"),false); |
be4401bf AL |
330 | Tmp->Next = 0; |
331 | ||
332 | // Append it to the list | |
333 | FetchItem **I = &Queue; | |
92e889c8 | 334 | for (; *I != 0; I = &(*I)->Next); |
be4401bf | 335 | *I = Tmp; |
5cb5d8dc AL |
336 | if (QueueBack == 0) |
337 | QueueBack = Tmp; | |
338 | ||
bfd22fc0 | 339 | // Notify that this item is to be fetched. |
be4401bf | 340 | if (Fetch(Tmp) == false) |
93bf083d | 341 | Fail(); |
bfd22fc0 | 342 | |
93bf083d AL |
343 | break; |
344 | } | |
345 | } | |
346 | } | |
347 | ||
348 | return 0; | |
349 | } | |
350 | /*}}}*/ | |
be4401bf AL |
351 | // AcqMethod::Log - Send a log message /*{{{*/ |
352 | // --------------------------------------------------------------------- | |
353 | /* */ | |
354 | void pkgAcqMethod::Log(const char *Format,...) | |
355 | { | |
356 | string CurrentURI = "<UNKNOWN>"; | |
357 | if (Queue != 0) | |
358 | CurrentURI = Queue->Uri; | |
359 | ||
360 | va_list args; | |
361 | va_start(args,Format); | |
362 | ||
363 | // sprintf the description | |
364 | char S[1024]; | |
365 | unsigned int Len = snprintf(S,sizeof(S),"101 Log\nURI: %s\n" | |
366 | "Message: ",CurrentURI.c_str()); | |
367 | ||
368 | vsnprintf(S+Len,sizeof(S)-Len,Format,args); | |
369 | strcat(S,"\n\n"); | |
370 | ||
371 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
372 | exit(100); | |
373 | } | |
374 | /*}}}*/ | |
375 | // AcqMethod::Status - Send a status message /*{{{*/ | |
376 | // --------------------------------------------------------------------- | |
377 | /* */ | |
378 | void pkgAcqMethod::Status(const char *Format,...) | |
379 | { | |
380 | string CurrentURI = "<UNKNOWN>"; | |
381 | if (Queue != 0) | |
382 | CurrentURI = Queue->Uri; | |
383 | ||
384 | va_list args; | |
385 | va_start(args,Format); | |
386 | ||
387 | // sprintf the description | |
388 | char S[1024]; | |
8267fe24 | 389 | unsigned int Len = snprintf(S,sizeof(S),"102 Status\nURI: %s\n" |
be4401bf AL |
390 | "Message: ",CurrentURI.c_str()); |
391 | ||
392 | vsnprintf(S+Len,sizeof(S)-Len,Format,args); | |
393 | strcat(S,"\n\n"); | |
394 | ||
395 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
396 | exit(100); | |
397 | } | |
398 | /*}}}*/ | |
399 | ||
93bf083d AL |
400 | // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/ |
401 | // --------------------------------------------------------------------- | |
402 | /* */ | |
93274b8d AL |
403 | pkgAcqMethod::FetchResult::FetchResult() : LastModified(0), |
404 | IMSHit(false), Size(0), ResumePoint(0) | |
93bf083d AL |
405 | { |
406 | } | |
407 | /*}}}*/ | |
408 |