]>
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 /*{{{*/
11 #include <apt-pkg/sourcelist.h>
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/fileutl.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/configuration.h>
24 // Global list of Items supported
25 static pkgSourceList::Type
*ItmList
[10];
26 pkgSourceList::Type
**pkgSourceList::Type::GlobalList
= ItmList
;
27 unsigned long pkgSourceList::Type::GlobalListLen
= 0;
29 // Type::Type - Constructor /*{{{*/
30 // ---------------------------------------------------------------------
31 /* Link this to the global list of items*/
32 pkgSourceList::Type::Type()
34 ItmList
[GlobalListLen
] = this;
38 // Type::GetType - Get a specific meta for a given type /*{{{*/
39 // ---------------------------------------------------------------------
41 pkgSourceList::Type
*pkgSourceList::Type::GetType(const char *Type
)
43 for (unsigned I
= 0; I
!= GlobalListLen
; I
++)
44 if (strcmp(GlobalList
[I
]->Name
,Type
) == 0)
49 // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
50 // ---------------------------------------------------------------------
52 bool pkgSourceList::Type::FixupURI(string
&URI
) const
54 if (URI
.empty() == true)
57 if (URI
.find(':') == string::npos
)
60 URI
= SubstVar(URI
,"$(ARCH)",_config
->Find("APT::Architecture"));
62 // Make sure that the URI is / postfixed
63 if (URI
[URI
.size() - 1] != '/')
69 // Type::ParseLine - Parse a single line /*{{{*/
70 // ---------------------------------------------------------------------
71 /* This is a generic one that is the 'usual' format for sources.list
72 Weird types may override this. */
73 bool pkgSourceList::Type::ParseLine(vector
<metaIndex
*> &List
,
75 unsigned long const &CurLine
,
76 string
const &File
) const
78 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
80 // Parse option field if it exists
81 // e.g.: [ option1=value1 option2=value2 ]
82 map
<string
, string
> Options
;
83 if (Buffer
!= 0 && Buffer
[0] == '[')
85 ++Buffer
; // ignore the [
86 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
87 while (*Buffer
!= ']')
89 // get one option, e.g. option1=value1
91 if (ParseQuoteWord(Buffer
,option
) == false)
92 return _error
->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine
,File
.c_str());
94 if (option
.length() < 3)
95 return _error
->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine
,File
.c_str());
97 size_t const needle
= option
.find('=');
98 if (needle
== string::npos
)
99 return _error
->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine
,File
.c_str(), option
.c_str());
101 string
const key
= string(option
, 0, needle
);
102 string
const value
= string(option
, needle
+ 1, option
.length());
104 if (key
.empty() == true)
105 return _error
->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine
,File
.c_str(), option
.c_str());
107 if (value
.empty() == true)
108 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());
110 Options
[key
] = value
;
112 ++Buffer
; // ignore the ]
113 for (;Buffer
!= 0 && isspace(*Buffer
); ++Buffer
); // Skip whitespaces
120 if (ParseQuoteWord(Buffer
,URI
) == false)
121 return _error
->Error(_("Malformed line %lu in source list %s (URI)"),CurLine
,File
.c_str());
122 if (ParseQuoteWord(Buffer
,Dist
) == false)
123 return _error
->Error(_("Malformed line %lu in source list %s (dist)"),CurLine
,File
.c_str());
125 if (FixupURI(URI
) == false)
126 return _error
->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine
,File
.c_str());
128 // Check for an absolute dists specification.
129 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] == '/')
131 if (ParseQuoteWord(Buffer
,Section
) == true)
132 return _error
->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine
,File
.c_str());
133 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
134 return CreateItem(List
, URI
, Dist
, Section
, Options
);
137 // Grab the rest of the dists
138 if (ParseQuoteWord(Buffer
,Section
) == false)
139 return _error
->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine
,File
.c_str());
143 if (CreateItem(List
, URI
, Dist
, Section
, Options
) == false)
146 while (ParseQuoteWord(Buffer
,Section
) == true);
152 // SourceList::pkgSourceList - Constructors /*{{{*/
153 // ---------------------------------------------------------------------
155 pkgSourceList::pkgSourceList()
159 pkgSourceList::pkgSourceList(string File
)
164 // SourceList::~pkgSourceList - Destructor /*{{{*/
165 // ---------------------------------------------------------------------
167 pkgSourceList::~pkgSourceList()
169 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
174 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
175 // ---------------------------------------------------------------------
177 bool pkgSourceList::ReadMainList()
179 // CNC:2003-03-03 - Multiple sources list support.
188 // CNC:2003-11-28 - Entries in sources.list have priority over
189 // entries in sources.list.d.
190 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
191 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
193 if (FileExists(Main
) == true)
194 Res
&= ReadAppend(Main
);
195 else if (FileExists(Parts
) == false)
196 // Only warn if there are no sources.list.d.
197 _error
->WarningE("FileExists",_("Unable to read %s"),Main
.c_str());
199 if (FileExists(Parts
) == true)
200 Res
&= ReadSourceDir(Parts
);
201 else if (FileExists(Main
) == false)
202 // Only warn if there is no sources.list file.
203 _error
->WarningE("FileExists",_("Unable to read %s"),Parts
.c_str());
208 // CNC:2003-03-03 - Needed to preserve backwards compatibility.
209 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
210 // ---------------------------------------------------------------------
212 void pkgSourceList::Reset()
214 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
216 SrcList
.erase(SrcList
.begin(),SrcList
.end());
219 // CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
220 // SourceList::Read - Parse the sourcelist file /*{{{*/
221 // ---------------------------------------------------------------------
223 bool pkgSourceList::Read(string File
)
226 return ReadAppend(File
);
229 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
230 // ---------------------------------------------------------------------
232 bool pkgSourceList::ReadAppend(string File
)
234 // Open the stream for reading
235 ifstream
F(File
.c_str(),ios::in
/*| ios::nocreate*/);
237 return _error
->Errno("ifstream::ifstream",_("Opening %s"),File
.c_str());
239 #if 0 // Now Reset() does this.
240 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
242 SrcList
.erase(SrcList
.begin(),SrcList
.end());
244 // CNC:2003-12-10 - 300 is too short.
248 while (F
.eof() == false)
250 F
.getline(Buffer
,sizeof(Buffer
));
252 _strtabexpand(Buffer
,sizeof(Buffer
));
253 if (F
.fail() && !F
.eof())
254 return _error
->Error(_("Line %u too long in source list %s."),
255 CurLine
,File
.c_str());
259 // CNC:2003-02-20 - Do not break if '#' is inside [].
260 for (I
= Buffer
; *I
!= 0 && *I
!= '#'; I
++)
262 for (I
++; *I
!= 0 && *I
!= ']'; I
++);
265 const char *C
= _strstrip(Buffer
);
268 if (C
[0] == '#' || C
[0] == 0)
273 if (ParseQuoteWord(C
,LineType
) == false)
274 return _error
->Error(_("Malformed line %u in source list %s (type)"),CurLine
,File
.c_str());
276 Type
*Parse
= Type::GetType(LineType
.c_str());
278 return _error
->Error(_("Type '%s' is not known on line %u in source list %s"),LineType
.c_str(),CurLine
,File
.c_str());
280 if (Parse
->ParseLine(SrcList
, C
, CurLine
, File
) == false)
286 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
287 // ---------------------------------------------------------------------
289 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File
,
290 pkgIndexFile
*&Found
) const
292 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
294 vector
<pkgIndexFile
*> *Indexes
= (*I
)->GetIndexFiles();
295 for (vector
<pkgIndexFile
*>::const_iterator J
= Indexes
->begin();
296 J
!= Indexes
->end(); J
++)
298 if ((*J
)->FindInCache(*File
.Cache()) == File
)
309 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
310 // ---------------------------------------------------------------------
312 bool pkgSourceList::GetIndexes(pkgAcquire
*Owner
, bool GetAll
) const
314 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
315 if ((*I
)->GetIndexes(Owner
,GetAll
) == false)
320 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
321 // SourceList::ReadSourceDir - Read a directory with sources files
322 // Based on ReadConfigDir() /*{{{*/
323 // ---------------------------------------------------------------------
325 bool pkgSourceList::ReadSourceDir(string Dir
)
327 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "list", true);
330 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
331 if (ReadAppend(*I
) == false)