pkgcachegen.cc 11 KB

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