]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-method.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-method.cc,v 1.14 1998/12/05 04:19:01 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>
18 #include <apt-pkg/fileutl.h>
23 // AcqMethod::pkgAcqMethod - Constructor /*{{{*/
24 // ---------------------------------------------------------------------
25 /* This constructs the initialization text */
26 pkgAcqMethod::pkgAcqMethod(const char *Ver
,unsigned long Flags
)
30 strcat(End
,"100 Capabilities\n");
31 sprintf(End
+strlen(End
),"Version: %s\n",Ver
);
33 if ((Flags
& SingleInstance
) == SingleInstance
)
34 strcat(End
,"Single-Instance: true\n");
36 if ((Flags
& Pipeline
) == Pipeline
)
37 strcat(End
,"Pipeline: true\n");
39 if ((Flags
& SendConfig
) == SendConfig
)
40 strcat(End
,"Send-Config: true\n");
42 if ((Flags
& LocalOnly
) == LocalOnly
)
43 strcat(End
,"Local-Only: true\n");
46 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
49 SetNonBlock(STDIN_FILENO
,true);
55 // AcqMethod::Fail - A fetch has failed /*{{{*/
56 // ---------------------------------------------------------------------
58 void pkgAcqMethod::Fail(bool Transient
)
60 string Err
= "Undetermined Error";
61 if (_error
->empty() == false)
62 _error
->PopMessage(Err
);
67 // AcqMethod::Fail - A fetch has failed /*{{{*/
68 // ---------------------------------------------------------------------
70 void pkgAcqMethod::Fail(string Err
,bool Transient
)
75 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: %s\n"
76 "Message: %s\n",Queue
->Uri
.c_str(),Err
.c_str());
79 FetchItem
*Tmp
= Queue
;
86 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: <UNKNOWN>\n"
87 "Message: %s\n",Err
.c_str());
89 // Set the transient flag
90 if (Transient
== true)
91 strcat(S
,"Transient-Failure: true\n\n");
95 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
99 // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
100 // ---------------------------------------------------------------------
102 void pkgAcqMethod::URIStart(FetchResult
&Res
)
110 End
+= snprintf(S
,sizeof(S
),"200 URI Start\nURI: %s\n",Queue
->Uri
.c_str());
112 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
114 if (Res
.LastModified
!= 0)
115 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
116 TimeRFC1123(Res
.LastModified
).c_str());
118 if (Res
.ResumePoint
!= 0)
119 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
123 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
127 // AcqMethod::URIDone - A URI is finished /*{{{*/
128 // ---------------------------------------------------------------------
130 void pkgAcqMethod::URIDone(FetchResult
&Res
, FetchResult
*Alt
)
138 End
+= snprintf(S
,sizeof(S
),"201 URI Done\nURI: %s\n",Queue
->Uri
.c_str());
140 if (Res
.Filename
.empty() == false)
141 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Filename: %s\n",Res
.Filename
.c_str());
144 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
146 if (Res
.LastModified
!= 0)
147 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
148 TimeRFC1123(Res
.LastModified
).c_str());
150 if (Res
.MD5Sum
.empty() == false)
151 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"MD5Sum: %s\n",Res
.MD5Sum
.c_str());
153 if (Res
.ResumePoint
!= 0)
154 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
157 if (Res
.IMSHit
== true)
158 strcat(End
,"IMS-Hit: true\n");
163 if (Alt
->Filename
.empty() == false)
164 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Filename: %s\n",Alt
->Filename
.c_str());
167 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Size: %u\n",Alt
->Size
);
169 if (Alt
->LastModified
!= 0)
170 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Last-Modified: %s\n",
171 TimeRFC1123(Alt
->LastModified
).c_str());
173 if (Alt
->MD5Sum
.empty() == false)
174 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-MD5Sum: %s\n",
175 Alt
->MD5Sum
.c_str());
177 if (Alt
->IMSHit
== true)
178 strcat(End
,"Alt-IMS-Hit: true\n");
182 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
186 FetchItem
*Tmp
= Queue
;
189 if (Tmp
== QueueBack
)
193 // AcqMethod::MediaFail - Syncronous request for new media /*{{{*/
194 // ---------------------------------------------------------------------
195 /* This sends a 403 Media Failure message to the APT and waits for it
197 bool pkgAcqMethod::MediaFail(string Required
,string Drive
)
200 snprintf(S
,sizeof(S
),"403 Media Failure\nMedia: %s\nDrive: %s\n\n",
201 Required
.c_str(),Drive
.c_str());
203 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
206 vector
<string
> MyMessages
;
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 */
212 if (WaitFd(STDIN_FILENO
) == false)
215 if (ReadMessages(STDIN_FILENO
,MyMessages
) == false)
218 string Message
= MyMessages
.front();
219 MyMessages
.erase(MyMessages
.begin());
221 // Fetch the message number
223 int Number
= strtol(Message
.c_str(),&End
,10);
224 if (End
== Message
.c_str())
226 cerr
<< "Malformed message!" << endl
;
233 while (MyMessages
.empty() == false)
235 Messages
.push_back(MyMessages
.front());
236 MyMessages
.erase(MyMessages
.begin());
239 return !StringToBool(LookupTag(Message
,"Fail"),false);
242 Messages
.push_back(Message
);
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
)
252 ::Configuration
&Cnf
= *_config
;
254 const char *I
= Message
.begin();
256 unsigned int Length
= strlen("Config-Item");
257 for (; I
+ Length
< Message
.end(); I
++)
260 if (I
[Length
] != ':' || stringcasecmp(I
,I
+Length
,"Config-Item") != 0)
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
++);
273 Cnf
.Set(string(I
,Equals
-I
),string(Equals
+1,End
-Equals
-1));
280 // AcqMethod::Run - Run the message engine /*{{{*/
281 // ---------------------------------------------------------------------
283 int pkgAcqMethod::Run(bool Single
)
287 // Block if the message queue is empty
288 if (Messages
.empty() == true)
291 if (WaitFd(STDIN_FILENO
) == false)
294 if (ReadMessages(STDIN_FILENO
,Messages
) == false)
298 // Single mode exits if the message queue is empty
299 if (Single
== true && Messages
.empty() == true)
302 string Message
= Messages
.front();
303 Messages
.erase(Messages
.begin());
305 // Fetch the message number
307 int Number
= strtol(Message
.c_str(),&End
,10);
308 if (End
== Message
.c_str())
310 cerr
<< "Malformed message!" << endl
;
317 if (Configuration(Message
) == false)
323 FetchItem
*Tmp
= new FetchItem
;
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);
332 // Append it to the list
333 FetchItem
**I
= &Queue
;
334 for (; *I
!= 0; I
= &(*I
)->Next
);
339 // Notify that this item is to be fetched.
340 if (Fetch(Tmp
) == false)
351 // AcqMethod::Log - Send a log message /*{{{*/
352 // ---------------------------------------------------------------------
354 void pkgAcqMethod::Log(const char *Format
,...)
356 string CurrentURI
= "<UNKNOWN>";
358 CurrentURI
= Queue
->Uri
;
361 va_start(args
,Format
);
363 // sprintf the description
365 unsigned int Len
= snprintf(S
,sizeof(S
),"101 Log\nURI: %s\n"
366 "Message: ",CurrentURI
.c_str());
368 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
371 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
375 // AcqMethod::Status - Send a status message /*{{{*/
376 // ---------------------------------------------------------------------
378 void pkgAcqMethod::Status(const char *Format
,...)
380 string CurrentURI
= "<UNKNOWN>";
382 CurrentURI
= Queue
->Uri
;
385 va_start(args
,Format
);
387 // sprintf the description
389 unsigned int Len
= snprintf(S
,sizeof(S
),"102 Status\nURI: %s\n"
390 "Message: ",CurrentURI
.c_str());
392 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
395 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
400 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
401 // ---------------------------------------------------------------------
403 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
404 IMSHit(false), Size(0), ResumePoint(0)