]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-method.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-method.cc,v 1.8 1998/11/14 01:39:41 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
& PreScan
) == PreScan
)
37 strcat(End
,"Pre-Scan: true\n");
39 if ((Flags
& Pipeline
) == Pipeline
)
40 strcat(End
,"Pipeline: true\n");
42 if ((Flags
& SendConfig
) == SendConfig
)
43 strcat(End
,"Send-Config: true\n");
45 if ((Flags
& LocalOnly
) == LocalOnly
)
46 strcat(End
,"Local-Only: true\n");
49 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
52 SetNonBlock(STDIN_FILENO
,true);
57 // AcqMethod::Fail - A fetch has failed /*{{{*/
58 // ---------------------------------------------------------------------
60 void pkgAcqMethod::Fail()
62 string Err
= "Undetermined Error";
63 if (_error
->empty() == false)
64 _error
->PopMessage(Err
);
69 // AcqMethod::Fail - A fetch has failed /*{{{*/
70 // ---------------------------------------------------------------------
72 void pkgAcqMethod::Fail(string Err
)
77 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: %s\n"
78 "Message: %s\n\n",Queue
->Uri
.c_str(),Err
.c_str());
81 FetchItem
*Tmp
= Queue
;
86 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: <UNKNOWN>\n"
87 "Message: %s\n\n",Err
.c_str());
89 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
93 // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
94 // ---------------------------------------------------------------------
96 void pkgAcqMethod::URIStart(FetchResult
&Res
)
104 End
+= snprintf(S
,sizeof(S
),"200 URI Start\nURI: %s\n",Queue
->Uri
.c_str());
106 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
108 if (Res
.LastModified
!= 0)
109 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
110 TimeRFC1123(Res
.LastModified
).c_str());
112 if (Res
.ResumePoint
!= 0)
113 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
117 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
121 // AcqMethod::URIDone - A URI is finished /*{{{*/
122 // ---------------------------------------------------------------------
124 void pkgAcqMethod::URIDone(FetchResult
&Res
, FetchResult
*Alt
)
132 End
+= snprintf(S
,sizeof(S
),"201 URI Done\nURI: %s\n",Queue
->Uri
.c_str());
134 if (Res
.Filename
.empty() == false)
135 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Filename: %s\n",Res
.Filename
.c_str());
138 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
140 if (Res
.LastModified
!= 0)
141 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
142 TimeRFC1123(Res
.LastModified
).c_str());
144 if (Res
.MD5Sum
.empty() == false)
145 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"MD5Sum: %s\n",Res
.MD5Sum
.c_str());
147 if (Res
.ResumePoint
!= 0)
148 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
151 if (Res
.IMSHit
== true)
152 strcat(End
,"IMS-Hit: true\n");
157 if (Alt
->Filename
.empty() == false)
158 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Filename: %s\n",Alt
->Filename
.c_str());
161 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Size: %u\n",Alt
->Size
);
163 if (Alt
->LastModified
!= 0)
164 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Last-Modified: %s\n",
165 TimeRFC1123(Alt
->LastModified
).c_str());
167 if (Alt
->MD5Sum
.empty() == false)
168 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-MD5Sum: %s\n",
169 Alt
->MD5Sum
.c_str());
171 if (Alt
->IMSHit
== true)
172 strcat(End
,"Alt-IMS-Hit: true\n");
176 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
180 FetchItem
*Tmp
= Queue
;
185 // AcqMethod::Configuration - Handle the configuration message /*{{{*/
186 // ---------------------------------------------------------------------
187 /* This parses each configuration entry and puts it into the _config
188 Configuration class. */
189 bool pkgAcqMethod::Configuration(string Message
)
191 ::Configuration
&Cnf
= *_config
;
193 const char *I
= Message
.begin();
195 unsigned int Length
= strlen("Config-Item");
196 for (; I
+ Length
< Message
.end(); I
++)
199 if (I
[Length
] != ':' || stringcasecmp(I
,I
+Length
,"Config-Item") != 0)
204 for (; I
< Message
.end() && *I
== ' '; I
++);
205 const char *Equals
= I
;
206 for (; Equals
< Message
.end() && *Equals
!= '='; Equals
++);
207 const char *End
= Equals
;
208 for (; End
< Message
.end() && *End
!= '\n'; End
++);
212 Cnf
.Set(string(I
,Equals
-I
),string(Equals
+1,End
-Equals
-1));
219 // AcqMethod::Run - Run the message engine /*{{{*/
220 // ---------------------------------------------------------------------
222 int pkgAcqMethod::Run(bool Single
)
226 // Block if the message queue is empty
227 if (Messages
.empty() == true)
230 if (WaitFd(STDIN_FILENO
) == false)
233 if (ReadMessages(STDIN_FILENO
,Messages
) == false)
237 // Single mode exits if the message queue is empty
238 if (Single
== true && Messages
.empty() == true)
241 string Message
= Messages
.front();
242 Messages
.erase(Messages
.begin());
244 // Fetch the message number
246 int Number
= strtol(Message
.c_str(),&End
,10);
247 if (End
== Message
.c_str())
249 cerr
<< "Malformed message!" << endl
;
256 if (Configuration(Message
) == false)
262 FetchItem
*Tmp
= new FetchItem
;
264 Tmp
->Uri
= LookupTag(Message
,"URI");
265 Tmp
->DestFile
= LookupTag(Message
,"FileName");
266 if (StrToTime(LookupTag(Message
,"Last-Modified"),Tmp
->LastModified
) == false)
267 Tmp
->LastModified
= 0;
271 // Append it to the list
272 FetchItem
**I
= &Queue
;
273 for (; *I
!= 0; I
= &(*I
)->Next
);
276 // Notify that this item is to be fetched.
277 if (Fetch(Tmp
) == false)
288 // AcqMethod::Log - Send a log message /*{{{*/
289 // ---------------------------------------------------------------------
291 void pkgAcqMethod::Log(const char *Format
,...)
293 string CurrentURI
= "<UNKNOWN>";
295 CurrentURI
= Queue
->Uri
;
298 va_start(args
,Format
);
300 // sprintf the description
302 unsigned int Len
= snprintf(S
,sizeof(S
),"101 Log\nURI: %s\n"
303 "Message: ",CurrentURI
.c_str());
305 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
308 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
312 // AcqMethod::Status - Send a status message /*{{{*/
313 // ---------------------------------------------------------------------
315 void pkgAcqMethod::Status(const char *Format
,...)
317 string CurrentURI
= "<UNKNOWN>";
319 CurrentURI
= Queue
->Uri
;
322 va_start(args
,Format
);
324 // sprintf the description
326 unsigned int Len
= snprintf(S
,sizeof(S
),"102 Status\nURI: %s\n"
327 "Message: ",CurrentURI
.c_str());
329 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
332 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
337 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
338 // ---------------------------------------------------------------------
340 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
341 IMSHit(false), Size(0), ResumePoint(0)