indexrecords.cc 6.2 KB

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