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 | #pragma once |
| 18 | |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | #include <chrono> |
| 22 | #include <mutex> |
| 23 | #include <cstdio> |
| 24 | #include <ios> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 25 | #include <filesystem> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 26 | |
Sébastien Blin | 7fbedf9 | 2023-08-30 08:44:47 -0400 | [diff] [blame] | 27 | #ifndef _WIN32 |
| 28 | #include <sys/stat.h> // mode_t |
| 29 | #endif |
| 30 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 31 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 32 | namespace fileutils { |
| 33 | |
| 34 | /** |
| 35 | * Check directory existence and create it with given mode if it doesn't. |
| 36 | * @param path to check, relative or absolute |
| 37 | * @param dir last directory creation mode |
| 38 | * @param parents default mode for all created directories except the last |
| 39 | */ |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 40 | bool check_dir(const std::filesystem::path& path, mode_t dir = 0755, mode_t parents = 0755); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 41 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 42 | bool recursive_mkdir(const std::filesystem::path& path, mode_t mode = 0755); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 43 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 44 | inline bool isPathRelative(const std::filesystem::path& path) { |
| 45 | return path.is_relative(); |
| 46 | } |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 47 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 48 | bool isFile(const std::filesystem::path& path, bool resolveSymlink = true); |
| 49 | bool isDirectory(const std::filesystem::path& path); |
| 50 | bool isSymLink(const std::filesystem::path& path); |
| 51 | bool hasHardLink(const std::filesystem::path& path); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Read content of the directory. |
| 55 | * The result is a list of relative (to @param dir) paths of all entries |
| 56 | * in the directory, without "." and "..". |
| 57 | */ |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 58 | std::vector<std::string> readDirectory(const std::filesystem::path& dir); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Read the full content of a file at path. |
| 62 | * If path is relative, it is appended to default_dir. |
| 63 | */ |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 64 | std::vector<uint8_t> loadFile(const std::filesystem::path& path); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 65 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 66 | void saveFile(const std::filesystem::path& path, const uint8_t* data, size_t data_size, mode_t mode = 0644); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 67 | inline void |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 68 | saveFile(const std::filesystem::path& path, const std::vector<uint8_t>& data, mode_t mode = 0644) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 69 | { |
| 70 | saveFile(path, data.data(), data.size(), mode); |
| 71 | } |
| 72 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 73 | std::mutex& getFileLock(const std::filesystem::path& path); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Remove a file with optional erasing of content. |
| 77 | * Return the same value as std::remove(). |
| 78 | */ |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 79 | int remove(const std::filesystem::path& path, bool erase = false); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Prune given directory's content and remove it, symlinks are not followed. |
| 83 | * Return 0 if succeed, -1 if directory is not removed (content can be removed partially). |
| 84 | */ |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 85 | int removeAll(const std::filesystem::path& path, bool erase = false); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Wrappers for fstream opening that will convert paths to wstring |
| 89 | * on windows |
| 90 | */ |
| 91 | void openStream(std::ifstream& file, |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 92 | const std::filesystem::path& path, |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 93 | std::ios_base::openmode mode = std::ios_base::in); |
| 94 | void openStream(std::ofstream& file, |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 95 | const std::filesystem::path& path, |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 96 | std::ios_base::openmode mode = std::ios_base::out); |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 97 | std::ifstream ifstream(const std::filesystem::path& path, std::ios_base::openmode mode = std::ios_base::in); |
| 98 | std::ofstream ofstream(const std::filesystem::path& path, std::ios_base::openmode mode = std::ios_base::out); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 99 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 100 | /** |
| 101 | * Windows compatibility wrapper for checking read-only attribute |
| 102 | */ |
| 103 | int accessFile(const std::string& file, int mode); |
| 104 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 105 | } // namespace fileutils |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 106 | } // namespace dhtnet |