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); |
| 71 | if (std::filesystem::create_directory(path)) { |
| 72 | std::filesystem::permissions(path, (std::filesystem::perms)dirmode); |
| 73 | return true; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 74 | } |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 75 | return false; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | std::mutex& |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 79 | getFileLock(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 80 | { |
| 81 | static std::mutex fileLockLock {}; |
| 82 | static std::map<std::string, std::mutex> fileLocks {}; |
| 83 | |
| 84 | std::lock_guard<std::mutex> l(fileLockLock); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 85 | return fileLocks[path.string()]; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 89 | isFile(const std::filesystem::path& path, bool resolveSymlink) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 90 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 91 | auto status = resolveSymlink ? std::filesystem::status(path) : std::filesystem::symlink_status(path); |
| 92 | return std::filesystem::is_regular_file(status); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 96 | isDirectory(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 97 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 98 | return std::filesystem::is_directory(path); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 102 | hasHardLink(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 103 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 104 | return std::filesystem::hard_link_count(path) > 1; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 108 | isSymLink(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 109 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 110 | return std::filesystem::is_symlink(path); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 113 | template <typename TP> |
| 114 | std::chrono::system_clock::time_point to_sysclock(TP tp) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 115 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 116 | using namespace std::chrono; |
| 117 | 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] | 118 | } |
| 119 | |
| 120 | bool |
| 121 | createSymlink(const std::string& linkFile, const std::string& target) |
| 122 | { |
| 123 | try { |
| 124 | std::filesystem::create_symlink(target, linkFile); |
| 125 | } catch (const std::exception& e) { |
| 126 | //JAMI_ERR("Couldn't create soft link: %s", e.what()); |
| 127 | return false; |
| 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | bool |
| 133 | createHardlink(const std::string& linkFile, const std::string& target) |
| 134 | { |
| 135 | try { |
| 136 | std::filesystem::create_hard_link(target, linkFile); |
| 137 | } catch (const std::exception& e) { |
| 138 | //JAMI_ERR("Couldn't create hard link: %s", e.what()); |
| 139 | return false; |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | createFileLink(const std::string& linkFile, const std::string& target, bool hard) |
| 146 | { |
| 147 | if (not hard or not createHardlink(linkFile, target)) |
| 148 | createSymlink(linkFile, target); |
| 149 | } |
| 150 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 151 | std::vector<uint8_t> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 152 | loadFile(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 153 | { |
| 154 | std::vector<uint8_t> buffer; |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 155 | std::ifstream file = ifstream(path, std::ios::binary); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 156 | if (!file) |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 157 | throw std::runtime_error("Can't read file: " + path.string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 158 | file.seekg(0, std::ios::end); |
| 159 | auto size = file.tellg(); |
| 160 | if (size > std::numeric_limits<unsigned>::max()) |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 161 | throw std::runtime_error("File is too big: " + path.string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 162 | buffer.resize(size); |
| 163 | file.seekg(0, std::ios::beg); |
| 164 | if (!file.read((char*) buffer.data(), size)) |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 165 | throw std::runtime_error("Can't load file: " + path.string()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 166 | return buffer; |
| 167 | } |
| 168 | |
| 169 | void |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 170 | 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] | 171 | { |
| 172 | std::ofstream file = fileutils::ofstream(path, std::ios::trunc | std::ios::binary); |
| 173 | if (!file.is_open()) { |
| 174 | //JAMI_ERR("Could not write data to %s", path.c_str()); |
| 175 | return; |
| 176 | } |
| 177 | file.write((char*) data, data_size); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 178 | file.close(); |
| 179 | std::filesystem::permissions(path, (std::filesystem::perms)mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | std::vector<std::string> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 183 | readDirectory(const std::filesystem::path& dir) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 184 | { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 185 | std::vector<std::string> files; |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 186 | std::error_code ec; |
| 187 | for (const auto& entry : std::filesystem::directory_iterator(dir, ec)) { |
| 188 | std::string fname {entry.path().filename().string()}; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 189 | if (fname == "." || fname == "..") |
| 190 | continue; |
| 191 | files.emplace_back(std::move(fname)); |
| 192 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 193 | return files; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 196 | bool |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 197 | recursive_mkdir(const std::filesystem::path& path, mode_t mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 198 | { |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 199 | std::error_code ec; |
| 200 | std::filesystem::create_directories(path, ec); |
| 201 | if (!ec) |
| 202 | std::filesystem::permissions(path, (std::filesystem::perms)mode, ec); |
| 203 | return !ec; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | #ifdef _WIN32 |
| 207 | bool |
| 208 | eraseFile_win32(const std::string& path, bool dosync) |
| 209 | { |
| 210 | HANDLE h |
| 211 | = CreateFileA(path.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
| 212 | if (h == INVALID_HANDLE_VALUE) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 213 | // 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] | 214 | return false; |
| 215 | } |
| 216 | |
| 217 | LARGE_INTEGER size; |
| 218 | if (!GetFileSizeEx(h, &size)) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 219 | // 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] | 220 | CloseHandle(h); |
| 221 | return false; |
| 222 | } |
| 223 | if (size.QuadPart == 0) { |
| 224 | CloseHandle(h); |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | uint64_t size_blocks = size.QuadPart / ERASE_BLOCK; |
| 229 | if (size.QuadPart % ERASE_BLOCK) |
| 230 | size_blocks++; |
| 231 | |
| 232 | char* buffer; |
| 233 | try { |
| 234 | buffer = new char[ERASE_BLOCK]; |
| 235 | } catch (std::bad_alloc& ba) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 236 | // 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] | 237 | CloseHandle(h); |
| 238 | return false; |
| 239 | } |
| 240 | memset(buffer, 0x00, ERASE_BLOCK); |
| 241 | |
| 242 | OVERLAPPED ovlp; |
| 243 | if (size.QuadPart < (1024 - 42)) { // a small file can be stored in the MFT record |
| 244 | ovlp.Offset = 0; |
| 245 | ovlp.OffsetHigh = 0; |
| 246 | WriteFile(h, buffer, (DWORD) size.QuadPart, 0, &ovlp); |
| 247 | FlushFileBuffers(h); |
| 248 | } |
| 249 | for (uint64_t i = 0; i < size_blocks; i++) { |
| 250 | uint64_t offset = i * ERASE_BLOCK; |
| 251 | ovlp.Offset = offset & 0x00000000FFFFFFFF; |
| 252 | ovlp.OffsetHigh = offset >> 32; |
| 253 | WriteFile(h, buffer, ERASE_BLOCK, 0, &ovlp); |
| 254 | } |
| 255 | |
| 256 | delete[] buffer; |
| 257 | |
| 258 | if (dosync) |
| 259 | FlushFileBuffers(h); |
| 260 | |
| 261 | CloseHandle(h); |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | #else |
| 266 | |
| 267 | bool |
| 268 | eraseFile_posix(const std::string& path, bool dosync) |
| 269 | { |
| 270 | struct stat st; |
| 271 | if (stat(path.c_str(), &st) == -1) { |
| 272 | //JAMI_WARN("Can not erase file %s: fstat() failed.", path.c_str()); |
| 273 | return false; |
| 274 | } |
| 275 | // Remove read-only flag if possible |
| 276 | chmod(path.c_str(), st.st_mode | (S_IWGRP+S_IWUSR) ); |
| 277 | |
| 278 | int fd = open(path.c_str(), O_WRONLY); |
| 279 | if (fd == -1) { |
| 280 | //JAMI_WARN("Can not open file %s for erasing.", path.c_str()); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | if (st.st_size == 0) { |
| 285 | close(fd); |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | lseek(fd, 0, SEEK_SET); |
| 290 | |
| 291 | std::array<char, ERASE_BLOCK> buffer; |
| 292 | buffer.fill(0); |
| 293 | decltype(st.st_size) written(0); |
| 294 | while (written < st.st_size) { |
| 295 | auto ret = write(fd, buffer.data(), buffer.size()); |
| 296 | if (ret < 0) { |
| 297 | //JAMI_WARNING("Error while overriding file with zeros."); |
| 298 | break; |
| 299 | } else |
| 300 | written += ret; |
| 301 | } |
| 302 | |
| 303 | if (dosync) |
| 304 | fsync(fd); |
| 305 | |
| 306 | close(fd); |
| 307 | return written >= st.st_size; |
| 308 | } |
| 309 | #endif |
| 310 | |
| 311 | bool |
| 312 | eraseFile(const std::string& path, bool dosync) |
| 313 | { |
| 314 | #ifdef _WIN32 |
| 315 | return eraseFile_win32(path, dosync); |
| 316 | #else |
| 317 | return eraseFile_posix(path, dosync); |
| 318 | #endif |
| 319 | } |
| 320 | |
| 321 | int |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 322 | remove(const std::filesystem::path& path, bool erase) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 323 | { |
| 324 | if (erase and isFile(path, false) and !hasHardLink(path)) |
| 325 | eraseFile(path, true); |
| 326 | |
| 327 | #ifdef _WIN32 |
| 328 | // use Win32 api since std::remove will not unlink directory in use |
| 329 | if (isDirectory(path)) |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 330 | return !RemoveDirectory(dhtnet::to_wstring(path).c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 331 | #endif |
| 332 | |
| 333 | return std::remove(path.c_str()); |
| 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 | { |
| 339 | if (path.empty()) |
| 340 | return -1; |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 341 | |
| 342 | auto status = std::filesystem::status(path); |
| 343 | if (std::filesystem::is_directory(status) and not std::filesystem::is_symlink(status)) { |
| 344 | for (const auto& entry: std::filesystem::directory_iterator(path)) { |
| 345 | auto fname = entry.path().filename().string(); |
| 346 | if (fname == "." || fname == "..") |
| 347 | continue; |
| 348 | removeAll(entry.path(), erase); |
| 349 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 350 | } |
| 351 | return remove(path, erase); |
| 352 | } |
| 353 | |
| 354 | void |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 355 | 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] | 356 | { |
| 357 | #ifdef _WIN32 |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 358 | file.open(dhtnet::to_wstring(path), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 359 | #else |
| 360 | file.open(path, mode); |
| 361 | #endif |
| 362 | } |
| 363 | |
| 364 | void |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 365 | 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] | 366 | { |
| 367 | #ifdef _WIN32 |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 368 | file.open(dhtnet::to_wstring(path), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 369 | #else |
| 370 | file.open(path, mode); |
| 371 | #endif |
| 372 | } |
| 373 | |
| 374 | std::ifstream |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 375 | ifstream(const std::filesystem::path& path, std::ios_base::openmode mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 376 | { |
| 377 | #ifdef _WIN32 |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 378 | return std::ifstream(dhtnet::to_wstring(path), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 379 | #else |
| 380 | return std::ifstream(path, mode); |
| 381 | #endif |
| 382 | } |
| 383 | |
| 384 | std::ofstream |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 385 | ofstream(const std::filesystem::path& path, std::ios_base::openmode mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 386 | { |
| 387 | #ifdef _WIN32 |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 388 | return std::ofstream(dhtnet::to_wstring(path), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 389 | #else |
| 390 | return std::ofstream(path, mode); |
| 391 | #endif |
| 392 | } |
| 393 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 394 | int |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 395 | accessFile(const std::filesystem::path& file, int mode) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 396 | { |
| 397 | #ifdef _WIN32 |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 398 | return _waccess(dhtnet::to_wstring(file).c_str(), mode); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 399 | #else |
| 400 | return access(file.c_str(), mode); |
| 401 | #endif |
| 402 | } |
| 403 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 404 | } // namespace fileutils |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 405 | } // namespace dhtnet |