indexrecords.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: indexrecords.cc,v 1.1.2.4 2003/12/30 02:11:43 mdz Exp $
  4. /*}}}*/
  5. // Include Files /*{{{*/
  6. #include<config.h>
  7. #include <apt-pkg/indexrecords.h>
  8. #include <apt-pkg/tagfile.h>
  9. #include <apt-pkg/error.h>
  10. #include <apt-pkg/strutl.h>
  11. #include <apt-pkg/configuration.h>
  12. #include <sys/stat.h>
  13. #include <clocale>
  14. #include <apti18n.h>
  15. /*}}}*/
  16. string indexRecords::GetDist() const
  17. {
  18. return this->Dist;
  19. }
  20. bool indexRecords::CheckDist(const string MaybeDist) const
  21. {
  22. return (this->Dist == MaybeDist
  23. || this->Suite == MaybeDist);
  24. }
  25. string indexRecords::GetExpectedDist() const
  26. {
  27. return this->ExpectedDist;
  28. }
  29. time_t indexRecords::GetValidUntil() const
  30. {
  31. return this->ValidUntil;
  32. }
  33. const indexRecords::checkSum *indexRecords::Lookup(const string MetaKey)
  34. {
  35. return Entries[MetaKey];
  36. }
  37. bool indexRecords::Exists(string const &MetaKey) const
  38. {
  39. return Entries.count(MetaKey) == 1;
  40. }
  41. bool indexRecords::Load(const string Filename) /*{{{*/
  42. {
  43. FileFd Fd(Filename, FileFd::ReadOnly);
  44. pkgTagFile TagFile(&Fd, Fd.Size() + 256); // XXX
  45. if (_error->PendingError() == true)
  46. {
  47. strprintf(ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
  48. return false;
  49. }
  50. pkgTagSection Section;
  51. const char *Start, *End;
  52. // Skip over sections beginning with ----- as this is an idicator for clearsigns
  53. do {
  54. if (TagFile.Step(Section) == false)
  55. {
  56. strprintf(ErrorText, _("No sections in Release file %s"), Filename.c_str());
  57. return false;
  58. }
  59. Section.Get (Start, End, 0);
  60. } while (End - Start > 5 && strncmp(Start, "-----", 5) == 0);
  61. Suite = Section.FindS("Suite");
  62. Dist = Section.FindS("Codename");
  63. int i;
  64. for (i=0;HashString::SupportedHashes()[i] != NULL; i++)
  65. {
  66. if (!Section.Find(HashString::SupportedHashes()[i], Start, End))
  67. continue;
  68. string Name;
  69. string Hash;
  70. unsigned long long Size;
  71. while (Start < End)
  72. {
  73. if (!parseSumData(Start, End, Name, Hash, Size))
  74. return false;
  75. indexRecords::checkSum *Sum = new indexRecords::checkSum;
  76. Sum->MetaKeyFilename = Name;
  77. Sum->Hash = HashString(HashString::SupportedHashes()[i],Hash);
  78. Sum->Size = Size;
  79. Entries[Name] = Sum;
  80. }
  81. break;
  82. }
  83. if(HashString::SupportedHashes()[i] == NULL)
  84. {
  85. strprintf(ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
  86. return false;
  87. }
  88. string Label = Section.FindS("Label");
  89. string StrDate = Section.FindS("Date");
  90. string StrValidUntil = Section.FindS("Valid-Until");
  91. // if we have a Valid-Until header in the Release file, use it as default
  92. if (StrValidUntil.empty() == false)
  93. {
  94. if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
  95. {
  96. strprintf(ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
  97. return false;
  98. }
  99. }
  100. // get the user settings for this archive and use what expires earlier
  101. int MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
  102. if (Label.empty() == false)
  103. MaxAge = _config->FindI(string("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
  104. int MinAge = _config->FindI("Acquire::Min-ValidTime", 0);
  105. if (Label.empty() == false)
  106. MinAge = _config->FindI(string("Acquire::Min-ValidTime::" + Label).c_str(), MinAge);
  107. if(MaxAge == 0 &&
  108. (MinAge == 0 || ValidUntil == 0)) // No user settings, use the one from the Release file
  109. return true;
  110. time_t date;
  111. if (RFC1123StrToTime(StrDate.c_str(), date) == false)
  112. {
  113. strprintf(ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
  114. return false;
  115. }
  116. if (MinAge != 0 && ValidUntil != 0) {
  117. time_t const min_date = date + MinAge;
  118. if (ValidUntil < min_date)
  119. ValidUntil = min_date;
  120. }
  121. if (MaxAge != 0) {
  122. time_t const max_date = date + MaxAge;
  123. if (ValidUntil == 0 || ValidUntil > max_date)
  124. ValidUntil = max_date;
  125. }
  126. return true;
  127. }
  128. /*}}}*/
  129. vector<string> indexRecords::MetaKeys() /*{{{*/
  130. {
  131. std::vector<std::string> keys;
  132. std::map<string,checkSum *>::iterator I = Entries.begin();
  133. while(I != Entries.end()) {
  134. keys.push_back((*I).first);
  135. ++I;
  136. }
  137. return keys;
  138. }
  139. /*}}}*/
  140. bool indexRecords::parseSumData(const char *&Start, const char *End, /*{{{*/
  141. string &Name, string &Hash, unsigned long long &Size)
  142. {
  143. Name = "";
  144. Hash = "";
  145. Size = 0;
  146. /* Skip over the first blank */
  147. while ((*Start == '\t' || *Start == ' ' || *Start == '\n')
  148. && Start < End)
  149. Start++;
  150. if (Start >= End)
  151. return false;
  152. /* Move EntryEnd to the end of the first entry (the hash) */
  153. const char *EntryEnd = Start;
  154. while ((*EntryEnd != '\t' && *EntryEnd != ' ')
  155. && EntryEnd < End)
  156. EntryEnd++;
  157. if (EntryEnd == End)
  158. return false;
  159. Hash.append(Start, EntryEnd-Start);
  160. /* Skip over intermediate blanks */
  161. Start = EntryEnd;
  162. while (*Start == '\t' || *Start == ' ')
  163. Start++;
  164. if (Start >= End)
  165. return false;
  166. EntryEnd = Start;
  167. /* Find the end of the second entry (the size) */
  168. while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
  169. && EntryEnd < End)
  170. EntryEnd++;
  171. if (EntryEnd == End)
  172. return false;
  173. Size = strtoull (Start, NULL, 10);
  174. /* Skip over intermediate blanks */
  175. Start = EntryEnd;
  176. while (*Start == '\t' || *Start == ' ')
  177. Start++;
  178. if (Start >= End)
  179. return false;
  180. EntryEnd = Start;
  181. /* Find the end of the third entry (the filename) */
  182. while ((*EntryEnd != '\t' && *EntryEnd != ' ' && *EntryEnd != '\n')
  183. && EntryEnd < End)
  184. EntryEnd++;
  185. Name.append(Start, EntryEnd-Start);
  186. Start = EntryEnd; //prepare for the next round
  187. return true;
  188. }
  189. /*}}}*/
  190. indexRecords::indexRecords()
  191. {
  192. }
  193. indexRecords::indexRecords(const string ExpectedDist) :
  194. ExpectedDist(ExpectedDist), ValidUntil(0)
  195. {
  196. }