CopyDirectory.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Martin Mattel <martin.mattel@diemattels.at>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Stefan Weil <sw@weilnetz.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Files\Storage\PolyFill;
  25. trait CopyDirectory {
  26. /**
  27. * Check if a path is a directory
  28. *
  29. * @param string $path
  30. * @return bool
  31. */
  32. abstract public function is_dir($path);
  33. /**
  34. * Check if a file or folder exists
  35. *
  36. * @param string $path
  37. * @return bool
  38. */
  39. abstract public function file_exists($path);
  40. /**
  41. * Delete a file or folder
  42. *
  43. * @param string $path
  44. * @return bool
  45. */
  46. abstract public function unlink($path);
  47. /**
  48. * Open a directory handle for a folder
  49. *
  50. * @param string $path
  51. * @return resource | bool
  52. */
  53. abstract public function opendir($path);
  54. /**
  55. * Create a new folder
  56. *
  57. * @param string $path
  58. * @return bool
  59. */
  60. abstract public function mkdir($path);
  61. public function copy($source, $target) {
  62. if ($this->is_dir($source)) {
  63. if ($this->file_exists($target)) {
  64. $this->unlink($target);
  65. }
  66. $this->mkdir($target);
  67. return $this->copyRecursive($source, $target);
  68. } else {
  69. return parent::copy($source, $target);
  70. }
  71. }
  72. /**
  73. * For adapters that don't support copying folders natively
  74. *
  75. * @param $source
  76. * @param $target
  77. * @return bool
  78. */
  79. protected function copyRecursive($source, $target) {
  80. $dh = $this->opendir($source);
  81. $result = true;
  82. while ($file = readdir($dh)) {
  83. if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
  84. if ($this->is_dir($source . '/' . $file)) {
  85. $this->mkdir($target . '/' . $file);
  86. $result = $this->copyRecursive($source . '/' . $file, $target . '/' . $file);
  87. } else {
  88. $result = parent::copy($source . '/' . $file, $target . '/' . $file);
  89. }
  90. if (!$result) {
  91. break;
  92. }
  93. }
  94. }
  95. return $result;
  96. }
  97. }