]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-method.cc
Error handling fix
[apt.git] / apt-pkg / acquire-method.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire-method.cc,v 1.17 1999/02/08 07:30:49 jgg Exp $
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>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/fileutl.h>
19
20 #include <stdio.h>
21 #include <unistd.h>
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
37 if ((Flags & Pipeline) == Pipeline)
38 strcat(End,"Pipeline: true\n");
39
40 if ((Flags & SendConfig) == SendConfig)
41 strcat(End,"Send-Config: true\n");
42
43 if ((Flags & LocalOnly) == LocalOnly)
44 strcat(End,"Local-Only: true\n");
45 strcat(End,"\n");
46
47 if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
48 exit(100);
49
50 SetNonBlock(STDIN_FILENO,true);
51
52 Queue = 0;
53 QueueBack = 0;
54 }
55 /*}}}*/
56 // AcqMethod::Fail - A fetch has failed /*{{{*/
57 // ---------------------------------------------------------------------
58 /* */
59 void pkgAcqMethod::Fail(bool Transient)
60 {
61 string Err = "Undetermined Error";
62 if (_error->empty() == false)
63 _error->PopMessage(Err);
64 _error->Discard();
65 Fail(Err,Transient);
66 }
67 /*}}}*/
68 // AcqMethod::Fail - A fetch has failed /*{{{*/
69 // ---------------------------------------------------------------------
70 /* */
71 void pkgAcqMethod::Fail(string Err,bool Transient)
72 {
73 char S[1024];
74 if (Queue != 0)
75 {
76 snprintf(S,sizeof(S),"400 URI Failure\nURI: %s\n"
77 "Message: %s\n",Queue->Uri.c_str(),Err.c_str());
78
79 // Dequeue
80 FetchItem *Tmp = Queue;
81 Queue = Queue->Next;
82 delete Tmp;
83 if (Tmp == QueueBack)
84 QueueBack = Queue;
85 }
86 else
87 snprintf(S,sizeof(S),"400 URI Failure\nURI: <UNKNOWN>\n"
88 "Message: %s\n",Err.c_str());
89
90 // Set the transient flag
91 if (Transient == true)
92 strcat(S,"Transient-Failure: true\n\n");
93 else
94 strcat(S,"\n");
95
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 /* */
103 void pkgAcqMethod::URIStart(FetchResult &Res)
104 {
105 if (Queue == 0)
106 abort();
107
108 char S[1024] = "";
109 char *End = S;
110
111 End += snprintf(S,sizeof(S),"200 URI Start\nURI: %s\n",Queue->Uri.c_str());
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
119 if (Res.ResumePoint != 0)
120 End += snprintf(End,sizeof(S) - (End - S),"Resume-Point: %u\n",
121 Res.ResumePoint);
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 {
133 if (Queue == 0)
134 abort();
135
136 char S[1024] = "";
137 char *End = S;
138
139 End += snprintf(S,sizeof(S),"201 URI Done\nURI: %s\n",Queue->Uri.c_str());
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)
152 End += snprintf(End,sizeof(S) - (End - S),"MD5-Hash: %s\n",Res.MD5Sum.c_str());
153
154 if (Res.ResumePoint != 0)
155 End += snprintf(End,sizeof(S) - (End - S),"Resume-Point: %u\n",
156 Res.ResumePoint);
157
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)
175 End += snprintf(End,sizeof(S) - (End - S),"Alt-MD5-Hash: %s\n",
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);
185
186 // Dequeue
187 FetchItem *Tmp = Queue;
188 Queue = Queue->Next;
189 delete Tmp;
190 if (Tmp == QueueBack)
191 QueueBack = Queue;
192 }
193 /*}}}*/
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 */
198 bool pkgAcqMethod::MediaFail(string Required,string Drive)
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)
214 return false;
215
216 if (ReadMessages(STDIN_FILENO,MyMessages) == false)
217 return false;
218
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 {
234 while (MyMessages.empty() == false)
235 {
236 Messages.push_back(MyMessages.front());
237 MyMessages.erase(MyMessages.begin());
238 }
239
240 return !StringToBool(LookupTag(Message,"Fail"),false);
241 }
242
243 Messages.push_back(Message);
244 }
245 }
246 /*}}}*/
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 /* */
284 int pkgAcqMethod::Run(bool Single)
285 {
286 while (1)
287 {
288 // Block if the message queue is empty
289 if (Messages.empty() == true)
290 {
291 if (Single == false)
292 if (WaitFd(STDIN_FILENO) == false)
293 return 0;
294
295 if (ReadMessages(STDIN_FILENO,Messages) == false)
296 return 0;
297 }
298
299 // Single mode exits if the message queue is empty
300 if (Single == true && Messages.empty() == true)
301 return 0;
302
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:
323 {
324 FetchItem *Tmp = new FetchItem;
325
326 Tmp->Uri = LookupTag(Message,"URI");
327 Tmp->DestFile = LookupTag(Message,"FileName");
328 if (StrToTime(LookupTag(Message,"Last-Modified"),Tmp->LastModified) == false)
329 Tmp->LastModified = 0;
330 Tmp->IndexFile = StringToBool(LookupTag(Message,"Index-File"),false);
331 Tmp->Next = 0;
332
333 // Append it to the list
334 FetchItem **I = &Queue;
335 for (; *I != 0; I = &(*I)->Next);
336 *I = Tmp;
337 if (QueueBack == 0)
338 QueueBack = Tmp;
339
340 // Notify that this item is to be fetched.
341 if (Fetch(Tmp) == false)
342 Fail();
343
344 break;
345 }
346 }
347 }
348
349 return 0;
350 }
351 /*}}}*/
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];
390 unsigned int Len = snprintf(S,sizeof(S),"102 Status\nURI: %s\n"
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
401 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
402 // ---------------------------------------------------------------------
403 /* */
404 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
405 IMSHit(false), Size(0), ResumePoint(0)
406 {
407 }
408 /*}}}*/
409