]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-method.cc
0629995a0b59e0e08e3c97d68999c8ed03a850b8
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-method.cc,v 1.2 1998/11/01 05:27:30 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);
52 // AcqMethod::Fail - A fetch has failed /*{{{*/
53 // ---------------------------------------------------------------------
55 void pkgAcqMethod::Fail()
57 string Err
= "Undetermined Error";
58 if (_error
->empty() == false)
59 _error
->PopMessage(Err
);
64 // AcqMethod::Fail - A fetch has failed /*{{{*/
65 // ---------------------------------------------------------------------
67 void pkgAcqMethod::Fail(string Err
)
72 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: %s\n"
73 "Message: %s\n\n",Queue
->Uri
.c_str(),Err
.c_str());
76 FetchItem
*Tmp
= Queue
;
81 snprintf(S
,sizeof(S
),"400 URI Failure\nURI: <UNKNOWN>\n"
82 "Message: %s\n\n",Err
.c_str());
84 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
88 // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
89 // ---------------------------------------------------------------------
91 void pkgAcqMethod::URIStart(FetchResult
&Res
)
99 End
+= snprintf(S
,sizeof(S
),"200 URI Start\nURI: %s\n",Queue
->Uri
.c_str());
101 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
103 if (Res
.LastModified
!= 0)
104 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
105 TimeRFC1123(Res
.LastModified
).c_str());
107 if (Res
.ResumePoint
!= 0)
108 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
112 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
116 // AcqMethod::URIDone - A URI is finished /*{{{*/
117 // ---------------------------------------------------------------------
119 void pkgAcqMethod::URIDone(FetchResult
&Res
, FetchResult
*Alt
)
127 End
+= snprintf(S
,sizeof(S
),"201 URI Done\nURI: %s\n",Queue
->Uri
.c_str());
129 if (Res
.Filename
.empty() == false)
130 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Filename: %s\n",Res
.Filename
.c_str());
133 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Size: %u\n",Res
.Size
);
135 if (Res
.LastModified
!= 0)
136 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Last-Modified: %s\n",
137 TimeRFC1123(Res
.LastModified
).c_str());
139 if (Res
.MD5Sum
.empty() == false)
140 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"MD5Sum: %s\n",Res
.MD5Sum
.c_str());
142 if (Res
.IMSHit
== true)
143 strcat(End
,"IMS-Hit: true\n");
148 if (Alt
->Filename
.empty() == false)
149 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Filename: %s\n",Alt
->Filename
.c_str());
152 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Size: %u\n",Alt
->Size
);
154 if (Alt
->LastModified
!= 0)
155 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Last-Modified: %s\n",
156 TimeRFC1123(Alt
->LastModified
).c_str());
158 if (Alt
->MD5Sum
.empty() == false)
159 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-MD5Sum: %s\n",
160 Alt
->MD5Sum
.c_str());
162 if (Alt
->IMSHit
== true)
163 strcat(End
,"Alt-IMS-Hit: true\n");
167 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
171 FetchItem
*Tmp
= Queue
;
176 // AcqMethod::Configuration - Handle the configuration message /*{{{*/
177 // ---------------------------------------------------------------------
178 /* This parses each configuration entry and puts it into the _config
179 Configuration class. */
180 bool pkgAcqMethod::Configuration(string Message
)
182 ::Configuration
&Cnf
= *_config
;
184 const char *I
= Message
.begin();
186 unsigned int Length
= strlen("Config-Item");
187 for (; I
+ Length
< Message
.end(); I
++)
190 if (I
[Length
] != ':' || stringcasecmp(I
,I
+Length
,"Config-Item") != 0)
195 for (; I
< Message
.end() && *I
== ' '; I
++);
196 const char *Equals
= I
;
197 for (; Equals
< Message
.end() && *Equals
!= '='; Equals
++);
198 const char *End
= Equals
;
199 for (; End
< Message
.end() && *End
!= '\n'; End
++);
203 Cnf
.Set(string(I
,Equals
-I
),string(Equals
+1,End
-Equals
-1));
210 // AcqMethod::Run - Run the message engine /*{{{*/
211 // ---------------------------------------------------------------------
213 int pkgAcqMethod::Run(bool Single
)
217 // Block if the message queue is empty
218 if (Messages
.empty() == true)
221 if (WaitFd(STDIN_FILENO
) == false)
225 if (ReadMessages(STDIN_FILENO
,Messages
) == false)
228 // Single mode exits if the message queue is empty
229 if (Single
== true && Messages
.empty() == true)
232 string Message
= Messages
.front();
233 Messages
.erase(Messages
.begin());
235 // Fetch the message number
237 int Number
= strtol(Message
.c_str(),&End
,10);
238 if (End
== Message
.c_str())
240 cerr
<< "Malformed message!" << endl
;
247 if (Configuration(Message
) == false)
253 FetchItem
*Tmp
= new FetchItem
;
255 Tmp
->Uri
= LookupTag(Message
,"URI");
256 Tmp
->DestFile
= LookupTag(Message
,"FileName");
257 StrToTime(LookupTag(Message
,"Last-Modified"),Tmp
->LastModified
);
260 // Append it to the list
261 FetchItem
**I
= &Queue
;
262 for (; *I
!= 0 && (*I
)->Next
!= 0; I
= &(*I
)->Next
);
265 if (Fetch(Tmp
) == false)
275 // AcqMethod::Log - Send a log message /*{{{*/
276 // ---------------------------------------------------------------------
278 void pkgAcqMethod::Log(const char *Format
,...)
280 string CurrentURI
= "<UNKNOWN>";
282 CurrentURI
= Queue
->Uri
;
285 va_start(args
,Format
);
287 // sprintf the description
289 unsigned int Len
= snprintf(S
,sizeof(S
),"101 Log\nURI: %s\n"
290 "Message: ",CurrentURI
.c_str());
292 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
295 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
299 // AcqMethod::Status - Send a status message /*{{{*/
300 // ---------------------------------------------------------------------
302 void pkgAcqMethod::Status(const char *Format
,...)
304 string CurrentURI
= "<UNKNOWN>";
306 CurrentURI
= Queue
->Uri
;
309 va_start(args
,Format
);
311 // sprintf the description
313 unsigned int Len
= snprintf(S
,sizeof(S
),"101 Log\nURI: %s\n"
314 "Message: ",CurrentURI
.c_str());
316 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
319 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
324 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
325 // ---------------------------------------------------------------------
327 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
328 IMSHit(false), Size(0)