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.
 
 
 
 
 
 

251 lines
8.1 KiB

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