indexrecords.cc 6.1 KB

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