FileUtils.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "FileUtils.h"
  27. #include <AK/FileSystemPath.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/DirIterator.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <sys/stat.h>
  33. #include <unistd.h>
  34. namespace FileUtils {
  35. int delete_directory(String directory, String& file_that_caused_error)
  36. {
  37. Core::DirIterator iterator(directory, Core::DirIterator::SkipDots);
  38. if (iterator.has_error()) {
  39. file_that_caused_error = directory;
  40. return -1;
  41. }
  42. while (iterator.has_next()) {
  43. auto file_to_delete = String::format("%s/%s", directory.characters(), iterator.next_path().characters());
  44. struct stat st;
  45. if (lstat(file_to_delete.characters(), &st)) {
  46. file_that_caused_error = file_to_delete;
  47. return errno;
  48. }
  49. if (S_ISDIR(st.st_mode)) {
  50. if (delete_directory(file_to_delete, file_to_delete)) {
  51. file_that_caused_error = file_to_delete;
  52. return errno;
  53. }
  54. } else if (unlink(file_to_delete.characters())) {
  55. file_that_caused_error = file_to_delete;
  56. return errno;
  57. }
  58. }
  59. if (rmdir(directory.characters())) {
  60. file_that_caused_error = directory;
  61. return errno;
  62. }
  63. return 0;
  64. }
  65. bool copy_file_or_directory(const String& src_path, const String& dst_path)
  66. {
  67. int duplicate_count = 0;
  68. while (access(get_duplicate_name(dst_path, duplicate_count).characters(), F_OK) == 0) {
  69. ++duplicate_count;
  70. }
  71. if (duplicate_count != 0) {
  72. return copy_file_or_directory(src_path, get_duplicate_name(dst_path, duplicate_count));
  73. }
  74. int src_fd = open(src_path.characters(), O_RDONLY);
  75. if (src_fd < 0) {
  76. return false;
  77. }
  78. struct stat src_stat;
  79. int rc = fstat(src_fd, &src_stat);
  80. if (rc < 0) {
  81. return false;
  82. }
  83. if (S_ISDIR(src_stat.st_mode)) {
  84. return copy_directory(src_path, dst_path);
  85. }
  86. return copy_file(src_path, dst_path, src_stat, src_fd);
  87. }
  88. bool copy_directory(const String& src_path, const String& dst_path)
  89. {
  90. int rc = mkdir(dst_path.characters(), 0755);
  91. if (rc < 0) {
  92. return false;
  93. }
  94. Core::DirIterator di(src_path, Core::DirIterator::SkipDots);
  95. if (di.has_error()) {
  96. return false;
  97. }
  98. while (di.has_next()) {
  99. String filename = di.next_path();
  100. bool is_copied = copy_file_or_directory(
  101. String::format("%s/%s", src_path.characters(), filename.characters()),
  102. String::format("%s/%s", dst_path.characters(), filename.characters()));
  103. if (!is_copied) {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. bool copy_file(const String& src_path, const String& dst_path, const struct stat& src_stat, int src_fd)
  110. {
  111. int dst_fd = creat(dst_path.characters(), 0666);
  112. if (dst_fd < 0) {
  113. if (errno != EISDIR) {
  114. return false;
  115. }
  116. auto dst_dir_path = String::format("%s/%s", dst_path.characters(), FileSystemPath(src_path).basename().characters());
  117. dst_fd = creat(dst_dir_path.characters(), 0666);
  118. if (dst_fd < 0) {
  119. return false;
  120. }
  121. }
  122. if (src_stat.st_size > 0) {
  123. if (ftruncate(dst_fd, src_stat.st_size) < 0) {
  124. perror("cp: ftruncate");
  125. return false;
  126. }
  127. }
  128. for (;;) {
  129. char buffer[32768];
  130. ssize_t nread = read(src_fd, buffer, sizeof(buffer));
  131. if (nread < 0) {
  132. return false;
  133. }
  134. if (nread == 0)
  135. break;
  136. ssize_t remaining_to_write = nread;
  137. char* bufptr = buffer;
  138. while (remaining_to_write) {
  139. ssize_t nwritten = write(dst_fd, bufptr, remaining_to_write);
  140. if (nwritten < 0) {
  141. return false;
  142. }
  143. assert(nwritten > 0);
  144. remaining_to_write -= nwritten;
  145. bufptr += nwritten;
  146. }
  147. }
  148. auto my_umask = umask(0);
  149. umask(my_umask);
  150. int rc = fchmod(dst_fd, src_stat.st_mode & ~my_umask);
  151. if (rc < 0) {
  152. return false;
  153. }
  154. close(src_fd);
  155. close(dst_fd);
  156. return true;
  157. }
  158. String get_duplicate_name(const String& path, int duplicate_count)
  159. {
  160. if (duplicate_count == 0) {
  161. return path;
  162. }
  163. FileSystemPath fsp(path);
  164. StringBuilder duplicated_name;
  165. duplicated_name.append('/');
  166. for (size_t i = 0; i < fsp.parts().size() - 1; ++i) {
  167. duplicated_name.appendf("%s/", fsp.parts()[i].characters());
  168. }
  169. auto prev_duplicate_tag = String::format("(%d)", duplicate_count);
  170. auto title = fsp.title();
  171. if (title.ends_with(prev_duplicate_tag)) {
  172. // remove the previous duplicate tag "(n)" so we can add a new tag.
  173. title = title.substring(0, title.length() - prev_duplicate_tag.length());
  174. }
  175. duplicated_name.appendf("%s (%d)", fsp.title().characters(), duplicate_count);
  176. if (!fsp.extension().is_empty()) {
  177. duplicated_name.appendf(".%s", fsp.extension().characters());
  178. }
  179. return duplicated_name.build();
  180. }
  181. }