// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: apt-cache.cc,v 1.2 1998/07/16 06:08:43 jgg Exp $
+// $Id: apt-cache.cc,v 1.7 1998/09/22 05:30:30 jgg Exp $
/* ######################################################################
apt-cache - Manages the cache file.
#include <apt-pkg/pkgcachegen.h>
#include <apt-pkg/deblistparser.h>
#include <apt-pkg/init.h>
+#include <apt-pkg/progress.h>
+#include <apt-pkg/sourcelist.h>
+#include <apt-pkg/cmndline.h>
#include <iostream.h>
#include <fstream.h>
return _error->Error("Malformed argument %s, must be in file:dist:rev form",Arg);
Ver = string(Start,I - Start);
- return true;
-}
- /*}}}*/
-// DoAdd - Perform an adding operation /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool DoAdd(int argc,char *argv[])
-{
- string FileName;
- string Dist;
- string Ver;
-
- File CacheF(CacheFile,File::WriteEmpty);
- if (_error->PendingError() == true)
- return false;
-
- DynamicMMap Map(CacheF,MMap::Public);
- if (_error->PendingError() == true)
- return false;
-
- pkgCacheGenerator Gen(Map);
- if (_error->PendingError() == true)
- return false;
-
- for (int I = 0; I != argc; I++)
- {
- if (SplitArg(argv[I],FileName,Dist,Ver) == false)
- return false;
- cout << FileName << endl;
-
- // Do the merge
- File TagF(FileName.c_str(),File::ReadOnly);
- debListParser Parser(TagF);
- if (_error->PendingError() == true)
- return _error->Error("Problem opening %s",FileName.c_str());
-
- if (Gen.SelectFile(FileName) == false)
- return _error->Error("Problem with SelectFile");
-
- if (Gen.MergeList(Parser) == false)
- return _error->Error("Problem with MergeList");
- }
-
return true;
}
/*}}}*/
// DumpPackage - Show a dump of a package record /*{{{*/
// ---------------------------------------------------------------------
/* */
-bool DumpPackage(int argc,char *argv[])
-{
- File CacheF(CacheFile,File::ReadOnly);
- if (_error->PendingError() == true)
- return false;
-
- MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
- if (_error->PendingError() == true)
- return false;
-
- pkgCache Cache(Map);
- if (_error->PendingError() == true)
- return false;
-
+bool DumpPackage(pkgCache &Cache,int argc,const char *argv[])
+{
for (int I = 0; I != argc; I++)
{
pkgCache::PkgIterator Pkg = Cache.FindPkg(argv[I]);
// Stats - Dump some nice statistics /*{{{*/
// ---------------------------------------------------------------------
/* */
-bool Stats(const char *FileName)
+bool Stats(pkgCache &Cache)
{
- File CacheF(FileName,File::ReadOnly);
- if (_error->PendingError() == true)
- return false;
-
- MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
- if (_error->PendingError() == true)
- return false;
-
- pkgCache Cache(Map);
- if (_error->PendingError() == true)
- return false;
-
cout << "Total Package Names : " << Cache.Head().PackageCount << endl;
pkgCache::PkgIterator I = Cache.PkgBegin();
// Dump - show everything /*{{{*/
// ---------------------------------------------------------------------
/* */
-bool Dump()
+bool Dump(pkgCache &Cache)
{
- File CacheF(CacheFile,File::ReadOnly);
- if (_error->PendingError() == true)
- return false;
-
- MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
- if (_error->PendingError() == true)
- return false;
-
- pkgCache Cache(Map);
- if (_error->PendingError() == true)
- return false;
-
for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
{
cout << "Package: " << P.Name() << endl;
// DumpAvail - Print out the available list /*{{{*/
// ---------------------------------------------------------------------
/* This is needed to make dpkg --merge happy */
-bool DumpAvail()
+bool DumpAvail(pkgCache &Cache)
{
-#if 0
- pkgCache Cache(CacheFile,true,true);
- if (_error->PendingError() == true)
- return false;
+ unsigned char *Buffer = new unsigned char[Cache.HeaderP->MaxVerFileSize];
- pkgControlCache CCache(Cache);
- if (_error->PendingError() == true)
- return false;
-
- vector<string> Lines;
- Lines.reserve(30);
-
- pkgCache::PkgIterator I = Cache.PkgBegin();
- for (;I.end() != true; I++)
+ for (pkgCache::PkgFileIterator I = Cache.FileBegin(); I.end() == false; I++)
{
- if (I->VersionList == 0)
+ if ((I->Flags & pkgCache::Flag::NotSource) != 0)
continue;
- pkgSPkgCtrlInfo Inf = CCache[I.VersionList()];
- if (Inf.isNull() == true)
- return _error->Error("Couldn't locate info record");
+ if (I.IsOk() == false)
+ {
+ delete [] Buffer;
+ return _error->Error("Package file %s is out of sync.",I.FileName());
+ }
- // Iterate over each element
- pkgPkgCtrlInfo::const_iterator Elm = Inf->begin();
- for (; Elm != Inf->end(); Elm++)
+ FileFd PkgF(I.FileName(),FileFd::ReadOnly);
+ if (_error->PendingError() == true)
{
- // Write the tag: value
- cout << (*Elm)->Tag() << ": " << (*Elm)->Value() << endl;
-
- // Write the multiline
- (*Elm)->GetLines(Lines);
- for (vector<string>::iterator j = Lines.begin(); j != Lines.end(); j++)
+ delete [] Buffer;
+ return false;
+ }
+
+ /* Write all of the records from this package file, we search the entire
+ structure to find them */
+ for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
+ {
+ for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
{
- if ((*j).length() == 0)
- cout << " ." << endl;
- else
- cout << " " << *j << endl;
+ if (V->FileList == 0)
+ continue;
+ if (V.FileList().File() != I)
+ continue;
+
+ // Read the record and then write it out again.
+ if (PkgF.Seek(V.FileList()->Offset) == false ||
+ PkgF.Read(Buffer,V.FileList()->Size) == false ||
+ write(STDOUT_FILENO,Buffer,V.FileList()->Size) != V.FileList()->Size)
+ {
+ delete [] Buffer;
+ return false;
+ }
}
-
- Lines.erase(Lines.begin(),Lines.end());
}
+ }
+
+ return true;
+}
+ /*}}}*/
+// DoAdd - Perform an adding operation /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool DoAdd(int argc,const char *argv[])
+{
+ string FileName;
+ string Dist;
+ string Ver;
+
+ // Open the cache
+ FileFd CacheF(CacheFile,FileFd::WriteEmpty);
+ if (_error->PendingError() == true)
+ return false;
+
+ DynamicMMap Map(CacheF,MMap::Public);
+ if (_error->PendingError() == true)
+ return false;
+
+ OpTextProgress Progress;
+ pkgCacheGenerator Gen(Map,Progress);
+ if (_error->PendingError() == true)
+ return false;
+
+ for (int I = 0; I != argc; I++)
+ {
+ Progress.OverallProgress(I,argc,1,"Generating cache");
+ if (SplitArg(argv[I],FileName,Dist,Ver) == false)
+ return false;
- cout << endl;
+ // Do the merge
+ FileFd TagF(FileName.c_str(),FileFd::ReadOnly);
+ debListParser Parser(TagF);
+ if (_error->PendingError() == true)
+ return _error->Error("Problem opening %s",FileName.c_str());
+
+ if (Gen.SelectFile(FileName) == false)
+ return _error->Error("Problem with SelectFile");
+
+ if (Gen.MergeList(Parser) == false)
+ return _error->Error("Problem with MergeList");
}
-#endif
+
+ Progress.Done();
+ Stats(Gen.GetCache());
+
return true;
}
/*}}}*/
+// GenCaches - Call the main cache generator /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool GenCaches()
+{
+ OpTextProgress Progress;
+ pkgSourceList List;
+ List.ReadMainList();
+ return pkgMakeStatusCache(List,Progress);
+}
+ /*}}}*/
-int main(int argc, char *argv[])
+int main(int argc,const char *argv[])
{
+ CommandLine::Args Args[] = {
+ {'h',"help","help",0},
+ {0,0,0,0}};
+
+ CommandLine Cmds(Args,_config);
+ if (pkgInitialize(*_config) == false ||
+ Cmds.Parse(argc,argv) == false)
+ {
+ _error->DumpErrors();
+ return 100;
+ }
+ cout << _config->Find("help") << endl;
+
// Check arguments.
if (argc < 3)
{
return 100;
}
- pkgInitialize(*_config);
while (1)
{
+ CacheFile = argv[2];
if (strcmp(argv[1],"add") == 0)
{
- CacheFile = argv[2];
- if (DoAdd(argc - 3,argv + 3) == true)
- Stats(argv[2]);
+ DoAdd(argc - 3,argv + 3);
break;
}
+
+ if (strcmp(argv[1],"gencaches") == 0)
+ {
+ GenCaches();
+ break;
+ }
+
+ // Open the cache file
+ FileFd CacheF(CacheFile,FileFd::ReadOnly);
+ if (_error->PendingError() == true)
+ break;
+
+ MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
+ if (_error->PendingError() == true)
+ break;
+
+ pkgCache Cache(Map);
+ if (_error->PendingError() == true)
+ break;
if (strcmp(argv[1],"showpkg") == 0)
{
CacheFile = argv[2];
- DumpPackage(argc - 3,argv + 3);
+ DumpPackage(Cache,argc - 3,argv + 3);
break;
}
if (strcmp(argv[1],"stats") == 0)
{
- Stats(argv[2]);
+ Stats(Cache);
break;
}
if (strcmp(argv[1],"dump") == 0)
{
- CacheFile = argv[2];
- Dump();
+ Dump(Cache);
break;
}
if (strcmp(argv[1],"dumpavail") == 0)
{
- CacheFile = argv[2];
- DumpAvail();
+ DumpAvail(Cache);
break;
}
-
+
_error->Error("Invalid operation %s", argv[1]);
break;
}