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