]>
git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
b083da936d6ff19ab4c439fff667ec68647ec581
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>
21 #include <apt-pkg/pkgcache.h>
22 #include <apt-pkg/cacheiterators.h>
39 // Global list of Items supported
40 static pkgSourceList :: Type
* ItmList
[ 10 ];
41 pkgSourceList :: Type
** pkgSourceList :: Type :: GlobalList
= ItmList
;
42 unsigned long pkgSourceList :: Type :: GlobalListLen
= 0 ;
44 // Type::Type - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
46 /* Link this to the global list of items*/
47 pkgSourceList :: Type :: Type ( char const * const pName
, char const * const pLabel
) : Name ( pName
), Label ( pLabel
)
49 ItmList
[ GlobalListLen
] = this ;
52 pkgSourceList :: Type ::~ Type () {}
54 // Type::GetType - Get a specific meta for a given type /*{{{*/
55 // ---------------------------------------------------------------------
57 pkgSourceList :: Type
* pkgSourceList :: Type :: GetType ( const char * Type
)
59 for ( unsigned I
= 0 ; I
!= GlobalListLen
; ++ I
)
60 if ( strcmp ( GlobalList
[ I
]-> Name
, Type
) == 0 )
65 // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
66 // ---------------------------------------------------------------------
68 bool pkgSourceList :: Type :: FixupURI ( string
& URI
) const
70 if ( URI
. empty () == true )
73 if ( URI
. find ( ':' ) == string :: npos
)
76 URI
= SubstVar ( URI
, "$(ARCH)" , _config
-> Find ( "APT::Architecture" ));
78 // Make sure that the URI is / postfixed
79 if ( URI
[ URI
. size () - 1 ] != '/' )
85 bool pkgSourceList :: Type :: ParseStanza ( vector
< metaIndex
*> & List
, /*{{{*/
90 map
< string
, string
> Options
;
92 string Enabled
= Tags
. FindS ( "Enabled" );
93 if ( Enabled
. empty () == false && StringToBool ( Enabled
) == false )
96 std :: map
< char const * const , std :: pair
< char const * const , bool > > mapping
;
97 #define APT_PLUSMINUS(X, Y) \
98 mapping.insert(std::make_pair(X, std::make_pair(Y, true))); \
99 mapping.insert(std::make_pair(X "Add" , std::make_pair(Y "+" , true))); \
100 mapping.insert(std::make_pair(X "Remove" , std::make_pair(Y "-" , true)))
101 APT_PLUSMINUS ( "Architectures" , "arch" );
102 APT_PLUSMINUS ( "Languages" , "lang" );
103 APT_PLUSMINUS ( "Targets" , "target" );
105 mapping
. insert ( std :: make_pair ( "Trusted" , std :: make_pair ( "trusted" , false )));
106 mapping
. insert ( std :: make_pair ( "Check-Valid-Until" , std :: make_pair ( "check-valid-until" , false )));
107 mapping
. insert ( std :: make_pair ( "Valid-Until-Min" , std :: make_pair ( "valid-until-min" , false )));
108 mapping
. insert ( std :: make_pair ( "Valid-Until-Max" , std :: make_pair ( "valid-until-max" , false )));
109 mapping
. insert ( std :: make_pair ( "Signed-By" , std :: make_pair ( "signed-by" , false )));
110 mapping
. insert ( std :: make_pair ( "PDiffs" , std :: make_pair ( "pdiffs" , false )));
112 for ( std :: map
< char const * const , std :: pair
< char const * const , bool > >:: const_iterator m
= mapping
. begin (); m
!= mapping
. end (); ++ m
)
113 if ( Tags
. Exists ( m
-> first
))
115 std :: string option
= Tags
. FindS ( m
-> first
);
116 // for deb822 the " " is the delimiter, but the backend expects ","
117 if ( m
-> second
. second
== true )
118 std :: replace ( option
. begin (), option
. end (), ' ' , ',' );
119 Options
[ m
-> second
. first
] = option
;
122 // now create one item per suite/section
123 string Suite
= Tags
. FindS ( "Suites" );
124 Suite
= SubstVar ( Suite
, "$(ARCH)" , _config
-> Find ( "APT::Architecture" ));
125 string
const Component
= Tags
. FindS ( "Components" );
126 string
const URIS
= Tags
. FindS ( "URIs" );
128 std :: vector
< std :: string
> const list_uris
= VectorizeString ( URIS
, ' ' );
129 std :: vector
< std :: string
> const list_suite
= VectorizeString ( Suite
, ' ' );
130 std :: vector
< std :: string
> const list_comp
= VectorizeString ( Component
, ' ' );
132 if ( list_uris
. empty ())
133 // TRANSLATOR: %u is a line number, the first %s is a filename of a file with the extension "second %s" and the third %s is a unique identifier for bugreports
134 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "URI" );
136 for ( std :: vector
< std :: string
>:: const_iterator U
= list_uris
. begin ();
137 U
!= list_uris
. end (); ++ U
)
139 std :: string URI
= * U
;
140 if ( U
-> empty () || FixupURI ( URI
) == false )
141 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "URI parse" );
143 if ( list_suite
. empty ())
144 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "Suite" );
146 for ( std :: vector
< std :: string
>:: const_iterator S
= list_suite
. begin ();
147 S
!= list_suite
. end (); ++ S
)
149 if ( S
-> empty () == false && (* S
)[ S
-> size () - 1 ] == '/' )
151 if ( list_comp
. empty () == false )
152 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "absolute Suite Component" );
153 if ( CreateItem ( List
, URI
, * S
, "" , Options
) == false )
158 if ( list_comp
. empty ())
159 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "Component" );
161 for ( std :: vector
< std :: string
>:: const_iterator C
= list_comp
. begin ();
162 C
!= list_comp
. end (); ++ C
)
164 if ( CreateItem ( List
, URI
, * S
, * C
, Options
) == false )
175 // Type::ParseLine - Parse a single line /*{{{*/
176 // ---------------------------------------------------------------------
177 /* This is a generic one that is the 'usual' format for sources.list
178 Weird types may override this. */
179 bool pkgSourceList :: Type :: ParseLine ( vector
< metaIndex
*> & List
,
181 unsigned int const CurLine
,
182 string
const & File
) const
184 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
186 // Parse option field if it exists
187 // e.g.: [ option1=value1 option2=value2 ]
188 map
< string
, string
> Options
;
189 if ( Buffer
!= 0 && Buffer
[ 0 ] == '[' )
191 ++ Buffer
; // ignore the [
192 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
193 while (* Buffer
!= ']' )
195 // get one option, e.g. option1=value1
197 if ( ParseQuoteWord ( Buffer
, option
) == false )
198 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] unparseable" );
200 if ( option
. length () < 3 )
201 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] too short" );
203 // accept options even if the last has no space before the ]-end marker
204 if ( option
. at ( option
. length ()- 1 ) == ']' )
206 for (; * Buffer
!= ']' ; -- Buffer
);
207 option
. resize ( option
. length ()- 1 );
210 size_t const needle
= option
. find ( '=' );
211 if ( needle
== string :: npos
)
212 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] not assignment" );
214 string
const key
= string ( option
, 0 , needle
);
215 string
const value
= string ( option
, needle
+ 1 , option
. length ());
217 if ( key
. empty () == true )
218 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] no key" );
220 if ( value
. empty () == true )
221 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] no value" );
223 Options
[ key
] = value
;
225 ++ Buffer
; // ignore the ]
226 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
233 if ( ParseQuoteWord ( Buffer
, URI
) == false )
234 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "URI" );
235 if ( ParseQuoteWord ( Buffer
, Dist
) == false )
236 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "Suite" );
238 if ( FixupURI ( URI
) == false )
239 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "URI parse" );
241 // Check for an absolute dists specification.
242 if ( Dist
. empty () == false && Dist
[ Dist
. size () - 1 ] == '/' )
244 if ( ParseQuoteWord ( Buffer
, Section
) == true )
245 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "absolute Suite Component" );
246 Dist
= SubstVar ( Dist
, "$(ARCH)" , _config
-> Find ( "APT::Architecture" ));
247 return CreateItem ( List
, URI
, Dist
, Section
, Options
);
250 // Grab the rest of the dists
251 if ( ParseQuoteWord ( Buffer
, Section
) == false )
252 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "Component" );
256 if ( CreateItem ( List
, URI
, Dist
, Section
, Options
) == false )
259 while ( ParseQuoteWord ( Buffer
, Section
) == true );
264 // SourceList::pkgSourceList - Constructors /*{{{*/
265 // ---------------------------------------------------------------------
267 pkgSourceList :: pkgSourceList () : d ( NULL
)
271 // SourceList::~pkgSourceList - Destructor /*{{{*/
272 // ---------------------------------------------------------------------
274 pkgSourceList ::~ pkgSourceList ()
276 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
279 for ( auto F
= VolatileFiles
. begin (); F
!= VolatileFiles
. end (); ++ F
)
281 VolatileFiles
. clear ();
284 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
285 // ---------------------------------------------------------------------
287 bool pkgSourceList :: ReadMainList ()
289 // CNC:2003-03-03 - Multiple sources list support.
298 // CNC:2003-11-28 - Entries in sources.list have priority over
299 // entries in sources.list.d.
300 string Main
= _config
-> FindFile ( "Dir::Etc::sourcelist" );
301 string Parts
= _config
-> FindDir ( "Dir::Etc::sourceparts" );
303 if ( RealFileExists ( Main
) == true )
304 Res
&= ReadAppend ( Main
);
305 else if ( DirectoryExists ( Parts
) == false )
306 // Only warn if there are no sources.list.d.
307 _error
-> WarningE ( "DirectoryExists" , _ ( "Unable to read %s " ), Parts
. c_str ());
309 if ( DirectoryExists ( Parts
) == true )
310 Res
&= ReadSourceDir ( Parts
);
311 else if ( RealFileExists ( Main
) == false )
312 // Only warn if there is no sources.list file.
313 _error
-> WarningE ( "RealFileExists" , _ ( "Unable to read %s " ), Main
. c_str ());
318 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
319 // ---------------------------------------------------------------------
321 void pkgSourceList :: Reset ()
323 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
328 // SourceList::Read - Parse the sourcelist file /*{{{*/
329 // ---------------------------------------------------------------------
331 bool pkgSourceList :: Read ( string
const & File
)
334 return ReadAppend ( File
);
337 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
338 // ---------------------------------------------------------------------
340 bool pkgSourceList :: ReadAppend ( string
const & File
)
342 if ( flExtension ( File
) == "sources" )
343 return ParseFileDeb822 ( File
);
345 return ParseFileOldStyle ( File
);
348 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
349 // ---------------------------------------------------------------------
351 bool pkgSourceList :: ParseFileOldStyle ( std :: string
const & File
)
353 // Open the stream for reading
354 ifstream
F ( File
. c_str (), ios :: in
/*| ios::nocreate*/ );
355 if ( F
. fail () == true )
356 return _error
-> Errno ( "ifstream::ifstream" , _ ( "Opening %s " ), File
. c_str ());
359 for ( unsigned int CurLine
= 1 ; std :: getline ( F
, Buffer
); ++ CurLine
)
363 while (( curpos
= Buffer
. find ( '#' , curpos
)) != std :: string :: npos
)
365 size_t const openbrackets
= std :: count ( Buffer
. begin (), Buffer
. begin () + curpos
, '[' );
366 size_t const closedbrackets
= std :: count ( Buffer
. begin (), Buffer
. begin () + curpos
, ']' );
367 if ( openbrackets
> closedbrackets
)
369 // a # in an option, unlikely, but oh well, it was supported so stick to it
373 Buffer
. erase ( curpos
);
376 // remove spaces before/after
377 curpos
= Buffer
. find_first_not_of ( " \t\r " );
379 Buffer
. erase ( 0 , curpos
);
380 curpos
= Buffer
. find_last_not_of ( " \t\r " );
381 if ( curpos
!= std :: string :: npos
)
382 Buffer
. erase ( curpos
+ 1 );
388 std :: string
const LineType
= Buffer
. substr ( 0 , Buffer
. find_first_of ( " \t\v " ));
389 if ( LineType
. empty () || LineType
== Buffer
)
390 return _error
-> Error ( _ ( "Malformed line %u in source list %s (type)" ), CurLine
, File
. c_str ());
392 Type
* Parse
= Type :: GetType ( LineType
. c_str ());
394 return _error
-> Error ( _ ( "Type ' %s ' is not known on line %u in source list %s " ), LineType
. c_str (), CurLine
, File
. c_str ());
396 if ( Parse
-> ParseLine ( SrcList
, Buffer
. c_str () + LineType
. length (), CurLine
, File
) == false )
402 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
403 // ---------------------------------------------------------------------
404 /* Returns: the number of stanzas parsed*/
405 bool pkgSourceList :: ParseFileDeb822 ( string
const & File
)
407 pkgUserTagSection Tags
;
410 // see if we can read the file
411 FileFd
Fd ( File
, FileFd :: ReadOnly
);
412 pkgTagFile
Sources (& Fd
);
413 if ( _error
-> PendingError () == true )
414 return _error
-> Error ( _ ( "Malformed stanza %u in source list %s (type)" ), i
, File
. c_str ());
417 while ( Sources
. Step ( Tags
) == true )
419 if ( Tags
. Exists ( "Types" ) == false )
420 return _error
-> Error ( _ ( "Malformed stanza %u in source list %s (type)" ), i
, File
. c_str ());
422 string
const types
= Tags
. FindS ( "Types" );
423 std :: vector
< std :: string
> const list_types
= VectorizeString ( types
, ' ' );
424 for ( std :: vector
< std :: string
>:: const_iterator I
= list_types
. begin ();
425 I
!= list_types
. end (); ++ I
)
427 Type
* Parse
= Type :: GetType ((* I
). c_str ());
430 _error
-> Error ( _ ( "Type ' %s ' is not known on stanza %u in source list %s " ), (* I
). c_str (), i
, Fd
. Name (). c_str ());
434 if (! Parse
-> ParseStanza ( SrcList
, Tags
, i
, Fd
))
443 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
444 static bool FindInIndexFileContainer ( std :: vector
< pkgIndexFile
*> const & Cont
, pkgCache :: PkgFileIterator
const & File
, pkgIndexFile
*& Found
)
446 auto const J
= std :: find_if ( Cont
. begin (), Cont
. end (), [& File
]( pkgIndexFile
const * const J
) {
447 return J
-> FindInCache (* File
. Cache ()) == File
;
456 bool pkgSourceList :: FindIndex ( pkgCache :: PkgFileIterator File
,
457 pkgIndexFile
*& Found
) const
459 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
460 if ( FindInIndexFileContainer (*(* I
)-> GetIndexFiles (), File
, Found
))
463 return FindInIndexFileContainer ( VolatileFiles
, File
, Found
);
466 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
467 // ---------------------------------------------------------------------
469 bool pkgSourceList :: GetIndexes ( pkgAcquire
* Owner
, bool GetAll
) const
471 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
472 if ((* I
)-> GetIndexes ( Owner
, GetAll
) == false )
477 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
478 // SourceList::ReadSourceDir - Read a directory with sources files
479 // Based on ReadConfigDir() /*{{{*/
480 // ---------------------------------------------------------------------
482 bool pkgSourceList :: ReadSourceDir ( string
const & Dir
)
484 std :: vector
< std :: string
> ext
;
485 ext
. push_back ( "list" );
486 ext
. push_back ( "sources" );
487 std :: vector
< std :: string
> const List
= GetListOfFilesInDir ( Dir
, ext
, true );
490 for ( vector
< string
>:: const_iterator I
= List
. begin (); I
!= List
. end (); ++ I
)
491 if ( ReadAppend (* I
) == false )
497 // GetLastModified() /*{{{*/
498 // ---------------------------------------------------------------------
500 time_t pkgSourceList :: GetLastModifiedTime ()
504 string Main
= _config
-> FindFile ( "Dir::Etc::sourcelist" );
505 string Parts
= _config
-> FindDir ( "Dir::Etc::sourceparts" );
508 if ( DirectoryExists ( Parts
) == true )
509 List
= GetListOfFilesInDir ( Parts
, "list" , true );
511 // calculate the time
512 std :: vector
< time_t > modtimes
;
513 modtimes
. reserve ( 1 + List
. size ());
514 modtimes
. push_back ( GetModificationTime ( Main
));
515 std :: transform ( List
. begin (), List
. end (), std :: back_inserter ( modtimes
), GetModificationTime
);
516 auto const maxmtime
= std :: max_element ( modtimes
. begin (), modtimes
. end ());
520 std :: vector
< pkgIndexFile
*> pkgSourceList :: GetVolatileFiles () const /*{{{*/
522 return VolatileFiles
;
525 void pkgSourceList :: AddVolatileFile ( pkgIndexFile
* const File
) /*{{{*/
528 VolatileFiles
. push_back ( File
);