debfile.cc 8.7 KB

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