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