indexrecords.cc 7.2 KB

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