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 | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 67 | if (std::filesystem::exists(path)) |
| 68 | return true; |
| 69 | if (path.has_parent_path()) |
| 70 | check_dir(path.parent_path(), parentmode, parentmode); |
Adrien Béraud | 6a529fb | 2023-09-14 14:34:23 -0400 | [diff] [blame] | 71 | std::error_code ec; |
| 72 | if (std::filesystem::create_directory(path, ec)) { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 73 | std::filesystem::permissions(path, (std::filesystem::perms)dirmode); |
| 74 | return true; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 75 | } |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 76 | return false; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | std::mutex& |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 80 | getFileLock(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 81 | { |
| 82 | static std::mutex fileLockLock {}; |
| 83 | static std::map<std::string, std::mutex> fileLocks {}; |
| 84 | |
| 85 | std::lock_guard<std::mutex> l(fileLockLock); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 86 | return fileLocks[path.string()]; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 90 | isFile(const std::filesystem::path& path, bool resolveSymlink) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 91 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 92 | auto status = resolveSymlink ? std::filesystem::status(path) : std::filesystem::symlink_status(path); |
| 93 | return std::filesystem::is_regular_file(status); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 97 | isDirectory(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 98 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 99 | return std::filesystem::is_directory(path); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 103 | hasHardLink(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 104 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 105 | return std::filesystem::hard_link_count(path) > 1; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 109 | isSymLink(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 110 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 111 | return std::filesystem::is_symlink(path); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 114 | template <typename TP> |
| 115 | std::chrono::system_clock::time_point to_sysclock(TP tp) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 116 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 117 | using namespace std::chrono; |
| 118 | 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] | 119 | } |
| 120 | |
| 121 | bool |
| 122 | createSymlink(const std::string& linkFile, const std::string& target) |
| 123 | { |
| 124 | try { |
| 125 | std::filesystem::create_symlink(target, linkFile); |
| 126 | } catch (const std::exception& e) { |
| 127 | //JAMI_ERR("Couldn't create soft link: %s", e.what()); |
| 128 | return false; |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | bool |
| 134 | createHardlink(const std::string& linkFile, const std::string& target) |
| 135 | { |
| 136 | try { |
| 137 | std::filesystem::create_hard_link(target, linkFile); |
| 138 | } catch (const std::exception& e) { |
| 139 | //JAMI_ERR("Couldn't create hard link: %s", e.what()); |
| 140 | return false; |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | createFileLink(const std::string& linkFile, const std::string& target, bool hard) |
| 147 | { |
| 148 | if (not hard or not createHardlink(linkFile, target)) |
| 149 | createSymlink(linkFile, target); |
| 150 | } |
| 151 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 152 | std::vector<uint8_t> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 153 | loadFile(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 154 | { |
| 155 | std::vector<uint8_t> buffer; |
Adrien Béraud | 1299a0d | 2023-09-19 15:03:28 -0400 | [diff] [blame] | 156 | std::ifstream file(path, std::ios::binary); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 157 | if (!file) |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 158 | throw std::runtime_error("Can't read file: " + path.string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 159 | file.seekg(0, std::ios::end); |
| 160 | auto size = file.tellg(); |
| 161 | if (size > std::numeric_limits<unsigned>::max()) |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 162 | throw std::runtime_error("File is too big: " + path.string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 163 | buffer.resize(size); |
| 164 | file.seekg(0, std::ios::beg); |
| 165 | if (!file.read((char*) buffer.data(), size)) |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 166 | throw std::runtime_error("Can't load file: " + path.string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 167 | return buffer; |
| 168 | } |
| 169 | |
| 170 | void |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 171 | 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] | 172 | { |
Adrien Béraud | 1299a0d | 2023-09-19 15:03:28 -0400 | [diff] [blame] | 173 | std::ofstream file(path, std::ios::trunc | std::ios::binary); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 174 | if (!file.is_open()) { |
| 175 | //JAMI_ERR("Could not write data to %s", path.c_str()); |
| 176 | return; |
| 177 | } |
| 178 | file.write((char*) data, data_size); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 179 | file.close(); |
| 180 | std::filesystem::permissions(path, (std::filesystem::perms)mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | std::vector<std::string> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 184 | readDirectory(const std::filesystem::path& dir) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 185 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 186 | std::vector<std::string> files; |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 187 | std::error_code ec; |
| 188 | for (const auto& entry : std::filesystem::directory_iterator(dir, ec)) { |
Adrien Béraud | 8294048 | 2023-08-27 13:03:20 -0400 | [diff] [blame] | 189 | files.emplace_back(entry.path().filename().string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 190 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 191 | return files; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 194 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 195 | recursive_mkdir(const std::filesystem::path& path, mode_t mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 196 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 197 | std::error_code ec; |
| 198 | std::filesystem::create_directories(path, ec); |
| 199 | if (!ec) |
| 200 | std::filesystem::permissions(path, (std::filesystem::perms)mode, ec); |
| 201 | return !ec; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | #ifdef _WIN32 |
| 205 | bool |
| 206 | eraseFile_win32(const std::string& path, bool dosync) |
| 207 | { |
| 208 | HANDLE h |
| 209 | = CreateFileA(path.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
| 210 | if (h == INVALID_HANDLE_VALUE) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 211 | // 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] | 212 | return false; |
| 213 | } |
| 214 | |
| 215 | LARGE_INTEGER size; |
| 216 | if (!GetFileSizeEx(h, &size)) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 217 | // 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] | 218 | CloseHandle(h); |
| 219 | return false; |
| 220 | } |
| 221 | if (size.QuadPart == 0) { |
| 222 | CloseHandle(h); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | uint64_t size_blocks = size.QuadPart / ERASE_BLOCK; |
| 227 | if (size.QuadPart % ERASE_BLOCK) |
| 228 | size_blocks++; |
| 229 | |
| 230 | char* buffer; |
| 231 | try { |
| 232 | buffer = new char[ERASE_BLOCK]; |
| 233 | } catch (std::bad_alloc& ba) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 234 | // 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] | 235 | CloseHandle(h); |
| 236 | return false; |
| 237 | } |
| 238 | memset(buffer, 0x00, ERASE_BLOCK); |
| 239 | |
| 240 | OVERLAPPED ovlp; |
| 241 | if (size.QuadPart < (1024 - 42)) { // a small file can be stored in the MFT record |
| 242 | ovlp.Offset = 0; |
| 243 | ovlp.OffsetHigh = 0; |
| 244 | WriteFile(h, buffer, (DWORD) size.QuadPart, 0, &ovlp); |
| 245 | FlushFileBuffers(h); |
| 246 | } |
| 247 | for (uint64_t i = 0; i < size_blocks; i++) { |
| 248 | uint64_t offset = i * ERASE_BLOCK; |
| 249 | ovlp.Offset = offset & 0x00000000FFFFFFFF; |
| 250 | ovlp.OffsetHigh = offset >> 32; |
| 251 | WriteFile(h, buffer, ERASE_BLOCK, 0, &ovlp); |
| 252 | } |
| 253 | |
| 254 | delete[] buffer; |
| 255 | |
| 256 | if (dosync) |
| 257 | FlushFileBuffers(h); |
| 258 | |
| 259 | CloseHandle(h); |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | #else |
| 264 | |
| 265 | bool |
| 266 | eraseFile_posix(const std::string& path, bool dosync) |
| 267 | { |
| 268 | struct stat st; |
| 269 | if (stat(path.c_str(), &st) == -1) { |
| 270 | //JAMI_WARN("Can not erase file %s: fstat() failed.", path.c_str()); |
| 271 | return false; |
| 272 | } |
| 273 | // Remove read-only flag if possible |
| 274 | chmod(path.c_str(), st.st_mode | (S_IWGRP+S_IWUSR) ); |
| 275 | |
| 276 | int fd = open(path.c_str(), O_WRONLY); |
| 277 | if (fd == -1) { |
| 278 | //JAMI_WARN("Can not open file %s for erasing.", path.c_str()); |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | if (st.st_size == 0) { |
| 283 | close(fd); |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | lseek(fd, 0, SEEK_SET); |
| 288 | |
| 289 | std::array<char, ERASE_BLOCK> buffer; |
| 290 | buffer.fill(0); |
| 291 | decltype(st.st_size) written(0); |
| 292 | while (written < st.st_size) { |
| 293 | auto ret = write(fd, buffer.data(), buffer.size()); |
| 294 | if (ret < 0) { |
| 295 | //JAMI_WARNING("Error while overriding file with zeros."); |
| 296 | break; |
| 297 | } else |
| 298 | written += ret; |
| 299 | } |
| 300 | |
| 301 | if (dosync) |
| 302 | fsync(fd); |
| 303 | |
| 304 | close(fd); |
| 305 | return written >= st.st_size; |
| 306 | } |
| 307 | #endif |
| 308 | |
| 309 | bool |
| 310 | eraseFile(const std::string& path, bool dosync) |
| 311 | { |
| 312 | #ifdef _WIN32 |
| 313 | return eraseFile_win32(path, dosync); |
| 314 | #else |
| 315 | return eraseFile_posix(path, dosync); |
| 316 | #endif |
| 317 | } |
| 318 | |
| 319 | int |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 320 | remove(const std::filesystem::path& path, bool erase) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 321 | { |
| 322 | if (erase and isFile(path, false) and !hasHardLink(path)) |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 323 | eraseFile(path.string(), true); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 324 | |
| 325 | #ifdef _WIN32 |
| 326 | // use Win32 api since std::remove will not unlink directory in use |
| 327 | if (isDirectory(path)) |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 328 | return !RemoveDirectory(dhtnet::to_wstring(path.string()).c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 329 | #endif |
| 330 | |
Adrien Béraud | 668b28c | 2023-10-25 00:50:56 -0400 | [diff] [blame] | 331 | std::error_code ec; |
| 332 | std::filesystem::remove(path, ec); |
| 333 | return ec.value(); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | int |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 337 | removeAll(const std::filesystem::path& path, bool erase) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 338 | { |
Adrien Béraud | add101f | 2023-11-23 14:45:31 -0500 | [diff] [blame] | 339 | try { |
| 340 | std::error_code ec; |
| 341 | if (not erase) { |
| 342 | std::filesystem::remove_all(path, ec); |
| 343 | return ec.value(); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 344 | } |
Adrien Béraud | add101f | 2023-11-23 14:45:31 -0500 | [diff] [blame] | 345 | if (path.empty()) |
| 346 | return -1; |
| 347 | |
| 348 | auto status = std::filesystem::status(path, ec); |
| 349 | if (!ec && std::filesystem::is_directory(status) and not std::filesystem::is_symlink(status)) { |
| 350 | for (const auto& entry: std::filesystem::directory_iterator(path, ec)) { |
| 351 | removeAll(entry.path(), erase); |
| 352 | } |
| 353 | } |
| 354 | return remove(path, erase); |
| 355 | } catch (const std::exception& e) { |
| 356 | //JAMI_ERR("Error while removing %s: %s", path.c_str(), e.what()); |
| 357 | return -1; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 358 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 362 | 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] | 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 | void |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 372 | 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] | 373 | { |
| 374 | #ifdef _WIN32 |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 375 | file.open(dhtnet::to_wstring(path.string()), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 376 | #else |
| 377 | file.open(path, mode); |
| 378 | #endif |
| 379 | } |
| 380 | |
| 381 | std::ifstream |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 382 | ifstream(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::ifstream(dhtnet::to_wstring(path.string()), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 386 | #else |
| 387 | return std::ifstream(path, mode); |
| 388 | #endif |
| 389 | } |
| 390 | |
| 391 | std::ofstream |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 392 | ofstream(const std::filesystem::path& path, std::ios_base::openmode 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 std::ofstream(dhtnet::to_wstring(path.string()), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 396 | #else |
| 397 | return std::ofstream(path, mode); |
| 398 | #endif |
| 399 | } |
| 400 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 401 | int |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 402 | accessFile(const std::filesystem::path& file, int mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 403 | { |
| 404 | #ifdef _WIN32 |
Aline Gondim Santos | 406c0f4 | 2023-09-13 12:10:23 -0300 | [diff] [blame] | 405 | return _waccess(dhtnet::to_wstring(file.string()).c_str(), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 406 | #else |
| 407 | return access(file.c_str(), mode); |
| 408 | #endif |
| 409 | } |
| 410 | |
Adrien Béraud | 5aec410 | 2024-02-22 14:15:56 -0500 | [diff] [blame] | 411 | constexpr auto ID_TIMEOUT = std::chrono::hours(24); |
| 412 | |
| 413 | void |
| 414 | IdList::load() |
| 415 | { |
| 416 | size_t pruned = 0; |
| 417 | auto now = std::chrono::system_clock::now(); |
| 418 | try { |
| 419 | std::ifstream file(path, std::ios::binary); |
| 420 | msgpack::unpacker unp; |
| 421 | auto timeout = now - ID_TIMEOUT; |
| 422 | while (file.is_open() && !file.eof()) { |
| 423 | unp.reserve_buffer(8 * 1024); |
| 424 | file.read(unp.buffer(), unp.buffer_capacity()); |
| 425 | unp.buffer_consumed(file.gcount()); |
| 426 | msgpack::unpacked result; |
| 427 | while (unp.next(result)) { |
| 428 | auto kv = result.get().as<std::pair<uint64_t, std::chrono::system_clock::time_point>>(); |
| 429 | if (kv.second > timeout) |
| 430 | ids.insert(std::move(kv)); |
| 431 | else |
| 432 | pruned++; |
| 433 | } |
| 434 | } |
| 435 | } catch (const std::exception& e) { |
| 436 | // discard corrupted files |
| 437 | std::error_code ec; |
| 438 | std::filesystem::remove(path, ec); |
| 439 | } |
| 440 | last_maintain = now; |
| 441 | if (pruned) { |
| 442 | std::ofstream file(path, std::ios::trunc | std::ios::binary); |
| 443 | for (auto& kv : ids) |
| 444 | msgpack::pack(file, kv); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | bool |
| 449 | IdList::add(uint64_t id) |
| 450 | { |
| 451 | auto now = std::chrono::system_clock::now(); |
| 452 | auto r = ids.emplace(id, now); |
| 453 | if (r.second) { |
| 454 | auto timeout = now - ID_TIMEOUT; |
| 455 | if (last_maintain > timeout) { |
| 456 | // append |
| 457 | std::ofstream file(path, std::ios::app | std::ios::binary); |
| 458 | if (file.is_open()) { |
| 459 | msgpack::pack(file, *r.first); |
| 460 | } |
| 461 | } else { |
| 462 | // maintain and save |
| 463 | std::ofstream file(path, std::ios::trunc | std::ios::binary); |
| 464 | for (auto it = ids.begin(); it != ids.end();) { |
| 465 | if (it->second < timeout) { |
| 466 | it = ids.erase(it); |
| 467 | } else { |
| 468 | msgpack::pack(file, *it); |
| 469 | ++it; |
| 470 | } |
| 471 | } |
| 472 | last_maintain = now; |
| 473 | } |
| 474 | return true; |
| 475 | } |
| 476 | return false; |
| 477 | } |
| 478 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 479 | } // namespace fileutils |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 480 | } // namespace dhtnet |