indexrecords.cc 6.1 KB

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