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