]>
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 , char const * const > mapping
;
97 #define APT_PLUSMINUS(X, Y) \
98 mapping.insert(std::make_pair(X, Y)); \
99 mapping.insert(std::make_pair(X "Add" , Y "+" )); \
100 mapping.insert(std::make_pair(X "Remove" , Y "-" ))
101 APT_PLUSMINUS ( "Architectures" , "arch" );
102 APT_PLUSMINUS ( "Languages" , "lang" );
103 APT_PLUSMINUS ( "Targets" , "target" );
105 mapping
. insert ( std :: make_pair ( "Trusted" , "trusted" ));
106 for ( std :: map
< char const * const , char const * const >:: const_iterator m
= mapping
. begin (); m
!= mapping
. end (); ++ m
)
107 if ( Tags
. Exists ( m
-> first
))
109 // for deb822 the " " is the delimiter, but the backend expects ","
110 std :: string option
= Tags
. FindS ( m
-> first
);
111 std :: replace ( option
. begin (), option
. end (), ' ' , ',' );
112 Options
[ m
-> second
] = option
;
115 // now create one item per suite/section
116 string Suite
= Tags
. FindS ( "Suites" );
117 Suite
= SubstVar ( Suite
, "$(ARCH)" , _config
-> Find ( "APT::Architecture" ));
118 string
const Component
= Tags
. FindS ( "Components" );
119 string
const URIS
= Tags
. FindS ( "URIs" );
121 std :: vector
< std :: string
> const list_uris
= VectorizeString ( URIS
, ' ' );
122 std :: vector
< std :: string
> const list_suite
= VectorizeString ( Suite
, ' ' );
123 std :: vector
< std :: string
> const list_comp
= VectorizeString ( Component
, ' ' );
125 if ( list_uris
. empty ())
126 // 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
127 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "URI" );
129 for ( std :: vector
< std :: string
>:: const_iterator U
= list_uris
. begin ();
130 U
!= list_uris
. end (); ++ U
)
132 std :: string URI
= * U
;
133 if ( U
-> empty () || FixupURI ( URI
) == false )
134 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "URI parse" );
136 if ( list_suite
. empty ())
137 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "Suite" );
139 for ( std :: vector
< std :: string
>:: const_iterator S
= list_suite
. begin ();
140 S
!= list_suite
. end (); ++ S
)
142 if ( S
-> empty () == false && (* S
)[ S
-> size () - 1 ] == '/' )
144 if ( list_comp
. empty () == false )
145 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "absolute Suite Component" );
146 if ( CreateItem ( List
, URI
, * S
, "" , Options
) == false )
151 if ( list_comp
. empty ())
152 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), i
, "sources" , Fd
. Name (). c_str (), "Component" );
154 for ( std :: vector
< std :: string
>:: const_iterator C
= list_comp
. begin ();
155 C
!= list_comp
. end (); ++ C
)
157 if ( CreateItem ( List
, URI
, * S
, * C
, Options
) == false )
168 // Type::ParseLine - Parse a single line /*{{{*/
169 // ---------------------------------------------------------------------
170 /* This is a generic one that is the 'usual' format for sources.list
171 Weird types may override this. */
172 bool pkgSourceList :: Type :: ParseLine ( vector
< metaIndex
*> & List
,
174 unsigned int const CurLine
,
175 string
const & File
) const
177 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
179 // Parse option field if it exists
180 // e.g.: [ option1=value1 option2=value2 ]
181 map
< string
, string
> Options
;
182 if ( Buffer
!= 0 && Buffer
[ 0 ] == '[' )
184 ++ Buffer
; // ignore the [
185 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
186 while (* Buffer
!= ']' )
188 // get one option, e.g. option1=value1
190 if ( ParseQuoteWord ( Buffer
, option
) == false )
191 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] unparseable" );
193 if ( option
. length () < 3 )
194 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] too short" );
196 // accept options even if the last has no space before the ]-end marker
197 if ( option
. at ( option
. length ()- 1 ) == ']' )
199 for (; * Buffer
!= ']' ; -- Buffer
);
200 option
. resize ( option
. length ()- 1 );
203 size_t const needle
= option
. find ( '=' );
204 if ( needle
== string :: npos
)
205 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] not assignment" );
207 string
const key
= string ( option
, 0 , needle
);
208 string
const value
= string ( option
, needle
+ 1 , option
. length ());
210 if ( key
. empty () == true )
211 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] no key" );
213 if ( value
. empty () == true )
214 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "[option] no value" );
216 Options
[ key
] = value
;
218 ++ Buffer
; // ignore the ]
219 for (; Buffer
!= 0 && isspace (* Buffer
); ++ Buffer
); // Skip whitespaces
226 if ( ParseQuoteWord ( Buffer
, URI
) == false )
227 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "URI" );
228 if ( ParseQuoteWord ( Buffer
, Dist
) == false )
229 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "Suite" );
231 if ( FixupURI ( URI
) == false )
232 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "URI parse" );
234 // Check for an absolute dists specification.
235 if ( Dist
. empty () == false && Dist
[ Dist
. size () - 1 ] == '/' )
237 if ( ParseQuoteWord ( Buffer
, Section
) == true )
238 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "absolute Suite Component" );
239 Dist
= SubstVar ( Dist
, "$(ARCH)" , _config
-> Find ( "APT::Architecture" ));
240 return CreateItem ( List
, URI
, Dist
, Section
, Options
);
243 // Grab the rest of the dists
244 if ( ParseQuoteWord ( Buffer
, Section
) == false )
245 return _error
-> Error ( _ ( "Malformed entry %u in %s file %s ( %s )" ), CurLine
, "list" , File
. c_str (), "Component" );
249 if ( CreateItem ( List
, URI
, Dist
, Section
, Options
) == false )
252 while ( ParseQuoteWord ( Buffer
, Section
) == true );
257 // SourceList::pkgSourceList - Constructors /*{{{*/
258 // ---------------------------------------------------------------------
260 pkgSourceList :: pkgSourceList () : d ( NULL
)
264 // SourceList::~pkgSourceList - Destructor /*{{{*/
265 // ---------------------------------------------------------------------
267 pkgSourceList ::~ pkgSourceList ()
269 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
273 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
274 // ---------------------------------------------------------------------
276 bool pkgSourceList :: ReadMainList ()
278 // CNC:2003-03-03 - Multiple sources list support.
287 // CNC:2003-11-28 - Entries in sources.list have priority over
288 // entries in sources.list.d.
289 string Main
= _config
-> FindFile ( "Dir::Etc::sourcelist" );
290 string Parts
= _config
-> FindDir ( "Dir::Etc::sourceparts" );
292 if ( RealFileExists ( Main
) == true )
293 Res
&= ReadAppend ( Main
);
294 else if ( DirectoryExists ( Parts
) == false )
295 // Only warn if there are no sources.list.d.
296 _error
-> WarningE ( "DirectoryExists" , _ ( "Unable to read %s " ), Parts
. c_str ());
298 if ( DirectoryExists ( Parts
) == true )
299 Res
&= ReadSourceDir ( Parts
);
300 else if ( RealFileExists ( Main
) == false )
301 // Only warn if there is no sources.list file.
302 _error
-> WarningE ( "RealFileExists" , _ ( "Unable to read %s " ), Main
. c_str ());
307 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
308 // ---------------------------------------------------------------------
310 void pkgSourceList :: Reset ()
312 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
314 SrcList
. erase ( SrcList
. begin (), SrcList
. end ());
317 // SourceList::Read - Parse the sourcelist file /*{{{*/
318 // ---------------------------------------------------------------------
320 bool pkgSourceList :: Read ( string
const & File
)
323 return ReadAppend ( File
);
326 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
327 // ---------------------------------------------------------------------
329 bool pkgSourceList :: ReadAppend ( string
const & File
)
331 if ( flExtension ( File
) == "sources" )
332 return ParseFileDeb822 ( File
);
334 return ParseFileOldStyle ( File
);
337 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
338 // ---------------------------------------------------------------------
340 bool pkgSourceList :: ParseFileOldStyle ( std :: string
const & File
)
342 // Open the stream for reading
343 ifstream
F ( File
. c_str (), ios :: in
/*| ios::nocreate*/ );
344 if ( F
. fail () == true )
345 return _error
-> Errno ( "ifstream::ifstream" , _ ( "Opening %s " ), File
. c_str ());
348 for ( unsigned int CurLine
= 1 ; std :: getline ( F
, Buffer
); ++ CurLine
)
352 while (( curpos
= Buffer
. find ( '#' , curpos
)) != std :: string :: npos
)
354 size_t const openbrackets
= std :: count ( Buffer
. begin (), Buffer
. begin () + curpos
, '[' );
355 size_t const closedbrackets
= std :: count ( Buffer
. begin (), Buffer
. begin () + curpos
, ']' );
356 if ( openbrackets
> closedbrackets
)
358 // a # in an option, unlikely, but oh well, it was supported so stick to it
362 Buffer
. erase ( curpos
);
365 // remove spaces before/after
366 curpos
= Buffer
. find_first_not_of ( " \t\r " );
368 Buffer
. erase ( 0 , curpos
);
369 curpos
= Buffer
. find_last_not_of ( " \t\r " );
370 if ( curpos
!= std :: string :: npos
)
371 Buffer
. erase ( curpos
+ 1 );
377 std :: string
const LineType
= Buffer
. substr ( 0 , Buffer
. find ( ' ' ));
378 if ( LineType
. empty () || LineType
== Buffer
)
379 return _error
-> Error ( _ ( "Malformed line %u in source list %s (type)" ), CurLine
, File
. c_str ());
381 Type
* Parse
= Type :: GetType ( LineType
. c_str ());
383 return _error
-> Error ( _ ( "Type ' %s ' is not known on line %u in source list %s " ), LineType
. c_str (), CurLine
, File
. c_str ());
385 if ( Parse
-> ParseLine ( SrcList
, Buffer
. c_str () + LineType
. length (), CurLine
, File
) == false )
391 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
392 // ---------------------------------------------------------------------
393 /* Returns: the number of stanzas parsed*/
394 bool pkgSourceList :: ParseFileDeb822 ( string
const & File
)
396 pkgUserTagSection Tags
;
399 // see if we can read the file
400 FileFd
Fd ( File
, FileFd :: ReadOnly
);
401 pkgTagFile
Sources (& Fd
);
402 if ( _error
-> PendingError () == true )
403 return _error
-> Error ( _ ( "Malformed stanza %u in source list %s (type)" ), i
, File
. c_str ());
406 while ( Sources
. Step ( Tags
) == true )
408 if ( Tags
. Exists ( "Types" ) == false )
409 return _error
-> Error ( _ ( "Malformed stanza %u in source list %s (type)" ), i
, File
. c_str ());
411 string
const types
= Tags
. FindS ( "Types" );
412 std :: vector
< std :: string
> const list_types
= VectorizeString ( types
, ' ' );
413 for ( std :: vector
< std :: string
>:: const_iterator I
= list_types
. begin ();
414 I
!= list_types
. end (); ++ I
)
416 Type
* Parse
= Type :: GetType ((* I
). c_str ());
419 _error
-> Error ( _ ( "Type ' %s ' is not known on stanza %u in source list %s " ), (* I
). c_str (), i
, Fd
. Name (). c_str ());
423 if (! Parse
-> ParseStanza ( SrcList
, Tags
, i
, Fd
))
432 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
433 // ---------------------------------------------------------------------
435 bool pkgSourceList :: FindIndex ( pkgCache :: PkgFileIterator File
,
436 pkgIndexFile
*& Found
) const
438 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
440 vector
< pkgIndexFile
*> * Indexes
= (* I
)-> GetIndexFiles ();
441 for ( vector
< pkgIndexFile
*>:: const_iterator J
= Indexes
-> begin ();
442 J
!= Indexes
-> end (); ++ J
)
444 if ((* J
)-> FindInCache (* File
. Cache ()) == File
)
455 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
456 // ---------------------------------------------------------------------
458 bool pkgSourceList :: GetIndexes ( pkgAcquire
* Owner
, bool GetAll
) const
460 for ( const_iterator I
= SrcList
. begin (); I
!= SrcList
. end (); ++ I
)
461 if ((* I
)-> GetIndexes ( Owner
, GetAll
) == false )
466 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
467 // SourceList::ReadSourceDir - Read a directory with sources files
468 // Based on ReadConfigDir() /*{{{*/
469 // ---------------------------------------------------------------------
471 bool pkgSourceList :: ReadSourceDir ( string
const & Dir
)
473 std :: vector
< std :: string
> ext
;
474 ext
. push_back ( "list" );
475 ext
. push_back ( "sources" );
476 std :: vector
< std :: string
> const List
= GetListOfFilesInDir ( Dir
, ext
, true );
479 for ( vector
< string
>:: const_iterator I
= List
. begin (); I
!= List
. end (); ++ I
)
480 if ( ReadAppend (* I
) == false )
486 // GetLastModified() /*{{{*/
487 // ---------------------------------------------------------------------
489 time_t pkgSourceList :: GetLastModifiedTime ()
493 string Main
= _config
-> FindFile ( "Dir::Etc::sourcelist" );
494 string Parts
= _config
-> FindDir ( "Dir::Etc::sourceparts" );
497 if ( DirectoryExists ( Parts
) == true )
498 List
= GetListOfFilesInDir ( Parts
, "list" , true );
500 // calculate the time
501 time_t mtime_sources
= GetModificationTime ( Main
);
502 for ( vector
< string
>:: const_iterator I
= List
. begin (); I
!= List
. end (); ++ I
)
503 mtime_sources
= std :: max ( mtime_sources
, GetModificationTime (* I
));
505 return mtime_sources
;