]> git.saurik.com Git - apt.git/blame_incremental - apt-inst/extract.cc
remove an extra argument for the error mesage format
[apt.git] / apt-inst / extract.cc
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: extract.cc,v 1.6.2.1 2004/01/16 18:58:50 mdz Exp $
4/* ######################################################################
5
6 Archive Extraction Directory Stream
7
8 Extraction for each file is a bit of an involved process. Each object
9 undergoes an atomic backup, overwrite, erase sequence. First the
10 object is unpacked to '.dpkg.new' then the original is hardlinked to
11 '.dpkg.tmp' and finally the new object is renamed to overwrite the old
12 one. From an external perspective the file never ceased to exist.
13 After the archive has been sucessfully unpacked the .dpkg.tmp files
14 are erased. A failure causes all the .dpkg.tmp files to be restored.
15
16 Decisions about unpacking go like this:
17 - Store the original filename in the file listing
18 - Resolve any diversions that would effect this file, all checks
19 below apply to the diverted name, not the real one.
20 - Resolve any symlinked configuration files.
21 - If the existing file does not exist then .dpkg-tmp is checked for.
22 [Note, this is reduced to only check if a file was expected to be
23 there]
24 - If the existing link/file is not a directory then it is replaced
25 irregardless
26 - If the existing link/directory is being replaced by a directory then
27 absolutely nothing happens.
28 - If the existing link/directory is being replaced by a link then
29 absolutely nothing happens.
30 - If the existing link/directory is being replaced by a non-directory
31 then this will abort if the package is not the sole owner of the
32 directory. [Note, this is changed to not happen if the directory
33 non-empty - that is, it only includes files that are part of this
34 package - prevents removing user files accidentally.]
35 - If the non-directory exists in the listing database and it
36 does not belong to the current package then an overwrite condition
37 is invoked.
38
39 As we unpack we record the file list differences in the FL cache. If
40 we need to unroll the the FL cache knows which files have been unpacked
41 and can undo. When we need to erase then it knows which files have not
42 been unpacked.
43
44 ##################################################################### */
45 /*}}}*/
46// Include Files /*{{{*/
47#include<config.h>
48
49#include <apt-pkg/extract.h>
50#include <apt-pkg/error.h>
51#include <apt-pkg/debversion.h>
52
53#include <sys/stat.h>
54#include <stdio.h>
55#include <unistd.h>
56#include <errno.h>
57#include <dirent.h>
58#include <iostream>
59#include <apti18n.h>
60 /*}}}*/
61using namespace std;
62
63static const char *TempExt = "dpkg-tmp";
64//static const char *NewExt = "dpkg-new";
65
66// Extract::pkgExtract - Constructor /*{{{*/
67// ---------------------------------------------------------------------
68/* */
69pkgExtract::pkgExtract(pkgFLCache &FLCache,pkgCache::VerIterator Ver) :
70 FLCache(FLCache), Ver(Ver)
71{
72 FLPkg = FLCache.GetPkg(Ver.ParentPkg().Name(),true);
73 if (FLPkg.end() == true)
74 return;
75 Debug = true;
76}
77 /*}}}*/
78// Extract::DoItem - Handle a single item from the stream /*{{{*/
79// ---------------------------------------------------------------------
80/* This performs the setup for the extraction.. */
81bool pkgExtract::DoItem(Item &Itm,int &Fd)
82{
83 char Temp[sizeof(FileName)];
84
85 /* Strip any leading/trailing /s from the filename, then copy it to the
86 temp buffer and re-apply the leading / We use a class variable
87 to store the new filename for use by the three extraction funcs */
88 char *End = FileName+1;
89 const char *I = Itm.Name;
90 for (; *I != 0 && *I == '/'; I++);
91 *FileName = '/';
92 for (; *I != 0 && End < FileName + sizeof(FileName); I++, End++)
93 *End = *I;
94 if (End + 20 >= FileName + sizeof(FileName))
95 return _error->Error(_("The path %s is too long"),Itm.Name);
96 for (; End > FileName && End[-1] == '/'; End--);
97 *End = 0;
98 Itm.Name = FileName;
99
100 /* Lookup the file. Nde is the file [group] we are going to write to and
101 RealNde is the actual node we are manipulating. Due to diversions
102 they may be entirely different. */
103 pkgFLCache::NodeIterator Nde = FLCache.GetNode(Itm.Name,End,0,false,false);
104 pkgFLCache::NodeIterator RealNde = Nde;
105
106 // See if the file is already in the file listing
107 unsigned long FileGroup = RealNde->File;
108 for (; RealNde.end() == false && FileGroup == RealNde->File; RealNde++)
109 if (RealNde.RealPackage() == FLPkg)
110 break;
111
112 // Nope, create an entry
113 if (RealNde.end() == true)
114 {
115 RealNde = FLCache.GetNode(Itm.Name,End,FLPkg.Offset(),true,false);
116 if (RealNde.end() == true)
117 return false;
118 RealNde->Flags |= pkgFLCache::Node::NewFile;
119 }
120
121 /* Check if this entry already was unpacked. The only time this should
122 ever happen is if someone has hacked tar to support capabilities, in
123 which case this needs to be modified anyhow.. */
124 if ((RealNde->Flags & pkgFLCache::Node::Unpacked) ==
125 pkgFLCache::Node::Unpacked)
126 return _error->Error(_("Unpacking %s more than once"),Itm.Name);
127
128 if (Nde.end() == true)
129 Nde = RealNde;
130
131 /* Consider a diverted file - We are not permitted to divert directories,
132 but everything else is fair game (including conf files!) */
133 if ((Nde->Flags & pkgFLCache::Node::Diversion) != 0)
134 {
135 if (Itm.Type == Item::Directory)
136 return _error->Error(_("The directory %s is diverted"),Itm.Name);
137
138 /* A package overwriting a diversion target is just the same as
139 overwriting a normally owned file and is checked for below in
140 the overwrites mechanism */
141
142 /* If this package is trying to overwrite the target of a diversion,
143 that is never, ever permitted */
144 pkgFLCache::DiverIterator Div = Nde.Diversion();
145 if (Div.DivertTo() == Nde)
146 return _error->Error(_("The package is trying to write to the "
147 "diversion target %s/%s"),Nde.DirN(),Nde.File());
148
149 // See if it is us and we are following it in the right direction
150 if (Div->OwnerPkg != FLPkg.Offset() && Div.DivertFrom() == Nde)
151 {
152 Nde = Div.DivertTo();
153 End = FileName + snprintf(FileName,sizeof(FileName)-20,"%s/%s",
154 Nde.DirN(),Nde.File());
155 if (End <= FileName)
156 return _error->Error(_("The diversion path is too long"));
157 }
158 }
159
160 // Deal with symlinks and conf files
161 if ((RealNde->Flags & pkgFLCache::Node::NewConfFile) ==
162 pkgFLCache::Node::NewConfFile)
163 {
164 string Res = flNoLink(Itm.Name);
165 if (Res.length() > sizeof(FileName))
166 return _error->Error(_("The path %s is too long"),Res.c_str());
167 if (Debug == true)
168 clog << "Followed conf file from " << FileName << " to " << Res << endl;
169 Itm.Name = strcpy(FileName,Res.c_str());
170 }
171
172 /* Get information about the existing file, and attempt to restore
173 a backup if it does not exist */
174 struct stat LExisting;
175 bool EValid = false;
176 if (lstat(Itm.Name,&LExisting) != 0)
177 {
178 // This is bad news.
179 if (errno != ENOENT)
180 return _error->Errno("stat",_("Failed to stat %s"),Itm.Name);
181
182 // See if we can recover the backup file
183 if (Nde.end() == false)
184 {
185 snprintf(Temp,sizeof(Temp),"%s.%s",Itm.Name,TempExt);
186 if (rename(Temp,Itm.Name) != 0 && errno != ENOENT)
187 return _error->Errno("rename",_("Failed to rename %s to %s"),
188 Temp,Itm.Name);
189 if (stat(Itm.Name,&LExisting) != 0)
190 {
191 if (errno != ENOENT)
192 return _error->Errno("stat",_("Failed to stat %s"),Itm.Name);
193 }
194 else
195 EValid = true;
196 }
197 }
198 else
199 EValid = true;
200
201 /* If the file is a link we need to stat its destination, get the
202 existing file modes */
203 struct stat Existing = LExisting;
204 if (EValid == true && S_ISLNK(Existing.st_mode))
205 {
206 if (stat(Itm.Name,&Existing) != 0)
207 {
208 if (errno != ENOENT)
209 return _error->Errno("stat",_("Failed to stat %s"),Itm.Name);
210 Existing = LExisting;
211 }
212 }
213
214 // We pretend a non-existing file looks like it is a normal file
215 if (EValid == false)
216 Existing.st_mode = S_IFREG;
217
218 /* Okay, at this point 'Existing' is the stat information for the
219 real non-link file */
220
221 /* The only way this can be a no-op is if a directory is being
222 replaced by a directory or by a link */
223 if (S_ISDIR(Existing.st_mode) != 0 &&
224 (Itm.Type == Item::Directory || Itm.Type == Item::SymbolicLink))
225 return true;
226
227 /* Non-Directory being replaced by non-directory. We check for over
228 writes here. */
229 if (Nde.end() == false)
230 {
231 if (HandleOverwrites(Nde) == false)
232 return false;
233 }
234
235 /* Directory being replaced by a non-directory - this needs to see if
236 the package is the owner and then see if the directory would be
237 empty after the package is removed [ie no user files will be
238 erased] */
239 if (S_ISDIR(Existing.st_mode) != 0)
240 {
241 if (CheckDirReplace(Itm.Name) == false)
242 return _error->Error(_("The directory %s is being replaced by a non-directory"),Itm.Name);
243 }
244
245 if (Debug == true)
246 clog << "Extract " << string(Itm.Name,End) << endl;
247/* if (Count != 0)
248 return _error->Error(_("Done"));*/
249
250 return true;
251}
252 /*}}}*/
253// Extract::Finished - Sequence finished, erase the temp files /*{{{*/
254// ---------------------------------------------------------------------
255/* */
256bool pkgExtract::Finished()
257{
258 return true;
259}
260 /*}}}*/
261// Extract::Aborted - Sequence aborted, undo all our unpacking /*{{{*/
262// ---------------------------------------------------------------------
263/* This undoes everything that was done by all calls to the DoItem method
264 and restores the File Listing cache to its original form. It bases its
265 actions on the flags value for each node in the cache. */
266bool pkgExtract::Aborted()
267{
268 if (Debug == true)
269 clog << "Aborted, backing out" << endl;
270
271 pkgFLCache::NodeIterator Files = FLPkg.Files();
272 map_ptrloc *Last = &FLPkg->Files;
273
274 /* Loop over all files, restore those that have been unpacked from their
275 dpkg-tmp entires */
276 while (Files.end() == false)
277 {
278 // Locate the hash bucket for the node and locate its group head
279 pkgFLCache::NodeIterator Nde(FLCache,FLCache.HashNode(Files));
280 for (; Nde.end() == false && Files->File != Nde->File; Nde++);
281 if (Nde.end() == true)
282 return _error->Error(_("Failed to locate node in its hash bucket"));
283
284 if (snprintf(FileName,sizeof(FileName)-20,"%s/%s",
285 Nde.DirN(),Nde.File()) <= 0)
286 return _error->Error(_("The path is too long"));
287
288 // Deal with diversions
289 if ((Nde->Flags & pkgFLCache::Node::Diversion) != 0)
290 {
291 pkgFLCache::DiverIterator Div = Nde.Diversion();
292
293 // See if it is us and we are following it in the right direction
294 if (Div->OwnerPkg != FLPkg.Offset() && Div.DivertFrom() == Nde)
295 {
296 Nde = Div.DivertTo();
297 if (snprintf(FileName,sizeof(FileName)-20,"%s/%s",
298 Nde.DirN(),Nde.File()) <= 0)
299 return _error->Error(_("The diversion path is too long"));
300 }
301 }
302
303 // Deal with overwrites+replaces
304 for (; Nde.end() == false && Files->File == Nde->File; Nde++)
305 {
306 if ((Nde->Flags & pkgFLCache::Node::Replaced) ==
307 pkgFLCache::Node::Replaced)
308 {
309 if (Debug == true)
310 clog << "De-replaced " << FileName << " from " << Nde.RealPackage()->Name << endl;
311 Nde->Flags &= ~pkgFLCache::Node::Replaced;
312 }
313 }
314
315 // Undo the change in the filesystem
316 if (Debug == true)
317 clog << "Backing out " << FileName;
318
319 // Remove a new node
320 if ((Files->Flags & pkgFLCache::Node::NewFile) ==
321 pkgFLCache::Node::NewFile)
322 {
323 if (Debug == true)
324 clog << " [new node]" << endl;
325 pkgFLCache::Node *Tmp = Files;
326 Files++;
327 *Last = Tmp->NextPkg;
328 Tmp->NextPkg = 0;
329
330 FLCache.DropNode(Tmp - FLCache.NodeP);
331 }
332 else
333 {
334 if (Debug == true)
335 clog << endl;
336
337 Last = &Files->NextPkg;
338 Files++;
339 }
340 }
341
342 return true;
343}
344 /*}}}*/
345// Extract::Fail - Extraction of a file Failed /*{{{*/
346// ---------------------------------------------------------------------
347/* */
348bool pkgExtract::Fail(Item &Itm,int Fd)
349{
350 return pkgDirStream::Fail(Itm,Fd);
351}
352 /*}}}*/
353// Extract::FinishedFile - Finished a file /*{{{*/
354// ---------------------------------------------------------------------
355/* */
356bool pkgExtract::FinishedFile(Item &Itm,int Fd)
357{
358 return pkgDirStream::FinishedFile(Itm,Fd);
359}
360 /*}}}*/
361// Extract::HandleOverwrites - See if a replaces covers this overwrite /*{{{*/
362// ---------------------------------------------------------------------
363/* Check if the file is in a package that is being replaced by this
364 package or if the file is being overwritten. Note that if the file
365 is really a directory but it has been erased from the filesystem
366 this will fail with an overwrite message. This is a limitation of the
367 dpkg file information format.
368
369 XX If a new package installs and another package replaces files in this
370 package what should we do? */
371bool pkgExtract::HandleOverwrites(pkgFLCache::NodeIterator Nde,
372 bool DiverCheck)
373{
374 pkgFLCache::NodeIterator TmpNde = Nde;
375 unsigned long DiverOwner = 0;
376 unsigned long FileGroup = Nde->File;
377 for (; Nde.end() == false && FileGroup == Nde->File; Nde++)
378 {
379 if ((Nde->Flags & pkgFLCache::Node::Diversion) != 0)
380 {
381 /* Store the diversion owner if this is the forward direction
382 of the diversion */
383 if (DiverCheck == true)
384 DiverOwner = Nde.Diversion()->OwnerPkg;
385 continue;
386 }
387
388 pkgFLCache::PkgIterator FPkg(FLCache,Nde.RealPackage());
389 if (FPkg.end() == true || FPkg == FLPkg)
390 continue;
391
392 /* This tests trips when we are checking a diversion to see
393 if something has already been diverted by this diversion */
394 if (FPkg.Offset() == DiverOwner)
395 continue;
396
397 // Now see if this package matches one in a replace depends
398 pkgCache::DepIterator Dep = Ver.DependsList();
399 bool Ok = false;
400 for (; Dep.end() == false; Dep++)
401 {
402 if (Dep->Type != pkgCache::Dep::Replaces)
403 continue;
404
405 // Does the replaces apply to this package?
406 if (strcmp(Dep.TargetPkg().Name(),FPkg.Name()) != 0)
407 continue;
408
409 /* Check the version for match. I do not think CurrentVer can be
410 0 if we are here.. */
411 pkgCache::PkgIterator Pkg = Dep.TargetPkg();
412 if (Pkg->CurrentVer == 0)
413 {
414 _error->Warning(_("Overwrite package match with no version for %s"),Pkg.Name());
415 continue;
416 }
417
418 // Replaces is met
419 if (debVS.CheckDep(Pkg.CurrentVer().VerStr(),Dep->CompareOp,Dep.TargetVer()) == true)
420 {
421 if (Debug == true)
422 clog << "Replaced file " << Nde.DirN() << '/' << Nde.File() << " from " << Pkg.Name() << endl;
423 Nde->Flags |= pkgFLCache::Node::Replaced;
424 Ok = true;
425 break;
426 }
427 }
428
429 // Negative Hit
430 if (Ok == false)
431 return _error->Error(_("File %s/%s overwrites the one in the package %s"),
432 Nde.DirN(),Nde.File(),FPkg.Name());
433 }
434
435 /* If this is a diversion we might have to recurse to process
436 the other side of it */
437 if ((TmpNde->Flags & pkgFLCache::Node::Diversion) != 0)
438 {
439 pkgFLCache::DiverIterator Div = TmpNde.Diversion();
440 if (Div.DivertTo() == TmpNde)
441 return HandleOverwrites(Div.DivertFrom(),true);
442 }
443
444 return true;
445}
446 /*}}}*/
447// Extract::CheckDirReplace - See if this directory can be erased /*{{{*/
448// ---------------------------------------------------------------------
449/* If this directory is owned by a single package and that package is
450 replacing it with something non-directoryish then dpkg allows this.
451 We increase the requirement to be that the directory is non-empty after
452 the package is removed */
453bool pkgExtract::CheckDirReplace(string Dir,unsigned int Depth)
454{
455 // Looping?
456 if (Depth > 40)
457 return false;
458
459 if (Dir[Dir.size() - 1] != '/')
460 Dir += '/';
461
462 DIR *D = opendir(Dir.c_str());
463 if (D == 0)
464 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
465
466 string File;
467 for (struct dirent *Dent = readdir(D); Dent != 0; Dent = readdir(D))
468 {
469 // Skip some files
470 if (strcmp(Dent->d_name,".") == 0 ||
471 strcmp(Dent->d_name,"..") == 0)
472 continue;
473
474 // Look up the node
475 File = Dir + Dent->d_name;
476 pkgFLCache::NodeIterator Nde = FLCache.GetNode(File.c_str(),
477 File.c_str() + File.length(),0,false,false);
478
479 // The file is not owned by this package
480 if (Nde.end() != false || Nde.RealPackage() != FLPkg)
481 {
482 closedir(D);
483 return false;
484 }
485
486 // See if it is a directory
487 struct stat St;
488 if (lstat(File.c_str(),&St) != 0)
489 {
490 closedir(D);
491 return _error->Errno("lstat",_("Unable to stat %s"),File.c_str());
492 }
493
494 // Recurse down directories
495 if (S_ISDIR(St.st_mode) != 0)
496 {
497 if (CheckDirReplace(File,Depth + 1) == false)
498 {
499 closedir(D);
500 return false;
501 }
502 }
503 }
504
505 // No conflicts
506 closedir(D);
507 return true;
508}
509 /*}}}*/