]>
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>
29 // Global list of Items supported
30 static pkgSourceList::Type
*ItmList
[10];
31 pkgSourceList::Type
**pkgSourceList::Type::GlobalList
= ItmList
;
32 unsigned long pkgSourceList::Type::GlobalListLen
= 0;
34 // Type::Type - Constructor /*{{{*/
35 // ---------------------------------------------------------------------
36 /* Link this to the global list of items*/
37 pkgSourceList::Type::Type() : Name(NULL
), Label(NULL
)
39 ItmList
[GlobalListLen
] = this;
43 // Type::GetType - Get a specific meta for a given type /*{{{*/
44 // ---------------------------------------------------------------------
46 pkgSourceList::Type
*pkgSourceList::Type::GetType(const char *Type
)
48 for (unsigned I
= 0; I
!= GlobalListLen
; I
++)
49 if (strcmp(GlobalList
[I
]->Name
,Type
) == 0)
54 // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
55 // ---------------------------------------------------------------------
57 bool pkgSourceList::Type::FixupURI(string
&URI
) const
59 if (URI
.empty() == true)
62 if (URI
.find(':') == string::npos
)
65 URI
= SubstVar(URI
,"$(ARCH)",_config
->Find("APT::Architecture"));
67 // Make sure that the URI is / postfixed
68 if (URI
[URI
.size() - 1] != '/')
74 bool pkgSourceList::Type::ParseStanza(vector
<metaIndex
*> &List
,
79 map
<string
, string
> Options
;
81 string Enabled
= Tags
.FindS("Enabled");
82 if (Enabled
.size() > 0 && StringToBool(Enabled
) == false)
85 // Define external/internal options
86 const char* option_deb822
[] = {
87 "Architectures", "Architectures-Add", "Architectures-Remove", "Trusted",
89 const char* option_internal
[] = {
90 "arch", "arch+", "arch-", "trusted",
92 for (unsigned int j
=0; j
< sizeof(option_deb822
)/sizeof(char*); j
++)
93 if (Tags
.Exists(option_deb822
[j
]))
94 Options
[option_internal
[j
]] = Tags
.FindS(option_deb822
[j
]);
96 // now create one item per suite/section
97 string Suite
= Tags
.FindS("Suites");
98 Suite
= SubstVar(Suite
,"$(ARCH)",_config
->Find("APT::Architecture"));
99 string
const Section
= Tags
.FindS("Sections");
100 string URIS
= Tags
.FindS("URIs");
102 std::vector
<std::string
> list_uris
= StringSplit(URIS
, " ");
103 std::vector
<std::string
> list_dist
= StringSplit(Suite
, " ");
104 std::vector
<std::string
> list_section
= StringSplit(Section
, " ");
106 for (std::vector
<std::string
>::const_iterator U
= list_uris
.begin();
107 U
!= list_uris
.end(); U
++)
109 std::string URI
= (*U
);
112 _error
->Error(_("Malformed stanza %u in source list %s (URI parse)"),i
,Fd
.Name().c_str());
116 for (std::vector
<std::string
>::const_iterator I
= list_dist
.begin();
117 I
!= list_dist
.end(); I
++)
119 for (std::vector
<std::string
>::const_iterator J
= list_section
.begin();
120 J
!= list_section
.end(); J
++)
122 if (CreateItem(List
, URI
, (*I
), (*J
), Options
) == false)
132 // Type::ParseLine - Parse a single line /*{{{*/
133 // ---------------------------------------------------------------------
134 /* This is a generic one that is the 'usual' format for sources.list
135 Weird types may override this. */
136 bool pkgSourceList::Type::ParseLine(vector
<metaIndex
*> &List
,
138 unsigned long const &CurLine
,
139 string
const &File
) const
141 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
143 // Parse option field if it exists
144 // e.g.: [ option1=value1 option2=value2 ]
145 map
<string
, string
> Options
;
146 if (Buffer
!= 0 && Buffer
[0] == '[')
148 ++Buffer
; // ignore the [
149 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
150 while (*Buffer
!= ']')
152 // get one option, e.g. option1=value1
154 if (ParseQuoteWord(Buffer
,option
) == false)
155 return _error
->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine
,File
.c_str());
157 if (option
.length() < 3)
158 return _error
->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine
,File
.c_str());
160 // accept options even if the last has no space before the ]-end marker
161 if (option
.at(option
.length()-1) == ']')
163 for (; *Buffer
!= ']'; --Buffer
);
164 option
.resize(option
.length()-1);
167 size_t const needle
= option
.find('=');
168 if (needle
== string::npos
)
169 return _error
->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine
,File
.c_str(), option
.c_str());
171 string
const key
= string(option
, 0, needle
);
172 string
const value
= string(option
, needle
+ 1, option
.length());
174 if (key
.empty() == true)
175 return _error
->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine
,File
.c_str(), option
.c_str());
177 if (value
.empty() == true)
178 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());
180 Options
[key
] = value
;
182 ++Buffer
; // ignore the ]
183 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
190 if (ParseQuoteWord(Buffer
,URI
) == false)
191 return _error
->Error(_("Malformed line %lu in source list %s (URI)"),CurLine
,File
.c_str());
192 if (ParseQuoteWord(Buffer
,Dist
) == false)
193 return _error
->Error(_("Malformed line %lu in source list %s (dist)"),CurLine
,File
.c_str());
195 if (FixupURI(URI
) == false)
196 return _error
->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine
,File
.c_str());
198 // Check for an absolute dists specification.
199 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] == '/')
201 if (ParseQuoteWord(Buffer
,Section
) == true)
202 return _error
->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine
,File
.c_str());
203 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
204 return CreateItem(List
, URI
, Dist
, Section
, Options
);
207 // Grab the rest of the dists
208 if (ParseQuoteWord(Buffer
,Section
) == false)
209 return _error
->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine
,File
.c_str());
213 if (CreateItem(List
, URI
, Dist
, Section
, Options
) == false)
216 while (ParseQuoteWord(Buffer
,Section
) == true);
221 // SourceList::pkgSourceList - Constructors /*{{{*/
222 // ---------------------------------------------------------------------
224 pkgSourceList::pkgSourceList()
228 pkgSourceList::pkgSourceList(string File
)
233 // SourceList::~pkgSourceList - Destructor /*{{{*/
234 // ---------------------------------------------------------------------
236 pkgSourceList::~pkgSourceList()
238 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
242 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
243 // ---------------------------------------------------------------------
245 bool pkgSourceList::ReadMainList()
247 // CNC:2003-03-03 - Multiple sources list support.
256 // CNC:2003-11-28 - Entries in sources.list have priority over
257 // entries in sources.list.d.
258 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
259 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
261 if (RealFileExists(Main
) == true)
262 Res
&= ReadAppend(Main
);
263 else if (DirectoryExists(Parts
) == false)
264 // Only warn if there are no sources.list.d.
265 _error
->WarningE("DirectoryExists", _("Unable to read %s"), Parts
.c_str());
267 if (DirectoryExists(Parts
) == true)
268 Res
&= ReadSourceDir(Parts
);
269 else if (RealFileExists(Main
) == false)
270 // Only warn if there is no sources.list file.
271 _error
->WarningE("RealFileExists", _("Unable to read %s"), Main
.c_str());
276 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
277 // ---------------------------------------------------------------------
279 void pkgSourceList::Reset()
281 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
283 SrcList
.erase(SrcList
.begin(),SrcList
.end());
286 // SourceList::Read - Parse the sourcelist file /*{{{*/
287 // ---------------------------------------------------------------------
289 bool pkgSourceList::Read(string File
)
292 return ReadAppend(File
);
295 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
296 // ---------------------------------------------------------------------
298 bool pkgSourceList::ReadAppend(string File
)
300 if (_config
->FindB("APT::Sources::Use-Deb822", false) == true)
302 int lines_parsed
=ParseFileDeb822(File
);
303 if (lines_parsed
< 0)
305 else if (lines_parsed
> 0)
307 // no lines parsed ... fall through and use old style parser
309 return ParseFileOldStyle(File
);
312 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
313 // ---------------------------------------------------------------------
315 bool pkgSourceList::ParseFileOldStyle(string File
)
317 // Open the stream for reading
318 ifstream
F(File
.c_str(),ios::in
/*| ios::nocreate*/);
320 return _error
->Errno("ifstream::ifstream",_("Opening %s"),File
.c_str());
322 // CNC:2003-12-10 - 300 is too short.
326 while (F
.eof() == false)
328 F
.getline(Buffer
,sizeof(Buffer
));
330 _strtabexpand(Buffer
,sizeof(Buffer
));
331 if (F
.fail() && !F
.eof())
332 return _error
->Error(_("Line %u too long in source list %s."),
333 CurLine
,File
.c_str());
337 // CNC:2003-02-20 - Do not break if '#' is inside [].
338 for (I
= Buffer
; *I
!= 0 && *I
!= '#'; I
++)
341 char *b_end
= strchr(I
+ 1, ']');
347 const char *C
= _strstrip(Buffer
);
350 if (C
[0] == '#' || C
[0] == 0)
355 if (ParseQuoteWord(C
,LineType
) == false)
356 return _error
->Error(_("Malformed line %u in source list %s (type)"),CurLine
,File
.c_str());
358 Type
*Parse
= Type::GetType(LineType
.c_str());
360 return _error
->Error(_("Type '%s' is not known on line %u in source list %s"),LineType
.c_str(),CurLine
,File
.c_str());
362 if (Parse
->ParseLine(SrcList
, C
, CurLine
, File
) == false)
368 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
369 // ---------------------------------------------------------------------
370 /* Returns: the number of stanzas parsed*/
371 int pkgSourceList::ParseFileDeb822(string File
)
376 // see if we can read the file
377 _error
->PushToStack();
378 FileFd
Fd(File
, FileFd::ReadOnly
);
379 pkgTagFile
Sources(&Fd
);
380 if (_error
->PendingError() == true)
382 _error
->RevertToStack();
385 _error
->MergeWithStack();
388 while (Sources
.Step(Tags
) == true)
390 if(!Tags
.Exists("Types"))
393 string
const types
= Tags
.FindS("Types");
394 std::vector
<std::string
> list_types
= StringSplit(types
, " ");
395 for (std::vector
<std::string
>::const_iterator I
= list_types
.begin();
396 I
!= list_types
.end(); I
++)
398 Type
*Parse
= Type::GetType((*I
).c_str());
401 _error
->Error(_("Type '%s' is not known on stanza %u in source list %s"), (*I
).c_str(),i
,Fd
.Name().c_str());
405 if (!Parse
->ParseStanza(SrcList
, Tags
, i
, Fd
))
412 // we are done, return the number of stanzas read
416 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
417 // ---------------------------------------------------------------------
419 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File
,
420 pkgIndexFile
*&Found
) const
422 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
424 vector
<pkgIndexFile
*> *Indexes
= (*I
)->GetIndexFiles();
425 for (vector
<pkgIndexFile
*>::const_iterator J
= Indexes
->begin();
426 J
!= Indexes
->end(); ++J
)
428 if ((*J
)->FindInCache(*File
.Cache()) == File
)
439 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
440 // ---------------------------------------------------------------------
442 bool pkgSourceList::GetIndexes(pkgAcquire
*Owner
, bool GetAll
) const
444 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
445 if ((*I
)->GetIndexes(Owner
,GetAll
) == false)
450 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
451 // SourceList::ReadSourceDir - Read a directory with sources files
452 // Based on ReadConfigDir() /*{{{*/
453 // ---------------------------------------------------------------------
455 bool pkgSourceList::ReadSourceDir(string Dir
)
457 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "list", true);
460 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
461 if (ReadAppend(*I
) == false)
467 // GetLastModified() /*{{{*/
468 // ---------------------------------------------------------------------
470 time_t pkgSourceList::GetLastModifiedTime()
474 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
475 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
478 if (DirectoryExists(Parts
) == true)
479 List
= GetListOfFilesInDir(Parts
, "list", true);
481 // calculate the time
482 time_t mtime_sources
= GetModificationTime(Main
);
483 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
484 mtime_sources
= std::max(mtime_sources
, GetModificationTime(*I
));
486 return mtime_sources
;