Serenity Operating System
at hosted 200 lines 6.5 kB view raw
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 27#include "FileUtils.h" 28#include <AK/FileSystemPath.h> 29#include <AK/StringBuilder.h> 30#include <LibCore/DirIterator.h> 31#include <stdio.h> 32#include <stdlib.h> 33#include <sys/stat.h> 34#include <unistd.h> 35 36namespace FileUtils { 37 38int delete_directory(String directory, String& file_that_caused_error) 39{ 40 Core::DirIterator iterator(directory, Core::DirIterator::SkipDots); 41 if (iterator.has_error()) { 42 file_that_caused_error = directory; 43 return -1; 44 } 45 46 while (iterator.has_next()) { 47 auto file_to_delete = String::format("%s/%s", directory.characters(), iterator.next_path().characters()); 48 struct stat st; 49 50 if (lstat(file_to_delete.characters(), &st)) { 51 file_that_caused_error = file_to_delete; 52 return errno; 53 } 54 55 if (S_ISDIR(st.st_mode)) { 56 if (delete_directory(file_to_delete, file_to_delete)) { 57 file_that_caused_error = file_to_delete; 58 return errno; 59 } 60 } else if (unlink(file_to_delete.characters())) { 61 file_that_caused_error = file_to_delete; 62 return errno; 63 } 64 } 65 66 if (rmdir(directory.characters())) { 67 file_that_caused_error = directory; 68 return errno; 69 } 70 71 return 0; 72} 73 74bool copy_file_or_directory(const String& src_path, const String& dst_path) 75{ 76 int duplicate_count = 0; 77 while (access(get_duplicate_name(dst_path, duplicate_count).characters(), F_OK) == 0) { 78 ++duplicate_count; 79 } 80 if (duplicate_count != 0) { 81 return copy_file_or_directory(src_path, get_duplicate_name(dst_path, duplicate_count)); 82 } 83 84 int src_fd = open(src_path.characters(), O_RDONLY); 85 if (src_fd < 0) { 86 return false; 87 } 88 89 struct stat src_stat; 90 int rc = fstat(src_fd, &src_stat); 91 if (rc < 0) { 92 return false; 93 } 94 95 if (S_ISDIR(src_stat.st_mode)) { 96 return copy_directory(src_path, dst_path); 97 } 98 return copy_file(src_path, dst_path, src_stat, src_fd); 99} 100 101bool copy_directory(const String& src_path, const String& dst_path) 102{ 103 int rc = mkdir(dst_path.characters(), 0755); 104 if (rc < 0) { 105 return false; 106 } 107 Core::DirIterator di(src_path, Core::DirIterator::SkipDots); 108 if (di.has_error()) { 109 return false; 110 } 111 while (di.has_next()) { 112 String filename = di.next_path(); 113 bool is_copied = copy_file_or_directory( 114 String::format("%s/%s", src_path.characters(), filename.characters()), 115 String::format("%s/%s", dst_path.characters(), filename.characters())); 116 if (!is_copied) { 117 return false; 118 } 119 } 120 return true; 121} 122 123bool copy_file(const String& src_path, const String& dst_path, const struct stat& src_stat, int src_fd) 124{ 125 int dst_fd = creat(dst_path.characters(), 0666); 126 if (dst_fd < 0) { 127 if (errno != EISDIR) { 128 return false; 129 } 130 auto dst_dir_path = String::format("%s/%s", dst_path.characters(), FileSystemPath(src_path).basename().characters()); 131 dst_fd = creat(dst_dir_path.characters(), 0666); 132 if (dst_fd < 0) { 133 return false; 134 } 135 } 136 137 if (src_stat.st_size > 0) { 138 if (ftruncate(dst_fd, src_stat.st_size) < 0) { 139 perror("cp: ftruncate"); 140 return false; 141 } 142 } 143 144 for (;;) { 145 char buffer[32768]; 146 ssize_t nread = read(src_fd, buffer, sizeof(buffer)); 147 if (nread < 0) { 148 return false; 149 } 150 if (nread == 0) 151 break; 152 ssize_t remaining_to_write = nread; 153 char* bufptr = buffer; 154 while (remaining_to_write) { 155 ssize_t nwritten = write(dst_fd, bufptr, remaining_to_write); 156 if (nwritten < 0) { 157 return false; 158 } 159 assert(nwritten > 0); 160 remaining_to_write -= nwritten; 161 bufptr += nwritten; 162 } 163 } 164 165 auto my_umask = umask(0); 166 umask(my_umask); 167 int rc = fchmod(dst_fd, src_stat.st_mode & ~my_umask); 168 if (rc < 0) { 169 return false; 170 } 171 172 close(src_fd); 173 close(dst_fd); 174 return true; 175} 176 177String get_duplicate_name(const String& path, int duplicate_count) 178{ 179 if (duplicate_count == 0) { 180 return path; 181 } 182 FileSystemPath fsp(path); 183 StringBuilder duplicated_name; 184 duplicated_name.append('/'); 185 for (size_t i = 0; i < fsp.parts().size() - 1; ++i) { 186 duplicated_name.appendf("%s/", fsp.parts()[i].characters()); 187 } 188 auto prev_duplicate_tag = String::format("(%d)", duplicate_count); 189 auto title = fsp.title(); 190 if (title.ends_with(prev_duplicate_tag)) { 191 // remove the previous duplicate tag "(n)" so we can add a new tag. 192 title = title.substring(0, title.length() - prev_duplicate_tag.length()); 193 } 194 duplicated_name.appendf("%s (%d)", fsp.title().characters(), duplicate_count); 195 if (!fsp.extension().is_empty()) { 196 duplicated_name.appendf(".%s", fsp.extension().characters()); 197 } 198 return duplicated_name.build(); 199} 200}