debfile.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debfile.cc,v 1.3.2.1 2004/01/16 18:58:50 mdz Exp $
  4. /* ######################################################################
  5. Debian Archive File (.deb)
  6. .DEB archives are AR files containing two tars and an empty marker
  7. member called 'debian-binary'. The two tars contain the meta data and
  8. the actual archive contents. Thus this class is a very simple wrapper
  9. around ar/tar to simply extract the right tar files.
  10. It also uses the deb package list parser to parse the control file
  11. into the cache.
  12. ##################################################################### */
  13. /*}}}*/
  14. // Include Files /*{{{*/
  15. #include<config.h>
  16. #include <apt-pkg/debfile.h>
  17. #include <apt-pkg/extracttar.h>
  18. #include <apt-pkg/error.h>
  19. #include <apt-pkg/aptconfiguration.h>
  20. #include <apt-pkg/arfile.h>
  21. #include <apt-pkg/dirstream.h>
  22. #include <apt-pkg/fileutl.h>
  23. #include <apt-pkg/tagfile.h>
  24. #include <string.h>
  25. #include <string>
  26. #include <vector>
  27. #include <sys/stat.h>
  28. #include <apti18n.h>
  29. /*}}}*/
  30. // DebFile::debDebFile - Constructor /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* Open the AR file and check for consistency */
  33. debDebFile::debDebFile(FileFd &File) : File(File), AR(File)
  34. {
  35. if (_error->PendingError() == true)
  36. return;
  37. if (!CheckMember("debian-binary")) {
  38. _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "debian-binary");
  39. return;
  40. }
  41. if (!CheckMember("control.tar") &&
  42. !CheckMember("control.tar.gz") &&
  43. !CheckMember("control.tar.xz")) {
  44. _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "control.tar");
  45. return;
  46. }
  47. if (!CheckMember("data.tar") &&
  48. !CheckMember("data.tar.gz") &&
  49. !CheckMember("data.tar.bz2") &&
  50. !CheckMember("data.tar.lzma") &&
  51. !CheckMember("data.tar.xz")) {
  52. _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "data.tar");
  53. return;
  54. }
  55. }
  56. /*}}}*/
  57. // DebFile::CheckMember - Check if a named member is in the archive /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* This is used to check for a correct deb and to give nicer error messages
  60. for people playing around. */
  61. bool debDebFile::CheckMember(const char *Name)
  62. {
  63. if (AR.FindMember(Name) == 0)
  64. return false;
  65. return true;
  66. }
  67. /*}}}*/
  68. // DebFile::GotoMember - Jump to a Member /*{{{*/
  69. // ---------------------------------------------------------------------
  70. /* Jump in the file to the start of a named member and return the information
  71. about that member. The caller can then read from the file up to the
  72. returned size. Note, since this relies on the file position this is
  73. a destructive operation, it also changes the last returned Member
  74. structure - so don't nest them! */
  75. const ARArchive::Member *debDebFile::GotoMember(const char *Name)
  76. {
  77. // Get the archive member and positition the file
  78. const ARArchive::Member *Member = AR.FindMember(Name);
  79. if (Member == 0)
  80. {
  81. return 0;
  82. }
  83. if (File.Seek(Member->Start) == false)
  84. return 0;
  85. return Member;
  86. }
  87. /*}}}*/
  88. // DebFile::ExtractTarMember - Extract the contents of a tar member /*{{{*/
  89. // ---------------------------------------------------------------------
  90. /* Simple wrapper around tar.. */
  91. bool debDebFile::ExtractTarMember(pkgDirStream &Stream,const char *Name)
  92. {
  93. // Get the archive member
  94. const ARArchive::Member *Member = NULL;
  95. std::string Compressor;
  96. std::vector<APT::Configuration::Compressor> compressor = APT::Configuration::getCompressors();
  97. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  98. c != compressor.end(); ++c)
  99. {
  100. Member = AR.FindMember(std::string(Name).append(c->Extension).c_str());
  101. if (Member == NULL)
  102. continue;
  103. Compressor = c->Binary;
  104. break;
  105. }
  106. if (Member == NULL)
  107. Member = AR.FindMember(std::string(Name).c_str());
  108. if (Member == NULL)
  109. {
  110. std::string ext = std::string(Name) + ".{";
  111. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  112. c != compressor.end(); ++c) {
  113. if (!c->Extension.empty())
  114. ext.append(c->Extension.substr(1));
  115. }
  116. ext.append("}");
  117. return _error->Error(_("Internal error, could not locate member %s"), ext.c_str());
  118. }
  119. if (File.Seek(Member->Start) == false)
  120. return false;
  121. // Prepare Tar
  122. ExtractTar Tar(File,Member->Size,Compressor);
  123. if (_error->PendingError() == true)
  124. return false;
  125. return Tar.Go(Stream);
  126. }
  127. /*}}}*/
  128. // DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
  129. // ---------------------------------------------------------------------
  130. /* Simple wrapper around DebFile::ExtractTarMember. */
  131. bool debDebFile::ExtractArchive(pkgDirStream &Stream)
  132. {
  133. return ExtractTarMember(Stream, "data.tar");
  134. }
  135. /*}}}*/
  136. // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
  137. // ---------------------------------------------------------------------
  138. /* This directory stream handler for the control tar handles extracting
  139. it into the temporary meta directory. It only extracts files, it does
  140. not create directories, links or anything else. */
  141. bool debDebFile::ControlExtract::DoItem(Item &Itm,int &Fd)
  142. {
  143. if (Itm.Type != Item::File)
  144. return true;
  145. /* Cleanse the file name, prevent people from trying to unpack into
  146. absolute paths, .., etc */
  147. for (char *I = Itm.Name; *I != 0; I++)
  148. if (*I == '/')
  149. *I = '_';
  150. /* Force the ownership to be root and ensure correct permissions,
  151. go-w, the rest are left untouched */
  152. Itm.UID = 0;
  153. Itm.GID = 0;
  154. Itm.Mode &= ~(S_IWGRP | S_IWOTH);
  155. return pkgDirStream::DoItem(Itm,Fd);
  156. }
  157. /*}}}*/
  158. // MemControlExtract::DoItem - Check if it is the control file /*{{{*/
  159. // ---------------------------------------------------------------------
  160. /* This sets up to extract the control block member file into a memory
  161. block of just the right size. All other files go into the bit bucket. */
  162. bool debDebFile::MemControlExtract::DoItem(Item &Itm,int &Fd)
  163. {
  164. // At the control file, allocate buffer memory.
  165. if (Member == Itm.Name)
  166. {
  167. delete [] Control;
  168. Control = new char[Itm.Size+2];
  169. IsControl = true;
  170. Fd = -2; // Signal to pass to Process
  171. Length = Itm.Size;
  172. }
  173. else
  174. IsControl = false;
  175. return true;
  176. }
  177. /*}}}*/
  178. // MemControlExtract::Process - Process extracting the control file /*{{{*/
  179. // ---------------------------------------------------------------------
  180. /* Just memcopy the block from the tar extractor and put it in the right
  181. place in the pre-allocated memory block. */
  182. bool debDebFile::MemControlExtract::Process(Item &/*Itm*/,const unsigned char *Data,
  183. unsigned long long Size,unsigned long long Pos)
  184. {
  185. memcpy(Control + Pos, Data,Size);
  186. return true;
  187. }
  188. /*}}}*/
  189. // MemControlExtract::Read - Read the control information from the deb /*{{{*/
  190. // ---------------------------------------------------------------------
  191. /* This uses the internal tar extractor to fetch the control file, and then
  192. it parses it into a tag section parser. */
  193. bool debDebFile::MemControlExtract::Read(debDebFile &Deb)
  194. {
  195. if (Deb.ExtractTarMember(*this, "control.tar") == false)
  196. return false;
  197. if (Control == 0)
  198. return true;
  199. Control[Length] = '\n';
  200. Control[Length+1] = '\n';
  201. if (Section.Scan(Control,Length+2) == false)
  202. return _error->Error(_("Unparsable control file"));
  203. return true;
  204. }
  205. /*}}}*/
  206. // MemControlExtract::TakeControl - Parse a memory block /*{{{*/
  207. // ---------------------------------------------------------------------
  208. /* The given memory block is loaded into the parser and parsed as a control
  209. record. */
  210. bool debDebFile::MemControlExtract::TakeControl(const void *Data,unsigned long long Size)
  211. {
  212. delete [] Control;
  213. Control = new char[Size+2];
  214. Length = Size;
  215. memcpy(Control,Data,Size);
  216. Control[Length] = '\n';
  217. Control[Length+1] = '\n';
  218. return Section.Scan(Control,Length+2);
  219. }
  220. /*}}}*/