]>
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>
18 #include <apt-pkg/metaindex.h>
19 #include <apt-pkg/indexfile.h>
20 #include <apt-pkg/tagfile.h>
30 // Global list of Items supported
31 static pkgSourceList::Type
*ItmList
[10];
32 pkgSourceList::Type
**pkgSourceList::Type::GlobalList
= ItmList
;
33 unsigned long pkgSourceList::Type::GlobalListLen
= 0;
35 // Type::Type - Constructor /*{{{*/
36 // ---------------------------------------------------------------------
37 /* Link this to the global list of items*/
38 pkgSourceList::Type::Type() : Name(NULL
), Label(NULL
)
40 ItmList
[GlobalListLen
] = this;
44 // Type::GetType - Get a specific meta for a given type /*{{{*/
45 // ---------------------------------------------------------------------
47 pkgSourceList::Type
*pkgSourceList::Type::GetType(const char *Type
)
49 for (unsigned I
= 0; I
!= GlobalListLen
; I
++)
50 if (strcmp(GlobalList
[I
]->Name
,Type
) == 0)
55 // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
56 // ---------------------------------------------------------------------
58 bool pkgSourceList::Type::FixupURI(string
&URI
) const
60 if (URI
.empty() == true)
63 if (URI
.find(':') == string::npos
)
66 URI
= SubstVar(URI
,"$(ARCH)",_config
->Find("APT::Architecture"));
68 // Make sure that the URI is / postfixed
69 if (URI
[URI
.size() - 1] != '/')
75 bool pkgSourceList::Type::ParseStanza(vector
<metaIndex
*> &List
,
80 map
<string
, string
> Options
;
82 string Enabled
= Tags
.FindS("Enabled");
83 if (Enabled
.size() > 0 && StringToBool(Enabled
) == false)
86 // Define external/internal options
87 const char* option_deb822
[] = {
88 "Architectures", "Architectures-Add", "Architectures-Remove", "Trusted",
90 const char* option_internal
[] = {
91 "arch", "arch+", "arch-", "trusted",
93 for (unsigned int j
=0; j
< sizeof(option_deb822
)/sizeof(char*); j
++)
94 if (Tags
.Exists(option_deb822
[j
]))
96 // for deb822 the " " is the delimiter, but the backend expects ","
97 std::string option
= Tags
.FindS(option_deb822
[j
]);
98 std::replace(option
.begin(), option
.end(), ' ', ',');
99 Options
[option_internal
[j
]] = option
;
102 // now create one item per suite/section
103 string Suite
= Tags
.FindS("Suites");
104 Suite
= SubstVar(Suite
,"$(ARCH)",_config
->Find("APT::Architecture"));
105 string
const Section
= Tags
.FindS("Sections");
106 string URIS
= Tags
.FindS("URIs");
108 std::vector
<std::string
> list_uris
= StringSplit(URIS
, " ");
109 std::vector
<std::string
> list_dist
= StringSplit(Suite
, " ");
110 std::vector
<std::string
> list_section
= StringSplit(Section
, " ");
112 for (std::vector
<std::string
>::const_iterator U
= list_uris
.begin();
113 U
!= list_uris
.end(); U
++)
115 std::string URI
= (*U
);
118 _error
->Error(_("Malformed stanza %u in source list %s (URI parse)"),i
,Fd
.Name().c_str());
122 for (std::vector
<std::string
>::const_iterator I
= list_dist
.begin();
123 I
!= list_dist
.end(); I
++)
125 for (std::vector
<std::string
>::const_iterator J
= list_section
.begin();
126 J
!= list_section
.end(); J
++)
128 if (CreateItem(List
, URI
, (*I
), (*J
), Options
) == false)
138 // Type::ParseLine - Parse a single line /*{{{*/
139 // ---------------------------------------------------------------------
140 /* This is a generic one that is the 'usual' format for sources.list
141 Weird types may override this. */
142 bool pkgSourceList::Type::ParseLine(vector
<metaIndex
*> &List
,
144 unsigned long const &CurLine
,
145 string
const &File
) const
147 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
149 // Parse option field if it exists
150 // e.g.: [ option1=value1 option2=value2 ]
151 map
<string
, string
> Options
;
152 if (Buffer
!= 0 && Buffer
[0] == '[')
154 ++Buffer
; // ignore the [
155 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
156 while (*Buffer
!= ']')
158 // get one option, e.g. option1=value1
160 if (ParseQuoteWord(Buffer
,option
) == false)
161 return _error
->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine
,File
.c_str());
163 if (option
.length() < 3)
164 return _error
->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine
,File
.c_str());
166 // accept options even if the last has no space before the ]-end marker
167 if (option
.at(option
.length()-1) == ']')
169 for (; *Buffer
!= ']'; --Buffer
);
170 option
.resize(option
.length()-1);
173 size_t const needle
= option
.find('=');
174 if (needle
== string::npos
)
175 return _error
->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine
,File
.c_str(), option
.c_str());
177 string
const key
= string(option
, 0, needle
);
178 string
const value
= string(option
, needle
+ 1, option
.length());
180 if (key
.empty() == true)
181 return _error
->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine
,File
.c_str(), option
.c_str());
183 if (value
.empty() == true)
184 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());
186 Options
[key
] = value
;
188 ++Buffer
; // ignore the ]
189 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
196 if (ParseQuoteWord(Buffer
,URI
) == false)
197 return _error
->Error(_("Malformed line %lu in source list %s (URI)"),CurLine
,File
.c_str());
198 if (ParseQuoteWord(Buffer
,Dist
) == false)
199 return _error
->Error(_("Malformed line %lu in source list %s (dist)"),CurLine
,File
.c_str());
201 if (FixupURI(URI
) == false)
202 return _error
->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine
,File
.c_str());
204 // Check for an absolute dists specification.
205 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] == '/')
207 if (ParseQuoteWord(Buffer
,Section
) == true)
208 return _error
->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine
,File
.c_str());
209 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
210 return CreateItem(List
, URI
, Dist
, Section
, Options
);
213 // Grab the rest of the dists
214 if (ParseQuoteWord(Buffer
,Section
) == false)
215 return _error
->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine
,File
.c_str());
219 if (CreateItem(List
, URI
, Dist
, Section
, Options
) == false)
222 while (ParseQuoteWord(Buffer
,Section
) == true);
227 // SourceList::pkgSourceList - Constructors /*{{{*/
228 // ---------------------------------------------------------------------
230 pkgSourceList::pkgSourceList()
234 pkgSourceList::pkgSourceList(string File
)
239 // SourceList::~pkgSourceList - Destructor /*{{{*/
240 // ---------------------------------------------------------------------
242 pkgSourceList::~pkgSourceList()
244 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
248 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
249 // ---------------------------------------------------------------------
251 bool pkgSourceList::ReadMainList()
253 // CNC:2003-03-03 - Multiple sources list support.
262 // CNC:2003-11-28 - Entries in sources.list have priority over
263 // entries in sources.list.d.
264 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
265 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
267 if (RealFileExists(Main
) == true)
268 Res
&= ReadAppend(Main
);
269 else if (DirectoryExists(Parts
) == false)
270 // Only warn if there are no sources.list.d.
271 _error
->WarningE("DirectoryExists", _("Unable to read %s"), Parts
.c_str());
273 if (DirectoryExists(Parts
) == true)
274 Res
&= ReadSourceDir(Parts
);
275 else if (RealFileExists(Main
) == false)
276 // Only warn if there is no sources.list file.
277 _error
->WarningE("RealFileExists", _("Unable to read %s"), Main
.c_str());
282 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
283 // ---------------------------------------------------------------------
285 void pkgSourceList::Reset()
287 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
289 SrcList
.erase(SrcList
.begin(),SrcList
.end());
292 // SourceList::Read - Parse the sourcelist file /*{{{*/
293 // ---------------------------------------------------------------------
295 bool pkgSourceList::Read(string File
)
298 return ReadAppend(File
);
301 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
302 // ---------------------------------------------------------------------
304 bool pkgSourceList::ReadAppend(string File
)
306 if (_config
->FindB("APT::Sources::Use-Deb822", false) == true)
308 int lines_parsed
=ParseFileDeb822(File
);
309 if (lines_parsed
< 0)
311 else if (lines_parsed
> 0)
313 // no lines parsed ... fall through and use old style parser
315 return ParseFileOldStyle(File
);
318 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
319 // ---------------------------------------------------------------------
321 bool pkgSourceList::ParseFileOldStyle(string File
)
323 // Open the stream for reading
324 ifstream
F(File
.c_str(),ios::in
/*| ios::nocreate*/);
326 return _error
->Errno("ifstream::ifstream",_("Opening %s"),File
.c_str());
328 // CNC:2003-12-10 - 300 is too short.
332 while (F
.eof() == false)
334 F
.getline(Buffer
,sizeof(Buffer
));
336 _strtabexpand(Buffer
,sizeof(Buffer
));
337 if (F
.fail() && !F
.eof())
338 return _error
->Error(_("Line %u too long in source list %s."),
339 CurLine
,File
.c_str());
343 // CNC:2003-02-20 - Do not break if '#' is inside [].
344 for (I
= Buffer
; *I
!= 0 && *I
!= '#'; I
++)
347 char *b_end
= strchr(I
+ 1, ']');
353 const char *C
= _strstrip(Buffer
);
356 if (C
[0] == '#' || C
[0] == 0)
361 if (ParseQuoteWord(C
,LineType
) == false)
362 return _error
->Error(_("Malformed line %u in source list %s (type)"),CurLine
,File
.c_str());
364 Type
*Parse
= Type::GetType(LineType
.c_str());
366 return _error
->Error(_("Type '%s' is not known on line %u in source list %s"),LineType
.c_str(),CurLine
,File
.c_str());
368 if (Parse
->ParseLine(SrcList
, C
, CurLine
, File
) == false)
374 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
375 // ---------------------------------------------------------------------
376 /* Returns: the number of stanzas parsed*/
377 int pkgSourceList::ParseFileDeb822(string File
)
382 // see if we can read the file
383 _error
->PushToStack();
384 FileFd
Fd(File
, FileFd::ReadOnly
);
385 pkgTagFile
Sources(&Fd
);
386 if (_error
->PendingError() == true)
388 _error
->RevertToStack();
391 _error
->MergeWithStack();
394 while (Sources
.Step(Tags
) == true)
396 if(!Tags
.Exists("Types"))
399 string
const types
= Tags
.FindS("Types");
400 std::vector
<std::string
> list_types
= StringSplit(types
, " ");
401 for (std::vector
<std::string
>::const_iterator I
= list_types
.begin();
402 I
!= list_types
.end(); I
++)
404 Type
*Parse
= Type::GetType((*I
).c_str());
407 _error
->Error(_("Type '%s' is not known on stanza %u in source list %s"), (*I
).c_str(),i
,Fd
.Name().c_str());
411 if (!Parse
->ParseStanza(SrcList
, Tags
, i
, Fd
))
418 // we are done, return the number of stanzas read
422 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
423 // ---------------------------------------------------------------------
425 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File
,
426 pkgIndexFile
*&Found
) const
428 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
430 vector
<pkgIndexFile
*> *Indexes
= (*I
)->GetIndexFiles();
431 for (vector
<pkgIndexFile
*>::const_iterator J
= Indexes
->begin();
432 J
!= Indexes
->end(); ++J
)
434 if ((*J
)->FindInCache(*File
.Cache()) == File
)
445 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
446 // ---------------------------------------------------------------------
448 bool pkgSourceList::GetIndexes(pkgAcquire
*Owner
, bool GetAll
) const
450 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
451 if ((*I
)->GetIndexes(Owner
,GetAll
) == false)
456 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
457 // SourceList::ReadSourceDir - Read a directory with sources files
458 // Based on ReadConfigDir() /*{{{*/
459 // ---------------------------------------------------------------------
461 bool pkgSourceList::ReadSourceDir(string Dir
)
463 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "list", true);
466 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
467 if (ReadAppend(*I
) == false)
473 // GetLastModified() /*{{{*/
474 // ---------------------------------------------------------------------
476 time_t pkgSourceList::GetLastModifiedTime()
480 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
481 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
484 if (DirectoryExists(Parts
) == true)
485 List
= GetListOfFilesInDir(Parts
, "list", true);
487 // calculate the time
488 time_t mtime_sources
= GetModificationTime(Main
);
489 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
490 mtime_sources
= std::max(mtime_sources
, GetModificationTime(*I
));
492 return mtime_sources
;