indexrecords.cc 5.8 KB

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