Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * Author: Rafaël Carré <rafael.carre@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | #include <chrono> |
| 26 | #include <mutex> |
| 27 | #include <cstdio> |
| 28 | #include <ios> |
| 29 | |
| 30 | #ifndef _WIN32 |
| 31 | #include <sys/stat.h> // mode_t |
| 32 | #define DIR_SEPARATOR_STR "/" // Directory separator string |
| 33 | #define DIR_SEPARATOR_CH '/' // Directory separator char |
| 34 | #define DIR_SEPARATOR_STR_ESC "\\/" // Escaped directory separator string |
| 35 | #else |
| 36 | #define mode_t unsigned |
| 37 | #define DIR_SEPARATOR_STR "\\" // Directory separator string |
| 38 | #define DIR_SEPARATOR_CH '\\' // Directory separator char |
| 39 | #define DIR_SEPARATOR_STR_ESC "//*" // Escaped directory separator string |
| 40 | #endif |
| 41 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 42 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 43 | namespace fileutils { |
| 44 | |
| 45 | /** |
| 46 | * Check directory existence and create it with given mode if it doesn't. |
| 47 | * @param path to check, relative or absolute |
| 48 | * @param dir last directory creation mode |
| 49 | * @param parents default mode for all created directories except the last |
| 50 | */ |
| 51 | bool check_dir(const char* path, mode_t dir = 0755, mode_t parents = 0755); |
| 52 | /*std::string expand_path(const std::string& path);*/ |
| 53 | bool isDirectoryWritable(const std::string& directory); |
| 54 | |
| 55 | bool recursive_mkdir(const std::string& path, mode_t mode = 0755); |
| 56 | |
| 57 | bool isPathRelative(const std::string& path); |
| 58 | /** |
| 59 | * If path is contained in base, return the suffix, otherwise return the full path. |
| 60 | * @param base must not finish with DIR_SEPARATOR_STR, can be empty |
| 61 | * @param path the path |
| 62 | */ |
| 63 | //std::string getCleanPath(const std::string& base, const std::string& path); |
| 64 | /** |
| 65 | * If path is relative, it is appended to base. |
| 66 | */ |
| 67 | //std::string getFullPath(const std::string& base, const std::string& path); |
| 68 | |
| 69 | bool isFile(const std::string& path, bool resolveSymlink = true); |
| 70 | bool isDirectory(const std::string& path); |
| 71 | bool isSymLink(const std::string& path); |
| 72 | bool hasHardLink(const std::string& path); |
| 73 | |
| 74 | std::chrono::system_clock::time_point writeTime(const std::string& path); |
| 75 | |
| 76 | /*void createFileLink(const std::string& src, const std::string& dest, bool hard = false); |
| 77 | |
| 78 | std::string_view getFileExtension(std::string_view filename);*/ |
| 79 | |
| 80 | /** |
| 81 | * Read content of the directory. |
| 82 | * The result is a list of relative (to @param dir) paths of all entries |
| 83 | * in the directory, without "." and "..". |
| 84 | */ |
| 85 | std::vector<std::string> readDirectory(const std::string& dir); |
| 86 | |
| 87 | /** |
| 88 | * Read the full content of a file at path. |
| 89 | * If path is relative, it is appended to default_dir. |
| 90 | */ |
| 91 | std::vector<uint8_t> loadFile(const std::string& path, const std::string& default_dir = {}); |
| 92 | std::string loadTextFile(const std::string& path, const std::string& default_dir = {}); |
| 93 | |
| 94 | void saveFile(const std::string& path, const uint8_t* data, size_t data_size, mode_t mode = 0644); |
| 95 | inline void |
| 96 | saveFile(const std::string& path, const std::vector<uint8_t>& data, mode_t mode = 0644) |
| 97 | { |
| 98 | saveFile(path, data.data(), data.size(), mode); |
| 99 | } |
| 100 | |
| 101 | /*std::vector<uint8_t> loadCacheFile(const std::string& path, |
| 102 | std::chrono::system_clock::duration maxAge); |
| 103 | std::string loadCacheTextFile(const std::string& path, std::chrono::system_clock::duration maxAge); |
| 104 | |
| 105 | std::vector<uint8_t> readArchive(const std::string& path, const std::string& password = {}); |
| 106 | void writeArchive(const std::string& data, |
| 107 | const std::string& path, |
| 108 | const std::string& password = {});*/ |
| 109 | |
| 110 | std::mutex& getFileLock(const std::string& path); |
| 111 | |
| 112 | /** |
| 113 | * Remove a file with optional erasing of content. |
| 114 | * Return the same value as std::remove(). |
| 115 | */ |
| 116 | //int remove(const std::string& path, bool erase = false); |
| 117 | |
| 118 | /** |
| 119 | * Prune given directory's content and remove it, symlinks are not followed. |
| 120 | * Return 0 if succeed, -1 if directory is not removed (content can be removed partially). |
| 121 | */ |
| 122 | int removeAll(const std::string& path, bool erase = false); |
| 123 | |
| 124 | /** |
| 125 | * Wrappers for fstream opening that will convert paths to wstring |
| 126 | * on windows |
| 127 | */ |
| 128 | void openStream(std::ifstream& file, |
| 129 | const std::string& path, |
| 130 | std::ios_base::openmode mode = std::ios_base::in); |
| 131 | void openStream(std::ofstream& file, |
| 132 | const std::string& path, |
| 133 | std::ios_base::openmode mode = std::ios_base::out); |
| 134 | std::ifstream ifstream(const std::string& path, std::ios_base::openmode mode = std::ios_base::in); |
| 135 | std::ofstream ofstream(const std::string& path, std::ios_base::openmode mode = std::ios_base::out); |
| 136 | |
| 137 | int64_t size(const std::string& path); |
| 138 | |
| 139 | std::string sha3File(const std::string& path); |
| 140 | std::string sha3sum(const std::vector<uint8_t>& buffer); |
| 141 | |
| 142 | /** |
| 143 | * Windows compatibility wrapper for checking read-only attribute |
| 144 | */ |
| 145 | int accessFile(const std::string& file, int mode); |
| 146 | |
| 147 | uint64_t lastWriteTime(const std::string& p); |
| 148 | |
| 149 | } // namespace fileutils |
| 150 | } // namespace jami |