]>
git.saurik.com Git - apt.git/blob - apt-inst/extract.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: extract.cc,v 1.4 2002/03/26 07:38:57 jgg Exp $
4 /* ######################################################################
6 Archive Extraction Directory Stream
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.
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
24 - If the existing link/file is not a directory then it is replaced
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
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
44 ##################################################################### */
46 // Include Files /*{{{*/
48 #pragma implementation "apt-pkg/extract.h"
50 #include <apt-pkg/extract.h>
51 #include <apt-pkg/error.h>
52 #include <apt-pkg/debversion.h>
62 static const char *TempExt
= "dpkg-tmp";
63 //static const char *NewExt = "dpkg-new";
65 // Extract::pkgExtract - Constructor /*{{{*/
66 // ---------------------------------------------------------------------
68 pkgExtract::pkgExtract(pkgFLCache
&FLCache
,pkgCache::VerIterator Ver
) :
69 FLCache(FLCache
), Ver(Ver
)
71 FLPkg
= FLCache
.GetPkg(Ver
.ParentPkg().Name(),true);
72 if (FLPkg
.end() == true)
77 // Extract::DoItem - Handle a single item from the stream /*{{{*/
78 // ---------------------------------------------------------------------
79 /* This performs the setup for the extraction.. */
80 bool pkgExtract::DoItem(Item
&Itm
,int &Fd
)
82 char Temp
[sizeof(FileName
)];
84 /* Strip any leading/trailing /s from the filename, then copy it to the
85 temp buffer and re-apply the leading / We use a class variable
86 to store the new filename for use by the three extraction funcs */
87 char *End
= FileName
+1;
88 const char *I
= Itm
.Name
;
89 for (; *I
!= 0 && *I
== '/'; I
++);
91 for (; *I
!= 0 && End
< FileName
+ sizeof(FileName
); I
++, End
++)
93 if (End
+ 20 >= FileName
+ sizeof(FileName
))
94 return _error
->Error("The path %s is too long",Itm
.Name
);
95 for (; End
> FileName
&& End
[-1] == '/'; End
--);
99 /* Lookup the file. Nde is the file [group] we are going to write to and
100 RealNde is the actual node we are manipulating. Due to diversions
101 they may be entirely different. */
102 pkgFLCache::NodeIterator Nde
= FLCache
.GetNode(Itm
.Name
,End
,0,false,false);
103 pkgFLCache::NodeIterator RealNde
= Nde
;
105 // See if the file is already in the file listing
106 unsigned long FileGroup
= RealNde
->File
;
107 for (; RealNde
.end() == false && FileGroup
== RealNde
->File
; RealNde
++)
108 if (RealNde
.RealPackage() == FLPkg
)
111 // Nope, create an entry
112 if (RealNde
.end() == true)
114 RealNde
= FLCache
.GetNode(Itm
.Name
,End
,FLPkg
.Offset(),true,false);
115 if (RealNde
.end() == true)
117 RealNde
->Flags
|= pkgFLCache::Node::NewFile
;
120 /* Check if this entry already was unpacked. The only time this should
121 ever happen is if someone has hacked tar to support capabilities, in
122 which case this needs to be modified anyhow.. */
123 if ((RealNde
->Flags
& pkgFLCache::Node::Unpacked
) ==
124 pkgFLCache::Node::Unpacked
)
125 return _error
->Error("Unpacking %s more than once",Itm
.Name
);
127 if (Nde
.end() == true)
130 /* Consider a diverted file - We are not permitted to divert directories,
131 but everything else is fair game (including conf files!) */
132 if ((Nde
->Flags
& pkgFLCache::Node::Diversion
) != 0)
134 if (Itm
.Type
== Item::Directory
)
135 return _error
->Error("The directory %s is diverted",Itm
.Name
);
137 /* A package overwriting a diversion target is just the same as
138 overwriting a normally owned file and is checked for below in
139 the overwrites mechanism */
141 /* If this package is trying to overwrite the target of a diversion,
142 that is never, ever permitted */
143 pkgFLCache::DiverIterator Div
= Nde
.Diversion();
144 if (Div
.DivertTo() == Nde
)
145 return _error
->Error("The package is trying to write to the "
146 "diversion target %s/%s",Nde
.DirN(),Nde
.File());
148 // See if it is us and we are following it in the right direction
149 if (Div
->OwnerPkg
!= FLPkg
.Offset() && Div
.DivertFrom() == Nde
)
151 Nde
= Div
.DivertTo();
152 End
= FileName
+ snprintf(FileName
,sizeof(FileName
)-20,"%s/%s",
153 Nde
.DirN(),Nde
.File());
155 return _error
->Error("The diversion path is too long");
159 // Deal with symlinks and conf files
160 if ((RealNde
->Flags
& pkgFLCache::Node::NewConfFile
) ==
161 pkgFLCache::Node::NewConfFile
)
163 string Res
= flNoLink(Itm
.Name
);
164 if (Res
.length() > sizeof(FileName
))
165 return _error
->Error("The path %s is too long",Res
.c_str());
167 clog
<< "Followed conf file from " << FileName
<< " to " << Res
<< endl
;
168 Itm
.Name
= strcpy(FileName
,Res
.c_str());
171 /* Get information about the existing file, and attempt to restore
172 a backup if it does not exist */
173 struct stat LExisting
;
175 if (lstat(Itm
.Name
,&LExisting
) != 0)
179 return _error
->Errno("stat","Failed to stat %s",Itm
.Name
);
181 // See if we can recover the backup file
182 if (Nde
.end() == false)
184 snprintf(Temp
,sizeof(Temp
),"%s.%s",Itm
.Name
,TempExt
);
185 if (rename(Temp
,Itm
.Name
) != 0 && errno
!= ENOENT
)
186 return _error
->Errno("rename","Failed to rename %s to %s",
188 if (stat(Itm
.Name
,&LExisting
) != 0)
191 return _error
->Errno("stat","Failed to stat %s",Itm
.Name
);
200 /* If the file is a link we need to stat its destination, get the
201 existing file modes */
202 struct stat Existing
= LExisting
;
203 if (EValid
== true && S_ISLNK(Existing
.st_mode
))
205 if (stat(Itm
.Name
,&Existing
) != 0)
208 return _error
->Errno("stat","Failed to stat %s",Itm
.Name
);
209 Existing
= LExisting
;
213 // We pretend a non-existing file looks like it is a normal file
215 Existing
.st_mode
= S_IFREG
;
217 /* Okay, at this point 'Existing' is the stat information for the
218 real non-link file */
220 /* The only way this can be a no-op is if a directory is being
221 replaced by a directory or by a link */
222 if (S_ISDIR(Existing
.st_mode
) != 0 &&
223 (Itm
.Type
== Item::Directory
|| Itm
.Type
== Item::SymbolicLink
))
226 /* Non-Directory being replaced by non-directory. We check for over
228 if (Nde
.end() == false)
230 if (HandleOverwrites(Nde
) == false)
234 /* Directory being replaced by a non-directory - this needs to see if
235 the package is the owner and then see if the directory would be
236 empty after the package is removed [ie no user files will be
238 if (S_ISDIR(Existing
.st_mode
) != 0)
240 if (CheckDirReplace(Itm
.Name
) == false)
241 return _error
->Error("The directory %s is being replaced by a non-directory",Itm
.Name
);
245 clog
<< "Extract " << string(Itm
.Name
,End
) << endl
;
247 return _error->Error("Done");*/
252 // Extract::Finished - Sequence finished, erase the temp files /*{{{*/
253 // ---------------------------------------------------------------------
255 bool pkgExtract::Finished()
260 // Extract::Aborted - Sequence aborted, undo all our unpacking /*{{{*/
261 // ---------------------------------------------------------------------
262 /* This undoes everything that was done by all calls to the DoItem method
263 and restores the File Listing cache to its original form. It bases its
264 actions on the flags value for each node in the cache. */
265 bool pkgExtract::Aborted()
268 clog
<< "Aborted, backing out" << endl
;
270 pkgFLCache::NodeIterator Files
= FLPkg
.Files();
271 map_ptrloc
*Last
= &FLPkg
->Files
;
273 /* Loop over all files, restore those that have been unpacked from their
275 while (Files
.end() == false)
277 // Locate the hash bucket for the node and locate its group head
278 pkgFLCache::NodeIterator
Nde(FLCache
,FLCache
.HashNode(Files
));
279 for (; Nde
.end() == false && Files
->File
!= Nde
->File
; Nde
++);
280 if (Nde
.end() == true)
281 return _error
->Error("Failed to locate node in its hash bucket");
283 if (snprintf(FileName
,sizeof(FileName
)-20,"%s/%s",
284 Nde
.DirN(),Nde
.File()) <= 0)
285 return _error
->Error("The path is too long");
287 // Deal with diversions
288 if ((Nde
->Flags
& pkgFLCache::Node::Diversion
) != 0)
290 pkgFLCache::DiverIterator Div
= Nde
.Diversion();
292 // See if it is us and we are following it in the right direction
293 if (Div
->OwnerPkg
!= FLPkg
.Offset() && Div
.DivertFrom() == Nde
)
295 Nde
= Div
.DivertTo();
296 if (snprintf(FileName
,sizeof(FileName
)-20,"%s/%s",
297 Nde
.DirN(),Nde
.File()) <= 0)
298 return _error
->Error("The diversion path is too long");
302 // Deal with overwrites+replaces
303 for (; Nde
.end() == false && Files
->File
== Nde
->File
; Nde
++)
305 if ((Nde
->Flags
& pkgFLCache::Node::Replaced
) ==
306 pkgFLCache::Node::Replaced
)
309 clog
<< "De-replaced " << FileName
<< " from " << Nde
.RealPackage()->Name
<< endl
;
310 Nde
->Flags
&= ~pkgFLCache::Node::Replaced
;
314 // Undo the change in the filesystem
316 clog
<< "Backing out " << FileName
;
319 if ((Files
->Flags
& pkgFLCache::Node::NewFile
) ==
320 pkgFLCache::Node::NewFile
)
323 clog
<< " [new node]" << endl
;
324 pkgFLCache::Node
*Tmp
= Files
;
326 *Last
= Tmp
->NextPkg
;
329 FLCache
.DropNode(Tmp
- FLCache
.NodeP
);
336 Last
= &Files
->NextPkg
;
344 // Extract::Fail - Extraction of a file Failed /*{{{*/
345 // ---------------------------------------------------------------------
347 bool pkgExtract::Fail(Item
&Itm
,int Fd
)
349 return pkgDirStream::Fail(Itm
,Fd
);
352 // Extract::FinishedFile - Finished a file /*{{{*/
353 // ---------------------------------------------------------------------
355 bool pkgExtract::FinishedFile(Item
&Itm
,int Fd
)
357 return pkgDirStream::FinishedFile(Itm
,Fd
);
360 // Extract::HandleOverwrites - See if a replaces covers this overwrite /*{{{*/
361 // ---------------------------------------------------------------------
362 /* Check if the file is in a package that is being replaced by this
363 package or if the file is being overwritten. Note that if the file
364 is really a directory but it has been erased from the filesystem
365 this will fail with an overwrite message. This is a limitation of the
366 dpkg file information format.
368 XX If a new package installs and another package replaces files in this
369 package what should we do? */
370 bool pkgExtract::HandleOverwrites(pkgFLCache::NodeIterator Nde
,
373 pkgFLCache::NodeIterator TmpNde
= Nde
;
374 unsigned long DiverOwner
= 0;
375 unsigned long FileGroup
= Nde
->File
;
376 const char *FirstOwner
= 0;
377 for (; Nde
.end() == false && FileGroup
== Nde
->File
; Nde
++)
379 if ((Nde
->Flags
& pkgFLCache::Node::Diversion
) != 0)
381 /* Store the diversion owner if this is the forward direction
383 if (DiverCheck
== true)
384 DiverOwner
= Nde
.Diversion()->OwnerPkg
;
388 pkgFLCache::PkgIterator
FPkg(FLCache
,Nde
.RealPackage());
389 if (FPkg
.end() == true || FPkg
== FLPkg
)
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
)
396 FirstOwner
= FPkg
.Name();
398 // Now see if this package matches one in a replace depends
399 pkgCache::DepIterator Dep
= Ver
.DependsList();
401 for (; Dep
.end() == false; Dep
++)
403 if (Dep
->Type
!= pkgCache::Dep::Replaces
)
406 // Does the replaces apply to this package?
407 if (strcmp(Dep
.TargetPkg().Name(),FPkg
.Name()) != 0)
410 /* Check the version for match. I do not think CurrentVer can be
411 0 if we are here.. */
412 pkgCache::PkgIterator Pkg
= Dep
.TargetPkg();
413 if (Pkg
->CurrentVer
== 0)
415 _error
->Warning("Overwrite package match with no version for %s",Pkg
.Name());
420 if (debVS
.CheckDep(Pkg
.CurrentVer().VerStr(),Dep
->CompareOp
,Dep
.TargetVer()) == true)
423 clog
<< "Replaced file " << Nde
.DirN() << '/' << Nde
.File() << " from " << Pkg
.Name() << endl
;
424 Nde
->Flags
|= pkgFLCache::Node::Replaced
;
432 return _error
->Error("File %s/%s overwrites the one in the package %s",
433 Nde
.DirN(),Nde
.File(),FPkg
.Name());
436 /* If this is a diversion we might have to recurse to process
437 the other side of it */
438 if ((TmpNde
->Flags
& pkgFLCache::Node::Diversion
) != 0)
440 pkgFLCache::DiverIterator Div
= TmpNde
.Diversion();
441 if (Div
.DivertTo() == TmpNde
)
442 return HandleOverwrites(Div
.DivertFrom(),true);
448 // Extract::CheckDirReplace - See if this directory can be erased /*{{{*/
449 // ---------------------------------------------------------------------
450 /* If this directory is owned by a single package and that package is
451 replacing it with something non-directoryish then dpkg allows this.
452 We increase the requirement to be that the directory is non-empty after
453 the package is removed */
454 bool pkgExtract::CheckDirReplace(string Dir
,unsigned int Depth
)
460 if (Dir
[Dir
.size() - 1] != '/')
463 DIR *D
= opendir(Dir
.c_str());
465 return _error
->Errno("opendir","Unable to read %s",Dir
.c_str());
468 for (struct dirent
*Dent
= readdir(D
); Dent
!= 0; Dent
= readdir(D
))
471 if (strcmp(Dent
->d_name
,".") == 0 ||
472 strcmp(Dent
->d_name
,"..") == 0)
476 File
= Dir
+ Dent
->d_name
;
477 pkgFLCache::NodeIterator Nde
= FLCache
.GetNode(File
.c_str(),
478 File
.c_str() + File
.length(),0,false,false);
480 // The file is not owned by this package
481 if (Nde
.end() != false || Nde
.RealPackage() != FLPkg
)
487 // See if it is a directory
489 if (lstat(File
.c_str(),&St
) != 0)
492 return _error
->Errno("lstat","Unable to stat %s",File
.c_str());
495 // Recurse down directories
496 if (S_ISDIR(St
.st_mode
) != 0)
498 if (CheckDirReplace(File
,Depth
+ 1) == false)