pkgcachegen.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcachegen.cc,v 1.10 1998/07/16 06:08:38 jgg Exp $
  4. /* ######################################################################
  5. Package Cache Generator - Generator for the cache structure.
  6. This builds the cache structure from the abstract package list parser.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #ifdef __GNUG__
  11. #pragma implementation "apt-pkg/pkgcachegen.h"
  12. #endif
  13. #include <apt-pkg/pkgcachegen.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/version.h>
  16. #include <strutl.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. /*}}}*/
  20. // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
  21. // ---------------------------------------------------------------------
  22. /* We set the diry flag and make sure that is written to the disk */
  23. pkgCacheGenerator::pkgCacheGenerator(DynamicMMap &Map) : Map(Map), Cache(Map)
  24. {
  25. if (_error->PendingError() == true)
  26. return;
  27. if (Map.Size() == 0)
  28. {
  29. Map.RawAllocate(sizeof(pkgCache::Header));
  30. *Cache.HeaderP = pkgCache::Header();
  31. }
  32. Cache.HeaderP->Dirty = true;
  33. Map.Sync(0,sizeof(pkgCache::Header));
  34. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  35. }
  36. /*}}}*/
  37. // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
  38. // ---------------------------------------------------------------------
  39. /* We sync the data then unset the dirty flag in two steps so as to
  40. advoid a problem during a crash */
  41. pkgCacheGenerator::~pkgCacheGenerator()
  42. {
  43. if (_error->PendingError() == true)
  44. return;
  45. if (Map.Sync() == false)
  46. return;
  47. Cache.HeaderP->Dirty = false;
  48. Map.Sync(0,sizeof(pkgCache::Header));
  49. }
  50. /*}}}*/
  51. // CacheGenerator::MergeList - Merge the package list /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* This provides the generation of the entries in the cache. Each loop
  54. goes through a single package record from the underlying parse engine. */
  55. bool pkgCacheGenerator::MergeList(ListParser &List)
  56. {
  57. List.Owner = this;
  58. while (List.Step() == true)
  59. {
  60. // Get a pointer to the package structure
  61. string PackageName = List.Package();
  62. pkgCache::PkgIterator Pkg;
  63. if (NewPackage(Pkg,PackageName) == false)
  64. return false;
  65. /* Get a pointer to the version structure. We know the list is sorted
  66. so we use that fact in the search. Insertion of new versions is
  67. done with correct sorting */
  68. string Version = List.Version();
  69. if (Version.empty() == true)
  70. {
  71. if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
  72. return false;
  73. continue;
  74. }
  75. pkgCache::VerIterator Ver = Pkg.VersionList();
  76. unsigned long *Last = &Pkg->VersionList;
  77. int Res = 1;
  78. for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
  79. {
  80. Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
  81. Ver.VerStr() + strlen(Ver.VerStr()));
  82. if (Res >= 0)
  83. break;
  84. }
  85. /* We already have a version for this item, record that we
  86. saw it */
  87. if (Res == 0)
  88. {
  89. if (List.UsePackage(Pkg,Ver) == false)
  90. return false;
  91. if (NewFileVer(Ver,List) == false)
  92. return false;
  93. continue;
  94. }
  95. // Add a new version
  96. *Last = NewVersion(Ver,Version,*Last);
  97. Ver->ParentPkg = Pkg.Index();
  98. if (List.NewVersion(Ver) == false)
  99. return false;
  100. if (List.UsePackage(Pkg,Ver) == false)
  101. return false;
  102. if (NewFileVer(Ver,List) == false)
  103. return false;
  104. }
  105. return true;
  106. }
  107. /*}}}*/
  108. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  109. // ---------------------------------------------------------------------
  110. /* This creates a new package structure and adds it to the hash table */
  111. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
  112. {
  113. Pkg = Cache.FindPkg(Name);
  114. if (Pkg.end() == false)
  115. return true;
  116. // Get a structure
  117. unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
  118. if (Package == 0)
  119. return false;
  120. Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
  121. // Insert it into the hash table
  122. unsigned long Hash = Cache.Hash(Name);
  123. Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
  124. Cache.HeaderP->HashTable[Hash] = Package;
  125. // Set the name and the ID
  126. Pkg->Name = Map.WriteString(Name);
  127. if (Pkg->Name == 0)
  128. return false;
  129. Pkg->ID = Cache.HeaderP->PackageCount++;
  130. return true;
  131. }
  132. /*}}}*/
  133. // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
  134. // ---------------------------------------------------------------------
  135. /* */
  136. bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
  137. ListParser &List)
  138. {
  139. // Get a structure
  140. unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
  141. if (VerFile == 0)
  142. return 0;
  143. pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
  144. VF->File = CurrentFile - Cache.PkgFileP;
  145. VF->NextFile = Ver->FileList;
  146. Ver->FileList = VF.Index();
  147. VF->Offset = List.Offset();
  148. VF->Size = List.Size();
  149. return true;
  150. }
  151. /*}}}*/
  152. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  153. // ---------------------------------------------------------------------
  154. /* This puts a version structure in the linked list */
  155. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  156. string VerStr,
  157. unsigned long Next)
  158. {
  159. // Get a structure
  160. unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
  161. if (Version == 0)
  162. return 0;
  163. // Fill it in
  164. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  165. Ver->NextVer = Next;
  166. Ver->ID = Cache.HeaderP->VersionCount++;
  167. Ver->VerStr = Map.WriteString(VerStr);
  168. if (Ver->VerStr == 0)
  169. return 0;
  170. return Version;
  171. }
  172. /*}}}*/
  173. // ListParser::NewDepends - Create a dependency element /*{{{*/
  174. // ---------------------------------------------------------------------
  175. /* This creates a dependency element in the tree. It is linked to the
  176. version and to the package that it is pointing to. */
  177. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
  178. string PackageName,
  179. string Version,
  180. unsigned int Op,
  181. unsigned int Type)
  182. {
  183. pkgCache &Cache = Owner->Cache;
  184. // Get a structure
  185. unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
  186. if (Dependency == 0)
  187. return false;
  188. // Fill it in
  189. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  190. Dep->ParentVer = Ver.Index();
  191. Dep->Type = Type;
  192. Dep->CompareOp = Op;
  193. Dep->ID = Cache.HeaderP->DependsCount++;
  194. // Locate the target package
  195. pkgCache::PkgIterator Pkg;
  196. if (Owner->NewPackage(Pkg,PackageName) == false)
  197. return false;
  198. // Probe the reverse dependency list for a version string that matches
  199. if (Version.empty() == false)
  200. {
  201. for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  202. if (I->Version != 0 && I.TargetVer() == Version)
  203. Dep->Version = I->Version;
  204. if (Dep->Version == 0)
  205. if ((Dep->Version = WriteString(Version)) == 0)
  206. return false;
  207. }
  208. // Link it to the package
  209. Dep->Package = Pkg.Index();
  210. Dep->NextRevDepends = Pkg->RevDepends;
  211. Pkg->RevDepends = Dep.Index();
  212. // Link it to the version (at the end of the list)
  213. unsigned long *Last = &Ver->DependsList;
  214. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  215. Last = &D->NextDepends;
  216. Dep->NextDepends = *Last;
  217. *Last = Dep.Index();
  218. return true;
  219. }
  220. /*}}}*/
  221. // ListParser::NewProvides - Create a Provides element /*{{{*/
  222. // ---------------------------------------------------------------------
  223. /* */
  224. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
  225. string PackageName,
  226. string Version)
  227. {
  228. pkgCache &Cache = Owner->Cache;
  229. // We do not add self referencing provides
  230. if (Ver.ParentPkg().Name() == PackageName)
  231. return true;
  232. // Get a structure
  233. unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
  234. if (Provides == 0)
  235. return false;
  236. // Fill it in
  237. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  238. Prv->Version = Ver.Index();
  239. Prv->NextPkgProv = Ver->ProvidesList;
  240. Ver->ProvidesList = Prv.Index();
  241. if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
  242. return false;
  243. // Locate the target package
  244. pkgCache::PkgIterator Pkg;
  245. if (Owner->NewPackage(Pkg,PackageName) == false)
  246. return false;
  247. // Link it to the package
  248. Prv->ParentPkg = Pkg.Index();
  249. Prv->NextProvides = Pkg->ProvidesList;
  250. Pkg->ProvidesList = Prv.Index();
  251. return true;
  252. }
  253. /*}}}*/
  254. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  255. // ---------------------------------------------------------------------
  256. /* This is used to select which file is to be associated with all newly
  257. added versions. */
  258. bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
  259. {
  260. struct stat Buf;
  261. if (stat(File.c_str(),&Buf) == -1)
  262. return _error->Errno("stat","Couldn't stat ",File.c_str());
  263. // Get some space for the structure
  264. CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
  265. if (CurrentFile == Cache.PkgFileP)
  266. return false;
  267. // Fill it in
  268. CurrentFile->FileName = Map.WriteString(File);
  269. CurrentFile->Size = Buf.st_size;
  270. CurrentFile->mtime = Buf.st_mtime;
  271. CurrentFile->NextFile = Cache.HeaderP->FileList;
  272. CurrentFile->Flags = Flags;
  273. PkgFileName = File;
  274. if (CurrentFile->FileName == 0)
  275. return false;
  276. return true;
  277. }
  278. /*}}}*/
  279. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  280. // ---------------------------------------------------------------------
  281. /* This is used to create handles to strings. Given the same text it
  282. always returns the same number */
  283. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  284. unsigned int Size)
  285. {
  286. // Search for an insertion point
  287. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  288. int Res = 1;
  289. unsigned long *Last = &Cache.HeaderP->StringList;
  290. for (; I != Cache.StringItemP; Last = &I->NextItem,
  291. I = Cache.StringItemP + I->NextItem)
  292. {
  293. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  294. if (Res >= 0)
  295. break;
  296. }
  297. // Match
  298. if (Res == 0)
  299. return I->String;
  300. // Get a structure
  301. unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
  302. if (Item == 0)
  303. return 0;
  304. // Fill in the structure
  305. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  306. ItemP->NextItem = I - Cache.StringItemP;
  307. *Last = Item;
  308. ItemP->String = Map.WriteString(S,Size);
  309. if (ItemP->String == 0)
  310. return 0;
  311. return ItemP->String;
  312. }
  313. /*}}}*/