]>
git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
ddebd206d5a0592d23f017a6dcd91caf0184b470
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("URL");
84 _error
->Error(_("Malformed stanza %u in source list %s (URI parse)"),i
,Fd
.Name().c_str());
88 string Dist
= Tags
.FindS("Suite");
89 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
91 // Define external/internal options
92 const char* option_deb822
[] = {
93 "Architectures", "Architectures-Add", "Architectures-Delete", "Trusted",
95 const char* option_internal
[] = {
96 "arch", "arch+", "arch-", "trusted",
98 for (unsigned int j
=0; j
< sizeof(option_deb822
)/sizeof(char*); j
++)
99 if (Tags
.Exists(option_deb822
[j
]))
100 Options
[option_internal
[j
]] = Tags
.FindS(option_deb822
[j
]);
102 // now create one item per section
103 string
const Section
= Tags
.FindS("Section");
104 std::vector
<std::string
> list
= StringSplit(Section
, " ");
105 for (std::vector
<std::string
>::const_iterator I
= list
.begin();
106 I
!= list
.end(); I
++)
107 return CreateItem(List
, URI
, Dist
, (*I
), Options
);
112 // Type::ParseLine - Parse a single line /*{{{*/
113 // ---------------------------------------------------------------------
114 /* This is a generic one that is the 'usual' format for sources.list
115 Weird types may override this. */
116 bool pkgSourceList::Type::ParseLine(vector
<metaIndex
*> &List
,
118 unsigned long const &CurLine
,
119 string
const &File
) const
121 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
123 // Parse option field if it exists
124 // e.g.: [ option1=value1 option2=value2 ]
125 map
<string
, string
> Options
;
126 if (Buffer
!= 0 && Buffer
[0] == '[')
128 ++Buffer
; // ignore the [
129 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
130 while (*Buffer
!= ']')
132 // get one option, e.g. option1=value1
134 if (ParseQuoteWord(Buffer
,option
) == false)
135 return _error
->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine
,File
.c_str());
137 if (option
.length() < 3)
138 return _error
->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine
,File
.c_str());
140 // accept options even if the last has no space before the ]-end marker
141 if (option
.at(option
.length()-1) == ']')
143 for (; *Buffer
!= ']'; --Buffer
);
144 option
.resize(option
.length()-1);
147 size_t const needle
= option
.find('=');
148 if (needle
== string::npos
)
149 return _error
->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine
,File
.c_str(), option
.c_str());
151 string
const key
= string(option
, 0, needle
);
152 string
const value
= string(option
, needle
+ 1, option
.length());
154 if (key
.empty() == true)
155 return _error
->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine
,File
.c_str(), option
.c_str());
157 if (value
.empty() == true)
158 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());
160 Options
[key
] = value
;
162 ++Buffer
; // ignore the ]
163 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
170 if (ParseQuoteWord(Buffer
,URI
) == false)
171 return _error
->Error(_("Malformed line %lu in source list %s (URI)"),CurLine
,File
.c_str());
172 if (ParseQuoteWord(Buffer
,Dist
) == false)
173 return _error
->Error(_("Malformed line %lu in source list %s (dist)"),CurLine
,File
.c_str());
175 if (FixupURI(URI
) == false)
176 return _error
->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine
,File
.c_str());
178 // Check for an absolute dists specification.
179 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] == '/')
181 if (ParseQuoteWord(Buffer
,Section
) == true)
182 return _error
->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine
,File
.c_str());
183 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
184 return CreateItem(List
, URI
, Dist
, Section
, Options
);
187 // Grab the rest of the dists
188 if (ParseQuoteWord(Buffer
,Section
) == false)
189 return _error
->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine
,File
.c_str());
193 if (CreateItem(List
, URI
, Dist
, Section
, Options
) == false)
196 while (ParseQuoteWord(Buffer
,Section
) == true);
201 // SourceList::pkgSourceList - Constructors /*{{{*/
202 // ---------------------------------------------------------------------
204 pkgSourceList::pkgSourceList()
208 pkgSourceList::pkgSourceList(string File
)
213 // SourceList::~pkgSourceList - Destructor /*{{{*/
214 // ---------------------------------------------------------------------
216 pkgSourceList::~pkgSourceList()
218 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
222 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
223 // ---------------------------------------------------------------------
225 bool pkgSourceList::ReadMainList()
227 // CNC:2003-03-03 - Multiple sources list support.
236 // CNC:2003-11-28 - Entries in sources.list have priority over
237 // entries in sources.list.d.
238 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
239 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
241 if (RealFileExists(Main
) == true)
242 Res
&= ReadAppend(Main
);
243 else if (DirectoryExists(Parts
) == false)
244 // Only warn if there are no sources.list.d.
245 _error
->WarningE("DirectoryExists", _("Unable to read %s"), Parts
.c_str());
247 if (DirectoryExists(Parts
) == true)
248 Res
&= ReadSourceDir(Parts
);
249 else if (RealFileExists(Main
) == false)
250 // Only warn if there is no sources.list file.
251 _error
->WarningE("RealFileExists", _("Unable to read %s"), Main
.c_str());
256 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
257 // ---------------------------------------------------------------------
259 void pkgSourceList::Reset()
261 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
263 SrcList
.erase(SrcList
.begin(),SrcList
.end());
266 // SourceList::Read - Parse the sourcelist file /*{{{*/
267 // ---------------------------------------------------------------------
269 bool pkgSourceList::Read(string File
)
272 return ReadAppend(File
);
275 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
276 // ---------------------------------------------------------------------
278 bool pkgSourceList::ReadAppend(string File
)
280 if (_config
->FindB("APT::Sources::Use-Deb822", true) == true)
282 int lines_parsed
=ParseFileDeb822(File
);
283 if (lines_parsed
< 0)
285 else if (lines_parsed
> 0)
287 // no lines parsed ... fall through and use old style parser
289 return ParseFileOldStyle(File
);
292 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
293 // ---------------------------------------------------------------------
295 bool pkgSourceList::ParseFileOldStyle(string File
)
297 // Open the stream for reading
298 ifstream
F(File
.c_str(),ios::in
/*| ios::nocreate*/);
300 return _error
->Errno("ifstream::ifstream",_("Opening %s"),File
.c_str());
302 // CNC:2003-12-10 - 300 is too short.
306 while (F
.eof() == false)
308 F
.getline(Buffer
,sizeof(Buffer
));
310 _strtabexpand(Buffer
,sizeof(Buffer
));
311 if (F
.fail() && !F
.eof())
312 return _error
->Error(_("Line %u too long in source list %s."),
313 CurLine
,File
.c_str());
317 // CNC:2003-02-20 - Do not break if '#' is inside [].
318 for (I
= Buffer
; *I
!= 0 && *I
!= '#'; I
++)
321 char *b_end
= strchr(I
+ 1, ']');
327 const char *C
= _strstrip(Buffer
);
330 if (C
[0] == '#' || C
[0] == 0)
335 if (ParseQuoteWord(C
,LineType
) == false)
336 return _error
->Error(_("Malformed line %u in source list %s (type)"),CurLine
,File
.c_str());
338 Type
*Parse
= Type::GetType(LineType
.c_str());
340 return _error
->Error(_("Type '%s' is not known on line %u in source list %s"),LineType
.c_str(),CurLine
,File
.c_str());
342 if (Parse
->ParseLine(SrcList
, C
, CurLine
, File
) == false)
348 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
349 // ---------------------------------------------------------------------
350 /* Returns: the number of stanzas parsed*/
351 int pkgSourceList::ParseFileDeb822(string File
)
356 // see if we can read the file
357 _error
->PushToStack();
358 FileFd
Fd(File
, FileFd::ReadOnly
);
359 pkgTagFile
Sources(&Fd
);
360 if (_error
->PendingError() == true)
362 _error
->RevertToStack();
365 _error
->MergeWithStack();
368 while (Sources
.Step(Tags
) == true)
370 if(!Tags
.Exists("Type"))
373 string
const type
= Tags
.FindS("Type");
374 Type
*Parse
= Type::GetType(type
.c_str());
377 _error
->Error(_("Type '%s' is not known on stanza %u in source list %s"),type
.c_str(),i
,Fd
.Name().c_str());
381 if (!Parse
->ParseStanza(SrcList
, Tags
, i
, Fd
))
387 // we are done, return the number of stanzas read
391 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
392 // ---------------------------------------------------------------------
394 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File
,
395 pkgIndexFile
*&Found
) const
397 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
399 vector
<pkgIndexFile
*> *Indexes
= (*I
)->GetIndexFiles();
400 for (vector
<pkgIndexFile
*>::const_iterator J
= Indexes
->begin();
401 J
!= Indexes
->end(); ++J
)
403 if ((*J
)->FindInCache(*File
.Cache()) == File
)
414 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
415 // ---------------------------------------------------------------------
417 bool pkgSourceList::GetIndexes(pkgAcquire
*Owner
, bool GetAll
) const
419 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); ++I
)
420 if ((*I
)->GetIndexes(Owner
,GetAll
) == false)
425 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
426 // SourceList::ReadSourceDir - Read a directory with sources files
427 // Based on ReadConfigDir() /*{{{*/
428 // ---------------------------------------------------------------------
430 bool pkgSourceList::ReadSourceDir(string Dir
)
432 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "list", true);
435 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
436 if (ReadAppend(*I
) == false)
442 // GetLastModified() /*{{{*/
443 // ---------------------------------------------------------------------
445 time_t pkgSourceList::GetLastModifiedTime()
449 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
450 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
453 if (DirectoryExists(Parts
) == true)
454 List
= GetListOfFilesInDir(Parts
, "list", true);
456 // calculate the time
457 time_t mtime_sources
= GetModificationTime(Main
);
458 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
459 mtime_sources
= std::max(mtime_sources
, GetModificationTime(*I
));
461 return mtime_sources
;