]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-method.cc
8d26537fa4e64d28e8b1b5fea1a552ccbe8575fd
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-method.cc,v 1.3 1998/11/01 08:07:11 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");
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()
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
)
74 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: %s\n"
75 "Message: %s\n\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\n",Err
.c_str());
86 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
90 // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
91 // ---------------------------------------------------------------------
93 void pkgAcqMethod::URIStart(FetchResult
&Res
)
101 End
+= snprintf(S
,sizeof(S
),"200 URI Start\nURI: %s\n",Queue
->Uri
.c_str());
103 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
105 if (Res
.LastModified
!= 0)
106 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
107 TimeRFC1123(Res
.LastModified
).c_str());
109 if (Res
.ResumePoint
!= 0)
110 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
114 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
118 // AcqMethod::URIDone - A URI is finished /*{{{*/
119 // ---------------------------------------------------------------------
121 void pkgAcqMethod::URIDone(FetchResult
&Res
, FetchResult
*Alt
)
129 End
+= snprintf(S
,sizeof(S
),"201 URI Done\nURI: %s\n",Queue
->Uri
.c_str());
131 if (Res
.Filename
.empty() == false)
132 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Filename: %s\n",Res
.Filename
.c_str());
135 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
137 if (Res
.LastModified
!= 0)
138 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
139 TimeRFC1123(Res
.LastModified
).c_str());
141 if (Res
.MD5Sum
.empty() == false)
142 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"MD5Sum: %s\n",Res
.MD5Sum
.c_str());
144 if (Res
.IMSHit
== true)
145 strcat(End
,"IMS-Hit: true\n");
150 if (Alt
->Filename
.empty() == false)
151 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Filename: %s\n",Alt
->Filename
.c_str());
154 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Size: %u\n",Alt
->Size
);
156 if (Alt
->LastModified
!= 0)
157 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Last-Modified: %s\n",
158 TimeRFC1123(Alt
->LastModified
).c_str());
160 if (Alt
->MD5Sum
.empty() == false)
161 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-MD5Sum: %s\n",
162 Alt
->MD5Sum
.c_str());
164 if (Alt
->IMSHit
== true)
165 strcat(End
,"Alt-IMS-Hit: true\n");
169 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
173 FetchItem
*Tmp
= Queue
;
178 // AcqMethod::Configuration - Handle the configuration message /*{{{*/
179 // ---------------------------------------------------------------------
180 /* This parses each configuration entry and puts it into the _config
181 Configuration class. */
182 bool pkgAcqMethod::Configuration(string Message
)
184 ::Configuration
&Cnf
= *_config
;
186 const char *I
= Message
.begin();
188 unsigned int Length
= strlen("Config-Item");
189 for (; I
+ Length
< Message
.end(); I
++)
192 if (I
[Length
] != ':' || stringcasecmp(I
,I
+Length
,"Config-Item") != 0)
197 for (; I
< Message
.end() && *I
== ' '; I
++);
198 const char *Equals
= I
;
199 for (; Equals
< Message
.end() && *Equals
!= '='; Equals
++);
200 const char *End
= Equals
;
201 for (; End
< Message
.end() && *End
!= '\n'; End
++);
205 Cnf
.Set(string(I
,Equals
-I
),string(Equals
+1,End
-Equals
-1));
212 // AcqMethod::Run - Run the message engine /*{{{*/
213 // ---------------------------------------------------------------------
215 int pkgAcqMethod::Run(bool Single
)
219 // Block if the message queue is empty
220 if (Messages
.empty() == true)
223 if (WaitFd(STDIN_FILENO
) == false)
226 if (ReadMessages(STDIN_FILENO
,Messages
) == false)
230 // Single mode exits if the message queue is empty
231 if (Single
== true && Messages
.empty() == true)
234 string Message
= Messages
.front();
235 Messages
.erase(Messages
.begin());
237 // Fetch the message number
239 int Number
= strtol(Message
.c_str(),&End
,10);
240 if (End
== Message
.c_str())
242 cerr
<< "Malformed message!" << endl
;
249 if (Configuration(Message
) == false)
255 FetchItem
*Tmp
= new FetchItem
;
257 Tmp
->Uri
= LookupTag(Message
,"URI");
258 Tmp
->DestFile
= LookupTag(Message
,"FileName");
259 StrToTime(LookupTag(Message
,"Last-Modified"),Tmp
->LastModified
);
262 // Append it to the list
263 FetchItem
**I
= &Queue
;
264 for (; *I
!= 0; I
= &(*I
)->Next
);
266 cout
<< "GOT " << Tmp
->Uri
<< endl
;
268 if (Fetch(Tmp
) == false)
278 // AcqMethod::Log - Send a log message /*{{{*/
279 // ---------------------------------------------------------------------
281 void pkgAcqMethod::Log(const char *Format
,...)
283 string CurrentURI
= "<UNKNOWN>";
285 CurrentURI
= Queue
->Uri
;
288 va_start(args
,Format
);
290 // sprintf the description
292 unsigned int Len
= snprintf(S
,sizeof(S
),"101 Log\nURI: %s\n"
293 "Message: ",CurrentURI
.c_str());
295 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
298 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
302 // AcqMethod::Status - Send a status message /*{{{*/
303 // ---------------------------------------------------------------------
305 void pkgAcqMethod::Status(const char *Format
,...)
307 string CurrentURI
= "<UNKNOWN>";
309 CurrentURI
= Queue
->Uri
;
312 va_start(args
,Format
);
314 // sprintf the description
316 unsigned int Len
= snprintf(S
,sizeof(S
),"101 Status\nURI: %s\n"
317 "Message: ",CurrentURI
.c_str());
319 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
322 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
327 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
328 // ---------------------------------------------------------------------
330 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
331 IMSHit(false), Size(0)