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> |
| 25 | |
| 26 | #ifndef _WIN32 |
| 27 | #include <sys/stat.h> // mode_t |
| 28 | #define DIR_SEPARATOR_STR "/" // Directory separator string |
| 29 | #define DIR_SEPARATOR_CH '/' // Directory separator char |
| 30 | #define DIR_SEPARATOR_STR_ESC "\\/" // Escaped directory separator string |
| 31 | #else |
| 32 | #define mode_t unsigned |
| 33 | #define DIR_SEPARATOR_STR "\\" // Directory separator string |
| 34 | #define DIR_SEPARATOR_CH '\\' // Directory separator char |
| 35 | #define DIR_SEPARATOR_STR_ESC "//*" // Escaped directory separator string |
| 36 | #endif |
| 37 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 38 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 39 | namespace fileutils { |
| 40 | |
| 41 | /** |
| 42 | * Check directory existence and create it with given mode if it doesn't. |
| 43 | * @param path to check, relative or absolute |
| 44 | * @param dir last directory creation mode |
| 45 | * @param parents default mode for all created directories except the last |
| 46 | */ |
| 47 | bool check_dir(const char* path, mode_t dir = 0755, mode_t parents = 0755); |
| 48 | /*std::string expand_path(const std::string& path);*/ |
| 49 | bool isDirectoryWritable(const std::string& directory); |
| 50 | |
| 51 | bool recursive_mkdir(const std::string& path, mode_t mode = 0755); |
| 52 | |
| 53 | bool isPathRelative(const std::string& path); |
| 54 | /** |
| 55 | * If path is contained in base, return the suffix, otherwise return the full path. |
| 56 | * @param base must not finish with DIR_SEPARATOR_STR, can be empty |
| 57 | * @param path the path |
| 58 | */ |
| 59 | //std::string getCleanPath(const std::string& base, const std::string& path); |
| 60 | /** |
| 61 | * If path is relative, it is appended to base. |
| 62 | */ |
| 63 | //std::string getFullPath(const std::string& base, const std::string& path); |
| 64 | |
| 65 | bool isFile(const std::string& path, bool resolveSymlink = true); |
| 66 | bool isDirectory(const std::string& path); |
| 67 | bool isSymLink(const std::string& path); |
| 68 | bool hasHardLink(const std::string& path); |
| 69 | |
| 70 | std::chrono::system_clock::time_point writeTime(const std::string& path); |
| 71 | |
| 72 | /*void createFileLink(const std::string& src, const std::string& dest, bool hard = false); |
| 73 | |
| 74 | std::string_view getFileExtension(std::string_view filename);*/ |
| 75 | |
| 76 | /** |
| 77 | * Read content of the directory. |
| 78 | * The result is a list of relative (to @param dir) paths of all entries |
| 79 | * in the directory, without "." and "..". |
| 80 | */ |
| 81 | std::vector<std::string> readDirectory(const std::string& dir); |
| 82 | |
| 83 | /** |
| 84 | * Read the full content of a file at path. |
| 85 | * If path is relative, it is appended to default_dir. |
| 86 | */ |
| 87 | std::vector<uint8_t> loadFile(const std::string& path, const std::string& default_dir = {}); |
| 88 | std::string loadTextFile(const std::string& path, const std::string& default_dir = {}); |
| 89 | |
| 90 | void saveFile(const std::string& path, const uint8_t* data, size_t data_size, mode_t mode = 0644); |
| 91 | inline void |
| 92 | saveFile(const std::string& path, const std::vector<uint8_t>& data, mode_t mode = 0644) |
| 93 | { |
| 94 | saveFile(path, data.data(), data.size(), mode); |
| 95 | } |
| 96 | |
| 97 | /*std::vector<uint8_t> loadCacheFile(const std::string& path, |
| 98 | std::chrono::system_clock::duration maxAge); |
| 99 | std::string loadCacheTextFile(const std::string& path, std::chrono::system_clock::duration maxAge); |
| 100 | |
| 101 | std::vector<uint8_t> readArchive(const std::string& path, const std::string& password = {}); |
| 102 | void writeArchive(const std::string& data, |
| 103 | const std::string& path, |
| 104 | const std::string& password = {});*/ |
| 105 | |
| 106 | std::mutex& getFileLock(const std::string& path); |
| 107 | |
| 108 | /** |
| 109 | * Remove a file with optional erasing of content. |
| 110 | * Return the same value as std::remove(). |
| 111 | */ |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 112 | int remove(const std::string& path, bool erase = false); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Prune given directory's content and remove it, symlinks are not followed. |
| 116 | * Return 0 if succeed, -1 if directory is not removed (content can be removed partially). |
| 117 | */ |
| 118 | int removeAll(const std::string& path, bool erase = false); |
| 119 | |
| 120 | /** |
| 121 | * Wrappers for fstream opening that will convert paths to wstring |
| 122 | * on windows |
| 123 | */ |
| 124 | void openStream(std::ifstream& file, |
| 125 | const std::string& path, |
| 126 | std::ios_base::openmode mode = std::ios_base::in); |
| 127 | void openStream(std::ofstream& file, |
| 128 | const std::string& path, |
| 129 | std::ios_base::openmode mode = std::ios_base::out); |
| 130 | std::ifstream ifstream(const std::string& path, std::ios_base::openmode mode = std::ios_base::in); |
| 131 | std::ofstream ofstream(const std::string& path, std::ios_base::openmode mode = std::ios_base::out); |
| 132 | |
| 133 | int64_t size(const std::string& path); |
| 134 | |
| 135 | std::string sha3File(const std::string& path); |
| 136 | std::string sha3sum(const std::vector<uint8_t>& buffer); |
| 137 | |
| 138 | /** |
| 139 | * Windows compatibility wrapper for checking read-only attribute |
| 140 | */ |
| 141 | int accessFile(const std::string& file, int mode); |
| 142 | |
| 143 | uint64_t lastWriteTime(const std::string& p); |
| 144 | |
| 145 | } // namespace fileutils |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 146 | } // namespace dhtnet |