]>
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 URI
= Tags
.FindS("URI");
84 _error
->Error(_("Malformed stanza %u in source list %s (URI parse)"),i
,Fd
.Name().c_str());
88 string Enabled
= Tags
.FindS("Enabled");
89 if (Enabled
.size() > 0 && StringToBool(Enabled
) == false)
92 // Define external/internal options
93 const char* option_deb822
[] = {
94 "Architectures", "Architectures-Add", "Architectures-Remove", "Trusted",
96 const char* option_internal
[] = {
97 "arch", "arch+", "arch-", "trusted",
99 for (unsigned int j
=0; j
< sizeof(option_deb822
)/sizeof(char*); j
++)
100 if (Tags
.Exists(option_deb822
[j
]))
101 Options
[option_internal
[j
]] = Tags
.FindS(option_deb822
[j
]);
103 // now create one item per suite/section
104 string Suite
= Tags
.FindS("Suites");
105 Suite
= SubstVar(Suite
,"$(ARCH)",_config
->Find("APT::Architecture"));
106 string
const Section
= Tags
.FindS("Sections");
108 std::vector
<std::string
> list_dist
= StringSplit(Suite
, " ");
109 std::vector
<std::string
> list_section
= StringSplit(Section
, " ");
110 for (std::vector
<std::string
>::const_iterator I
= list_dist
.begin();
111 I
!= list_dist
.end(); I
++)
113 for (std::vector
<std::string
>::const_iterator J
= list_section
.begin();
114 J
!= list_section
.end(); J
++)
116 if (CreateItem(List
, URI
, (*I
), (*J
), Options
) == false)
125 // Type::ParseLine - Parse a single line /*{{{*/
126 // ---------------------------------------------------------------------
127 /* This is a generic one that is the 'usual' format for sources.list
128 Weird types may override this. */
129 bool pkgSourceList::Type::ParseLine(vector
<metaIndex
*> &List
,
131 unsigned long const &CurLine
,
132 string
const &File
) const
134 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
136 // Parse option field if it exists
137 // e.g.: [ option1=value1 option2=value2 ]
138 map
<string
, string
> Options
;
139 if (Buffer
!= 0 && Buffer
[0] == '[')
141 ++Buffer
; // ignore the [
142 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
143 while (*Buffer
!= ']')
145 // get one option, e.g. option1=value1
147 if (ParseQuoteWord(Buffer
,option
) == false)
148 return _error
->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine
,File
.c_str());
150 if (option
.length() < 3)
151 return _error
->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine
,File
.c_str());
153 // accept options even if the last has no space before the ]-end marker
154 if (option
.at(option
.length()-1) == ']')
156 for (; *Buffer
!= ']'; --Buffer
);
157 option
.resize(option
.length()-1);
160 size_t const needle
= option
.find('=');
161 if (needle
== string::npos
)
162 return _error
->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine
,File
.c_str(), option
.c_str());
164 string
const key
= string(option
, 0, needle
);
165 string
const value
= string(option
, needle
+ 1, option
.length());
167 if (key
.empty() == true)
168 return _error
->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine
,File
.c_str(), option
.c_str());
170 if (value
.empty() == true)
171 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());
173 Options
[key
] = value
;
175 ++Buffer
; // ignore the ]
176 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
183 if (ParseQuoteWord(Buffer
,URI
) == false)
184 return _error
->Error(_("Malformed line %lu in source list %s (URI)"),CurLine
,File
.c_str());
185 if (ParseQuoteWord(Buffer
,Dist
) == false)
186 return _error
->Error(_("Malformed line %lu in source list %s (dist)"),CurLine
,File
.c_str());
188 if (FixupURI(URI
) == false)
189 return _error
->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine
,File
.c_str());
191 // Check for an absolute dists specification.
192 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] == '/')
194 if (ParseQuoteWord(Buffer
,Section
) == true)
195 return _error
->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine
,File
.c_str());
196 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
197 return CreateItem(List
, URI
, Dist
, Section
, Options
);
200 // Grab the rest of the dists
201 if (ParseQuoteWord(Buffer
,Section
) == false)
202 return _error
->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine
,File
.c_str());
206 if (CreateItem(List
, URI
, Dist
, Section
, Options
) == false)
209 while (ParseQuoteWord(Buffer
,Section
) == true);
214 // SourceList::pkgSourceList - Constructors /*{{{*/
215 // ---------------------------------------------------------------------
217 pkgSourceList::pkgSourceList()
221 pkgSourceList::pkgSourceList(string File
)
226 // SourceList::~pkgSourceList - Destructor /*{{{*/
227 // ---------------------------------------------------------------------
229 pkgSourceList::~pkgSourceList()
231 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
235 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
236 // ---------------------------------------------------------------------
238 bool pkgSourceList::ReadMainList()
240 // CNC:2003-03-03 - Multiple sources list support.
249 // CNC:2003-11-28 - Entries in sources.list have priority over
250 // entries in sources.list.d.
251 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
252 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
254 if (RealFileExists(Main
) == true)
255 Res
&= ReadAppend(Main
);
256 else if (DirectoryExists(Parts
) == false)
257 // Only warn if there are no sources.list.d.
258 _error
->WarningE("DirectoryExists", _("Unable to read %s"), Parts
.c_str());
260 if (DirectoryExists(Parts
) == true)
261 Res
&= ReadSourceDir(Parts
);
262 else if (RealFileExists(Main
) == false)
263 // Only warn if there is no sources.list file.
264 _error
->WarningE("RealFileExists", _("Unable to read %s"), Main
.c_str());
269 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
270 // ---------------------------------------------------------------------
272 void pkgSourceList::Reset()
274 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
276 SrcList
.erase(SrcList
.begin(),SrcList
.end());
279 // SourceList::Read - Parse the sourcelist file /*{{{*/
280 // ---------------------------------------------------------------------
282 bool pkgSourceList::Read(string File
)
285 return ReadAppend(File
);
288 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
289 // ---------------------------------------------------------------------
291 bool pkgSourceList::ReadAppend(string File
)
293 if (_config
->FindB("APT::Sources::Use-Deb822", true) == true)
295 int lines_parsed
=ParseFileDeb822(File
);
296 if (lines_parsed
< 0)
298 else if (lines_parsed
> 0)
300 // no lines parsed ... fall through and use old style parser
302 return ParseFileOldStyle(File
);
305 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
306 // ---------------------------------------------------------------------
308 bool pkgSourceList::ParseFileOldStyle(string File
)
310 // Open the stream for reading
311 ifstream
F(File
.c_str(),ios::in
/*| ios::nocreate*/);
313 return _error
->Errno("ifstream::ifstream",_("Opening %s"),File
.c_str());
315 // CNC:2003-12-10 - 300 is too short.
319 while (F
.eof() == false)
321 F
.getline(Buffer
,sizeof(Buffer
));
323 _strtabexpand(Buffer
,sizeof(Buffer
));
324 if (F
.fail() && !F
.eof())
325 return _error
->Error(_("Line %u too long in source list %s."),
326 CurLine
,File
.c_str());
330 // CNC:2003-02-20 - Do not break if '#' is inside [].
331 for (I
= Buffer
; *I
!= 0 && *I
!= '#'; I
++)
334 char *b_end
= strchr(I
+ 1, ']');
340 const char *C
= _strstrip(Buffer
);
343 if (C
[0] == '#' || C
[0] == 0)
348 if (ParseQuoteWord(C
,LineType
) == false)
349 return _error
->Error(_("Malformed line %u in source list %s (type)"),CurLine
,File
.c_str());
351 Type
*Parse
= Type::GetType(LineType
.c_str());
353 return _error
->Error(_("Type '%s' is not known on line %u in source list %s"),LineType
.c_str(),CurLine
,File
.c_str());
355 if (Parse
->ParseLine(SrcList
, C
, CurLine
, File
) == false)
361 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
362 // ---------------------------------------------------------------------
363 /* Returns: the number of stanzas parsed*/
364 int pkgSourceList::ParseFileDeb822(string File
)
369 // see if we can read the file
370 _error
->PushToStack();
371 FileFd
Fd(File
, FileFd::ReadOnly
);
372 pkgTagFile
Sources(&Fd
);
373 if (_error
->PendingError() == true)
375 _error
->RevertToStack();
378 _error
->MergeWithStack();
381 while (Sources
.Step(Tags
) == true)
383 if(!Tags
.Exists("Type"))
386 string
const type
= Tags
.FindS("Type");
387 Type
*Parse
= Type::GetType(type
.c_str());
390 _error
->Error(_("Type '%s' is not known on stanza %u in source list %s"),type
.c_str(),i
,Fd
.Name().c_str());
394 if (!Parse
->ParseStanza(SrcList
, Tags
, i
, Fd
))
400 // we are done, return the number of stanzas read
404 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
405 // ---------------------------------------------------------------------
407 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File
,
408 pkgIndexFile
*&Found
) const
410 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
412 vector
<pkgIndexFile
*> *Indexes
= (*I
)->GetIndexFiles();
413 for (vector
<pkgIndexFile
*>::const_iterator J
= Indexes
->begin();
414 J
!= Indexes
->end(); ++J
)
416 if ((*J
)->FindInCache(*File
.Cache()) == File
)
427 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
428 // ---------------------------------------------------------------------
430 bool pkgSourceList::GetIndexes(pkgAcquire
*Owner
, bool GetAll
) const
432 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
433 if ((*I
)->GetIndexes(Owner
,GetAll
) == false)
438 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
439 // SourceList::ReadSourceDir - Read a directory with sources files
440 // Based on ReadConfigDir() /*{{{*/
441 // ---------------------------------------------------------------------
443 bool pkgSourceList::ReadSourceDir(string Dir
)
445 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "list", true);
448 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
449 if (ReadAppend(*I
) == false)
455 // GetLastModified() /*{{{*/
456 // ---------------------------------------------------------------------
458 time_t pkgSourceList::GetLastModifiedTime()
462 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
463 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
466 if (DirectoryExists(Parts
) == true)
467 List
= GetListOfFilesInDir(Parts
, "list", true);
469 // calculate the time
470 time_t mtime_sources
= GetModificationTime(Main
);
471 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
472 mtime_sources
= std::max(mtime_sources
, GetModificationTime(*I
));
474 return mtime_sources
;