]>
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 )));
111 for ( std :: map
< char const * const , std :: pair
< char const * const , bool > >:: const_iterator m
= mapping
. begin (); m
!= mapping
. end (); ++ m
)
112 if ( Tags
. Exists ( m
-> first
))
114 std :: string option
= Tags
. FindS ( m
-> first
);
115 // for deb822 the " " is the delimiter, but the backend expects ","
116 if ( m
-> second
. second
== true )
117 std :: replace ( option
. begin (), option
. end (), ' ' , ',' );
118 Options
[ m
-> second
. first
] = option
;
121 // now create one item per suite/section
122 string Suite
= Tags
. FindS ( "Suites" );
123 Suite
= SubstVar ( Suite
, "$(ARCH)" , _config
-> Find ( "APT::Architecture" ));
124 string
const Component
= Tags
. FindS ( "Components" );
125 string
const URIS
= Tags
. FindS ( "URIs" );
127 std :: vector
< std :: string
> const list_uris
= VectorizeString ( URIS
, ' ' );
128 std :: vector
< std :: string
> const list_suite
= VectorizeString ( Suite
, ' ' );
129 std :: vector
< std :: string
> const list_comp
= VectorizeString ( Component
, ' ' );
131 if ( list_uris
. empty ())
132 // 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
133 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "URI" );
135 for ( std :: vector
< std :: string
>:: const_iterator U
= list_uris
. begin ();
136 U
!= list_uris
. end (); ++ U
)
138 std :: string URI
= * U
;
139 if ( U
-> empty () || FixupURI ( URI
) == false )
140 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "URI parse" );
142 if ( list_suite
. empty ())
143 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "Suite" );
145 for ( std :: vector
< std :: string
>:: const_iterator S
= list_suite
. begin ();
146 S
!= list_suite
. end (); ++ S
)
148 if ( S
-> empty () == false && (* S
)[ S
-> size () - 1 ] == '/' )
150 if ( list_comp
. empty () == false )
151 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "absolute Suite Component" );
152 if ( CreateItem ( List
, URI
, * S
, "" , Options
) == false )
157 if ( list_comp
. empty ())
158 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "Component" );
160 for ( std :: vector
< std :: string
>:: const_iterator C
= list_comp
. begin ();
161 C
!= list_comp
. end (); ++ C
)
163 if ( CreateItem ( List
, URI
, * S
, * C
, Options
) == false )
174 // Type::ParseLine - Parse a single line /*{{{*/
175 // ---------------------------------------------------------------------
176 /* This is a generic one that is the 'usual' format for sources.list
177 Weird types may override this. */
178 bool pkgSourceList :: Type :: ParseLine ( vector
< metaIndex
*> & List
,
180 unsigned int const CurLine
,
181 string
const & File
) const
183 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
185 // Parse option field if it exists
186 // e.g.: [ option1=value1 option2=value2 ]
187 map
< string
, string
> Options
;
188 if ( Buffer
!= 0 && Buffer
[ 0 ] == '[' )
190 ++ Buffer
; // ignore the [
191 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
192 while (* Buffer
!= ']' )
194 // get one option, e.g. option1=value1
196 if ( ParseQuoteWord ( Buffer
, option
) == false )
197 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] unparseable" );
199 if ( option
. length () < 3 )
200 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] too short" );
202 // accept options even if the last has no space before the ]-end marker
203 if ( option
. at ( option
. length ()- 1 ) == ']' )
205 for (; * Buffer
!= ']' ; -- Buffer
);
206 option
. resize ( option
. length ()- 1 );
209 size_t const needle
= option
. find ( '=' );
210 if ( needle
== string :: npos
)
211 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] not assignment" );
213 string
const key
= string ( option
, 0 , needle
);
214 string
const value
= string ( option
, needle
+ 1 , option
. length ());
216 if ( key
. empty () == true )
217 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] no key" );
219 if ( value
. empty () == true )
220 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] no value" );
222 Options
[ key
] = value
;
224 ++ Buffer
; // ignore the ]
225 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
232 if ( ParseQuoteWord ( Buffer
, URI
) == false )
233 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "URI" );
234 if ( ParseQuoteWord ( Buffer
, Dist
) == false )
235 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "Suite" );
237 if ( FixupURI ( URI
) == false )
238 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "URI parse" );
240 // Check for an absolute dists specification.
241 if ( Dist
. empty () == false && Dist
[ Dist
. size () - 1 ] == '/' )
243 if ( ParseQuoteWord ( Buffer
, Section
) == true )
244 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "absolute Suite Component" );
245 Dist
= SubstVar ( Dist
, "$(ARCH)" , _config
-> Find ( "APT::Architecture" ));
246 return CreateItem ( List
, URI
, Dist
, Section
, Options
);
249 // Grab the rest of the dists
250 if ( ParseQuoteWord ( Buffer
, Section
) == false )
251 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "Component" );
255 if ( CreateItem ( List
, URI
, Dist
, Section
, Options
) == false )
258 while ( ParseQuoteWord ( Buffer
, Section
) == true );
263 // SourceList::pkgSourceList - Constructors /*{{{*/
264 // ---------------------------------------------------------------------
266 pkgSourceList :: pkgSourceList () : d ( NULL
)
270 // SourceList::~pkgSourceList - Destructor /*{{{*/
271 // ---------------------------------------------------------------------
273 pkgSourceList ::~ pkgSourceList ()
275 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
278 for ( auto F
= VolatileFiles
. begin (); F
!= VolatileFiles
. end (); ++ F
)
280 VolatileFiles
. clear ();
283 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
284 // ---------------------------------------------------------------------
286 bool pkgSourceList :: ReadMainList ()
288 // CNC:2003-03-03 - Multiple sources list support.
297 // CNC:2003-11-28 - Entries in sources.list have priority over
298 // entries in sources.list.d.
299 string Main
= _config
-> FindFile ( "Dir::Etc::sourcelist" );
300 string Parts
= _config
-> FindDir ( "Dir::Etc::sourceparts" );
302 if ( RealFileExists ( Main
) == true )
303 Res
&= ReadAppend ( Main
);
304 else if ( DirectoryExists ( Parts
) == false )
305 // Only warn if there are no sources.list.d.
306 _error
-> WarningE ( "DirectoryExists" , _ ( "Unable to read %s " ), Parts
. c_str ());
308 if ( DirectoryExists ( Parts
) == true )
309 Res
&= ReadSourceDir ( Parts
);
310 else if ( RealFileExists ( Main
) == false )
311 // Only warn if there is no sources.list file.
312 _error
-> WarningE ( "RealFileExists" , _ ( "Unable to read %s " ), Main
. c_str ());
317 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
318 // ---------------------------------------------------------------------
320 void pkgSourceList :: Reset ()
322 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
324 SrcList
. erase ( SrcList
. begin (), SrcList
. end ());
327 // SourceList::Read - Parse the sourcelist file /*{{{*/
328 // ---------------------------------------------------------------------
330 bool pkgSourceList :: Read ( string
const & File
)
333 return ReadAppend ( File
);
336 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
337 // ---------------------------------------------------------------------
339 bool pkgSourceList :: ReadAppend ( string
const & File
)
341 if ( flExtension ( File
) == "sources" )
342 return ParseFileDeb822 ( File
);
344 return ParseFileOldStyle ( File
);
347 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
348 // ---------------------------------------------------------------------
350 bool pkgSourceList :: ParseFileOldStyle ( std :: string
const & File
)
352 // Open the stream for reading
353 ifstream
F ( File
. c_str (), ios :: in
/*| ios::nocreate*/ );
354 if ( F
. fail () == true )
355 return _error
-> Errno ( "ifstream::ifstream" , _ ( "Opening %s " ), File
. c_str ());
358 for ( unsigned int CurLine
= 1 ; std :: getline ( F
, Buffer
); ++ CurLine
)
362 while (( curpos
= Buffer
. find ( '#' , curpos
)) != std :: string :: npos
)
364 size_t const openbrackets
= std :: count ( Buffer
. begin (), Buffer
. begin () + curpos
, '[' );
365 size_t const closedbrackets
= std :: count ( Buffer
. begin (), Buffer
. begin () + curpos
, ']' );
366 if ( openbrackets
> closedbrackets
)
368 // a # in an option, unlikely, but oh well, it was supported so stick to it
372 Buffer
. erase ( curpos
);
375 // remove spaces before/after
376 curpos
= Buffer
. find_first_not_of ( " \t\r " );
378 Buffer
. erase ( 0 , curpos
);
379 curpos
= Buffer
. find_last_not_of ( " \t\r " );
380 if ( curpos
!= std :: string :: npos
)
381 Buffer
. erase ( curpos
+ 1 );
387 std :: string
const LineType
= Buffer
. substr ( 0 , Buffer
. find_first_of ( " \t\v " ));
388 if ( LineType
. empty () || LineType
== Buffer
)
389 return _error
-> Error ( _ ( "Malformed line %u in source list %s (type)" ), CurLine
, File
. c_str ());
391 Type
* Parse
= Type :: GetType ( LineType
. c_str ());
393 return _error
-> Error ( _ ( "Type ' %s ' is not known on line %u in source list %s " ), LineType
. c_str (), CurLine
, File
. c_str ());
395 if ( Parse
-> ParseLine ( SrcList
, Buffer
. c_str () + LineType
. length (), CurLine
, File
) == false )
401 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
402 // ---------------------------------------------------------------------
403 /* Returns: the number of stanzas parsed*/
404 bool pkgSourceList :: ParseFileDeb822 ( string
const & File
)
406 pkgUserTagSection Tags
;
409 // see if we can read the file
410 FileFd
Fd ( File
, FileFd :: ReadOnly
);
411 pkgTagFile
Sources (& Fd
);
412 if ( _error
-> PendingError () == true )
413 return _error
-> Error ( _ ( "Malformed stanza %u in source list %s (type)" ), i
, File
. c_str ());
416 while ( Sources
. Step ( Tags
) == true )
418 if ( Tags
. Exists ( "Types" ) == false )
419 return _error
-> Error ( _ ( "Malformed stanza %u in source list %s (type)" ), i
, File
. c_str ());
421 string
const types
= Tags
. FindS ( "Types" );
422 std :: vector
< std :: string
> const list_types
= VectorizeString ( types
, ' ' );
423 for ( std :: vector
< std :: string
>:: const_iterator I
= list_types
. begin ();
424 I
!= list_types
. end (); ++ I
)
426 Type
* Parse
= Type :: GetType ((* I
). c_str ());
429 _error
-> Error ( _ ( "Type ' %s ' is not known on stanza %u in source list %s " ), (* I
). c_str (), i
, Fd
. Name (). c_str ());
433 if (! Parse
-> ParseStanza ( SrcList
, Tags
, i
, Fd
))
442 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
443 // ---------------------------------------------------------------------
445 bool pkgSourceList :: FindIndex ( pkgCache :: PkgFileIterator File
,
446 pkgIndexFile
*& Found
) const
448 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
450 vector
< pkgIndexFile
*> * Indexes
= (* I
)-> GetIndexFiles ();
451 for ( vector
< pkgIndexFile
*>:: const_iterator J
= Indexes
-> begin ();
452 J
!= Indexes
-> end (); ++ J
)
454 if ((* J
)-> FindInCache (* File
. Cache ()) == File
)
461 for ( vector
< pkgIndexFile
*>:: const_iterator J
= VolatileFiles
. begin ();
462 J
!= VolatileFiles
. end (); ++ J
)
464 if ((* J
)-> FindInCache (* File
. Cache ()) == File
)
473 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
474 // ---------------------------------------------------------------------
476 bool pkgSourceList :: GetIndexes ( pkgAcquire
* Owner
, bool GetAll
) const
478 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
479 if ((* I
)-> GetIndexes ( Owner
, GetAll
) == false )
484 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
485 // SourceList::ReadSourceDir - Read a directory with sources files
486 // Based on ReadConfigDir() /*{{{*/
487 // ---------------------------------------------------------------------
489 bool pkgSourceList :: ReadSourceDir ( string
const & Dir
)
491 std :: vector
< std :: string
> ext
;
492 ext
. push_back ( "list" );
493 ext
. push_back ( "sources" );
494 std :: vector
< std :: string
> const List
= GetListOfFilesInDir ( Dir
, ext
, true );
497 for ( vector
< string
>:: const_iterator I
= List
. begin (); I
!= List
. end (); ++ I
)
498 if ( ReadAppend (* I
) == false )
504 // GetLastModified() /*{{{*/
505 // ---------------------------------------------------------------------
507 time_t pkgSourceList :: GetLastModifiedTime ()
511 string Main
= _config
-> FindFile ( "Dir::Etc::sourcelist" );
512 string Parts
= _config
-> FindDir ( "Dir::Etc::sourceparts" );
515 if ( DirectoryExists ( Parts
) == true )
516 List
= GetListOfFilesInDir ( Parts
, "list" , true );
518 // calculate the time
519 time_t mtime_sources
= GetModificationTime ( Main
);
520 for ( vector
< string
>:: const_iterator I
= List
. begin (); I
!= List
. end (); ++ I
)
521 mtime_sources
= std :: max ( mtime_sources
, GetModificationTime (* I
));
523 return mtime_sources
;
526 std :: vector
< pkgIndexFile
*> pkgSourceList :: GetVolatileFiles () const /*{{{*/
528 return VolatileFiles
;
531 void pkgSourceList :: AddVolatileFile ( pkgIndexFile
* const File
) /*{{{*/
534 VolatileFiles
. push_back ( File
);