You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

240 lines
6.0 KiB

  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. string indexRecords::GetSuite() const
  25. {
  26. return this->Suite;
  27. }
  28. bool indexRecords::CheckDist(const string MaybeDist) const
  29. {
  30. return (this->Dist == MaybeDist
  31. || this->Suite == MaybeDist);
  32. }
  33. string indexRecords::GetExpectedDist() const
  34. {
  35. return this->ExpectedDist;
  36. }
  37. time_t indexRecords::GetValidUntil() const
  38. {
  39. return this->ValidUntil;
  40. }
  41. const indexRecords::checkSum *indexRecords::Lookup(const string MetaKey)
  42. {
  43. std::map<std::string, indexRecords::checkSum* >::const_iterator sum = Entries.find(MetaKey);
  44. if (sum == Entries.end())
  45. return NULL;
  46. return sum->second;
  47. }
  48. bool indexRecords::Exists(string const &MetaKey) const
  49. {
  50. return Entries.count(MetaKey) == 1;
  51. }
  52. bool indexRecords::Load(const string Filename) /*{{{*/
  53. {
  54. FileFd Fd;
  55. if (OpenMaybeClearSignedFile(Filename, Fd) == false)
  56. return false;
  57. pkgTagFile TagFile(&Fd, Fd.Size());
  58. if (_error->PendingError() == true)
  59. {
  60. strprintf(ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
  61. return false;
  62. }
  63. pkgTagSection Section;
  64. const char *Start, *End;
  65. if (TagFile.Step(Section) == false)
  66. {
  67. strprintf(ErrorText, _("No sections in Release file %s"), Filename.c_str());
  68. return false;
  69. }
  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(("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(("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' || *Start == '\r')
  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 != ' ' &&
  192. *EntryEnd != '\n' && *EntryEnd != '\r')
  193. && EntryEnd < End)
  194. EntryEnd++;
  195. Name.append(Start, EntryEnd-Start);
  196. Start = EntryEnd; //prepare for the next round
  197. return true;
  198. }
  199. /*}}}*/
  200. indexRecords::indexRecords()
  201. {
  202. }
  203. indexRecords::indexRecords(const string ExpectedDist) :
  204. ExpectedDist(ExpectedDist), ValidUntil(0)
  205. {
  206. }