indexrecords.cc 5.9 KB

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