extract.cc 17 KB

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