pkgcachegen.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcachegen.cc,v 1.8 1998/07/09 05:12:27 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 "pkglib/pkgcachegen.h"
  12. #endif
  13. #include <pkglib/pkgcachegen.h>
  14. #include <pkglib/error.h>
  15. #include <pkglib/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. Cache.FindPkg(PackageName);
  64. if (Pkg.end() == true)
  65. {
  66. if (NewPackage(Pkg,PackageName) == false)
  67. return false;
  68. }
  69. /* Get a pointer to the version structure. We know the list is sorted
  70. so we use that fact in the search. Insertion of new versions is
  71. done with correct sorting */
  72. string Version = List.Version();
  73. if (Version.empty() == true)
  74. {
  75. if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
  76. return false;
  77. continue;
  78. }
  79. pkgCache::VerIterator Ver = Pkg.VersionList();
  80. unsigned long *Last = &Pkg->VersionList;
  81. int Res = 1;
  82. for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
  83. {
  84. Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
  85. Ver.VerStr() + strlen(Ver.VerStr()));
  86. if (Res >= 0)
  87. break;
  88. }
  89. /* We already have a version for this item, record that we
  90. saw it */
  91. if (Res == 0)
  92. {
  93. if (List.UsePackage(Pkg,Ver) == false)
  94. return false;
  95. if (NewFileVer(Ver,List) == false)
  96. return false;
  97. continue;
  98. }
  99. // Add a new version
  100. *Last = NewVersion(Ver,Version,*Last);
  101. Ver->ParentPkg = Pkg.Index();
  102. if (List.NewVersion(Ver) == false)
  103. return false;
  104. if (List.UsePackage(Pkg,Ver) == false)
  105. return false;
  106. if (NewFileVer(Ver,List) == false)
  107. return false;
  108. }
  109. return true;
  110. }
  111. /*}}}*/
  112. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* This creates a new package structure and adds it to the hash table */
  115. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
  116. {
  117. // Get a structure
  118. unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
  119. if (Package == 0)
  120. return false;
  121. Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
  122. // Insert it into the hash table
  123. unsigned long Hash = Cache.Hash(Name);
  124. Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
  125. Cache.HeaderP->HashTable[Hash] = Package;
  126. // Set the name and the ID
  127. Pkg->Name = Map.WriteString(Name);
  128. if (Pkg->Name == 0)
  129. return false;
  130. Pkg->ID = Cache.HeaderP->PackageCount++;
  131. return true;
  132. }
  133. /*}}}*/
  134. // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
  135. // ---------------------------------------------------------------------
  136. /* */
  137. bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
  138. ListParser &List)
  139. {
  140. // Get a structure
  141. unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
  142. if (VerFile == 0)
  143. return 0;
  144. pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
  145. VF->File = CurrentFile - Cache.PkgFileP;
  146. VF->NextFile = Ver->FileList;
  147. Ver->FileList = VF.Index();
  148. VF->Offset = List.Offset();
  149. VF->Size = List.Size();
  150. return true;
  151. }
  152. /*}}}*/
  153. // CacheGenerator::NewVersion - Create a new Version /*{{{*/
  154. // ---------------------------------------------------------------------
  155. /* This puts a version structure in the linked list */
  156. unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
  157. string VerStr,
  158. unsigned long Next)
  159. {
  160. // Get a structure
  161. unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
  162. if (Version == 0)
  163. return 0;
  164. // Fill it in
  165. Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
  166. Ver->NextVer = Next;
  167. Ver->ID = Cache.HeaderP->VersionCount++;
  168. Ver->VerStr = Map.WriteString(VerStr);
  169. if (Ver->VerStr == 0)
  170. return 0;
  171. return Version;
  172. }
  173. /*}}}*/
  174. // ListParser::NewDepends - Create a dependency element /*{{{*/
  175. // ---------------------------------------------------------------------
  176. /* This creates a dependency element in the tree. It is linked to the
  177. version and to the package that it is pointing to. */
  178. bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
  179. string PackageName,
  180. string Version,
  181. unsigned int Op,
  182. unsigned int Type)
  183. {
  184. pkgCache &Cache = Owner->Cache;
  185. // Get a structure
  186. unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
  187. if (Dependency == 0)
  188. return false;
  189. // Fill it in
  190. pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
  191. Dep->ParentVer = Ver.Index();
  192. Dep->Type = Type;
  193. Dep->CompareOp = Op;
  194. Dep->ID = Cache.HeaderP->DependsCount++;
  195. // Locate the target package
  196. pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
  197. if (Pkg.end() == true)
  198. if (Owner->NewPackage(Pkg,PackageName) == false)
  199. return false;
  200. // Probe the reverse dependency list for a version string that matches
  201. if (Version.empty() == false)
  202. {
  203. for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  204. if (I->Version != 0 && I.TargetVer() == Version)
  205. Dep->Version = I->Version;
  206. if (Dep->Version == 0)
  207. if ((Dep->Version = WriteString(Version)) == 0)
  208. return false;
  209. }
  210. // Link it to the package
  211. Dep->Package = Pkg.Index();
  212. Dep->NextRevDepends = Pkg->RevDepends;
  213. Pkg->RevDepends = Dep.Index();
  214. // Link it to the version (at the end of the list)
  215. unsigned long *Last = &Ver->DependsList;
  216. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  217. Last = &D->NextDepends;
  218. Dep->NextDepends = *Last;
  219. *Last = Dep.Index();
  220. return true;
  221. }
  222. /*}}}*/
  223. // ListParser::NewProvides - Create a Provides element /*{{{*/
  224. // ---------------------------------------------------------------------
  225. /* */
  226. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
  227. string PackageName,
  228. string Version)
  229. {
  230. pkgCache &Cache = Owner->Cache;
  231. // Get a structure
  232. unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
  233. if (Provides == 0)
  234. return false;
  235. // Fill it in
  236. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  237. Prv->Version = Ver.Index();
  238. Prv->NextPkgProv = Ver->ProvidesList;
  239. Ver->ProvidesList = Prv.Index();
  240. if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
  241. return false;
  242. // Locate the target package
  243. pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
  244. if (Pkg.end() == true)
  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. }
  277. /*}}}*/
  278. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  279. // ---------------------------------------------------------------------
  280. /* This is used to create handles to strings. Given the same text it
  281. always returns the same number */
  282. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  283. unsigned int Size)
  284. {
  285. // Search for an insertion point
  286. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  287. int Res = 1;
  288. unsigned long *Last = &Cache.HeaderP->StringList;
  289. for (; I != Cache.StringItemP; Last = &I->NextItem,
  290. I = Cache.StringItemP + I->NextItem)
  291. {
  292. Res = stringcmp(S,S+Size,Cache.StrP + I->String);
  293. if (Res >= 0)
  294. break;
  295. }
  296. // Match
  297. if (Res == 0)
  298. return I->String;
  299. // Get a structure
  300. unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
  301. if (Item == 0)
  302. return 0;
  303. // Fill in the structure
  304. pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
  305. ItemP->NextItem = I - Cache.StringItemP;
  306. *Last = Item;
  307. ItemP->String = Map.WriteString(S,Size);
  308. if (ItemP->String == 0)
  309. return 0;
  310. return ItemP->String;
  311. }
  312. /*}}}*/