debfile.cc 9.0 KB

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