indexrecords.cc 6.0 KB

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