Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 4 | * This program is free software: you can redistribute it and/or modify |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 5 | * it under the terms of the GNU General Public License as published by |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 6 | * the Free Software Foundation, either version 3 of the License, or |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 16 | */ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 17 | #include "fileutils.h" |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 18 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 19 | #include <opendht/crypto.h> |
| 20 | |
| 21 | #ifdef RING_UWP |
| 22 | #include <io.h> // for access and close |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | #ifdef __APPLE__ |
| 26 | #include <TargetConditionals.h> |
| 27 | #endif |
| 28 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 29 | #ifdef _WIN32 |
| 30 | #include <windows.h> |
| 31 | #include "string_utils.h" |
| 32 | #endif |
| 33 | |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 36 | #ifndef _MSC_VER |
| 37 | #include <libgen.h> |
| 38 | #endif |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 39 | #include <fcntl.h> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 40 | #include <signal.h> |
| 41 | #include <unistd.h> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 42 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 43 | #include <sstream> |
| 44 | #include <fstream> |
| 45 | #include <iostream> |
| 46 | #include <stdexcept> |
| 47 | #include <limits> |
| 48 | #include <array> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 49 | #include <filesystem> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 50 | |
| 51 | #include <cstdlib> |
| 52 | #include <cstring> |
| 53 | #include <cerrno> |
| 54 | #include <cstddef> |
| 55 | #include <ciso646> |
| 56 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 57 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 58 | #define ERASE_BLOCK 4096 |
| 59 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 60 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 61 | namespace fileutils { |
| 62 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 63 | // returns true if directory exists or was created |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 64 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 65 | check_dir(const std::filesystem::path& path, mode_t dirmode, mode_t parentmode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 66 | { |
Adrien Béraud | 6a529fb | 2023-09-14 14:34:23 -0400 | [diff] [blame] | 67 | fmt::print("check_dir: {}\n", path.string()); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 68 | if (std::filesystem::exists(path)) |
| 69 | return true; |
| 70 | if (path.has_parent_path()) |
| 71 | check_dir(path.parent_path(), parentmode, parentmode); |
Adrien Béraud | 6a529fb | 2023-09-14 14:34:23 -0400 | [diff] [blame] | 72 | std::error_code ec; |
| 73 | if (std::filesystem::create_directory(path, ec)) { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 74 | std::filesystem::permissions(path, (std::filesystem::perms)dirmode); |
| 75 | return true; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 76 | } |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 77 | return false; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | std::mutex& |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 81 | getFileLock(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 82 | { |
| 83 | static std::mutex fileLockLock {}; |
| 84 | static std::map<std::string, std::mutex> fileLocks {}; |
| 85 | |
| 86 | std::lock_guard<std::mutex> l(fileLockLock); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 87 | return fileLocks[path.string()]; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 91 | isFile(const std::filesystem::path& path, bool resolveSymlink) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 92 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 93 | auto status = resolveSymlink ? std::filesystem::status(path) : std::filesystem::symlink_status(path); |
| 94 | return std::filesystem::is_regular_file(status); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 98 | isDirectory(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 99 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 100 | return std::filesystem::is_directory(path); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 104 | hasHardLink(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 105 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 106 | return std::filesystem::hard_link_count(path) > 1; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 110 | isSymLink(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 111 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 112 | return std::filesystem::is_symlink(path); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 115 | template <typename TP> |
| 116 | std::chrono::system_clock::time_point to_sysclock(TP tp) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 117 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 118 | using namespace std::chrono; |
| 119 | return time_point_cast<system_clock::duration>(tp - TP::clock::now() + system_clock::now()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | bool |
| 123 | createSymlink(const std::string& linkFile, const std::string& target) |
| 124 | { |
| 125 | try { |
| 126 | std::filesystem::create_symlink(target, linkFile); |
| 127 | } catch (const std::exception& e) { |
| 128 | //JAMI_ERR("Couldn't create soft link: %s", e.what()); |
| 129 | return false; |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | bool |
| 135 | createHardlink(const std::string& linkFile, const std::string& target) |
| 136 | { |
| 137 | try { |
| 138 | std::filesystem::create_hard_link(target, linkFile); |
| 139 | } catch (const std::exception& e) { |
| 140 | //JAMI_ERR("Couldn't create hard link: %s", e.what()); |
| 141 | return false; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | createFileLink(const std::string& linkFile, const std::string& target, bool hard) |
| 148 | { |
| 149 | if (not hard or not createHardlink(linkFile, target)) |
| 150 | createSymlink(linkFile, target); |
| 151 | } |
| 152 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 153 | std::vector<uint8_t> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 154 | loadFile(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 155 | { |
| 156 | std::vector<uint8_t> buffer; |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 157 | std::ifstream file = ifstream(path, std::ios::binary); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 158 | if (!file) |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 159 | throw std::runtime_error("Can't read file: " + path.string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 160 | file.seekg(0, std::ios::end); |
| 161 | auto size = file.tellg(); |
| 162 | if (size > std::numeric_limits<unsigned>::max()) |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 163 | throw std::runtime_error("File is too big: " + path.string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 164 | buffer.resize(size); |
| 165 | file.seekg(0, std::ios::beg); |
| 166 | if (!file.read((char*) buffer.data(), size)) |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 167 | throw std::runtime_error("Can't load file: " + path.string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 168 | return buffer; |
| 169 | } |
| 170 | |
| 171 | void |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 172 | saveFile(const std::filesystem::path& path, const uint8_t* data, size_t data_size, mode_t mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 173 | { |
| 174 | std::ofstream file = fileutils::ofstream(path, std::ios::trunc | std::ios::binary); |
| 175 | if (!file.is_open()) { |
| 176 | //JAMI_ERR("Could not write data to %s", path.c_str()); |
| 177 | return; |
| 178 | } |
| 179 | file.write((char*) data, data_size); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 180 | file.close(); |
| 181 | std::filesystem::permissions(path, (std::filesystem::perms)mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | std::vector<std::string> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 185 | readDirectory(const std::filesystem::path& dir) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 186 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 187 | std::vector<std::string> files; |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 188 | std::error_code ec; |
| 189 | for (const auto& entry : std::filesystem::directory_iterator(dir, ec)) { |
Adrien Béraud | 8294048 | 2023-08-27 13:03:20 -0400 | [diff] [blame] | 190 | files.emplace_back(entry.path().filename().string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 191 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 192 | return files; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 193 | } |
| 194 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 195 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 196 | recursive_mkdir(const std::filesystem::path& path, mode_t mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 197 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 198 | std::error_code ec; |
| 199 | std::filesystem::create_directories(path, ec); |
| 200 | if (!ec) |
| 201 | std::filesystem::permissions(path, (std::filesystem::perms)mode, ec); |
| 202 | return !ec; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | #ifdef _WIN32 |
| 206 | bool |
| 207 | eraseFile_win32(const std::string& path, bool dosync) |
| 208 | { |
| 209 | HANDLE h |
| 210 | = CreateFileA(path.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
| 211 | if (h == INVALID_HANDLE_VALUE) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 212 | // JAMI_WARN("Can not open file %s for erasing.", path.c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
| 216 | LARGE_INTEGER size; |
| 217 | if (!GetFileSizeEx(h, &size)) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 218 | // JAMI_WARN("Can not erase file %s: GetFileSizeEx() failed.", path.c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 219 | CloseHandle(h); |
| 220 | return false; |
| 221 | } |
| 222 | if (size.QuadPart == 0) { |
| 223 | CloseHandle(h); |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | uint64_t size_blocks = size.QuadPart / ERASE_BLOCK; |
| 228 | if (size.QuadPart % ERASE_BLOCK) |
| 229 | size_blocks++; |
| 230 | |
| 231 | char* buffer; |
| 232 | try { |
| 233 | buffer = new char[ERASE_BLOCK]; |
| 234 | } catch (std::bad_alloc& ba) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 235 | // JAMI_WARN("Can not allocate buffer for erasing %s.", path.c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 236 | CloseHandle(h); |
| 237 | return false; |
| 238 | } |
| 239 | memset(buffer, 0x00, ERASE_BLOCK); |
| 240 | |
| 241 | OVERLAPPED ovlp; |
| 242 | if (size.QuadPart < (1024 - 42)) { // a small file can be stored in the MFT record |
| 243 | ovlp.Offset = 0; |
| 244 | ovlp.OffsetHigh = 0; |
| 245 | WriteFile(h, buffer, (DWORD) size.QuadPart, 0, &ovlp); |
| 246 | FlushFileBuffers(h); |
| 247 | } |
| 248 | for (uint64_t i = 0; i < size_blocks; i++) { |
| 249 | uint64_t offset = i * ERASE_BLOCK; |
| 250 | ovlp.Offset = offset & 0x00000000FFFFFFFF; |
| 251 | ovlp.OffsetHigh = offset >> 32; |
| 252 | WriteFile(h, buffer, ERASE_BLOCK, 0, &ovlp); |
| 253 | } |
| 254 | |
| 255 | delete[] buffer; |
| 256 | |
| 257 | if (dosync) |
| 258 | FlushFileBuffers(h); |
| 259 | |
| 260 | CloseHandle(h); |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | #else |
| 265 | |
| 266 | bool |
| 267 | eraseFile_posix(const std::string& path, bool dosync) |
| 268 | { |
| 269 | struct stat st; |
| 270 | if (stat(path.c_str(), &st) == -1) { |
| 271 | //JAMI_WARN("Can not erase file %s: fstat() failed.", path.c_str()); |
| 272 | return false; |
| 273 | } |
| 274 | // Remove read-only flag if possible |
| 275 | chmod(path.c_str(), st.st_mode | (S_IWGRP+S_IWUSR) ); |
| 276 | |
| 277 | int fd = open(path.c_str(), O_WRONLY); |
| 278 | if (fd == -1) { |
| 279 | //JAMI_WARN("Can not open file %s for erasing.", path.c_str()); |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | if (st.st_size == 0) { |
| 284 | close(fd); |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | lseek(fd, 0, SEEK_SET); |
| 289 | |
| 290 | std::array<char, ERASE_BLOCK> buffer; |
| 291 | buffer.fill(0); |
| 292 | decltype(st.st_size) written(0); |
| 293 | while (written < st.st_size) { |
| 294 | auto ret = write(fd, buffer.data(), buffer.size()); |
| 295 | if (ret < 0) { |
| 296 | //JAMI_WARNING("Error while overriding file with zeros."); |
| 297 | break; |
| 298 | } else |
| 299 | written += ret; |
| 300 | } |
| 301 | |
| 302 | if (dosync) |
| 303 | fsync(fd); |
| 304 | |
| 305 | close(fd); |
| 306 | return written >= st.st_size; |
| 307 | } |
| 308 | #endif |
| 309 | |
| 310 | bool |
| 311 | eraseFile(const std::string& path, bool dosync) |
| 312 | { |
| 313 | #ifdef _WIN32 |
| 314 | return eraseFile_win32(path, dosync); |
| 315 | #else |
| 316 | return eraseFile_posix(path, dosync); |
| 317 | #endif |
| 318 | } |
| 319 | |
| 320 | int |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 321 | remove(const std::filesystem::path& path, bool erase) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 322 | { |
| 323 | if (erase and isFile(path, false) and !hasHardLink(path)) |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 324 | eraseFile(path.string(), true); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 325 | |
| 326 | #ifdef _WIN32 |
| 327 | // use Win32 api since std::remove will not unlink directory in use |
| 328 | if (isDirectory(path)) |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 329 | return !RemoveDirectory(dhtnet::to_wstring(path.string()).c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 330 | #endif |
| 331 | |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 332 | return std::remove(path.string().c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | int |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 336 | removeAll(const std::filesystem::path& path, bool erase) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 337 | { |
| 338 | if (path.empty()) |
| 339 | return -1; |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 340 | |
| 341 | auto status = std::filesystem::status(path); |
| 342 | if (std::filesystem::is_directory(status) and not std::filesystem::is_symlink(status)) { |
Adrien Béraud | 85d738b | 2023-09-06 22:39:46 -0400 | [diff] [blame] | 343 | std::error_code ec; |
| 344 | for (const auto& entry: std::filesystem::directory_iterator(path, ec)) { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 345 | removeAll(entry.path(), erase); |
| 346 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 347 | } |
| 348 | return remove(path, erase); |
| 349 | } |
| 350 | |
| 351 | void |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 352 | openStream(std::ifstream& file, const std::filesystem::path& path, std::ios_base::openmode mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 353 | { |
| 354 | #ifdef _WIN32 |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 355 | file.open(dhtnet::to_wstring(path.string()), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 356 | #else |
| 357 | file.open(path, mode); |
| 358 | #endif |
| 359 | } |
| 360 | |
| 361 | void |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 362 | openStream(std::ofstream& file, const std::filesystem::path& path, std::ios_base::openmode mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 363 | { |
| 364 | #ifdef _WIN32 |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 365 | file.open(dhtnet::to_wstring(path.string()), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 366 | #else |
| 367 | file.open(path, mode); |
| 368 | #endif |
| 369 | } |
| 370 | |
| 371 | std::ifstream |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 372 | ifstream(const std::filesystem::path& path, std::ios_base::openmode mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 373 | { |
| 374 | #ifdef _WIN32 |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 375 | return std::ifstream(dhtnet::to_wstring(path.string()), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 376 | #else |
| 377 | return std::ifstream(path, mode); |
| 378 | #endif |
| 379 | } |
| 380 | |
| 381 | std::ofstream |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 382 | ofstream(const std::filesystem::path& path, std::ios_base::openmode mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 383 | { |
| 384 | #ifdef _WIN32 |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 385 | return std::ofstream(dhtnet::to_wstring(path.string()), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 386 | #else |
| 387 | return std::ofstream(path, mode); |
| 388 | #endif |
| 389 | } |
| 390 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 391 | int |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 392 | accessFile(const std::filesystem::path& file, int mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 393 | { |
| 394 | #ifdef _WIN32 |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 395 | return _waccess(dhtnet::to_wstring(file.string()).c_str(), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 396 | #else |
| 397 | return access(file.c_str(), mode); |
| 398 | #endif |
| 399 | } |
| 400 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 401 | } // namespace fileutils |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 402 | } // namespace dhtnet |