debfile.cc 9.8 KB

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