]>
Commit | Line | Data |
---|---|---|
b2e465d6 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
3 | // $Id: debfile.cc,v 1.2 2001/02/20 07:03:17 jgg Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | Debian Archive File (.deb) | |
7 | ||
8 | .DEB archives are AR files containing two tars and an empty marker | |
9 | member called 'debian-binary'. The two tars contain the meta data and | |
10 | the actual archive contents. Thus this class is a very simple wrapper | |
11 | around ar/tar to simply extract the right tar files. | |
12 | ||
13 | It also uses the deb package list parser to parse the control file | |
14 | into the cache. | |
15 | ||
16 | ##################################################################### */ | |
17 | /*}}}*/ | |
18 | // Include Files /*{{{*/ | |
19 | #ifdef __GNUG__ | |
20 | #pragma implementation "apt-pkg/debfile.h" | |
21 | #endif | |
22 | ||
23 | #include <apt-pkg/debfile.h> | |
24 | #include <apt-pkg/extracttar.h> | |
25 | #include <apt-pkg/error.h> | |
26 | #include <apt-pkg/deblistparser.h> | |
27 | ||
28 | #include <sys/stat.h> | |
29 | #include <unistd.h> | |
30 | /*}}}*/ | |
31 | ||
32 | // DebFile::debDebFile - Constructor /*{{{*/ | |
33 | // --------------------------------------------------------------------- | |
34 | /* Open the AR file and check for consistency */ | |
35 | debDebFile::debDebFile(FileFd &File) : File(File), AR(File) | |
36 | { | |
37 | if (_error->PendingError() == true) | |
38 | return; | |
39 | ||
40 | // Check the members for validity | |
41 | if (CheckMember("debian-binary") == false || | |
42 | CheckMember("control.tar.gz") == false || | |
43 | CheckMember("data.tar.gz") == false) | |
44 | return; | |
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 _error->Error("This is not a valid DEB archive, missing '%s' member",Name); | |
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 | _error->Error("Internal Error, could not locate member %s",Name); | |
72 | return 0; | |
73 | } | |
74 | if (File.Seek(Member->Start) == false) | |
75 | return 0; | |
76 | ||
77 | return Member; | |
78 | } | |
79 | /*}}}*/ | |
80 | // DebFile::ExtractControl - Extract Control information /*{{{*/ | |
81 | // --------------------------------------------------------------------- | |
82 | /* Extract the control information into the Database's temporary | |
83 | directory. */ | |
84 | bool debDebFile::ExtractControl(pkgDataBase &DB) | |
85 | { | |
86 | // Get the archive member and positition the file | |
87 | const ARArchive::Member *Member = GotoMember("control.tar.gz"); | |
88 | if (Member == 0) | |
89 | return false; | |
90 | ||
91 | // Prepare Tar | |
92 | ControlExtract Extract; | |
93 | ExtractTar Tar(File,Member->Size); | |
94 | if (_error->PendingError() == true) | |
95 | return false; | |
96 | ||
97 | // Get into the temporary directory | |
98 | string Cwd = SafeGetCWD(); | |
99 | 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 | ||
105 | // Do extraction | |
106 | if (Tar.Go(Extract) == false) | |
107 | return false; | |
108 | ||
109 | // Switch out of the tmp directory. | |
110 | if (chdir(Cwd.c_str()) != 0) | |
111 | chdir("/"); | |
112 | ||
113 | return true; | |
114 | } | |
115 | /*}}}*/ | |
116 | // DebFile::ExtractArchive - Extract the archive data itself /*{{{*/ | |
117 | // --------------------------------------------------------------------- | |
118 | /* Simple wrapper around tar.. */ | |
119 | bool debDebFile::ExtractArchive(pkgDirStream &Stream) | |
120 | { | |
121 | // Get the archive member and positition the file | |
122 | const ARArchive::Member *Member = AR.FindMember("data.tar.gz"); | |
123 | if (Member == 0) | |
124 | return _error->Error("Internal Error, could not locate member"); | |
125 | if (File.Seek(Member->Start) == false) | |
126 | return false; | |
127 | ||
128 | // Prepare Tar | |
129 | ExtractTar Tar(File,Member->Size); | |
130 | if (_error->PendingError() == true) | |
131 | return false; | |
132 | return Tar.Go(Stream); | |
133 | } | |
134 | /*}}}*/ | |
135 | // DebFile::MergeControl - Merge the control information /*{{{*/ | |
136 | // --------------------------------------------------------------------- | |
137 | /* This reads the extracted control file into the cache and returns the | |
138 | version that was parsed. All this really does is select the correct | |
139 | parser and correct file to parse. */ | |
140 | pkgCache::VerIterator debDebFile::MergeControl(pkgDataBase &DB) | |
141 | { | |
142 | // Open the control file | |
143 | string Tmp; | |
144 | if (DB.GetMetaTmp(Tmp) == false) | |
145 | return pkgCache::VerIterator(DB.GetCache()); | |
146 | FileFd Fd(Tmp + "control",FileFd::ReadOnly); | |
147 | if (_error->PendingError() == true) | |
148 | return pkgCache::VerIterator(DB.GetCache()); | |
149 | ||
150 | // Parse it | |
151 | debListParser Parse(&Fd); | |
152 | pkgCache::VerIterator Ver(DB.GetCache()); | |
153 | if (DB.GetGenerator().MergeList(Parse,&Ver) == false) | |
154 | return pkgCache::VerIterator(DB.GetCache()); | |
155 | ||
156 | if (Ver.end() == true) | |
157 | _error->Error("Failed to locate a valid control file"); | |
158 | return Ver; | |
159 | } | |
160 | /*}}}*/ | |
161 | ||
162 | // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/ | |
163 | // --------------------------------------------------------------------- | |
164 | /* This directory stream handler for the control tar handles extracting | |
165 | it into the temporary meta directory. It only extracts files, it does | |
166 | not create directories, links or anything else. */ | |
167 | bool debDebFile::ControlExtract::DoItem(Item &Itm,int &Fd) | |
168 | { | |
169 | if (Itm.Type != Item::File) | |
170 | return true; | |
171 | ||
172 | /* Cleanse the file name, prevent people from trying to unpack into | |
173 | absolute paths, .., etc */ | |
174 | for (char *I = Itm.Name; *I != 0; I++) | |
175 | if (*I == '/') | |
176 | *I = '_'; | |
177 | ||
178 | /* Force the ownership to be root and ensure correct permissions, | |
179 | go-w, the rest are left untouched */ | |
180 | Itm.UID = 0; | |
181 | Itm.GID = 0; | |
182 | Itm.Mode &= ~(S_IWGRP | S_IWOTH); | |
183 | ||
184 | return pkgDirStream::DoItem(Itm,Fd); | |
185 | } | |
186 | /*}}}*/ | |
187 | ||
188 | // MemControlExtract::DoItem - Check if it is the control file /*{{{*/ | |
189 | // --------------------------------------------------------------------- | |
190 | /* This sets up to extract the control block member file into a memory | |
191 | block of just the right size. All other files go into the bit bucket. */ | |
192 | bool debDebFile::MemControlExtract::DoItem(Item &Itm,int &Fd) | |
193 | { | |
194 | // At the control file, allocate buffer memory. | |
195 | if (Member == Itm.Name) | |
196 | { | |
197 | delete [] Control; | |
198 | Control = new char[Itm.Size+2]; | |
199 | IsControl = true; | |
200 | Fd = -2; // Signal to pass to Process | |
201 | Length = Itm.Size; | |
202 | } | |
203 | else | |
204 | IsControl = false; | |
205 | ||
206 | return true; | |
207 | } | |
208 | /*}}}*/ | |
209 | // MemControlExtract::Process - Process extracting the control file /*{{{*/ | |
210 | // --------------------------------------------------------------------- | |
211 | /* Just memcopy the block from the tar extractor and put it in the right | |
212 | place in the pre-allocated memory block. */ | |
213 | bool debDebFile::MemControlExtract::Process(Item &Itm,const unsigned char *Data, | |
214 | unsigned long Size,unsigned long Pos) | |
215 | { | |
216 | memcpy(Control + Pos, Data,Size); | |
217 | return true; | |
218 | } | |
219 | /*}}}*/ | |
220 | // MemControlExtract::Read - Read the control information from the deb /*{{{*/ | |
221 | // --------------------------------------------------------------------- | |
222 | /* This uses the internal tar extractor to fetch the control file, and then | |
223 | it parses it into a tag section parser. */ | |
224 | bool debDebFile::MemControlExtract::Read(debDebFile &Deb) | |
225 | { | |
226 | // Get the archive member and positition the file | |
227 | const ARArchive::Member *Member = Deb.GotoMember("control.tar.gz"); | |
228 | if (Member == 0) | |
229 | return false; | |
230 | ||
231 | // Extract it. | |
232 | ExtractTar Tar(Deb.GetFile(),Member->Size); | |
233 | if (Tar.Go(*this) == false) | |
234 | return false; | |
235 | ||
236 | if (Control == 0) | |
237 | return true; | |
238 | ||
239 | Control[Length] = '\n'; | |
240 | Control[Length+1] = '\n'; | |
241 | if (Section.Scan(Control,Length+2) == false) | |
242 | return _error->Error("Unparsible control file"); | |
243 | return true; | |
244 | } | |
245 | /*}}}*/ | |
246 | // MemControlExtract::TakeControl - Parse a memory block /*{{{*/ | |
247 | // --------------------------------------------------------------------- | |
248 | /* The given memory block is loaded into the parser and parsed as a control | |
249 | record. */ | |
250 | bool debDebFile::MemControlExtract::TakeControl(const void *Data,unsigned long Size) | |
251 | { | |
252 | delete [] Control; | |
253 | Control = new char[Size+2]; | |
254 | Length = Size; | |
255 | memcpy(Control,Data,Size); | |
256 | ||
257 | Control[Length] = '\n'; | |
258 | Control[Length+1] = '\n'; | |
259 | return Section.Scan(Control,Length+2); | |
260 | } | |
261 | /*}}}*/ | |
262 |