Fs.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Backup\Filesystem\Rollback;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Archive;
  9. use Magento\Framework\Archive\Gz;
  10. use Magento\Framework\Archive\Helper\File;
  11. use Magento\Framework\Archive\Helper\File\Gz as HelperGz;
  12. use Magento\Framework\Archive\Tar;
  13. use Magento\Framework\Backup\Exception\CantLoadSnapshot;
  14. use Magento\Framework\Backup\Exception\NotEnoughPermissions;
  15. use Magento\Framework\Backup\Filesystem\Helper;
  16. use Magento\Framework\Exception\LocalizedException;
  17. use Magento\Framework\Phrase;
  18. /**
  19. * Rollback worker for rolling back via local filesystem
  20. *
  21. * @author Magento Core Team <core@magentocommerce.com>
  22. */
  23. class Fs extends AbstractRollback
  24. {
  25. /**
  26. * @var Helper
  27. */
  28. private $fsHelper;
  29. /**
  30. * Files rollback implementation via local filesystem
  31. *
  32. * @return void
  33. * @throws LocalizedException
  34. *
  35. * @see AbstractRollback::run()
  36. */
  37. public function run()
  38. {
  39. $snapshotPath = $this->_snapshot->getBackupPath();
  40. if (!is_file($snapshotPath) || !is_readable($snapshotPath)) {
  41. throw new CantLoadSnapshot(
  42. new Phrase('Can\'t load snapshot archive')
  43. );
  44. }
  45. $fsHelper = $this->getFsHelper();
  46. $filesInfo = $fsHelper->getInfo(
  47. $this->_snapshot->getRootDir(),
  48. Helper::INFO_WRITABLE,
  49. $this->_snapshot->getIgnorePaths()
  50. );
  51. if (!$filesInfo['writable']) {
  52. if (!empty($filesInfo['writableMeta'])) {
  53. throw new NotEnoughPermissions(
  54. new Phrase(
  55. 'You need write permissions for: %1',
  56. [implode(', ', $filesInfo['writableMeta'])]
  57. )
  58. );
  59. }
  60. throw new NotEnoughPermissions(
  61. new Phrase("The rollback can't be executed because not all files are writable.")
  62. );
  63. }
  64. $archiver = new Archive();
  65. /**
  66. * we need these fake initializations because all magento's files in filesystem will be deleted and autoloader
  67. * won't be able to load classes that we need for unpacking
  68. */
  69. new Tar();
  70. new Gz();
  71. new File('');
  72. new HelperGz('');
  73. new LocalizedException(new Phrase('dummy'));
  74. if (!$this->_snapshot->keepSourceFile()) {
  75. $fsHelper->rm($this->_snapshot->getRootDir(), $this->_snapshot->getIgnorePaths());
  76. }
  77. $archiver->unpack($snapshotPath, $this->_snapshot->getRootDir());
  78. if ($this->_snapshot->keepSourceFile() === false) {
  79. @unlink($snapshotPath);
  80. }
  81. }
  82. /**
  83. * Get file system helper instance
  84. *
  85. * @return Helper
  86. * @deprecated 101.0.0
  87. */
  88. private function getFsHelper()
  89. {
  90. if (!$this->fsHelper) {
  91. $this->fsHelper = ObjectManager::getInstance()->get(Helper::class);
  92. }
  93. return $this->fsHelper;
  94. }
  95. }