]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire-method.cc
706222c4058896615af9d1ab1e9f3a61cabcf468
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-method.cc,v 1.7 1998/11/11 07:30:54 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
.ResumePoint
!= 0)
145 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Resume-Point: %u\n",
148 if (Res
.IMSHit
== true)
149 strcat(End
,"IMS-Hit: true\n");
154 if (Alt
->Filename
.empty() == false)
155 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Filename: %s\n",Alt
->Filename
.c_str());
158 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Size: %u\n",Alt
->Size
);
160 if (Alt
->LastModified
!= 0)
161 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-Last-Modified: %s\n",
162 TimeRFC1123(Alt
->LastModified
).c_str());
164 if (Alt
->MD5Sum
.empty() == false)
165 End
+= snprintf(End
,sizeof(S
) - (End
- S
),"Alt-MD5Sum: %s\n",
166 Alt
->MD5Sum
.c_str());
168 if (Alt
->IMSHit
== true)
169 strcat(End
,"Alt-IMS-Hit: true\n");
173 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
177 FetchItem
*Tmp
= Queue
;
182 // AcqMethod::Configuration - Handle the configuration message /*{{{*/
183 // ---------------------------------------------------------------------
184 /* This parses each configuration entry and puts it into the _config
185 Configuration class. */
186 bool pkgAcqMethod::Configuration(string Message
)
188 ::Configuration
&Cnf
= *_config
;
190 const char *I
= Message
.begin();
192 unsigned int Length
= strlen("Config-Item");
193 for (; I
+ Length
< Message
.end(); I
++)
196 if (I
[Length
] != ':' || stringcasecmp(I
,I
+Length
,"Config-Item") != 0)
201 for (; I
< Message
.end() && *I
== ' '; I
++);
202 const char *Equals
= I
;
203 for (; Equals
< Message
.end() && *Equals
!= '='; Equals
++);
204 const char *End
= Equals
;
205 for (; End
< Message
.end() && *End
!= '\n'; End
++);
209 Cnf
.Set(string(I
,Equals
-I
),string(Equals
+1,End
-Equals
-1));
216 // AcqMethod::Run - Run the message engine /*{{{*/
217 // ---------------------------------------------------------------------
219 int pkgAcqMethod::Run(bool Single
)
223 // Block if the message queue is empty
224 if (Messages
.empty() == true)
227 if (WaitFd(STDIN_FILENO
) == false)
230 if (ReadMessages(STDIN_FILENO
,Messages
) == false)
234 // Single mode exits if the message queue is empty
235 if (Single
== true && Messages
.empty() == true)
238 string Message
= Messages
.front();
239 Messages
.erase(Messages
.begin());
241 // Fetch the message number
243 int Number
= strtol(Message
.c_str(),&End
,10);
244 if (End
== Message
.c_str())
246 cerr
<< "Malformed message!" << endl
;
253 if (Configuration(Message
) == false)
259 FetchItem
*Tmp
= new FetchItem
;
261 Tmp
->Uri
= LookupTag(Message
,"URI");
262 Tmp
->DestFile
= LookupTag(Message
,"FileName");
263 if (StrToTime(LookupTag(Message
,"Last-Modified"),Tmp
->LastModified
) == false)
264 Tmp
->LastModified
= 0;
268 // Append it to the list
269 FetchItem
**I
= &Queue
;
270 for (; *I
!= 0; I
= &(*I
)->Next
);
273 // Notify that this item is to be fetched.
274 if (Fetch(Tmp
) == false)
285 // AcqMethod::Log - Send a log message /*{{{*/
286 // ---------------------------------------------------------------------
288 void pkgAcqMethod::Log(const char *Format
,...)
290 string CurrentURI
= "<UNKNOWN>";
292 CurrentURI
= Queue
->Uri
;
295 va_start(args
,Format
);
297 // sprintf the description
299 unsigned int Len
= snprintf(S
,sizeof(S
),"101 Log\nURI: %s\n"
300 "Message: ",CurrentURI
.c_str());
302 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
305 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
309 // AcqMethod::Status - Send a status message /*{{{*/
310 // ---------------------------------------------------------------------
312 void pkgAcqMethod::Status(const char *Format
,...)
314 string CurrentURI
= "<UNKNOWN>";
316 CurrentURI
= Queue
->Uri
;
319 va_start(args
,Format
);
321 // sprintf the description
323 unsigned int Len
= snprintf(S
,sizeof(S
),"102 Status\nURI: %s\n"
324 "Message: ",CurrentURI
.c_str());
326 vsnprintf(S
+Len
,sizeof(S
)-Len
,Format
,args
);
329 if (write(STDOUT_FILENO
,S
,strlen(S
)) != (signed)strlen(S
))
334 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
335 // ---------------------------------------------------------------------
337 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
338 IMSHit(false), Size(0), ResumePoint(0)