]>
git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
51b766095edee84fd307e232e8b0cf00df53da9b
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 // Define external/internal options
89 const char* option_deb822
[] = {
90 "Architectures", "Architectures-Add", "Architectures-Remove", "Trusted",
92 const char* option_internal
[] = {
93 "arch", "arch+", "arch-", "trusted",
95 for (unsigned int j
=0; j
< sizeof(option_deb822
)/sizeof(char*); j
++)
96 if (Tags
.Exists(option_deb822
[j
]))
97 Options
[option_internal
[j
]] = Tags
.FindS(option_deb822
[j
]);
99 // now create one item per suite/section
100 string Suite
= Tags
.FindS("Suite");
101 Suite
= SubstVar(Suite
,"$(ARCH)",_config
->Find("APT::Architecture"));
102 string
const Section
= Tags
.FindS("Section");
104 std::vector
<std::string
> list_dist
= StringSplit(Suite
, " ");
105 std::vector
<std::string
> list_section
= StringSplit(Section
, " ");
106 for (std::vector
<std::string
>::const_iterator I
= list_dist
.begin();
107 I
!= list_dist
.end(); I
++)
109 for (std::vector
<std::string
>::const_iterator J
= list_section
.begin();
110 J
!= list_section
.end(); J
++)
112 if (CreateItem(List
, URI
, (*I
), (*J
), Options
) == false)
121 // Type::ParseLine - Parse a single line /*{{{*/
122 // ---------------------------------------------------------------------
123 /* This is a generic one that is the 'usual' format for sources.list
124 Weird types may override this. */
125 bool pkgSourceList::Type::ParseLine(vector
<metaIndex
*> &List
,
127 unsigned long const &CurLine
,
128 string
const &File
) const
130 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
132 // Parse option field if it exists
133 // e.g.: [ option1=value1 option2=value2 ]
134 map
<string
, string
> Options
;
135 if (Buffer
!= 0 && Buffer
[0] == '[')
137 ++Buffer
; // ignore the [
138 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
139 while (*Buffer
!= ']')
141 // get one option, e.g. option1=value1
143 if (ParseQuoteWord(Buffer
,option
) == false)
144 return _error
->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine
,File
.c_str());
146 if (option
.length() < 3)
147 return _error
->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine
,File
.c_str());
149 // accept options even if the last has no space before the ]-end marker
150 if (option
.at(option
.length()-1) == ']')
152 for (; *Buffer
!= ']'; --Buffer
);
153 option
.resize(option
.length()-1);
156 size_t const needle
= option
.find('=');
157 if (needle
== string::npos
)
158 return _error
->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine
,File
.c_str(), option
.c_str());
160 string
const key
= string(option
, 0, needle
);
161 string
const value
= string(option
, needle
+ 1, option
.length());
163 if (key
.empty() == true)
164 return _error
->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine
,File
.c_str(), option
.c_str());
166 if (value
.empty() == true)
167 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());
169 Options
[key
] = value
;
171 ++Buffer
; // ignore the ]
172 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
179 if (ParseQuoteWord(Buffer
,URI
) == false)
180 return _error
->Error(_("Malformed line %lu in source list %s (URI)"),CurLine
,File
.c_str());
181 if (ParseQuoteWord(Buffer
,Dist
) == false)
182 return _error
->Error(_("Malformed line %lu in source list %s (dist)"),CurLine
,File
.c_str());
184 if (FixupURI(URI
) == false)
185 return _error
->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine
,File
.c_str());
187 // Check for an absolute dists specification.
188 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] == '/')
190 if (ParseQuoteWord(Buffer
,Section
) == true)
191 return _error
->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine
,File
.c_str());
192 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
193 return CreateItem(List
, URI
, Dist
, Section
, Options
);
196 // Grab the rest of the dists
197 if (ParseQuoteWord(Buffer
,Section
) == false)
198 return _error
->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine
,File
.c_str());
202 if (CreateItem(List
, URI
, Dist
, Section
, Options
) == false)
205 while (ParseQuoteWord(Buffer
,Section
) == true);
210 // SourceList::pkgSourceList - Constructors /*{{{*/
211 // ---------------------------------------------------------------------
213 pkgSourceList::pkgSourceList()
217 pkgSourceList::pkgSourceList(string File
)
222 // SourceList::~pkgSourceList - Destructor /*{{{*/
223 // ---------------------------------------------------------------------
225 pkgSourceList::~pkgSourceList()
227 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
231 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
232 // ---------------------------------------------------------------------
234 bool pkgSourceList::ReadMainList()
236 // CNC:2003-03-03 - Multiple sources list support.
245 // CNC:2003-11-28 - Entries in sources.list have priority over
246 // entries in sources.list.d.
247 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
248 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
250 if (RealFileExists(Main
) == true)
251 Res
&= ReadAppend(Main
);
252 else if (DirectoryExists(Parts
) == false)
253 // Only warn if there are no sources.list.d.
254 _error
->WarningE("DirectoryExists", _("Unable to read %s"), Parts
.c_str());
256 if (DirectoryExists(Parts
) == true)
257 Res
&= ReadSourceDir(Parts
);
258 else if (RealFileExists(Main
) == false)
259 // Only warn if there is no sources.list file.
260 _error
->WarningE("RealFileExists", _("Unable to read %s"), Main
.c_str());
265 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
266 // ---------------------------------------------------------------------
268 void pkgSourceList::Reset()
270 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
272 SrcList
.erase(SrcList
.begin(),SrcList
.end());
275 // SourceList::Read - Parse the sourcelist file /*{{{*/
276 // ---------------------------------------------------------------------
278 bool pkgSourceList::Read(string File
)
281 return ReadAppend(File
);
284 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
285 // ---------------------------------------------------------------------
287 bool pkgSourceList::ReadAppend(string File
)
289 if (_config
->FindB("APT::Sources::Use-Deb822", true) == true)
291 int lines_parsed
=ParseFileDeb822(File
);
292 if (lines_parsed
< 0)
294 else if (lines_parsed
> 0)
296 // no lines parsed ... fall through and use old style parser
298 return ParseFileOldStyle(File
);
301 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
302 // ---------------------------------------------------------------------
304 bool pkgSourceList::ParseFileOldStyle(string File
)
306 // Open the stream for reading
307 ifstream
F(File
.c_str(),ios::in
/*| ios::nocreate*/);
309 return _error
->Errno("ifstream::ifstream",_("Opening %s"),File
.c_str());
311 // CNC:2003-12-10 - 300 is too short.
315 while (F
.eof() == false)
317 F
.getline(Buffer
,sizeof(Buffer
));
319 _strtabexpand(Buffer
,sizeof(Buffer
));
320 if (F
.fail() && !F
.eof())
321 return _error
->Error(_("Line %u too long in source list %s."),
322 CurLine
,File
.c_str());
326 // CNC:2003-02-20 - Do not break if '#' is inside [].
327 for (I
= Buffer
; *I
!= 0 && *I
!= '#'; I
++)
330 char *b_end
= strchr(I
+ 1, ']');
336 const char *C
= _strstrip(Buffer
);
339 if (C
[0] == '#' || C
[0] == 0)
344 if (ParseQuoteWord(C
,LineType
) == false)
345 return _error
->Error(_("Malformed line %u in source list %s (type)"),CurLine
,File
.c_str());
347 Type
*Parse
= Type::GetType(LineType
.c_str());
349 return _error
->Error(_("Type '%s' is not known on line %u in source list %s"),LineType
.c_str(),CurLine
,File
.c_str());
351 if (Parse
->ParseLine(SrcList
, C
, CurLine
, File
) == false)
357 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
358 // ---------------------------------------------------------------------
359 /* Returns: the number of stanzas parsed*/
360 int pkgSourceList::ParseFileDeb822(string File
)
365 // see if we can read the file
366 _error
->PushToStack();
367 FileFd
Fd(File
, FileFd::ReadOnly
);
368 pkgTagFile
Sources(&Fd
);
369 if (_error
->PendingError() == true)
371 _error
->RevertToStack();
374 _error
->MergeWithStack();
377 while (Sources
.Step(Tags
) == true)
379 if(!Tags
.Exists("Type"))
382 string
const type
= Tags
.FindS("Type");
383 Type
*Parse
= Type::GetType(type
.c_str());
386 _error
->Error(_("Type '%s' is not known on stanza %u in source list %s"),type
.c_str(),i
,Fd
.Name().c_str());
390 if (!Parse
->ParseStanza(SrcList
, Tags
, i
, Fd
))
396 // we are done, return the number of stanzas read
400 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
401 // ---------------------------------------------------------------------
403 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File
,
404 pkgIndexFile
*&Found
) const
406 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
408 vector
<pkgIndexFile
*> *Indexes
= (*I
)->GetIndexFiles();
409 for (vector
<pkgIndexFile
*>::const_iterator J
= Indexes
->begin();
410 J
!= Indexes
->end(); ++J
)
412 if ((*J
)->FindInCache(*File
.Cache()) == File
)
423 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
424 // ---------------------------------------------------------------------
426 bool pkgSourceList::GetIndexes(pkgAcquire
*Owner
, bool GetAll
) const
428 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
429 if ((*I
)->GetIndexes(Owner
,GetAll
) == false)
434 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
435 // SourceList::ReadSourceDir - Read a directory with sources files
436 // Based on ReadConfigDir() /*{{{*/
437 // ---------------------------------------------------------------------
439 bool pkgSourceList::ReadSourceDir(string Dir
)
441 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "list", true);
444 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
445 if (ReadAppend(*I
) == false)
451 // GetLastModified() /*{{{*/
452 // ---------------------------------------------------------------------
454 time_t pkgSourceList::GetLastModifiedTime()
458 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
459 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
462 if (DirectoryExists(Parts
) == true)
463 List
= GetListOfFilesInDir(Parts
, "list", true);
465 // calculate the time
466 time_t mtime_sources
= GetModificationTime(Main
);
467 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
468 mtime_sources
= std::max(mtime_sources
, GetModificationTime(*I
));
470 return mtime_sources
;