extract.cc 17 KB

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