Sync
[ntk/apt.git] / apt-pkg / acquire-method.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire-method.cc,v 1.1 1998/10/30 07:53:35 jgg Exp $
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);
48 }
49 /*}}}*/
50 // AcqMethod::Fail - A fetch has failed /*{{{*/
51 // ---------------------------------------------------------------------
52 /* */
53 void pkgAcqMethod::Fail()
54 {
55 string Err = "Undetermined Error";
56 if (_error->empty() == false)
57 _error->PopMessage(Err);
58 _error->Discard();
59 Fail(Err);
60 }
61 /*}}}*/
62 // AcqMethod::Fail - A fetch has failed /*{{{*/
63 // ---------------------------------------------------------------------
64 /* */
65 void pkgAcqMethod::Fail(string Err)
66 {
67 char S[1024];
68 snprintf(S,sizeof(S),"400 URI Failure\nURI: %s\n"
69 "Message %s\n\n",CurrentURI.c_str(),Err.c_str());
70
71 if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
72 exit(100);
73 }
74 /*}}}*/
75 // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
76 // ---------------------------------------------------------------------
77 /* */
78 void pkgAcqMethod::URIStart(FetchResult &Res,unsigned long Resume = 0)
79 {
80 char S[1024] = "";
81 char *End = S;
82
83 End += snprintf(S,sizeof(S),"200 URI Start\nURI: %s\n",CurrentURI.c_str());
84 if (Res.Size != 0)
85 End += snprintf(End,sizeof(S) - (End - S),"Size: %u\n",Res.Size);
86
87 if (Res.LastModified != 0)
88 End += snprintf(End,sizeof(S) - (End - S),"Last-Modified: %s\n",
89 TimeRFC1123(Res.LastModified).c_str());
90
91 if (Resume != 0)
92 End += snprintf(End,sizeof(S) - (End - S),"Resume-Point: %u\n",
93 Resume);
94
95 strcat(End,"\n");
96 if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
97 exit(100);
98 }
99 /*}}}*/
100 // AcqMethod::URIDone - A URI is finished /*{{{*/
101 // ---------------------------------------------------------------------
102 /* */
103 void pkgAcqMethod::URIDone(FetchResult &Res, FetchResult *Alt)
104 {
105 char S[1024] = "";
106 char *End = S;
107
108 End += snprintf(S,sizeof(S),"201 URI Done\nURI: %s\n",CurrentURI.c_str());
109
110 if (Res.Filename.empty() == false)
111 End += snprintf(End,sizeof(S) - (End - S),"Filename: %s\n",Res.Filename.c_str());
112
113 if (Res.Size != 0)
114 End += snprintf(End,sizeof(S) - (End - S),"Size: %u\n",Res.Size);
115
116 if (Res.LastModified != 0)
117 End += snprintf(End,sizeof(S) - (End - S),"Last-Modified: %s\n",
118 TimeRFC1123(Res.LastModified).c_str());
119
120 if (Res.MD5Sum.empty() == false)
121 End += snprintf(End,sizeof(S) - (End - S),"MD5Sum: %s\n",Res.MD5Sum.c_str());
122
123 if (Res.IMSHit == true)
124 strcat(End,"IMS-Hit: true\n");
125 End = S + strlen(S);
126
127 if (Alt != 0)
128 {
129 if (Alt->Filename.empty() == false)
130 End += snprintf(End,sizeof(S) - (End - S),"Alt-Filename: %s\n",Alt->Filename.c_str());
131
132 if (Alt->Size != 0)
133 End += snprintf(End,sizeof(S) - (End - S),"Alt-Size: %u\n",Alt->Size);
134
135 if (Alt->LastModified != 0)
136 End += snprintf(End,sizeof(S) - (End - S),"Alt-Last-Modified: %s\n",
137 TimeRFC1123(Alt->LastModified).c_str());
138
139 if (Alt->MD5Sum.empty() == false)
140 End += snprintf(End,sizeof(S) - (End - S),"Alt-MD5Sum: %s\n",
141 Alt->MD5Sum.c_str());
142
143 if (Alt->IMSHit == true)
144 strcat(End,"Alt-IMS-Hit: true\n");
145 }
146
147 strcat(End,"\n");
148 if (write(STDOUT_FILENO,S,strlen(S)) != (signed)strlen(S))
149 exit(100);
150 }
151 /*}}}*/
152 // AcqMethod::Configuration - Handle the configuration message /*{{{*/
153 // ---------------------------------------------------------------------
154 /* This parses each configuration entry and puts it into the _config
155 Configuration class. */
156 bool pkgAcqMethod::Configuration(string Message)
157 {
158 ::Configuration &Cnf = *_config;
159
160 const char *I = Message.begin();
161
162 unsigned int Length = strlen("Config-Item");
163 for (; I + Length < Message.end(); I++)
164 {
165 // Not a config item
166 if (I[Length] != ':' || stringcasecmp(I,I+Length,"Config-Item") != 0)
167 continue;
168
169 I += Length + 1;
170
171 for (; I < Message.end() && *I == ' '; I++);
172 const char *Equals = I;
173 for (; Equals < Message.end() && *Equals != '='; Equals++);
174 const char *End = Equals;
175 for (; End < Message.end() && *End != '\n'; End++);
176 if (End == Equals)
177 return false;
178
179 Cnf.Set(string(I,Equals-I),string(Equals+1,End-Equals-1));
180 I = End;
181 }
182
183 return true;
184 }
185 /*}}}*/
186 // AcqMethod::Run - Run the message engine /*{{{*/
187 // ---------------------------------------------------------------------
188 /* */
189 int pkgAcqMethod::Run()
190 {
191 SetNonBlock(STDIN_FILENO,true);
192
193 while (1)
194 {
195 if (Messages.empty() == true)
196 if (WaitFd(STDIN_FILENO) == false)
197 return 0;
198
199 if (ReadMessages(STDIN_FILENO,Messages) == false)
200 return 0;
201
202 string Message = Messages.front();
203 Messages.erase(Messages.begin());
204
205 // Fetch the message number
206 char *End;
207 int Number = strtol(Message.c_str(),&End,10);
208 if (End == Message.c_str())
209 {
210 cerr << "Malformed message!" << endl;
211 return 100;
212 }
213
214 switch (Number)
215 {
216 case 601:
217 if (Configuration(Message) == false)
218 return 100;
219 break;
220
221 case 600:
222 {
223 CurrentURI = LookupTag(Message,"URI");
224 DestFile = LookupTag(Message,"FileName");
225 StrToTime(LookupTag(Message,"Last-Modified"),LastModified);
226
227 if (Fetch(Message,CurrentURI) == false)
228 Fail();
229 break;
230 }
231 }
232 }
233
234 return 0;
235 }
236 /*}}}*/
237 // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
238 // ---------------------------------------------------------------------
239 /* */
240 pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
241 IMSHit(false), Size(0)
242 {
243 }
244 /*}}}*/
245