debfile.cc 9.1 KB

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