debfile.cc 9.8 KB

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