debfile.cc 9.4 KB

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