]>
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 // Type::ParseLine - Parse a single line /*{{{*/
75 // ---------------------------------------------------------------------
76 /* This is a generic one that is the 'usual' format for sources.list
77 Weird types may override this. */
78 bool pkgSourceList::Type::ParseLine(vector
<metaIndex
*> &List
,
80 unsigned long const &CurLine
,
81 string
const &File
) const
83 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
85 // Parse option field if it exists
86 // e.g.: [ option1=value1 option2=value2 ]
87 map
<string
, string
> Options
;
88 if (Buffer
!= 0 && Buffer
[0] == '[')
90 ++Buffer
; // ignore the [
91 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
92 while (*Buffer
!= ']')
94 // get one option, e.g. option1=value1
96 if (ParseQuoteWord(Buffer
,option
) == false)
97 return _error
->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine
,File
.c_str());
99 if (option
.length() < 3)
100 return _error
->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine
,File
.c_str());
102 // accept options even if the last has no space before the ]-end marker
103 if (option
.at(option
.length()-1) == ']')
105 for (; *Buffer
!= ']'; --Buffer
);
106 option
.resize(option
.length()-1);
109 size_t const needle
= option
.find('=');
110 if (needle
== string::npos
)
111 return _error
->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine
,File
.c_str(), option
.c_str());
113 string
const key
= string(option
, 0, needle
);
114 string
const value
= string(option
, needle
+ 1, option
.length());
116 if (key
.empty() == true)
117 return _error
->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine
,File
.c_str(), option
.c_str());
119 if (value
.empty() == true)
120 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());
122 Options
[key
] = value
;
124 ++Buffer
; // ignore the ]
125 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
132 if (ParseQuoteWord(Buffer
,URI
) == false)
133 return _error
->Error(_("Malformed line %lu in source list %s (URI)"),CurLine
,File
.c_str());
134 if (ParseQuoteWord(Buffer
,Dist
) == false)
135 return _error
->Error(_("Malformed line %lu in source list %s (dist)"),CurLine
,File
.c_str());
137 if (FixupURI(URI
) == false)
138 return _error
->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine
,File
.c_str());
140 // Check for an absolute dists specification.
141 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] == '/')
143 if (ParseQuoteWord(Buffer
,Section
) == true)
144 return _error
->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine
,File
.c_str());
145 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
146 return CreateItem(List
, URI
, Dist
, Section
, Options
);
149 // Grab the rest of the dists
150 if (ParseQuoteWord(Buffer
,Section
) == false)
151 return _error
->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine
,File
.c_str());
155 if (CreateItem(List
, URI
, Dist
, Section
, Options
) == false)
158 while (ParseQuoteWord(Buffer
,Section
) == true);
163 // SourceList::pkgSourceList - Constructors /*{{{*/
164 // ---------------------------------------------------------------------
166 pkgSourceList::pkgSourceList()
170 pkgSourceList::pkgSourceList(string File
)
175 // SourceList::~pkgSourceList - Destructor /*{{{*/
176 // ---------------------------------------------------------------------
178 pkgSourceList::~pkgSourceList()
180 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
184 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
185 // ---------------------------------------------------------------------
187 bool pkgSourceList::ReadMainList()
189 // CNC:2003-03-03 - Multiple sources list support.
198 // CNC:2003-11-28 - Entries in sources.list have priority over
199 // entries in sources.list.d.
200 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
201 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
203 if (RealFileExists(Main
) == true)
204 Res
&= ReadAppend(Main
);
205 else if (DirectoryExists(Parts
) == false)
206 // Only warn if there are no sources.list.d.
207 _error
->WarningE("DirectoryExists", _("Unable to read %s"), Parts
.c_str());
209 if (DirectoryExists(Parts
) == true)
210 Res
&= ReadSourceDir(Parts
);
211 else if (RealFileExists(Main
) == false)
212 // Only warn if there is no sources.list file.
213 _error
->WarningE("RealFileExists", _("Unable to read %s"), Main
.c_str());
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 // SourceList::Read - Parse the sourcelist file /*{{{*/
229 // ---------------------------------------------------------------------
231 bool pkgSourceList::Read(string File
)
234 return ReadAppend(File
);
237 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
238 // ---------------------------------------------------------------------
240 bool pkgSourceList::ReadAppend(string File
)
242 if (_config
->FindB("APT::Sources::Use-Deb822", true) == true)
244 int lines_parsed
=ParseFileDeb822(File
);
245 if (lines_parsed
< 0)
247 else if (lines_parsed
> 0)
249 // no lines parsed ... fall through and use old style parser
251 return ParseFileOldStyle(File
);
254 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
255 // ---------------------------------------------------------------------
257 bool pkgSourceList::ParseFileOldStyle(string File
)
259 // Open the stream for reading
260 ifstream
F(File
.c_str(),ios::in
/*| ios::nocreate*/);
262 return _error
->Errno("ifstream::ifstream",_("Opening %s"),File
.c_str());
264 // CNC:2003-12-10 - 300 is too short.
268 while (F
.eof() == false)
270 F
.getline(Buffer
,sizeof(Buffer
));
272 _strtabexpand(Buffer
,sizeof(Buffer
));
273 if (F
.fail() && !F
.eof())
274 return _error
->Error(_("Line %u too long in source list %s."),
275 CurLine
,File
.c_str());
279 // CNC:2003-02-20 - Do not break if '#' is inside [].
280 for (I
= Buffer
; *I
!= 0 && *I
!= '#'; I
++)
283 char *b_end
= strchr(I
+ 1, ']');
289 const char *C
= _strstrip(Buffer
);
292 if (C
[0] == '#' || C
[0] == 0)
297 if (ParseQuoteWord(C
,LineType
) == false)
298 return _error
->Error(_("Malformed line %u in source list %s (type)"),CurLine
,File
.c_str());
300 Type
*Parse
= Type::GetType(LineType
.c_str());
302 return _error
->Error(_("Type '%s' is not known on line %u in source list %s"),LineType
.c_str(),CurLine
,File
.c_str());
304 if (Parse
->ParseLine(SrcList
, C
, CurLine
, File
) == false)
310 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
311 // ---------------------------------------------------------------------
312 /* Returns: the number of stanzas parsed*/
313 int pkgSourceList::ParseFileDeb822(string File
)
316 map
<string
, string
> Options
;
319 // see if we can read the file
320 _error
->PushToStack();
321 FileFd
Fd(File
, FileFd::ReadOnly
);
322 pkgTagFile
Sources(&Fd
);
323 if (_error
->PendingError() == true)
325 _error
->RevertToStack();
328 _error
->MergeWithStack();
331 while (Sources
.Step(Tags
) == true)
333 if(!Tags
.Exists("Type"))
336 string
const type
= Tags
.FindS("Type");
337 Type
*Parse
= Type::GetType(type
.c_str());
340 _error
->Error(_("Type '%s' is not known on stanza %u in source list %s"),type
.c_str(),i
,Fd
.Name().c_str());
341 // true means we do not retry with old-style sources.list
345 string URI
= Tags
.FindS("URL");
346 if (!Parse
->FixupURI(URI
))
348 _error
->Error(_("Malformed stanza %u in source list %s (URI parse)"),i
,Fd
.Name().c_str());
349 // means we do not retry with old-style sources.list
353 string Dist
= Tags
.FindS("Dist");
354 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
356 // check if there are any options we support
357 const char* option_str
[] = {
358 "arch", "arch+", "arch-", "trusted",
360 for (unsigned int j
=0; j
< sizeof(option_str
)/sizeof(char*); j
++)
361 if (Tags
.Exists(option_str
[j
]))
362 Options
[option_str
[j
]] = Tags
.FindS(option_str
[j
]);
364 // now create one item per section
365 string
const Section
= Tags
.FindS("Section");
366 std::vector
<std::string
> list
;
367 if (Section
.find(","))
368 list
= StringSplit(Section
, ",");
370 list
= StringSplit(Section
, " ");
371 for (std::vector
<std::string
>::const_iterator I
= list
.begin();
372 I
!= list
.end(); I
++)
373 Parse
->CreateItem(SrcList
, URI
, Dist
, (*I
), Options
);
378 // we are done, return the number of stanzas read
382 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
383 // ---------------------------------------------------------------------
385 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File
,
386 pkgIndexFile
*&Found
) const
388 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
390 vector
<pkgIndexFile
*> *Indexes
= (*I
)->GetIndexFiles();
391 for (vector
<pkgIndexFile
*>::const_iterator J
= Indexes
->begin();
392 J
!= Indexes
->end(); ++J
)
394 if ((*J
)->FindInCache(*File
.Cache()) == File
)
405 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
406 // ---------------------------------------------------------------------
408 bool pkgSourceList::GetIndexes(pkgAcquire
*Owner
, bool GetAll
) const
410 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
411 if ((*I
)->GetIndexes(Owner
,GetAll
) == false)
416 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
417 // SourceList::ReadSourceDir - Read a directory with sources files
418 // Based on ReadConfigDir() /*{{{*/
419 // ---------------------------------------------------------------------
421 bool pkgSourceList::ReadSourceDir(string Dir
)
423 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "list", true);
426 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
427 if (ReadAppend(*I
) == false)
433 // GetLastModified() /*{{{*/
434 // ---------------------------------------------------------------------
436 time_t pkgSourceList::GetLastModifiedTime()
440 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
441 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
444 if (DirectoryExists(Parts
) == true)
445 List
= GetListOfFilesInDir(Parts
, "list", true);
447 // calculate the time
448 time_t mtime_sources
= GetModificationTime(Main
);
449 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
450 mtime_sources
= std::max(mtime_sources
, GetModificationTime(*I
));
452 return mtime_sources
;