]>
git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: sourcelist.cc,v 1.3 2002/08/15 20:51:37 niemeyer Exp $
4 /* ######################################################################
8 ##################################################################### */
10 // Include Files /*{{{*/
13 #include <apt-pkg/sourcelist.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/strutl.h>
17 #include <apt-pkg/configuration.h>
26 // Global list of Items supported
27 static pkgSourceList::Type
*ItmList
[10];
28 pkgSourceList::Type
**pkgSourceList::Type::GlobalList
= ItmList
;
29 unsigned long pkgSourceList::Type::GlobalListLen
= 0;
31 // Type::Type - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
33 /* Link this to the global list of items*/
34 pkgSourceList::Type::Type()
36 ItmList
[GlobalListLen
] = this;
40 // Type::GetType - Get a specific meta for a given type /*{{{*/
41 // ---------------------------------------------------------------------
43 pkgSourceList::Type
*pkgSourceList::Type::GetType(const char *Type
)
45 for (unsigned I
= 0; I
!= GlobalListLen
; I
++)
46 if (strcmp(GlobalList
[I
]->Name
,Type
) == 0)
51 // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
52 // ---------------------------------------------------------------------
54 bool pkgSourceList::Type::FixupURI(string
&URI
) const
56 if (URI
.empty() == true)
59 if (URI
.find(':') == string::npos
)
62 URI
= SubstVar(URI
,"$(ARCH)",_config
->Find("APT::Architecture"));
64 // Make sure that the URI is / postfixed
65 if (URI
[URI
.size() - 1] != '/')
71 // Type::ParseLine - Parse a single line /*{{{*/
72 // ---------------------------------------------------------------------
73 /* This is a generic one that is the 'usual' format for sources.list
74 Weird types may override this. */
75 bool pkgSourceList::Type::ParseLine(vector
<metaIndex
*> &List
,
77 unsigned long const &CurLine
,
78 string
const &File
) const
80 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
82 // Parse option field if it exists
83 // e.g.: [ option1=value1 option2=value2 ]
84 map
<string
, string
> Options
;
85 if (Buffer
!= 0 && Buffer
[0] == '[')
87 ++Buffer
; // ignore the [
88 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
89 while (*Buffer
!= ']')
91 // get one option, e.g. option1=value1
93 if (ParseQuoteWord(Buffer
,option
) == false)
94 return _error
->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine
,File
.c_str());
96 if (option
.length() < 3)
97 return _error
->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine
,File
.c_str());
99 // accept options even if the last has no space before the ]-end marker
100 if (option
.at(option
.length()-1) == ']')
102 for (; *Buffer
!= ']'; --Buffer
);
103 option
.resize(option
.length()-1);
106 size_t const needle
= option
.find('=');
107 if (needle
== string::npos
)
108 return _error
->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine
,File
.c_str(), option
.c_str());
110 string
const key
= string(option
, 0, needle
);
111 string
const value
= string(option
, needle
+ 1, option
.length());
113 if (key
.empty() == true)
114 return _error
->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine
,File
.c_str(), option
.c_str());
116 if (value
.empty() == true)
117 return _error
->Error(_("Malformed line %lu in source list %s ([%s] key %s has no value)"),CurLine
,File
.c_str(),option
.c_str(),key
.c_str());
119 Options
[key
] = value
;
121 ++Buffer
; // ignore the ]
122 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
129 if (ParseQuoteWord(Buffer
,URI
) == false)
130 return _error
->Error(_("Malformed line %lu in source list %s (URI)"),CurLine
,File
.c_str());
131 if (ParseQuoteWord(Buffer
,Dist
) == false)
132 return _error
->Error(_("Malformed line %lu in source list %s (dist)"),CurLine
,File
.c_str());
134 if (FixupURI(URI
) == false)
135 return _error
->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine
,File
.c_str());
137 // Check for an absolute dists specification.
138 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] == '/')
140 if (ParseQuoteWord(Buffer
,Section
) == true)
141 return _error
->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine
,File
.c_str());
142 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
143 return CreateItem(List
, URI
, Dist
, Section
, Options
);
146 // Grab the rest of the dists
147 if (ParseQuoteWord(Buffer
,Section
) == false)
148 return _error
->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine
,File
.c_str());
152 if (CreateItem(List
, URI
, Dist
, Section
, Options
) == false)
155 while (ParseQuoteWord(Buffer
,Section
) == true);
161 // SourceList::pkgSourceList - Constructors /*{{{*/
162 // ---------------------------------------------------------------------
164 pkgSourceList::pkgSourceList()
168 pkgSourceList::pkgSourceList(string File
)
173 // SourceList::~pkgSourceList - Destructor /*{{{*/
174 // ---------------------------------------------------------------------
176 pkgSourceList::~pkgSourceList()
178 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
183 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
184 // ---------------------------------------------------------------------
186 bool pkgSourceList::ReadMainList()
188 // CNC:2003-03-03 - Multiple sources list support.
197 // CNC:2003-11-28 - Entries in sources.list have priority over
198 // entries in sources.list.d.
199 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
200 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
202 if (RealFileExists(Main
) == true)
203 Res
&= ReadAppend(Main
);
204 else if (DirectoryExists(Parts
) == false)
205 // Only warn if there are no sources.list.d.
206 _error
->WarningE("DirectoryExists", _("Unable to read %s"), Parts
.c_str());
208 if (DirectoryExists(Parts
) == true)
209 Res
&= ReadSourceDir(Parts
);
210 else if (RealFileExists(Main
) == false)
211 // Only warn if there is no sources.list file.
212 _error
->WarningE("RealFileExists", _("Unable to read %s"), Main
.c_str());
217 // CNC:2003-03-03 - Needed to preserve backwards compatibility.
218 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
219 // ---------------------------------------------------------------------
221 void pkgSourceList::Reset()
223 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
225 SrcList
.erase(SrcList
.begin(),SrcList
.end());
228 // CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
229 // SourceList::Read - Parse the sourcelist file /*{{{*/
230 // ---------------------------------------------------------------------
232 bool pkgSourceList::Read(string File
)
235 return ReadAppend(File
);
238 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
239 // ---------------------------------------------------------------------
241 bool pkgSourceList::ReadAppend(string File
)
243 // Open the stream for reading
244 ifstream
F(File
.c_str(),ios::in
/*| ios::nocreate*/);
246 return _error
->Errno("ifstream::ifstream",_("Opening %s"),File
.c_str());
248 #if 0 // Now Reset() does this.
249 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
251 SrcList
.erase(SrcList
.begin(),SrcList
.end());
253 // CNC:2003-12-10 - 300 is too short.
257 while (F
.eof() == false)
259 F
.getline(Buffer
,sizeof(Buffer
));
261 _strtabexpand(Buffer
,sizeof(Buffer
));
262 if (F
.fail() && !F
.eof())
263 return _error
->Error(_("Line %u too long in source list %s."),
264 CurLine
,File
.c_str());
268 // CNC:2003-02-20 - Do not break if '#' is inside [].
269 for (I
= Buffer
; *I
!= 0 && *I
!= '#'; I
++)
271 for (I
++; *I
!= 0 && *I
!= ']'; I
++);
274 const char *C
= _strstrip(Buffer
);
277 if (C
[0] == '#' || C
[0] == 0)
282 if (ParseQuoteWord(C
,LineType
) == false)
283 return _error
->Error(_("Malformed line %u in source list %s (type)"),CurLine
,File
.c_str());
285 Type
*Parse
= Type::GetType(LineType
.c_str());
287 return _error
->Error(_("Type '%s' is not known on line %u in source list %s"),LineType
.c_str(),CurLine
,File
.c_str());
289 if (Parse
->ParseLine(SrcList
, C
, CurLine
, File
) == false)
295 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
296 // ---------------------------------------------------------------------
298 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File
,
299 pkgIndexFile
*&Found
) const
301 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
303 vector
<pkgIndexFile
*> *Indexes
= (*I
)->GetIndexFiles();
304 for (vector
<pkgIndexFile
*>::const_iterator J
= Indexes
->begin();
305 J
!= Indexes
->end(); J
++)
307 if ((*J
)->FindInCache(*File
.Cache()) == File
)
318 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
319 // ---------------------------------------------------------------------
321 bool pkgSourceList::GetIndexes(pkgAcquire
*Owner
, bool GetAll
) const
323 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
324 if ((*I
)->GetIndexes(Owner
,GetAll
) == false)
329 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
330 // SourceList::ReadSourceDir - Read a directory with sources files
331 // Based on ReadConfigDir() /*{{{*/
332 // ---------------------------------------------------------------------
334 bool pkgSourceList::ReadSourceDir(string Dir
)
336 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "list", true);
339 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
340 if (ReadAppend(*I
) == false)