debfile.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <apt-pkg/debfile.h>
  16. #include <apt-pkg/extracttar.h>
  17. #include <apt-pkg/error.h>
  18. #include <apt-pkg/deblistparser.h>
  19. #include <apt-pkg/aptconfiguration.h>
  20. #include <sys/stat.h>
  21. #include <unistd.h>
  22. #include <apti18n.h>
  23. /*}}}*/
  24. // DebFile::debDebFile - Constructor /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* Open the AR file and check for consistency */
  27. debDebFile::debDebFile(FileFd &File) : File(File), AR(File)
  28. {
  29. if (_error->PendingError() == true)
  30. return;
  31. if (!CheckMember("debian-binary")) {
  32. _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "debian-binary");
  33. return;
  34. }
  35. if (!CheckMember("control.tar.gz")) {
  36. _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "control.tar.gz");
  37. return;
  38. }
  39. if (!CheckMember("data.tar.gz") &&
  40. !CheckMember("data.tar.bz2") &&
  41. !CheckMember("data.tar.lzma") &&
  42. !CheckMember("data.tar.xz")) {
  43. // FIXME: add data.tar.xz here - adding it now would require a Translation round for a very small gain
  44. _error->Error(_("This is not a valid DEB archive, it has no '%s', '%s' or '%s' member"), "data.tar.gz", "data.tar.bz2", "data.tar.lzma");
  45. return;
  46. }
  47. }
  48. /*}}}*/
  49. // DebFile::CheckMember - Check if a named member is in the archive /*{{{*/
  50. // ---------------------------------------------------------------------
  51. /* This is used to check for a correct deb and to give nicer error messages
  52. for people playing around. */
  53. bool debDebFile::CheckMember(const char *Name)
  54. {
  55. if (AR.FindMember(Name) == 0)
  56. return false;
  57. return true;
  58. }
  59. /*}}}*/
  60. // DebFile::GotoMember - Jump to a Member /*{{{*/
  61. // ---------------------------------------------------------------------
  62. /* Jump in the file to the start of a named member and return the information
  63. about that member. The caller can then read from the file up to the
  64. returned size. Note, since this relies on the file position this is
  65. a destructive operation, it also changes the last returned Member
  66. structure - so don't nest them! */
  67. const ARArchive::Member *debDebFile::GotoMember(const char *Name)
  68. {
  69. // Get the archive member and positition the file
  70. const ARArchive::Member *Member = AR.FindMember(Name);
  71. if (Member == 0)
  72. {
  73. return 0;
  74. }
  75. if (File.Seek(Member->Start) == false)
  76. return 0;
  77. return Member;
  78. }
  79. /*}}}*/
  80. // DebFile::ExtractControl - Extract Control information /*{{{*/
  81. // ---------------------------------------------------------------------
  82. /* Extract the control information into the Database's temporary
  83. directory. */
  84. bool debDebFile::ExtractControl(pkgDataBase &DB)
  85. {
  86. // Get the archive member and positition the file
  87. const ARArchive::Member *Member = GotoMember("control.tar.gz");
  88. if (Member == 0)
  89. return false;
  90. // Prepare Tar
  91. ControlExtract Extract;
  92. ExtractTar Tar(File,Member->Size,"gzip");
  93. if (_error->PendingError() == true)
  94. return false;
  95. // Get into the temporary directory
  96. string Cwd = SafeGetCWD();
  97. string Tmp;
  98. if (DB.GetMetaTmp(Tmp) == false)
  99. return false;
  100. if (chdir(Tmp.c_str()) != 0)
  101. return _error->Errno("chdir",_("Couldn't change to %s"),Tmp.c_str());
  102. // Do extraction
  103. if (Tar.Go(Extract) == false)
  104. return false;
  105. // Switch out of the tmp directory.
  106. if (chdir(Cwd.c_str()) != 0)
  107. chdir("/");
  108. return true;
  109. }
  110. /*}}}*/
  111. // DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
  112. // ---------------------------------------------------------------------
  113. /* Simple wrapper around tar.. */
  114. bool debDebFile::ExtractArchive(pkgDirStream &Stream)
  115. {
  116. // Get the archive member
  117. const ARArchive::Member *Member = NULL;
  118. std::string Compressor;
  119. std::string const data = "data.tar";
  120. std::vector<APT::Configuration::Compressor> compressor = APT::Configuration::getCompressors();
  121. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  122. c != compressor.end(); ++c)
  123. {
  124. Member = AR.FindMember(std::string(data).append(c->Extension).c_str());
  125. if (Member == NULL)
  126. continue;
  127. Compressor = c->Binary;
  128. break;
  129. }
  130. if (Member == NULL)
  131. {
  132. std::string ext = "data.tar.{";
  133. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  134. c != compressor.end(); ++c)
  135. ext.append(c->Extension.substr(1));
  136. ext.append("}");
  137. return _error->Error(_("Internal error, could not locate member %s"), ext.c_str());
  138. }
  139. if (File.Seek(Member->Start) == false)
  140. return false;
  141. // Prepare Tar
  142. ExtractTar Tar(File,Member->Size,Compressor);
  143. if (_error->PendingError() == true)
  144. return false;
  145. return Tar.Go(Stream);
  146. }
  147. /*}}}*/
  148. // DebFile::MergeControl - Merge the control information /*{{{*/
  149. // ---------------------------------------------------------------------
  150. /* This reads the extracted control file into the cache and returns the
  151. version that was parsed. All this really does is select the correct
  152. parser and correct file to parse. */
  153. pkgCache::VerIterator debDebFile::MergeControl(pkgDataBase &DB)
  154. {
  155. // Open the control file
  156. string Tmp;
  157. if (DB.GetMetaTmp(Tmp) == false)
  158. return pkgCache::VerIterator(DB.GetCache());
  159. FileFd Fd(Tmp + "control",FileFd::ReadOnly);
  160. if (_error->PendingError() == true)
  161. return pkgCache::VerIterator(DB.GetCache());
  162. // Parse it
  163. debListParser Parse(&Fd);
  164. pkgCache::VerIterator Ver(DB.GetCache());
  165. if (DB.GetGenerator().MergeList(Parse,&Ver) == false)
  166. return pkgCache::VerIterator(DB.GetCache());
  167. if (Ver.end() == true)
  168. _error->Error(_("Failed to locate a valid control file"));
  169. return Ver;
  170. }
  171. /*}}}*/
  172. // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
  173. // ---------------------------------------------------------------------
  174. /* This directory stream handler for the control tar handles extracting
  175. it into the temporary meta directory. It only extracts files, it does
  176. not create directories, links or anything else. */
  177. bool debDebFile::ControlExtract::DoItem(Item &Itm,int &Fd)
  178. {
  179. if (Itm.Type != Item::File)
  180. return true;
  181. /* Cleanse the file name, prevent people from trying to unpack into
  182. absolute paths, .., etc */
  183. for (char *I = Itm.Name; *I != 0; I++)
  184. if (*I == '/')
  185. *I = '_';
  186. /* Force the ownership to be root and ensure correct permissions,
  187. go-w, the rest are left untouched */
  188. Itm.UID = 0;
  189. Itm.GID = 0;
  190. Itm.Mode &= ~(S_IWGRP | S_IWOTH);
  191. return pkgDirStream::DoItem(Itm,Fd);
  192. }
  193. /*}}}*/
  194. // MemControlExtract::DoItem - Check if it is the control file /*{{{*/
  195. // ---------------------------------------------------------------------
  196. /* This sets up to extract the control block member file into a memory
  197. block of just the right size. All other files go into the bit bucket. */
  198. bool debDebFile::MemControlExtract::DoItem(Item &Itm,int &Fd)
  199. {
  200. // At the control file, allocate buffer memory.
  201. if (Member == Itm.Name)
  202. {
  203. delete [] Control;
  204. Control = new char[Itm.Size+2];
  205. IsControl = true;
  206. Fd = -2; // Signal to pass to Process
  207. Length = Itm.Size;
  208. }
  209. else
  210. IsControl = false;
  211. return true;
  212. }
  213. /*}}}*/
  214. // MemControlExtract::Process - Process extracting the control file /*{{{*/
  215. // ---------------------------------------------------------------------
  216. /* Just memcopy the block from the tar extractor and put it in the right
  217. place in the pre-allocated memory block. */
  218. bool debDebFile::MemControlExtract::Process(Item &Itm,const unsigned char *Data,
  219. unsigned long Size,unsigned long Pos)
  220. {
  221. memcpy(Control + Pos, Data,Size);
  222. return true;
  223. }
  224. /*}}}*/
  225. // MemControlExtract::Read - Read the control information from the deb /*{{{*/
  226. // ---------------------------------------------------------------------
  227. /* This uses the internal tar extractor to fetch the control file, and then
  228. it parses it into a tag section parser. */
  229. bool debDebFile::MemControlExtract::Read(debDebFile &Deb)
  230. {
  231. // Get the archive member and positition the file
  232. const ARArchive::Member *Member = Deb.GotoMember("control.tar.gz");
  233. if (Member == 0)
  234. return false;
  235. // Extract it.
  236. ExtractTar Tar(Deb.GetFile(),Member->Size,"gzip");
  237. if (Tar.Go(*this) == false)
  238. return false;
  239. if (Control == 0)
  240. return true;
  241. Control[Length] = '\n';
  242. Control[Length+1] = '\n';
  243. if (Section.Scan(Control,Length+2) == false)
  244. return _error->Error(_("Unparsable control file"));
  245. return true;
  246. }
  247. /*}}}*/
  248. // MemControlExtract::TakeControl - Parse a memory block /*{{{*/
  249. // ---------------------------------------------------------------------
  250. /* The given memory block is loaded into the parser and parsed as a control
  251. record. */
  252. bool debDebFile::MemControlExtract::TakeControl(const void *Data,unsigned long Size)
  253. {
  254. delete [] Control;
  255. Control = new char[Size+2];
  256. Length = Size;
  257. memcpy(Control,Data,Size);
  258. Control[Length] = '\n';
  259. Control[Length+1] = '\n';
  260. return Section.Scan(Control,Length+2);
  261. }
  262. /*}}}*/