]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-method.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-method.cc,v 1.27.2.1 2003/12/24 23:09:17 mdz Exp $
4 /* ######################################################################
8 This is a skeleton class that implements most of the functionality
9 of a method and some useful functions to make method implementation
10 simpler. The methods all derive this and specialize it. The most
11 complex implementation is the http method which needs to provide
12 pipelining, it runs the message engine at the same time it is
15 ##################################################################### */
17 // Include Files /*{{{*/
20 #include <apt-pkg/acquire-method.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/configuration.h>
23 #include <apt-pkg/strutl.h>
24 #include <apt-pkg/fileutl.h>
25 #include <apt-pkg/hashes.h>
26 #include <apt-pkg/md5.h>
27 #include <apt-pkg/sha1.h>
28 #include <apt-pkg/sha2.h>
42 // AcqMethod::pkgAcqMethod - Constructor /*{{{*/
43 // ---------------------------------------------------------------------
44 /* This constructs the initialization text */
45 pkgAcqMethod::pkgAcqMethod(const char *Ver
,unsigned long Flags
)
47 std::cout
<< "100 Capabilities\n"
48 << "Version: " << Ver
<< "\n";
50 if ((Flags
& SingleInstance
) == SingleInstance
)
51 std::cout
<< "Single-Instance: true\n";
53 if ((Flags
& Pipeline
) == Pipeline
)
54 std::cout
<< "Pipeline: true\n";
56 if ((Flags
& SendConfig
) == SendConfig
)
57 std::cout
<< "Send-Config: true\n";
59 if ((Flags
& LocalOnly
) == LocalOnly
)
60 std::cout
<<"Local-Only: true\n";
62 if ((Flags
& NeedsCleanup
) == NeedsCleanup
)
63 std::cout
<< "Needs-Cleanup: true\n";
65 if ((Flags
& Removable
) == Removable
)
66 std::cout
<< "Removable: true\n";
68 std::cout
<< "\n" << std::flush
;
70 SetNonBlock(STDIN_FILENO
,true);
76 // AcqMethod::Fail - A fetch has failed /*{{{*/
77 // ---------------------------------------------------------------------
79 void pkgAcqMethod::Fail(bool Transient
)
81 string Err
= "Undetermined Error";
82 if (_error
->empty() == false)
83 _error
->PopMessage(Err
);
88 // AcqMethod::Fail - A fetch has failed /*{{{*/
89 // ---------------------------------------------------------------------
91 void pkgAcqMethod::Fail(string Err
,bool Transient
)
93 // Strip out junk from the error messages
94 for (string::iterator I
= Err
.begin(); I
!= Err
.end(); ++I
)
104 std::cout
<< "400 URI Failure\nURI: " << Queue
->Uri
<< "\n"
105 << "Message: " << Err
<< " " << IP
<< "\n";
109 std::cout
<< "400 URI Failure\nURI: <UNKNOWN>\nMessage: " << Err
<< "\n";
111 if(FailReason
.empty() == false)
112 std::cout
<< "FailReason: " << FailReason
<< "\n";
113 if (UsedMirror
.empty() == false)
114 std::cout
<< "UsedMirror: " << UsedMirror
<< "\n";
115 // Set the transient flag
116 if (Transient
== true)
117 std::cout
<< "Transient-Failure: true\n";
119 std::cout
<< "\n" << std::flush
;
122 // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
123 // ---------------------------------------------------------------------
125 void pkgAcqMethod::URIStart(FetchResult
&Res
)
130 std::cout
<< "200 URI Start\n"
131 << "URI: " << Queue
->Uri
<< "\n";
133 std::cout
<< "Size: " << Res
.Size
<< "\n";
135 if (Res
.LastModified
!= 0)
136 std::cout
<< "Last-Modified: " << TimeRFC1123(Res
.LastModified
) << "\n";
138 if (Res
.ResumePoint
!= 0)
139 std::cout
<< "Resume-Point: " << Res
.ResumePoint
<< "\n";
141 if (UsedMirror
.empty() == false)
142 std::cout
<< "UsedMirror: " << UsedMirror
<< "\n";
144 std::cout
<< "\n" << std::flush
;
147 // AcqMethod::URIDone - A URI is finished /*{{{*/
148 // ---------------------------------------------------------------------
150 void pkgAcqMethod::URIDone(FetchResult
&Res
, FetchResult
*Alt
)
155 std::cout
<< "201 URI Done\n"
156 << "URI: " << Queue
->Uri
<< "\n";
158 if (Res
.Filename
.empty() == false)
159 std::cout
<< "Filename: " << Res
.Filename
<< "\n";
162 std::cout
<< "Size: " << Res
.Size
<< "\n";
164 if (Res
.LastModified
!= 0)
165 std::cout
<< "Last-Modified: " << TimeRFC1123(Res
.LastModified
) << "\n";
167 if (Res
.MD5Sum
.empty() == false)
168 std::cout
<< "MD5-Hash: " << Res
.MD5Sum
<< "\n"
169 << "MD5Sum-Hash: " << Res
.MD5Sum
<< "\n";
170 if (Res
.SHA1Sum
.empty() == false)
171 std::cout
<< "SHA1-Hash: " << Res
.SHA1Sum
<< "\n";
172 if (Res
.SHA256Sum
.empty() == false)
173 std::cout
<< "SHA256-Hash: " << Res
.SHA256Sum
<< "\n";
174 if (Res
.SHA512Sum
.empty() == false)
175 std::cout
<< "SHA512-Hash: " << Res
.SHA512Sum
<< "\n";
176 if (UsedMirror
.empty() == false)
177 std::cout
<< "UsedMirror: " << UsedMirror
<< "\n";
178 if (Res
.GPGVOutput
.empty() == false)
180 std::cout
<< "GPGVOutput:\n";
181 for (vector
<string
>::const_iterator I
= Res
.GPGVOutput
.begin();
182 I
!= Res
.GPGVOutput
.end(); ++I
)
183 std::cout
<< " " << *I
<< "\n";
186 if (Res
.ResumePoint
!= 0)
187 std::cout
<< "Resume-Point: " << Res
.ResumePoint
<< "\n";
189 if (Res
.IMSHit
== true)
190 std::cout
<< "IMS-Hit: true\n";
194 if (Alt
->Filename
.empty() == false)
195 std::cout
<< "Alt-Filename: " << Alt
->Filename
<< "\n";
198 std::cout
<< "Alt-Size: " << Alt
->Size
<< "\n";
200 if (Alt
->LastModified
!= 0)
201 std::cout
<< "Alt-Last-Modified: " << TimeRFC1123(Alt
->LastModified
) << "\n";
203 if (Alt
->MD5Sum
.empty() == false)
204 std::cout
<< "Alt-MD5-Hash: " << Alt
->MD5Sum
<< "\n";
205 if (Alt
->SHA1Sum
.empty() == false)
206 std::cout
<< "Alt-SHA1-Hash: " << Alt
->SHA1Sum
<< "\n";
207 if (Alt
->SHA256Sum
.empty() == false)
208 std::cout
<< "Alt-SHA256-Hash: " << Alt
->SHA256Sum
<< "\n";
209 if (Alt
->SHA512Sum
.empty() == false)
210 std::cout
<< "Alt-SHA512-Hash: " << Alt
->SHA512Sum
<< "\n";
212 if (Alt
->IMSHit
== true)
213 std::cout
<< "Alt-IMS-Hit: true\n";
216 std::cout
<< "\n" << std::flush
;
220 // AcqMethod::MediaFail - Syncronous request for new media /*{{{*/
221 // ---------------------------------------------------------------------
222 /* This sends a 403 Media Failure message to the APT and waits for it
224 bool pkgAcqMethod::MediaFail(string Required
,string Drive
)
226 fprintf(stdout
, "403 Media Failure\nMedia: %s\nDrive: %s\n",
227 Required
.c_str(),Drive
.c_str());
228 std::cout
<< "\n" << std::flush
;
230 vector
<string
> MyMessages
;
232 /* Here we read messages until we find a 603, each non 603 message is
233 appended to the main message list for later processing */
236 if (WaitFd(STDIN_FILENO
) == false)
239 if (ReadMessages(STDIN_FILENO
,MyMessages
) == false)
242 string Message
= MyMessages
.front();
243 MyMessages
.erase(MyMessages
.begin());
245 // Fetch the message number
247 int Number
= strtol(Message
.c_str(),&End
,10);
248 if (End
== Message
.c_str())
250 cerr
<< "Malformed message!" << endl
;
257 while (MyMessages
.empty() == false)
259 Messages
.push_back(MyMessages
.front());
260 MyMessages
.erase(MyMessages
.begin());
263 return !StringToBool(LookupTag(Message
,"Failed"),false);
266 Messages
.push_back(Message
);
270 // AcqMethod::Configuration - Handle the configuration message /*{{{*/
271 // ---------------------------------------------------------------------
272 /* This parses each configuration entry and puts it into the _config
273 Configuration class. */
274 bool pkgAcqMethod::Configuration(string Message
)
276 ::Configuration
&Cnf
= *_config
;
278 const char *I
= Message
.c_str();
279 const char *MsgEnd
= I
+ Message
.length();
281 unsigned int Length
= strlen("Config-Item");
282 for (; I
+ Length
< MsgEnd
; I
++)
285 if (I
[Length
] != ':' || stringcasecmp(I
,I
+Length
,"Config-Item") != 0)
290 for (; I
< MsgEnd
&& *I
== ' '; I
++);
291 const char *Equals
= (const char*) memchr(I
, '=', MsgEnd
- I
);
294 const char *End
= (const char*) memchr(Equals
, '\n', MsgEnd
- Equals
);
298 Cnf
.Set(DeQuoteString(string(I
,Equals
-I
)),
299 DeQuoteString(string(Equals
+1,End
-Equals
-1)));
306 // AcqMethod::Run - Run the message engine /*{{{*/
307 // ---------------------------------------------------------------------
308 /* Fetch any messages and execute them. In single mode it returns 1 if
309 there are no more available messages - any other result is a
310 fatal failure code! */
311 int pkgAcqMethod::Run(bool Single
)
315 // Block if the message queue is empty
316 if (Messages
.empty() == true)
319 if (WaitFd(STDIN_FILENO
) == false)
321 if (ReadMessages(STDIN_FILENO
,Messages
) == false)
325 // Single mode exits if the message queue is empty
326 if (Single
== true && Messages
.empty() == true)
329 string Message
= Messages
.front();
330 Messages
.erase(Messages
.begin());
332 // Fetch the message number
334 int Number
= strtol(Message
.c_str(),&End
,10);
335 if (End
== Message
.c_str())
337 cerr
<< "Malformed message!" << endl
;
344 if (Configuration(Message
) == false)
350 FetchItem
*Tmp
= new FetchItem
;
352 Tmp
->Uri
= LookupTag(Message
,"URI");
353 Tmp
->DestFile
= LookupTag(Message
,"FileName");
354 if (RFC1123StrToTime(LookupTag(Message
,"Last-Modified").c_str(),Tmp
->LastModified
) == false)
355 Tmp
->LastModified
= 0;
356 Tmp
->IndexFile
= StringToBool(LookupTag(Message
,"Index-File"),false);
357 Tmp
->FailIgnore
= StringToBool(LookupTag(Message
,"Fail-Ignore"),false);
360 // Append it to the list
361 FetchItem
**I
= &Queue
;
362 for (; *I
!= 0; I
= &(*I
)->Next
);
367 // Notify that this item is to be fetched.
368 if (Fetch(Tmp
) == false)
380 // AcqMethod::PrintStatus - privately really send a log/status message /*{{{*/
381 // ---------------------------------------------------------------------
383 void pkgAcqMethod::PrintStatus(char const * const header
, const char* Format
,
386 string CurrentURI
= "<UNKNOWN>";
388 CurrentURI
= Queue
->Uri
;
389 if (UsedMirror
.empty() == true)
390 fprintf(stdout
, "%s\nURI: %s\nMessage: ",
391 header
, CurrentURI
.c_str());
393 fprintf(stdout
, "%s\nURI: %s\nUsedMirror: %s\nMessage: ",
394 header
, CurrentURI
.c_str(), UsedMirror
.c_str());
395 vfprintf(stdout
,Format
,args
);
396 std::cout
<< "\n\n" << std::flush
;
399 // AcqMethod::Log - Send a log message /*{{{*/
400 // ---------------------------------------------------------------------
402 void pkgAcqMethod::Log(const char *Format
,...)
405 va_start(args
,Format
);
406 PrintStatus("101 Log", Format
, args
);
410 // AcqMethod::Status - Send a status message /*{{{*/
411 // ---------------------------------------------------------------------
413 void pkgAcqMethod::Status(const char *Format
,...)
416 va_start(args
,Format
);
417 PrintStatus("102 Status", Format
, args
);
421 // AcqMethod::Redirect - Send a redirect message /*{{{*/
422 // ---------------------------------------------------------------------
423 /* This method sends the redirect message and dequeues the item as
424 * the worker will enqueue again later on to the right queue */
425 void pkgAcqMethod::Redirect(const string
&NewURI
)
427 std::cout
<< "103 Redirect\nURI: " << Queue
->Uri
<< "\n"
428 << "New-URI: " << NewURI
<< "\n"
429 << "\n" << std::flush
;
433 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
434 // ---------------------------------------------------------------------
436 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
437 IMSHit(false), Size(0), ResumePoint(0)
441 // AcqMethod::FetchResult::TakeHashes - Load hashes /*{{{*/
442 // ---------------------------------------------------------------------
443 /* This hides the number of hashes we are supporting from the caller.
444 It just deals with the hash class. */
445 void pkgAcqMethod::FetchResult::TakeHashes(Hashes
&Hash
)
447 MD5Sum
= Hash
.MD5
.Result();
448 SHA1Sum
= Hash
.SHA1
.Result();
449 SHA256Sum
= Hash
.SHA256
.Result();
450 SHA512Sum
= Hash
.SHA512
.Result();
453 void pkgAcqMethod::Dequeue() { /*{{{*/
454 FetchItem
const * const Tmp
= Queue
;
456 if (Tmp
== QueueBack
)