]>
Commit | Line | Data |
---|---|---|
93bf083d AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
a72ace20 | 3 | // $Id: acquire-method.cc,v 1.9 1998/11/29 01:24:15 jgg Exp $ |
93bf083d AL |
4 | /* ###################################################################### |
5 | ||
6 | Acquire Method | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Include Files /*{{{*/ | |
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "apt-pkg/acquire-method.h" | |
13 | #endif | |
14 | #include <apt-pkg/acquire-method.h> | |
15 | #include <apt-pkg/error.h> | |
16 | #include <apt-pkg/configuration.h> | |
17 | #include <strutl.h> | |
18 | #include <apt-pkg/fileutl.h> | |
19 | ||
20 | #include <stdio.h> | |
21 | /*}}}*/ | |
22 | ||
23 | // AcqMethod::pkgAcqMethod - Constructor /*{{{*/ | |
24 | // --------------------------------------------------------------------- | |
25 | /* This constructs the initialization text */ | |
26 | pkgAcqMethod::pkgAcqMethod(const char *Ver,unsigned long Flags) | |
27 | { | |
28 | char S[300] = ""; | |
29 | char *End = S; | |
30 | strcat(End,"100 Capabilities\n"); | |
31 | sprintf(End+strlen(End),"Version: %s\n",Ver); | |
32 | ||
33 | if ((Flags & SingleInstance) == SingleInstance) | |
34 | strcat(End,"Single-Instance: true\n"); | |
35 | ||
93bf083d AL |
36 | if ((Flags & Pipeline) == Pipeline) |
37 | strcat(End,"Pipeline: true\n"); | |
38 | ||
39 | if ((Flags & SendConfig) == SendConfig) | |
40 | strcat(End,"Send-Config: true\n"); | |
e331f6ed AL |
41 | |
42 | if ((Flags & LocalOnly) == LocalOnly) | |
43 | strcat(End,"Local-Only: true\n"); | |
93bf083d AL |
44 | strcat(End,"\n"); |
45 | ||
46 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
47 | exit(100); | |
be4401bf AL |
48 | |
49 | SetNonBlock(STDIN_FILENO,true); | |
92e889c8 AL |
50 | |
51 | Queue = 0; | |
93bf083d AL |
52 | } |
53 | /*}}}*/ | |
54 | // AcqMethod::Fail - A fetch has failed /*{{{*/ | |
55 | // --------------------------------------------------------------------- | |
56 | /* */ | |
a72ace20 | 57 | void pkgAcqMethod::Fail(bool Transient) |
93bf083d AL |
58 | { |
59 | string Err = "Undetermined Error"; | |
60 | if (_error->empty() == false) | |
61 | _error->PopMessage(Err); | |
62 | _error->Discard(); | |
a72ace20 | 63 | Fail(Err,Transient); |
93bf083d AL |
64 | } |
65 | /*}}}*/ | |
66 | // AcqMethod::Fail - A fetch has failed /*{{{*/ | |
67 | // --------------------------------------------------------------------- | |
68 | /* */ | |
a72ace20 | 69 | void pkgAcqMethod::Fail(string Err,bool Transient) |
93bf083d AL |
70 | { |
71 | char S[1024]; | |
be4401bf AL |
72 | if (Queue != 0) |
73 | { | |
74 | snprintf(S,sizeof(S),"400 URI Failure\nURI: %s\n" | |
a72ace20 AL |
75 | "Message: %s\n",Queue->Uri.c_str(),Err.c_str()); |
76 | ||
be4401bf AL |
77 | // Dequeue |
78 | FetchItem *Tmp = Queue; | |
79 | Queue = Queue->Next; | |
80 | delete Tmp; | |
81 | } | |
82 | else | |
83 | snprintf(S,sizeof(S),"400 URI Failure\nURI: <UNKNOWN>\n" | |
a72ace20 | 84 | "Message: %s\n",Err.c_str()); |
be4401bf | 85 | |
a72ace20 AL |
86 | // Set the transient flag |
87 | if (Transient == true) | |
88 | strcat(S,"Transient-Failure: true\n\n"); | |
89 | else | |
90 | strcat(S,"\n"); | |
91 | ||
93bf083d AL |
92 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) |
93 | exit(100); | |
94 | } | |
95 | /*}}}*/ | |
96 | // AcqMethod::URIStart - Indicate a download is starting /*{{{*/ | |
97 | // --------------------------------------------------------------------- | |
98 | /* */ | |
be4401bf | 99 | void pkgAcqMethod::URIStart(FetchResult &Res) |
93bf083d | 100 | { |
be4401bf AL |
101 | if (Queue == 0) |
102 | abort(); | |
103 | ||
93bf083d AL |
104 | char S[1024] = ""; |
105 | char *End = S; | |
106 | ||
be4401bf | 107 | End += snprintf(S,sizeof(S),"200 URI Start\nURI: %s\n",Queue->Uri.c_str()); |
93bf083d AL |
108 | if (Res.Size != 0) |
109 | End += snprintf(End,sizeof(S) - (End - S),"Size: %u\n",Res.Size); | |
110 | ||
111 | if (Res.LastModified != 0) | |
112 | End += snprintf(End,sizeof(S) - (End - S),"Last-Modified: %s\n", | |
113 | TimeRFC1123(Res.LastModified).c_str()); | |
114 | ||
be4401bf | 115 | if (Res.ResumePoint != 0) |
93bf083d | 116 | End += snprintf(End,sizeof(S) - (End - S),"Resume-Point: %u\n", |
be4401bf | 117 | Res.ResumePoint); |
93bf083d AL |
118 | |
119 | strcat(End,"\n"); | |
120 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
121 | exit(100); | |
122 | } | |
123 | /*}}}*/ | |
124 | // AcqMethod::URIDone - A URI is finished /*{{{*/ | |
125 | // --------------------------------------------------------------------- | |
126 | /* */ | |
127 | void pkgAcqMethod::URIDone(FetchResult &Res, FetchResult *Alt) | |
128 | { | |
be4401bf AL |
129 | if (Queue == 0) |
130 | abort(); | |
131 | ||
93bf083d AL |
132 | char S[1024] = ""; |
133 | char *End = S; | |
134 | ||
be4401bf | 135 | End += snprintf(S,sizeof(S),"201 URI Done\nURI: %s\n",Queue->Uri.c_str()); |
93bf083d AL |
136 | |
137 | if (Res.Filename.empty() == false) | |
138 | End += snprintf(End,sizeof(S) - (End - S),"Filename: %s\n",Res.Filename.c_str()); | |
139 | ||
140 | if (Res.Size != 0) | |
141 | End += snprintf(End,sizeof(S) - (End - S),"Size: %u\n",Res.Size); | |
142 | ||
143 | if (Res.LastModified != 0) | |
144 | End += snprintf(End,sizeof(S) - (End - S),"Last-Modified: %s\n", | |
145 | TimeRFC1123(Res.LastModified).c_str()); | |
146 | ||
147 | if (Res.MD5Sum.empty() == false) | |
148 | End += snprintf(End,sizeof(S) - (End - S),"MD5Sum: %s\n",Res.MD5Sum.c_str()); | |
149 | ||
b98f2859 AL |
150 | if (Res.ResumePoint != 0) |
151 | End += snprintf(End,sizeof(S) - (End - S),"Resume-Point: %u\n", | |
152 | Res.ResumePoint); | |
153 | ||
93bf083d AL |
154 | if (Res.IMSHit == true) |
155 | strcat(End,"IMS-Hit: true\n"); | |
156 | End = S + strlen(S); | |
157 | ||
158 | if (Alt != 0) | |
159 | { | |
160 | if (Alt->Filename.empty() == false) | |
161 | End += snprintf(End,sizeof(S) - (End - S),"Alt-Filename: %s\n",Alt->Filename.c_str()); | |
162 | ||
163 | if (Alt->Size != 0) | |
164 | End += snprintf(End,sizeof(S) - (End - S),"Alt-Size: %u\n",Alt->Size); | |
165 | ||
166 | if (Alt->LastModified != 0) | |
167 | End += snprintf(End,sizeof(S) - (End - S),"Alt-Last-Modified: %s\n", | |
168 | TimeRFC1123(Alt->LastModified).c_str()); | |
169 | ||
170 | if (Alt->MD5Sum.empty() == false) | |
171 | End += snprintf(End,sizeof(S) - (End - S),"Alt-MD5Sum: %s\n", | |
172 | Alt->MD5Sum.c_str()); | |
173 | ||
174 | if (Alt->IMSHit == true) | |
175 | strcat(End,"Alt-IMS-Hit: true\n"); | |
176 | } | |
177 | ||
178 | strcat(End,"\n"); | |
179 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
180 | exit(100); | |
be4401bf AL |
181 | |
182 | // Dequeue | |
183 | FetchItem *Tmp = Queue; | |
184 | Queue = Queue->Next; | |
185 | delete Tmp; | |
93bf083d AL |
186 | } |
187 | /*}}}*/ | |
188 | // AcqMethod::Configuration - Handle the configuration message /*{{{*/ | |
189 | // --------------------------------------------------------------------- | |
190 | /* This parses each configuration entry and puts it into the _config | |
191 | Configuration class. */ | |
192 | bool pkgAcqMethod::Configuration(string Message) | |
193 | { | |
194 | ::Configuration &Cnf = *_config; | |
195 | ||
196 | const char *I = Message.begin(); | |
197 | ||
198 | unsigned int Length = strlen("Config-Item"); | |
199 | for (; I + Length < Message.end(); I++) | |
200 | { | |
201 | // Not a config item | |
202 | if (I[Length] != ':' || stringcasecmp(I,I+Length,"Config-Item") != 0) | |
203 | continue; | |
204 | ||
205 | I += Length + 1; | |
206 | ||
207 | for (; I < Message.end() && *I == ' '; I++); | |
208 | const char *Equals = I; | |
209 | for (; Equals < Message.end() && *Equals != '='; Equals++); | |
210 | const char *End = Equals; | |
211 | for (; End < Message.end() && *End != '\n'; End++); | |
212 | if (End == Equals) | |
213 | return false; | |
214 | ||
215 | Cnf.Set(string(I,Equals-I),string(Equals+1,End-Equals-1)); | |
216 | I = End; | |
217 | } | |
218 | ||
219 | return true; | |
220 | } | |
221 | /*}}}*/ | |
222 | // AcqMethod::Run - Run the message engine /*{{{*/ | |
223 | // --------------------------------------------------------------------- | |
224 | /* */ | |
be4401bf | 225 | int pkgAcqMethod::Run(bool Single) |
93bf083d | 226 | { |
93bf083d AL |
227 | while (1) |
228 | { | |
be4401bf | 229 | // Block if the message queue is empty |
93bf083d | 230 | if (Messages.empty() == true) |
be4401bf AL |
231 | { |
232 | if (Single == false) | |
233 | if (WaitFd(STDIN_FILENO) == false) | |
234 | return 0; | |
be4401bf | 235 | |
92e889c8 AL |
236 | if (ReadMessages(STDIN_FILENO,Messages) == false) |
237 | return 0; | |
238 | } | |
239 | ||
be4401bf AL |
240 | // Single mode exits if the message queue is empty |
241 | if (Single == true && Messages.empty() == true) | |
242 | return 0; | |
243 | ||
93bf083d AL |
244 | string Message = Messages.front(); |
245 | Messages.erase(Messages.begin()); | |
246 | ||
247 | // Fetch the message number | |
248 | char *End; | |
249 | int Number = strtol(Message.c_str(),&End,10); | |
250 | if (End == Message.c_str()) | |
251 | { | |
252 | cerr << "Malformed message!" << endl; | |
253 | return 100; | |
254 | } | |
255 | ||
256 | switch (Number) | |
257 | { | |
258 | case 601: | |
259 | if (Configuration(Message) == false) | |
260 | return 100; | |
261 | break; | |
262 | ||
263 | case 600: | |
be4401bf AL |
264 | { |
265 | FetchItem *Tmp = new FetchItem; | |
266 | ||
267 | Tmp->Uri = LookupTag(Message,"URI"); | |
268 | Tmp->DestFile = LookupTag(Message,"FileName"); | |
b98f2859 AL |
269 | if (StrToTime(LookupTag(Message,"Last-Modified"),Tmp->LastModified) == false) |
270 | Tmp->LastModified = 0; | |
a72ace20 | 271 | Tmp->IndexFile = StringToBool(LookupTag(Message,"Index-File"),false); |
be4401bf AL |
272 | Tmp->Next = 0; |
273 | ||
274 | // Append it to the list | |
275 | FetchItem **I = &Queue; | |
92e889c8 | 276 | for (; *I != 0; I = &(*I)->Next); |
be4401bf | 277 | *I = Tmp; |
bfd22fc0 AL |
278 | |
279 | // Notify that this item is to be fetched. | |
be4401bf | 280 | if (Fetch(Tmp) == false) |
93bf083d | 281 | Fail(); |
bfd22fc0 | 282 | |
93bf083d AL |
283 | break; |
284 | } | |
285 | } | |
286 | } | |
287 | ||
288 | return 0; | |
289 | } | |
290 | /*}}}*/ | |
be4401bf AL |
291 | // AcqMethod::Log - Send a log message /*{{{*/ |
292 | // --------------------------------------------------------------------- | |
293 | /* */ | |
294 | void pkgAcqMethod::Log(const char *Format,...) | |
295 | { | |
296 | string CurrentURI = "<UNKNOWN>"; | |
297 | if (Queue != 0) | |
298 | CurrentURI = Queue->Uri; | |
299 | ||
300 | va_list args; | |
301 | va_start(args,Format); | |
302 | ||
303 | // sprintf the description | |
304 | char S[1024]; | |
305 | unsigned int Len = snprintf(S,sizeof(S),"101 Log\nURI: %s\n" | |
306 | "Message: ",CurrentURI.c_str()); | |
307 | ||
308 | vsnprintf(S+Len,sizeof(S)-Len,Format,args); | |
309 | strcat(S,"\n\n"); | |
310 | ||
311 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
312 | exit(100); | |
313 | } | |
314 | /*}}}*/ | |
315 | // AcqMethod::Status - Send a status message /*{{{*/ | |
316 | // --------------------------------------------------------------------- | |
317 | /* */ | |
318 | void pkgAcqMethod::Status(const char *Format,...) | |
319 | { | |
320 | string CurrentURI = "<UNKNOWN>"; | |
321 | if (Queue != 0) | |
322 | CurrentURI = Queue->Uri; | |
323 | ||
324 | va_list args; | |
325 | va_start(args,Format); | |
326 | ||
327 | // sprintf the description | |
328 | char S[1024]; | |
8267fe24 | 329 | unsigned int Len = snprintf(S,sizeof(S),"102 Status\nURI: %s\n" |
be4401bf AL |
330 | "Message: ",CurrentURI.c_str()); |
331 | ||
332 | vsnprintf(S+Len,sizeof(S)-Len,Format,args); | |
333 | strcat(S,"\n\n"); | |
334 | ||
335 | if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S)) | |
336 | exit(100); | |
337 | } | |
338 | /*}}}*/ | |
339 | ||
93bf083d AL |
340 | // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/ |
341 | // --------------------------------------------------------------------- | |
342 | /* */ | |
93274b8d AL |
343 | pkgAcqMethod::FetchResult::FetchResult() : LastModified(0), |
344 | IMSHit(false), Size(0), ResumePoint(0) | |
93bf083d AL |
345 | { |
346 | } | |
347 | /*}}}*/ | |
348 |