]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-method.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-method.cc,v 1.12 1998/12/04 22:56:50 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);
54 // AcqMethod::Fail - A fetch has failed /*{{{*/
55 // ---------------------------------------------------------------------
57 void pkgAcqMethod::Fail(bool Transient
)
59 string Err
= "Undetermined Error";
60 if (_error
->empty() == false)
61 _error
->PopMessage(Err
);
66 // AcqMethod::Fail - A fetch has failed /*{{{*/
67 // ---------------------------------------------------------------------
69 void pkgAcqMethod::Fail(string Err
,bool Transient
)
74 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: %s\n"
75 "Message: %s\n",Queue
->Uri
.c_str(),Err
.c_str());
78 FetchItem
*Tmp
= Queue
;
83 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: <UNKNOWN>\n"
84 "Message: %s\n",Err
.c_str());
86 // Set the transient flag
87 if (Transient
== true)
88 strcat(S
,"Transient-Failure: true\n\n");
92 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
96 // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
97 // ---------------------------------------------------------------------
99 void pkgAcqMethod::URIStart(FetchResult
&Res
)
107 End
+= snprintf(S
,sizeof(S
),"200 URI Start\nURI: %s\n",Queue
->Uri
.c_str());
109 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
111 if (Res
.LastModified
!= 0)
112 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
113 TimeRFC1123(Res
.LastModified
).c_str());
115 if (Res
.ResumePoint
!= 0)
116 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
120 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
124 // AcqMethod::URIDone - A URI is finished /*{{{*/
125 // ---------------------------------------------------------------------
127 void pkgAcqMethod::URIDone(FetchResult
&Res
, FetchResult
*Alt
)
135 End
+= snprintf(S
,sizeof(S
),"201 URI Done\nURI: %s\n",Queue
->Uri
.c_str());
137 if (Res
.Filename
.empty() == false)
138 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Filename: %s\n",Res
.Filename
.c_str());
141 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
143 if (Res
.LastModified
!= 0)
144 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
145 TimeRFC1123(Res
.LastModified
).c_str());
147 if (Res
.MD5Sum
.empty() == false)
148 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"MD5Sum: %s\n",Res
.MD5Sum
.c_str());
150 if (Res
.ResumePoint
!= 0)
151 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
154 if (Res
.IMSHit
== true)
155 strcat(End
,"IMS-Hit: true\n");
160 if (Alt
->Filename
.empty() == false)
161 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Filename: %s\n",Alt
->Filename
.c_str());
164 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Size: %u\n",Alt
->Size
);
166 if (Alt
->LastModified
!= 0)
167 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Last-Modified: %s\n",
168 TimeRFC1123(Alt
->LastModified
).c_str());
170 if (Alt
->MD5Sum
.empty() == false)
171 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-MD5Sum: %s\n",
172 Alt
->MD5Sum
.c_str());
174 if (Alt
->IMSHit
== true)
175 strcat(End
,"Alt-IMS-Hit: true\n");
179 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
183 FetchItem
*Tmp
= Queue
;
188 // AcqMethod::MediaFail - Syncronous request for new media /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This sends a 403 Media Failure message to the APT and waits for it
192 bool pkgAcqMethod::MediaFail(string Required
,string Drive
)
195 snprintf(S
,sizeof(S
),"403 Media Failure\nMedia: %s\nDrive: %s\n\n",
196 Required
.c_str(),Drive
.c_str());
198 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
201 vector
<string
> MyMessages
;
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 */
207 if (WaitFd(STDIN_FILENO
) == false)
210 if (ReadMessages(STDIN_FILENO
,MyMessages
) == false)
213 string Message
= MyMessages
.front();
214 MyMessages
.erase(MyMessages
.begin());
216 // Fetch the message number
218 int Number
= strtol(Message
.c_str(),&End
,10);
219 if (End
== Message
.c_str())
221 cerr
<< "Malformed message!" << endl
;
228 while (Message
.empty() == false)
230 Messages
.push_back(MyMessages
.front());
231 MyMessages
.erase(MyMessages
.begin());
234 return StringToBool(LookupTag(Message
,"Fail"),false);
237 Messages
.push_back(Message
);
241 // AcqMethod::Configuration - Handle the configuration message /*{{{*/
242 // ---------------------------------------------------------------------
243 /* This parses each configuration entry and puts it into the _config
244 Configuration class. */
245 bool pkgAcqMethod::Configuration(string Message
)
247 ::Configuration
&Cnf
= *_config
;
249 const char *I
= Message
.begin();
251 unsigned int Length
= strlen("Config-Item");
252 for (; I
+ Length
< Message
.end(); I
++)
255 if (I
[Length
] != ':' || stringcasecmp(I
,I
+Length
,"Config-Item") != 0)
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
++);
268 Cnf
.Set(string(I
,Equals
-I
),string(Equals
+1,End
-Equals
-1));
275 // AcqMethod::Run - Run the message engine /*{{{*/
276 // ---------------------------------------------------------------------
278 int pkgAcqMethod::Run(bool Single
)
282 // Block if the message queue is empty
283 if (Messages
.empty() == true)
286 if (WaitFd(STDIN_FILENO
) == false)
289 if (ReadMessages(STDIN_FILENO
,Messages
) == false)
293 // Single mode exits if the message queue is empty
294 if (Single
== true && Messages
.empty() == true)
297 string Message
= Messages
.front();
298 Messages
.erase(Messages
.begin());
300 // Fetch the message number
302 int Number
= strtol(Message
.c_str(),&End
,10);
303 if (End
== Message
.c_str())
305 cerr
<< "Malformed message!" << endl
;
312 if (Configuration(Message
) == false)
318 FetchItem
*Tmp
= new FetchItem
;
320 Tmp
->Uri
= LookupTag(Message
,"URI");
321 Tmp
->DestFile
= LookupTag(Message
,"FileName");
322 if (StrToTime(LookupTag(Message
,"Last-Modified"),Tmp
->LastModified
) == false)
323 Tmp
->LastModified
= 0;
324 Tmp
->IndexFile
= StringToBool(LookupTag(Message
,"Index-File"),false);
327 // Append it to the list
328 FetchItem
**I
= &Queue
;
329 for (; *I
!= 0; I
= &(*I
)->Next
);
332 // Notify that this item is to be fetched.
333 if (Fetch(Tmp
) == false)
344 // AcqMethod::Log - Send a log message /*{{{*/
345 // ---------------------------------------------------------------------
347 void pkgAcqMethod::Log(const char *Format
,...)
349 string CurrentURI
= "<UNKNOWN>";
351 CurrentURI
= Queue
->Uri
;
354 va_start(args
,Format
);
356 // sprintf the description
358 unsigned int Len
= snprintf(S
,sizeof(S
),"101 Log\nURI: %s\n"
359 "Message: ",CurrentURI
.c_str());
361 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
364 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
368 // AcqMethod::Status - Send a status message /*{{{*/
369 // ---------------------------------------------------------------------
371 void pkgAcqMethod::Status(const char *Format
,...)
373 string CurrentURI
= "<UNKNOWN>";
375 CurrentURI
= Queue
->Uri
;
378 va_start(args
,Format
);
380 // sprintf the description
382 unsigned int Len
= snprintf(S
,sizeof(S
),"102 Status\nURI: %s\n"
383 "Message: ",CurrentURI
.c_str());
385 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
388 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
393 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
394 // ---------------------------------------------------------------------
396 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
397 IMSHit(false), Size(0), ResumePoint(0)