pkgcachegen.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcachegen.cc,v 1.7 1998/07/07 04:17:04 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 <sys/stat.h>
  17. #include <unistd.h>
  18. /*}}}*/
  19. // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
  20. // ---------------------------------------------------------------------
  21. /* We set the diry flag and make sure that is written to the disk */
  22. pkgCacheGenerator::pkgCacheGenerator(DynamicMMap &Map) : Map(Map), Cache(Map)
  23. {
  24. if (_error->PendingError() == true)
  25. return;
  26. if (Map.Size() == 0)
  27. {
  28. Map.RawAllocate(sizeof(pkgCache::Header));
  29. *Cache.HeaderP = pkgCache::Header();
  30. }
  31. Cache.HeaderP->Dirty = true;
  32. Map.Sync(0,sizeof(pkgCache::Header));
  33. Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
  34. }
  35. /*}}}*/
  36. // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
  37. // ---------------------------------------------------------------------
  38. /* We sync the data then unset the dirty flag in two steps so as to
  39. advoid a problem during a crash */
  40. pkgCacheGenerator::~pkgCacheGenerator()
  41. {
  42. if (_error->PendingError() == true)
  43. return;
  44. if (Map.Sync() == false)
  45. return;
  46. Cache.HeaderP->Dirty = false;
  47. Map.Sync(0,sizeof(pkgCache::Header));
  48. }
  49. /*}}}*/
  50. // CacheGenerator::MergeList - Merge the package list /*{{{*/
  51. // ---------------------------------------------------------------------
  52. /* This provides the generation of the entries in the cache. Each loop
  53. goes through a single package record from the underlying parse engine. */
  54. bool pkgCacheGenerator::MergeList(ListParser &List)
  55. {
  56. List.Owner = this;
  57. while (List.Step() == true)
  58. {
  59. // Get a pointer to the package structure
  60. string PackageName = List.Package();
  61. pkgCache::PkgIterator Pkg;
  62. Cache.FindPkg(PackageName);
  63. if (Pkg.end() == true)
  64. {
  65. if (NewPackage(Pkg,PackageName) == false)
  66. return false;
  67. }
  68. /* Get a pointer to the version structure. We know the list is sorted
  69. so we use that fact in the search. Insertion of new versions is
  70. done with correct sorting */
  71. string Version = List.Version();
  72. if (Version.empty() == true)
  73. {
  74. if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
  75. return false;
  76. continue;
  77. }
  78. pkgCache::VerIterator Ver = Pkg.VersionList();
  79. unsigned long *Last = &Pkg->VersionList;
  80. int Res = 1;
  81. for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
  82. {
  83. Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
  84. Ver.VerStr() + strlen(Ver.VerStr()));
  85. if (Res >= 0)
  86. break;
  87. }
  88. /* We already have a version for this item, record that we
  89. saw it */
  90. if (Res == 0)
  91. {
  92. if (List.UsePackage(Pkg,Ver) == false)
  93. return false;
  94. if (NewFileVer(Ver,List) == false)
  95. return false;
  96. continue;
  97. }
  98. // Add a new version
  99. *Last = NewVersion(Ver,Version,*Last);
  100. Ver->ParentPkg = Pkg.Index();
  101. if (List.NewVersion(Ver) == false)
  102. return false;
  103. if (List.UsePackage(Pkg,Ver) == false)
  104. return false;
  105. if (NewFileVer(Ver,List) == false)
  106. return false;
  107. }
  108. return true;
  109. }
  110. /*}}}*/
  111. // CacheGenerator::NewPackage - Add a new package /*{{{*/
  112. // ---------------------------------------------------------------------
  113. /* This creates a new package structure and adds it to the hash table */
  114. bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
  115. {
  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 = Cache.FindPkg(PackageName);
  196. if (Pkg.end() == true)
  197. if (Owner->NewPackage(Pkg,PackageName) == false)
  198. return false;
  199. // Probe the reverse dependency list for a version string that matches
  200. if (Version.empty() == false)
  201. {
  202. for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
  203. if (I->Version != 0 && I.TargetVer() == Version)
  204. Dep->Version = I->Version;
  205. if (Dep->Version == 0)
  206. if ((Dep->Version = WriteString(Version)) == 0)
  207. return false;
  208. }
  209. // Link it to the package
  210. Dep->Package = Pkg.Index();
  211. Dep->NextRevDepends = Pkg->RevDepends;
  212. Pkg->RevDepends = Dep.Index();
  213. // Link it to the version (at the end of the list)
  214. unsigned long *Last = &Ver->DependsList;
  215. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  216. Last = &D->NextDepends;
  217. Dep->NextDepends = *Last;
  218. *Last = Dep.Index();
  219. return true;
  220. }
  221. /*}}}*/
  222. // ListParser::NewProvides - Create a Provides element /*{{{*/
  223. // ---------------------------------------------------------------------
  224. /* */
  225. bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
  226. string PackageName,
  227. string Version)
  228. {
  229. pkgCache &Cache = Owner->Cache;
  230. // Get a structure
  231. unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
  232. if (Provides == 0)
  233. return false;
  234. // Fill it in
  235. pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
  236. Prv->Version = Ver.Index();
  237. Prv->NextPkgProv = Ver->ProvidesList;
  238. Ver->ProvidesList = Prv.Index();
  239. if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
  240. return false;
  241. // Locate the target package
  242. pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
  243. if (Pkg.end() == true)
  244. if (Owner->NewPackage(Pkg,PackageName) == false)
  245. return false;
  246. // Link it to the package
  247. Prv->ParentPkg = Pkg.Index();
  248. Prv->NextProvides = Pkg->ProvidesList;
  249. Pkg->ProvidesList = Prv.Index();
  250. return true;
  251. }
  252. /*}}}*/
  253. // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
  254. // ---------------------------------------------------------------------
  255. /* This is used to select which file is to be associated with all newly
  256. added versions. */
  257. bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
  258. {
  259. struct stat Buf;
  260. if (stat(File.c_str(),&Buf) == -1)
  261. return _error->Errno("stat","Couldn't stat ",File.c_str());
  262. // Get some space for the structure
  263. CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
  264. if (CurrentFile == Cache.PkgFileP)
  265. return false;
  266. // Fill it in
  267. CurrentFile->FileName = Map.WriteString(File);
  268. CurrentFile->Size = Buf.st_size;
  269. CurrentFile->mtime = Buf.st_mtime;
  270. CurrentFile->NextFile = Cache.HeaderP->FileList;
  271. CurrentFile->Flags = Flags;
  272. PkgFileName = File;
  273. if (CurrentFile->FileName == 0)
  274. return false;
  275. }
  276. /*}}}*/
  277. // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
  278. // ---------------------------------------------------------------------
  279. /* This is used to create handles to strings. Given the same text it
  280. always returns the same number */
  281. unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
  282. unsigned int Size)
  283. {
  284. // Search for an insertion point
  285. pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
  286. int Res = 1;
  287. unsigned long *Last = &Cache.HeaderP->StringList;
  288. for (; I != Cache.StringItemP; Last = &I->NextItem,
  289. I = Cache.StringItemP + I->NextItem)
  290. {
  291. Res = strncmp(Cache.StrP + I->String,S,Size);
  292. if (Res == 0 && *(Cache.StrP + I->String + Size) != 0)
  293. Res = 1;
  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. /*}}}*/