]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-method.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-method.cc,v 1.17 1999/02/08 07:30:49 jgg Exp $
4 /* ######################################################################
8 ##################################################################### */
10 // Include Files /*{{{*/
12 #pragma implementation "apt-pkg/acquire-method.h"
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>
24 // AcqMethod::pkgAcqMethod - Constructor /*{{{*/
25 // ---------------------------------------------------------------------
26 /* This constructs the initialization text */
27 pkgAcqMethod::pkgAcqMethod(const char *Ver
,unsigned long Flags
)
31 strcat(End
,"100 Capabilities\n");
32 sprintf(End
+strlen(End
),"Version: %s\n",Ver
);
34 if ((Flags
& SingleInstance
) == SingleInstance
)
35 strcat(End
,"Single-Instance: true\n");
37 if ((Flags
& Pipeline
) == Pipeline
)
38 strcat(End
,"Pipeline: true\n");
40 if ((Flags
& SendConfig
) == SendConfig
)
41 strcat(End
,"Send-Config: true\n");
43 if ((Flags
& LocalOnly
) == LocalOnly
)
44 strcat(End
,"Local-Only: true\n");
47 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
50 SetNonBlock(STDIN_FILENO
,true);
56 // AcqMethod::Fail - A fetch has failed /*{{{*/
57 // ---------------------------------------------------------------------
59 void pkgAcqMethod::Fail(bool Transient
)
61 string Err
= "Undetermined Error";
62 if (_error
->empty() == false)
63 _error
->PopMessage(Err
);
68 // AcqMethod::Fail - A fetch has failed /*{{{*/
69 // ---------------------------------------------------------------------
71 void pkgAcqMethod::Fail(string Err
,bool Transient
)
76 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: %s\n"
77 "Message: %s\n",Queue
->Uri
.c_str(),Err
.c_str());
80 FetchItem
*Tmp
= Queue
;
87 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: <UNKNOWN>\n"
88 "Message: %s\n",Err
.c_str());
90 // Set the transient flag
91 if (Transient
== true)
92 strcat(S
,"Transient-Failure: true\n\n");
96 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
100 // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
101 // ---------------------------------------------------------------------
103 void pkgAcqMethod::URIStart(FetchResult
&Res
)
111 End
+= snprintf(S
,sizeof(S
),"200 URI Start\nURI: %s\n",Queue
->Uri
.c_str());
113 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
115 if (Res
.LastModified
!= 0)
116 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
117 TimeRFC1123(Res
.LastModified
).c_str());
119 if (Res
.ResumePoint
!= 0)
120 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
124 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
128 // AcqMethod::URIDone - A URI is finished /*{{{*/
129 // ---------------------------------------------------------------------
131 void pkgAcqMethod::URIDone(FetchResult
&Res
, FetchResult
*Alt
)
139 End
+= snprintf(S
,sizeof(S
),"201 URI Done\nURI: %s\n",Queue
->Uri
.c_str());
141 if (Res
.Filename
.empty() == false)
142 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Filename: %s\n",Res
.Filename
.c_str());
145 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
147 if (Res
.LastModified
!= 0)
148 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
149 TimeRFC1123(Res
.LastModified
).c_str());
151 if (Res
.MD5Sum
.empty() == false)
152 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"MD5-Hash: %s\n",Res
.MD5Sum
.c_str());
154 if (Res
.ResumePoint
!= 0)
155 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
158 if (Res
.IMSHit
== true)
159 strcat(End
,"IMS-Hit: true\n");
164 if (Alt
->Filename
.empty() == false)
165 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Filename: %s\n",Alt
->Filename
.c_str());
168 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Size: %u\n",Alt
->Size
);
170 if (Alt
->LastModified
!= 0)
171 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Last-Modified: %s\n",
172 TimeRFC1123(Alt
->LastModified
).c_str());
174 if (Alt
->MD5Sum
.empty() == false)
175 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-MD5-Hash: %s\n",
176 Alt
->MD5Sum
.c_str());
178 if (Alt
->IMSHit
== true)
179 strcat(End
,"Alt-IMS-Hit: true\n");
183 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
187 FetchItem
*Tmp
= Queue
;
190 if (Tmp
== QueueBack
)
194 // AcqMethod::MediaFail - Syncronous request for new media /*{{{*/
195 // ---------------------------------------------------------------------
196 /* This sends a 403 Media Failure message to the APT and waits for it
198 bool pkgAcqMethod::MediaFail(string Required
,string Drive
)
201 snprintf(S
,sizeof(S
),"403 Media Failure\nMedia: %s\nDrive: %s\n\n",
202 Required
.c_str(),Drive
.c_str());
204 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
207 vector
<string
> MyMessages
;
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 */
213 if (WaitFd(STDIN_FILENO
) == false)
216 if (ReadMessages(STDIN_FILENO
,MyMessages
) == false)
219 string Message
= MyMessages
.front();
220 MyMessages
.erase(MyMessages
.begin());
222 // Fetch the message number
224 int Number
= strtol(Message
.c_str(),&End
,10);
225 if (End
== Message
.c_str())
227 cerr
<< "Malformed message!" << endl
;
234 while (MyMessages
.empty() == false)
236 Messages
.push_back(MyMessages
.front());
237 MyMessages
.erase(MyMessages
.begin());
240 return !StringToBool(LookupTag(Message
,"Fail"),false);
243 Messages
.push_back(Message
);
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
)
253 ::Configuration
&Cnf
= *_config
;
255 const char *I
= Message
.begin();
257 unsigned int Length
= strlen("Config-Item");
258 for (; I
+ Length
< Message
.end(); I
++)
261 if (I
[Length
] != ':' || stringcasecmp(I
,I
+Length
,"Config-Item") != 0)
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
++);
274 Cnf
.Set(string(I
,Equals
-I
),string(Equals
+1,End
-Equals
-1));
281 // AcqMethod::Run - Run the message engine /*{{{*/
282 // ---------------------------------------------------------------------
284 int pkgAcqMethod::Run(bool Single
)
288 // Block if the message queue is empty
289 if (Messages
.empty() == true)
292 if (WaitFd(STDIN_FILENO
) == false)
295 if (ReadMessages(STDIN_FILENO
,Messages
) == false)
299 // Single mode exits if the message queue is empty
300 if (Single
== true && Messages
.empty() == true)
303 string Message
= Messages
.front();
304 Messages
.erase(Messages
.begin());
306 // Fetch the message number
308 int Number
= strtol(Message
.c_str(),&End
,10);
309 if (End
== Message
.c_str())
311 cerr
<< "Malformed message!" << endl
;
318 if (Configuration(Message
) == false)
324 FetchItem
*Tmp
= new FetchItem
;
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);
333 // Append it to the list
334 FetchItem
**I
= &Queue
;
335 for (; *I
!= 0; I
= &(*I
)->Next
);
340 // Notify that this item is to be fetched.
341 if (Fetch(Tmp
) == false)
352 // AcqMethod::Log - Send a log message /*{{{*/
353 // ---------------------------------------------------------------------
355 void pkgAcqMethod::Log(const char *Format
,...)
357 string CurrentURI
= "<UNKNOWN>";
359 CurrentURI
= Queue
->Uri
;
362 va_start(args
,Format
);
364 // sprintf the description
366 unsigned int Len
= snprintf(S
,sizeof(S
),"101 Log\nURI: %s\n"
367 "Message: ",CurrentURI
.c_str());
369 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
372 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
376 // AcqMethod::Status - Send a status message /*{{{*/
377 // ---------------------------------------------------------------------
379 void pkgAcqMethod::Status(const char *Format
,...)
381 string CurrentURI
= "<UNKNOWN>";
383 CurrentURI
= Queue
->Uri
;
386 va_start(args
,Format
);
388 // sprintf the description
390 unsigned int Len
= snprintf(S
,sizeof(S
),"102 Status\nURI: %s\n"
391 "Message: ",CurrentURI
.c_str());
393 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
396 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
401 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
402 // ---------------------------------------------------------------------
404 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
405 IMSHit(false), Size(0), ResumePoint(0)