]>
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>
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 )));
111 mapping
. insert ( std :: make_pair ( "By-Hash" , std :: make_pair ( "by-hash" , false )));
113 for ( std :: map
< char const * const , std :: pair
< char const * const , bool > >:: const_iterator m
= mapping
. begin (); m
!= mapping
. end (); ++ m
)
114 if ( Tags
. Exists ( m
-> first
))
116 std :: string option
= Tags
. FindS ( m
-> first
);
117 // for deb822 the " " is the delimiter, but the backend expects ","
118 if ( m
-> second
. second
== true )
119 std :: replace ( option
. begin (), option
. end (), ' ' , ',' );
120 Options
[ m
-> second
. first
] = option
;
125 strprintf ( entry
, " %s : %i " , Fd
. Name (). c_str (), i
);
126 Options
[ "sourceslist-entry" ] = entry
;
129 // now create one item per suite/section
130 string Suite
= Tags
. FindS ( "Suites" );
131 Suite
= SubstVar ( Suite
, "$(ARCH)" , _config
-> Find ( "APT::Architecture" ));
132 string
const Component
= Tags
. FindS ( "Components" );
133 string
const URIS
= Tags
. FindS ( "URIs" );
135 std :: vector
< std :: string
> const list_uris
= VectorizeString ( URIS
, ' ' );
136 std :: vector
< std :: string
> const list_suite
= VectorizeString ( Suite
, ' ' );
137 std :: vector
< std :: string
> const list_comp
= VectorizeString ( Component
, ' ' );
139 if ( list_uris
. empty ())
140 // 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
141 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "URI" );
143 for ( std :: vector
< std :: string
>:: const_iterator U
= list_uris
. begin ();
144 U
!= list_uris
. end (); ++ U
)
146 std :: string URI
= * U
;
147 if ( U
-> empty () || FixupURI ( URI
) == false )
148 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "URI parse" );
150 if ( list_suite
. empty ())
151 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "Suite" );
153 for ( std :: vector
< std :: string
>:: const_iterator S
= list_suite
. begin ();
154 S
!= list_suite
. end (); ++ S
)
156 if ( S
-> empty () == false && (* S
)[ S
-> size () - 1 ] == '/' )
158 if ( list_comp
. empty () == false )
159 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "absolute Suite Component" );
160 if ( CreateItem ( List
, URI
, * S
, "" , Options
) == false )
165 if ( list_comp
. empty ())
166 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "Component" );
168 for ( std :: vector
< std :: string
>:: const_iterator C
= list_comp
. begin ();
169 C
!= list_comp
. end (); ++ C
)
171 if ( CreateItem ( List
, URI
, * S
, * C
, Options
) == false )
182 // Type::ParseLine - Parse a single line /*{{{*/
183 // ---------------------------------------------------------------------
184 /* This is a generic one that is the 'usual' format for sources.list
185 Weird types may override this. */
186 bool pkgSourceList :: Type :: ParseLine ( vector
< metaIndex
*> & List
,
188 unsigned int const CurLine
,
189 string
const & File
) const
191 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
193 // Parse option field if it exists
194 // e.g.: [ option1=value1 option2=value2 ]
195 map
< string
, string
> Options
;
198 strprintf ( entry
, " %s : %i " , File
. c_str (), CurLine
);
199 Options
[ "sourceslist-entry" ] = entry
;
201 if ( Buffer
!= 0 && Buffer
[ 0 ] == '[' )
203 ++ Buffer
; // ignore the [
204 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
205 while (* Buffer
!= ']' )
207 // get one option, e.g. option1=value1
209 if ( ParseQuoteWord ( Buffer
, option
) == false )
210 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] unparseable" );
212 if ( option
. length () < 3 )
213 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] too short" );
215 // accept options even if the last has no space before the ]-end marker
216 if ( option
. at ( option
. length ()- 1 ) == ']' )
218 for (; * Buffer
!= ']' ; -- Buffer
);
219 option
. resize ( option
. length ()- 1 );
222 size_t const needle
= option
. find ( '=' );
223 if ( needle
== string :: npos
)
224 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] not assignment" );
226 string
const key
= string ( option
, 0 , needle
);
227 string
const value
= string ( option
, needle
+ 1 , option
. length ());
229 if ( key
. empty () == true )
230 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] no key" );
232 if ( value
. empty () == true )
233 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] no value" );
235 Options
[ key
] = value
;
237 ++ Buffer
; // ignore the ]
238 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
245 if ( ParseQuoteWord ( Buffer
, URI
) == false )
246 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "URI" );
247 if ( ParseQuoteWord ( Buffer
, Dist
) == false )
248 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "Suite" );
250 if ( FixupURI ( URI
) == false )
251 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "URI parse" );
253 // Check for an absolute dists specification.
254 if ( Dist
. empty () == false && Dist
[ Dist
. size () - 1 ] == '/' )
256 if ( ParseQuoteWord ( Buffer
, Section
) == true )
257 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "absolute Suite Component" );
258 Dist
= SubstVar ( Dist
, "$(ARCH)" , _config
-> Find ( "APT::Architecture" ));
259 return CreateItem ( List
, URI
, Dist
, Section
, Options
);
262 // Grab the rest of the dists
263 if ( ParseQuoteWord ( Buffer
, Section
) == false )
264 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "Component" );
268 if ( CreateItem ( List
, URI
, Dist
, Section
, Options
) == false )
271 while ( ParseQuoteWord ( Buffer
, Section
) == true );
276 // SourceList::pkgSourceList - Constructors /*{{{*/
277 // ---------------------------------------------------------------------
279 pkgSourceList :: pkgSourceList () : d ( NULL
)
283 // SourceList::~pkgSourceList - Destructor /*{{{*/
284 // ---------------------------------------------------------------------
286 pkgSourceList ::~ pkgSourceList ()
288 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
291 for ( auto F
= VolatileFiles
. begin (); F
!= VolatileFiles
. end (); ++ F
)
293 VolatileFiles
. clear ();
296 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
297 // ---------------------------------------------------------------------
299 bool pkgSourceList :: ReadMainList ()
301 // CNC:2003-03-03 - Multiple sources list support.
310 // CNC:2003-11-28 - Entries in sources.list have priority over
311 // entries in sources.list.d.
312 string Main
= _config
-> FindFile ( "Dir::Etc::sourcelist" );
313 string Parts
= _config
-> FindDir ( "Dir::Etc::sourceparts" );
315 if ( RealFileExists ( Main
) == true )
316 Res
&= ReadAppend ( Main
);
317 else if ( DirectoryExists ( Parts
) == false )
318 // Only warn if there are no sources.list.d.
319 _error
-> WarningE ( "DirectoryExists" , _ ( "Unable to read %s " ), Parts
. c_str ());
321 if ( DirectoryExists ( Parts
) == true )
322 Res
&= ReadSourceDir ( Parts
);
323 else if ( RealFileExists ( Main
) == false )
324 // Only warn if there is no sources.list file.
325 _error
-> WarningE ( "RealFileExists" , _ ( "Unable to read %s " ), Main
. c_str ());
330 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
331 // ---------------------------------------------------------------------
333 void pkgSourceList :: Reset ()
335 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
340 // SourceList::Read - Parse the sourcelist file /*{{{*/
341 // ---------------------------------------------------------------------
343 bool pkgSourceList :: Read ( string
const & File
)
346 return ReadAppend ( File
);
349 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
350 // ---------------------------------------------------------------------
352 bool pkgSourceList :: ReadAppend ( string
const & File
)
354 if ( flExtension ( File
) == "sources" )
355 return ParseFileDeb822 ( File
);
357 return ParseFileOldStyle ( File
);
360 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
361 // ---------------------------------------------------------------------
363 bool pkgSourceList :: ParseFileOldStyle ( std :: string
const & File
)
365 // Open the stream for reading
366 ifstream
F ( File
. c_str (), ios :: in
/*| ios::nocreate*/ );
367 if ( F
. fail () == true )
368 return _error
-> Errno ( "ifstream::ifstream" , _ ( "Opening %s " ), File
. c_str ());
371 for ( unsigned int CurLine
= 1 ; std :: getline ( F
, Buffer
); ++ CurLine
)
375 while (( curpos
= Buffer
. find ( '#' , curpos
)) != std :: string :: npos
)
377 size_t const openbrackets
= std :: count ( Buffer
. begin (), Buffer
. begin () + curpos
, '[' );
378 size_t const closedbrackets
= std :: count ( Buffer
. begin (), Buffer
. begin () + curpos
, ']' );
379 if ( openbrackets
> closedbrackets
)
381 // a # in an option, unlikely, but oh well, it was supported so stick to it
385 Buffer
. erase ( curpos
);
388 // remove spaces before/after
389 curpos
= Buffer
. find_first_not_of ( " \t\r " );
391 Buffer
. erase ( 0 , curpos
);
392 curpos
= Buffer
. find_last_not_of ( " \t\r " );
393 if ( curpos
!= std :: string :: npos
)
394 Buffer
. erase ( curpos
+ 1 );
400 std :: string
const LineType
= Buffer
. substr ( 0 , Buffer
. find_first_of ( " \t\v " ));
401 if ( LineType
. empty () || LineType
== Buffer
)
402 return _error
-> Error ( _ ( "Malformed line %u in source list %s (type)" ), CurLine
, File
. c_str ());
404 Type
* Parse
= Type :: GetType ( LineType
. c_str ());
406 return _error
-> Error ( _ ( "Type ' %s ' is not known on line %u in source list %s " ), LineType
. c_str (), CurLine
, File
. c_str ());
408 if ( Parse
-> ParseLine ( SrcList
, Buffer
. c_str () + LineType
. length (), CurLine
, File
) == false )
414 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
415 // ---------------------------------------------------------------------
416 /* Returns: the number of stanzas parsed*/
417 bool pkgSourceList :: ParseFileDeb822 ( string
const & File
)
419 pkgUserTagSection Tags
;
422 // see if we can read the file
423 FileFd
Fd ( File
, FileFd :: ReadOnly
);
424 pkgTagFile
Sources (& Fd
);
425 if ( Fd
. IsOpen () == false || Fd
. Failed ())
426 return _error
-> Error ( _ ( "Malformed stanza %u in source list %s (type)" ), i
, File
. c_str ());
429 while ( Sources
. Step ( Tags
) == true )
431 if ( Tags
. Exists ( "Types" ) == false )
432 return _error
-> Error ( _ ( "Malformed stanza %u in source list %s (type)" ), i
, File
. c_str ());
434 string
const types
= Tags
. FindS ( "Types" );
435 std :: vector
< std :: string
> const list_types
= VectorizeString ( types
, ' ' );
436 for ( std :: vector
< std :: string
>:: const_iterator I
= list_types
. begin ();
437 I
!= list_types
. end (); ++ I
)
439 Type
* Parse
= Type :: GetType ((* I
). c_str ());
442 _error
-> Error ( _ ( "Type ' %s ' is not known on stanza %u in source list %s " ), (* I
). c_str (), i
, Fd
. Name (). c_str ());
446 if (! Parse
-> ParseStanza ( SrcList
, Tags
, i
, Fd
))
455 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
456 static bool FindInIndexFileContainer ( std :: vector
< pkgIndexFile
*> const & Cont
, pkgCache :: PkgFileIterator
const & File
, pkgIndexFile
*& Found
)
458 auto const J
= std :: find_if ( Cont
. begin (), Cont
. end (), [& File
]( pkgIndexFile
const * const J
) {
459 return J
-> FindInCache (* File
. Cache ()) == File
;
468 bool pkgSourceList :: FindIndex ( pkgCache :: PkgFileIterator File
,
469 pkgIndexFile
*& Found
) const
471 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
472 if ( FindInIndexFileContainer (*(* I
)-> GetIndexFiles (), File
, Found
))
475 return FindInIndexFileContainer ( VolatileFiles
, File
, Found
);
478 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
479 // ---------------------------------------------------------------------
481 bool pkgSourceList :: GetIndexes ( pkgAcquire
* Owner
, bool GetAll
) const
483 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
484 if ((* I
)-> GetIndexes ( Owner
, GetAll
) == false )
489 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
490 // SourceList::ReadSourceDir - Read a directory with sources files
491 // Based on ReadConfigDir() /*{{{*/
492 // ---------------------------------------------------------------------
494 bool pkgSourceList :: ReadSourceDir ( string
const & Dir
)
496 std :: vector
< std :: string
> ext
;
497 ext
. push_back ( "list" );
498 ext
. push_back ( "sources" );
499 std :: vector
< std :: string
> const List
= GetListOfFilesInDir ( Dir
, ext
, true );
502 for ( vector
< string
>:: const_iterator I
= List
. begin (); I
!= List
. end (); ++ I
)
503 if ( ReadAppend (* I
) == false )
509 // GetLastModified() /*{{{*/
510 // ---------------------------------------------------------------------
512 time_t pkgSourceList :: GetLastModifiedTime ()
516 string Main
= _config
-> FindFile ( "Dir::Etc::sourcelist" );
517 string Parts
= _config
-> FindDir ( "Dir::Etc::sourceparts" );
520 if ( DirectoryExists ( Parts
) == true )
521 List
= GetListOfFilesInDir ( Parts
, "list" , true );
523 // calculate the time
524 std :: vector
< time_t > modtimes
;
525 modtimes
. reserve ( 1 + List
. size ());
526 modtimes
. push_back ( GetModificationTime ( Main
));
527 std :: transform ( List
. begin (), List
. end (), std :: back_inserter ( modtimes
), GetModificationTime
);
528 auto const maxmtime
= std :: max_element ( modtimes
. begin (), modtimes
. end ());
532 std :: vector
< pkgIndexFile
*> pkgSourceList :: GetVolatileFiles () const /*{{{*/
534 return VolatileFiles
;
537 void pkgSourceList :: AddVolatileFile ( pkgIndexFile
* const File
) /*{{{*/
540 VolatileFiles
. push_back ( File
);