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