]>
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>
21 // CNC:2003-03-03 - This is needed for ReadDir stuff.
31 // Global list of Items supported
32 static pkgSourceList::Type
*ItmList
[10];
33 pkgSourceList::Type
**pkgSourceList::Type::GlobalList
= ItmList
;
34 unsigned long pkgSourceList::Type::GlobalListLen
= 0;
36 // Type::Type - Constructor /*{{{*/
37 // ---------------------------------------------------------------------
38 /* Link this to the global list of items*/
39 pkgSourceList::Type::Type()
41 ItmList
[GlobalListLen
] = this;
45 // Type::GetType - Get a specific meta for a given type /*{{{*/
46 // ---------------------------------------------------------------------
48 pkgSourceList::Type
*pkgSourceList::Type::GetType(const char *Type
)
50 for (unsigned I
= 0; I
!= GlobalListLen
; I
++)
51 if (strcmp(GlobalList
[I
]->Name
,Type
) == 0)
56 // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
57 // ---------------------------------------------------------------------
59 bool pkgSourceList::Type::FixupURI(string
&URI
) const
61 if (URI
.empty() == true)
64 if (URI
.find(':') == string::npos
)
67 URI
= SubstVar(URI
,"$(ARCH)",_config
->Find("APT::Architecture"));
69 // Make sure that the URI is / postfixed
70 if (URI
[URI
.size() - 1] != '/')
76 // Type::ParseLine - Parse a single line /*{{{*/
77 // ---------------------------------------------------------------------
78 /* This is a generic one that is the 'usual' format for sources.list
79 Weird types may override this. */
80 bool pkgSourceList::Type::ParseLine(vector
<metaIndex
*> &List
,
82 unsigned long CurLine
,
89 if (ParseQuoteWord(Buffer
,URI
) == false)
90 return _error
->Error(_("Malformed line %lu in source list %s (URI)"),CurLine
,File
.c_str());
91 if (ParseQuoteWord(Buffer
,Dist
) == false)
92 return _error
->Error(_("Malformed line %lu in source list %s (dist)"),CurLine
,File
.c_str());
94 if (FixupURI(URI
) == false)
95 return _error
->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine
,File
.c_str());
97 // Check for an absolute dists specification.
98 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] == '/')
100 if (ParseQuoteWord(Buffer
,Section
) == true)
101 return _error
->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine
,File
.c_str());
102 Dist
= SubstVar(Dist
,"$(ARCH)",_config
->Find("APT::Architecture"));
103 return CreateItem(List
,URI
,Dist
,Section
);
106 // Grab the rest of the dists
107 if (ParseQuoteWord(Buffer
,Section
) == false)
108 return _error
->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine
,File
.c_str());
112 if (CreateItem(List
,URI
,Dist
,Section
) == false)
115 while (ParseQuoteWord(Buffer
,Section
) == true);
121 // SourceList::pkgSourceList - Constructors /*{{{*/
122 // ---------------------------------------------------------------------
124 pkgSourceList::pkgSourceList()
128 pkgSourceList::pkgSourceList(string File
)
133 // SourceList::~pkgSourceList - Destructor /*{{{*/
134 // ---------------------------------------------------------------------
136 pkgSourceList::~pkgSourceList()
138 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
143 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
144 // ---------------------------------------------------------------------
146 bool pkgSourceList::ReadMainList()
148 // CNC:2003-03-03 - Multiple sources list support.
157 // CNC:2003-11-28 - Entries in sources.list have priority over
158 // entries in sources.list.d.
159 string Main
= _config
->FindFile("Dir::Etc::sourcelist");
160 string Parts
= _config
->FindDir("Dir::Etc::sourceparts");
162 if (FileExists(Main
) == true)
163 Res
&= ReadAppend(Main
);
164 else if (FileExists(Parts
) == false)
165 // Only warn if there are no sources.list.d.
166 _error
->WarningE("FileExists",_("Unable to read %s"),Main
.c_str());
168 if (FileExists(Parts
) == true)
169 Res
&= ReadSourceDir(Parts
);
170 else if (FileExists(Main
) == false)
171 // Only warn if there is no sources.list file.
172 _error
->WarningE("FileExists",_("Unable to read %s"),Parts
.c_str());
177 // CNC:2003-03-03 - Needed to preserve backwards compatibility.
178 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
179 // ---------------------------------------------------------------------
181 void pkgSourceList::Reset()
183 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
185 SrcList
.erase(SrcList
.begin(),SrcList
.end());
188 // CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
189 // SourceList::Read - Parse the sourcelist file /*{{{*/
190 // ---------------------------------------------------------------------
192 bool pkgSourceList::Read(string File
)
195 return ReadAppend(File
);
198 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
199 // ---------------------------------------------------------------------
201 bool pkgSourceList::ReadAppend(string File
)
203 // Open the stream for reading
204 ifstream
F(File
.c_str(),ios::in
/*| ios::nocreate*/);
206 return _error
->Errno("ifstream::ifstream",_("Opening %s"),File
.c_str());
208 #if 0 // Now Reset() does this.
209 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
211 SrcList
.erase(SrcList
.begin(),SrcList
.end());
213 // CNC:2003-12-10 - 300 is too short.
217 while (F
.eof() == false)
219 F
.getline(Buffer
,sizeof(Buffer
));
221 _strtabexpand(Buffer
,sizeof(Buffer
));
222 if (F
.fail() && !F
.eof())
223 return _error
->Error(_("Line %u too long in source list %s."),
224 CurLine
,File
.c_str());
228 // CNC:2003-02-20 - Do not break if '#' is inside [].
229 for (I
= Buffer
; *I
!= 0 && *I
!= '#'; I
++)
231 for (I
++; *I
!= 0 && *I
!= ']'; I
++);
234 const char *C
= _strstrip(Buffer
);
237 if (C
[0] == '#' || C
[0] == 0)
242 if (ParseQuoteWord(C
,LineType
) == false)
243 return _error
->Error(_("Malformed line %u in source list %s (type)"),CurLine
,File
.c_str());
245 Type
*Parse
= Type::GetType(LineType
.c_str());
247 return _error
->Error(_("Type '%s' is not known on line %u in source list %s"),LineType
.c_str(),CurLine
,File
.c_str());
249 // Vendor name specified
254 if (ParseQuoteWord(C
,VendorID
) == false)
255 return _error
->Error(_("Malformed line %u in source list %s (vendor id)"),CurLine
,File
.c_str());
257 if (VendorID
.length() < 2 || VendorID
.end()[-1] != ']')
258 return _error
->Error(_("Malformed line %u in source list %s (vendor id)"),CurLine
,File
.c_str());
259 VendorID
= string(VendorID
,1,VendorID
.size()-2);
261 // for (vector<const Vendor *>::const_iterator iter = VendorList.begin();
262 // iter != VendorList.end(); iter++)
264 // if ((*iter)->GetVendorID() == VendorID)
266 // if (_config->FindB("Debug::sourceList", false))
267 // std::cerr << "Comparing VendorID \"" << VendorID << "\" with \"" << (*iter)->GetVendorID() << '"' << std::endl;
273 // if (Verifier == 0)
274 // return _error->Error(_("Unknown vendor ID '%s' in line %u of source list %s"),
275 // VendorID.c_str(),CurLine,File.c_str());
278 if (Parse
->ParseLine(SrcList
,C
,CurLine
,File
) == false)
284 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
285 // ---------------------------------------------------------------------
287 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File
,
288 pkgIndexFile
*&Found
) const
290 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
292 vector
<pkgIndexFile
*> *Indexes
= (*I
)->GetIndexFiles();
293 for (vector
<pkgIndexFile
*>::const_iterator J
= Indexes
->begin();
294 J
!= Indexes
->end(); J
++)
296 if ((*J
)->FindInCache(*File
.Cache()) == File
)
307 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
308 // ---------------------------------------------------------------------
310 bool pkgSourceList::GetIndexes(pkgAcquire
*Owner
, bool GetAll
) const
312 for (const_iterator I
= SrcList
.begin(); I
!= SrcList
.end(); I
++)
313 if ((*I
)->GetIndexes(Owner
,GetAll
) == false)
318 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
319 // SourceList::ReadSourceDir - Read a directory with sources files
320 // Based on ReadConfigDir() /*{{{*/
321 // ---------------------------------------------------------------------
323 bool pkgSourceList::ReadSourceDir(string Dir
)
325 DIR *D
= opendir(Dir
.c_str());
327 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
331 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
333 if (Ent
->d_name
[0] == '.')
336 // CNC:2003-12-02 Only accept .list files as valid sourceparts
337 if (flExtension(Ent
->d_name
) != "list")
340 // Skip bad file names ala run-parts
341 const char *C
= Ent
->d_name
;
343 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
344 && *C
!= '_' && *C
!= '-' && *C
!= '.')
349 // Make sure it is a file and not something else
350 string File
= flCombine(Dir
,Ent
->d_name
);
352 if (stat(File
.c_str(),&St
) != 0 || S_ISREG(St
.st_mode
) == 0)
355 List
.push_back(File
);
359 sort(List
.begin(),List
.end());
362 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
363 if (ReadAppend(*I
) == false)