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