]>
git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
6ef99863dd2b3d39d2ca2d61218ba768e335d274
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>
18 #include <apt-pkg/metaindex.h>
19 #include <apt-pkg/indexfile.h>
20 #include <apt-pkg/tagfile.h>
21 #include <apt-pkg/pkgcache.h>
22 #include <apt-pkg/cacheiterators.h>
39 // Global list of Items supported
40 static pkgSourceList::Type
*ItmList
[10];
41 pkgSourceList::Type
**pkgSourceList::Type::GlobalList
= ItmList
;
42 unsigned long pkgSourceList::Type::GlobalListLen
= 0;
44 // Type::Type - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
46 /* Link this to the global list of items*/
47 pkgSourceList::Type::Type(char const * const pName
, char const * const pLabel
) : Name(pName
), Label(pLabel
)
49 ItmList
[GlobalListLen
] = this;
52 pkgSourceList::Type::~Type() {}
54 // Type::GetType - Get a specific meta for a given type /*{{{*/
55 // ---------------------------------------------------------------------
57 pkgSourceList::Type
*pkgSourceList::Type::GetType(const char *Type
)
59 for (unsigned I
= 0; I
!= GlobalListLen
; ++I
)
60 if (strcmp(GlobalList
[I
]->Name
,Type
) == 0)
65 // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
66 // ---------------------------------------------------------------------
68 bool pkgSourceList::Type::FixupURI(string
&URI
) const
70 if (URI
.empty() == true)
73 if (URI
.find(':') == string::npos
)
76 URI
= SubstVar(URI
,"$(ARCH)",_config
->Find("APT::Architecture"));
78 // Make sure that the URI is / postfixed
79 if (URI
[URI
.size() - 1] != '/')
85 bool pkgSourceList::Type::ParseStanza(vector
<metaIndex
*> &List
,
90 map
<string
, string
> Options
;
92 string Enabled
= Tags
.FindS("Enabled");
93 if (Enabled
.size() > 0 && StringToBool(Enabled
) == false)
96 std::map
<char const * const, char const * const> mapping
;
97 #define APT_PLUSMINUS(X, Y) \
98 mapping.insert(std::make_pair(X, Y)); \
99 mapping.insert(std::make_pair(X "Add", Y "+")); \
100 mapping.insert(std::make_pair(X "Remove", Y "-"))
101 APT_PLUSMINUS("Architectures", "arch");
102 APT_PLUSMINUS("Languages", "lang");
103 APT_PLUSMINUS("Targets", "target");
105 mapping
.insert(std::make_pair("Trusted", "trusted"));
106 for (std::map
<char const * const, char const * const>::const_iterator m
= mapping
.begin(); m
!= mapping
.end(); ++m
)
107 if (Tags
.Exists(m
->first
))
109 // for deb822 the " " is the delimiter, but the backend expects ","
110 std::string option
= Tags
.FindS(m
->first
);
111 std::replace(option
.begin(), option
.end(), ' ', ',');
112 Options
[m
->second
] = option
;
115 // now create one item per suite/section
116 string Suite
= Tags
.FindS("Suites");
117 Suite
= SubstVar(Suite
,"$(ARCH)",_config
->Find("APT::Architecture"));
118 string
const Section
= Tags
.FindS("Sections");
119 string URIS
= Tags
.FindS("URIs");
121 std::vector
<std::string
> list_uris
= StringSplit(URIS
, " ");
122 std::vector
<std::string
> list_dist
= StringSplit(Suite
, " ");
123 std::vector
<std::string
> list_section
= StringSplit(Section
, " ");
125 for (std::vector
<std::string
>::const_iterator U
= list_uris
.begin();
126 U
!= list_uris
.end(); ++U
)
128 std::string URI
= (*U
);
131 _error
->Error(_("Malformed stanza %u in source list %s (URI parse)"),i
,Fd
.Name().c_str());
135 for (std::vector
<std::string
>::const_iterator I
= list_dist
.begin();
136 I
!= list_dist
.end(); ++I
)
138 for (std::vector
<std::string
>::const_iterator J
= list_section
.begin();
139 J
!= list_section
.end(); ++J
)
141 if (CreateItem(List
, URI
, (*I
), (*J
), Options
) == false)
151 // Type::ParseLine - Parse a single line /*{{{*/
152 // ---------------------------------------------------------------------
153 /* This is a generic one that is the 'usual' format for sources.list
154 Weird types may override this. */
155 bool pkgSourceList::Type::ParseLine(vector
<metaIndex
*> &List
,
157 unsigned long const &CurLine
,
158 string
const &File
) const
160 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
162 // Parse option field if it exists
163 // e.g.: [ option1=value1 option2=value2 ]
164 map
<string
, string
> Options
;
165 if (Buffer
!= 0 && Buffer
[0] == '[')
167 ++Buffer
; // ignore the [
168 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
169 while (*Buffer
!= ']')
171 // get one option, e.g. option1=value1
173 if (ParseQuoteWord(Buffer
,option
) == false)
174 return _error
->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine
,File
.c_str());
176 if (option
.length() < 3)
177 return _error
->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine
,File
.c_str());
179 // accept options even if the last has no space before the ]-end marker
180 if (option
.at(option
.length()-1) == ']')
182 for (; *Buffer
!= ']'; --Buffer
);
183 option
.resize(option
.length()-1);
186 size_t const needle
= option
.find('=');
187 if (needle
== string::npos
)
188 return _error
->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine
,File
.c_str(), option
.c_str());
190 string
const key
= string(option
, 0, needle
);
191 string
const value
= string(option
, needle
+ 1, option
.length());
193 if (key
.empty() == true)
194 return _error
->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine
,File
.c_str(), option
.c_str());
196 if (value
.empty() == true)
197 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());
199 Options
[key
] = value
;
201 ++Buffer
; // ignore the ]
202 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
209 if (ParseQuoteWord(Buffer
,URI
) == false)
210 return _error
->Error(_("Malformed line %lu in source list %s (URI)"),CurLine
,File
.c_str());
211 if (ParseQuoteWord(Buffer
,Dist
) == false)
212 return _error
->Error(_("Malformed line %lu in source list %s (dist)"),CurLine
,File
.c_str());
214 if (FixupURI(URI
) == false)
215 return _error
->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine
,File
.c_str());
217 // Check for an absolute dists specification.
218 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] == '/')
220 if (ParseQuoteWord(Buffer
,Section
) == true)
221 return _error
->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine
,File
.c_str());
222 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
223 return CreateItem(List
, URI
, Dist
, Section
, Options
);
226 // Grab the rest of the dists
227 if (ParseQuoteWord(Buffer
,Section
) == false)
228 return _error
->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine
,File
.c_str());
232 if (CreateItem(List
, URI
, Dist
, Section
, Options
) == false)
235 while (ParseQuoteWord(Buffer
,Section
) == true);
240 // SourceList::pkgSourceList - Constructors /*{{{*/
241 // ---------------------------------------------------------------------
243 pkgSourceList::pkgSourceList() : d(NULL
)
247 pkgSourceList::pkgSourceList(string File
) : d(NULL
)
252 // SourceList::~pkgSourceList - Destructor /*{{{*/
253 // ---------------------------------------------------------------------
255 pkgSourceList::~pkgSourceList()
257 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
261 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
262 // ---------------------------------------------------------------------
264 bool pkgSourceList::ReadMainList()
266 // CNC:2003-03-03 - Multiple sources list support.
275 // CNC:2003-11-28 - Entries in sources.list have priority over
276 // entries in sources.list.d.
277 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
278 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
280 if (RealFileExists(Main
) == true)
281 Res
&= ReadAppend(Main
);
282 else if (DirectoryExists(Parts
) == false)
283 // Only warn if there are no sources.list.d.
284 _error
->WarningE("DirectoryExists", _("Unable to read %s"), Parts
.c_str());
286 if (DirectoryExists(Parts
) == true)
287 Res
&= ReadSourceDir(Parts
);
288 else if (RealFileExists(Main
) == false)
289 // Only warn if there is no sources.list file.
290 _error
->WarningE("RealFileExists", _("Unable to read %s"), Main
.c_str());
295 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
296 // ---------------------------------------------------------------------
298 void pkgSourceList::Reset()
300 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
302 SrcList
.erase(SrcList
.begin(),SrcList
.end());
305 // SourceList::Read - Parse the sourcelist file /*{{{*/
306 // ---------------------------------------------------------------------
308 bool pkgSourceList::Read(string File
)
311 return ReadAppend(File
);
314 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
315 // ---------------------------------------------------------------------
317 bool pkgSourceList::ReadAppend(string File
)
319 if (_config
->FindB("APT::Sources::Use-Deb822", false) == true)
321 int lines_parsed
=ParseFileDeb822(File
);
322 if (lines_parsed
< 0)
324 else if (lines_parsed
> 0)
326 // no lines parsed ... fall through and use old style parser
328 return ParseFileOldStyle(File
);
331 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
332 // ---------------------------------------------------------------------
334 bool pkgSourceList::ParseFileOldStyle(string File
)
336 // Open the stream for reading
337 ifstream
F(File
.c_str(),ios::in
/*| ios::nocreate*/);
338 if (F
.fail() == true)
339 return _error
->Errno("ifstream::ifstream",_("Opening %s"),File
.c_str());
341 // CNC:2003-12-10 - 300 is too short.
345 while (F
.eof() == false)
347 F
.getline(Buffer
,sizeof(Buffer
));
349 _strtabexpand(Buffer
,sizeof(Buffer
));
350 if (F
.fail() && !F
.eof())
351 return _error
->Error(_("Line %u too long in source list %s."),
352 CurLine
,File
.c_str());
356 // CNC:2003-02-20 - Do not break if '#' is inside [].
357 for (I
= Buffer
; *I
!= 0 && *I
!= '#'; I
++)
360 char *b_end
= strchr(I
+ 1, ']');
366 const char *C
= _strstrip(Buffer
);
369 if (C
[0] == '#' || C
[0] == 0)
374 if (ParseQuoteWord(C
,LineType
) == false)
375 return _error
->Error(_("Malformed line %u in source list %s (type)"),CurLine
,File
.c_str());
377 Type
*Parse
= Type::GetType(LineType
.c_str());
379 return _error
->Error(_("Type '%s' is not known on line %u in source list %s"),LineType
.c_str(),CurLine
,File
.c_str());
381 if (Parse
->ParseLine(SrcList
, C
, CurLine
, File
) == false)
387 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
388 // ---------------------------------------------------------------------
389 /* Returns: the number of stanzas parsed*/
390 int pkgSourceList::ParseFileDeb822(string File
)
395 // see if we can read the file
396 _error
->PushToStack();
397 FileFd
Fd(File
, FileFd::ReadOnly
);
398 pkgTagFile
Sources(&Fd
);
399 if (_error
->PendingError() == true)
401 _error
->RevertToStack();
404 _error
->MergeWithStack();
407 while (Sources
.Step(Tags
) == true)
409 if(!Tags
.Exists("Types"))
412 string
const types
= Tags
.FindS("Types");
413 std::vector
<std::string
> list_types
= StringSplit(types
, " ");
414 for (std::vector
<std::string
>::const_iterator I
= list_types
.begin();
415 I
!= list_types
.end(); ++I
)
417 Type
*Parse
= Type::GetType((*I
).c_str());
420 _error
->Error(_("Type '%s' is not known on stanza %u in source list %s"), (*I
).c_str(),i
,Fd
.Name().c_str());
424 if (!Parse
->ParseStanza(SrcList
, Tags
, i
, Fd
))
431 // we are done, return the number of stanzas read
435 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
436 // ---------------------------------------------------------------------
438 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File
,
439 pkgIndexFile
*&Found
) const
441 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
443 vector
<pkgIndexFile
*> *Indexes
= (*I
)->GetIndexFiles();
444 for (vector
<pkgIndexFile
*>::const_iterator J
= Indexes
->begin();
445 J
!= Indexes
->end(); ++J
)
447 if ((*J
)->FindInCache(*File
.Cache()) == File
)
458 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
459 // ---------------------------------------------------------------------
461 bool pkgSourceList::GetIndexes(pkgAcquire
*Owner
, bool GetAll
) const
463 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
464 if ((*I
)->GetIndexes(Owner
,GetAll
) == false)
469 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
470 // SourceList::ReadSourceDir - Read a directory with sources files
471 // Based on ReadConfigDir() /*{{{*/
472 // ---------------------------------------------------------------------
474 bool pkgSourceList::ReadSourceDir(string Dir
)
476 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "list", true);
479 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
480 if (ReadAppend(*I
) == false)
486 // GetLastModified() /*{{{*/
487 // ---------------------------------------------------------------------
489 time_t pkgSourceList::GetLastModifiedTime()
493 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
494 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
497 if (DirectoryExists(Parts
) == true)
498 List
= GetListOfFilesInDir(Parts
, "list", true);
500 // calculate the time
501 time_t mtime_sources
= GetModificationTime(Main
);
502 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
503 mtime_sources
= std::max(mtime_sources
, GetModificationTime(*I
));
505 return mtime_sources
;