cp.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <AK/LexicalPath.h>
  27. #include <AK/String.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/ArgsParser.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <assert.h>
  32. #include <fcntl.h>
  33. #include <stdio.h>
  34. #include <sys/stat.h>
  35. #include <unistd.h>
  36. bool copy_file_or_directory(String, String, bool);
  37. bool copy_file(String, String, struct stat, int);
  38. bool copy_directory(String, String);
  39. int main(int argc, char** argv)
  40. {
  41. if (pledge("stdio rpath wpath cpath fattr", nullptr) < 0) {
  42. perror("pledge");
  43. return 1;
  44. }
  45. bool recursion_allowed = false;
  46. Vector<const char*> sources;
  47. const char* destination = nullptr;
  48. Core::ArgsParser args_parser;
  49. args_parser.add_option(recursion_allowed, "Copy directories recursively", "recursive", 'r');
  50. args_parser.add_positional_argument(sources, "Source file path", "source");
  51. args_parser.add_positional_argument(destination, "Destination file path", "destination");
  52. args_parser.parse(argc, argv);
  53. for (auto& source : sources) {
  54. bool ok = copy_file_or_directory(source, destination, recursion_allowed);
  55. if (!ok)
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. /**
  61. * Copy a file or directory to a new location. Returns true if successful, false
  62. * otherwise. If there is an error, its description is output to stderr.
  63. *
  64. * Directories should only be copied if recursion_allowed is set.
  65. */
  66. bool copy_file_or_directory(String src_path, String dst_path, bool recursion_allowed)
  67. {
  68. int src_fd = open(src_path.characters(), O_RDONLY);
  69. if (src_fd < 0) {
  70. perror("open src");
  71. return false;
  72. }
  73. struct stat src_stat;
  74. int rc = fstat(src_fd, &src_stat);
  75. if (rc < 0) {
  76. perror("stat src");
  77. return false;
  78. }
  79. if (S_ISDIR(src_stat.st_mode)) {
  80. if (!recursion_allowed) {
  81. fprintf(stderr, "cp: -r not specified; omitting directory '%s'\n", src_path.characters());
  82. return false;
  83. }
  84. return copy_directory(src_path, dst_path);
  85. }
  86. return copy_file(src_path, dst_path, src_stat, src_fd);
  87. }
  88. /**
  89. * Copy a source file to a destination file. Returns true if successful, false
  90. * otherwise. If there is an error, its description is output to stderr.
  91. *
  92. * To avoid repeated work, the source file's stat and file descriptor are required.
  93. */
  94. bool copy_file(String src_path, String dst_path, struct stat src_stat, int src_fd)
  95. {
  96. int dst_fd = creat(dst_path.characters(), 0666);
  97. if (dst_fd < 0) {
  98. if (errno != EISDIR) {
  99. perror("open dst");
  100. return false;
  101. }
  102. StringBuilder builder;
  103. builder.append(dst_path);
  104. builder.append('/');
  105. builder.append(LexicalPath(src_path).basename());
  106. dst_path = builder.to_string();
  107. dst_fd = creat(dst_path.characters(), 0666);
  108. if (dst_fd < 0) {
  109. perror("open dst");
  110. return false;
  111. }
  112. }
  113. if (src_stat.st_size > 0) {
  114. if (ftruncate(dst_fd, src_stat.st_size) < 0) {
  115. perror("cp: ftruncate");
  116. return false;
  117. }
  118. }
  119. for (;;) {
  120. char buffer[32768];
  121. ssize_t nread = read(src_fd, buffer, sizeof(buffer));
  122. if (nread < 0) {
  123. perror("read src");
  124. return false;
  125. }
  126. if (nread == 0)
  127. break;
  128. ssize_t remaining_to_write = nread;
  129. char* bufptr = buffer;
  130. while (remaining_to_write) {
  131. ssize_t nwritten = write(dst_fd, bufptr, remaining_to_write);
  132. if (nwritten < 0) {
  133. perror("write dst");
  134. return false;
  135. }
  136. assert(nwritten > 0);
  137. remaining_to_write -= nwritten;
  138. bufptr += nwritten;
  139. }
  140. }
  141. auto my_umask = umask(0);
  142. umask(my_umask);
  143. int rc = fchmod(dst_fd, src_stat.st_mode & ~my_umask);
  144. if (rc < 0) {
  145. perror("fchmod dst");
  146. return false;
  147. }
  148. close(src_fd);
  149. close(dst_fd);
  150. return true;
  151. }
  152. /**
  153. * Copy the contents of a source directory into a destination directory.
  154. */
  155. bool copy_directory(String src_path, String dst_path)
  156. {
  157. int rc = mkdir(dst_path.characters(), 0755);
  158. if (rc < 0) {
  159. perror("cp: mkdir");
  160. return false;
  161. }
  162. Core::DirIterator di(src_path, Core::DirIterator::SkipDots);
  163. if (di.has_error()) {
  164. fprintf(stderr, "cp: DirIterator: %s\n", di.error_string());
  165. return false;
  166. }
  167. while (di.has_next()) {
  168. String filename = di.next_path();
  169. bool is_copied = copy_file_or_directory(
  170. String::format("%s/%s", src_path.characters(), filename.characters()),
  171. String::format("%s/%s", dst_path.characters(), filename.characters()),
  172. true);
  173. if (!is_copied) {
  174. return false;
  175. }
  176. }
  177. return true;
  178. }