blob: c2dcdc9f1f9b76602703a4cc9450251345e6543f [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
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éraud1ae60aa2023-07-07 09:55:09 -040042namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040043namespace 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 */
51bool check_dir(const char* path, mode_t dir = 0755, mode_t parents = 0755);
52/*std::string expand_path(const std::string& path);*/
53bool isDirectoryWritable(const std::string& directory);
54
55bool recursive_mkdir(const std::string& path, mode_t mode = 0755);
56
57bool 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
69bool isFile(const std::string& path, bool resolveSymlink = true);
70bool isDirectory(const std::string& path);
71bool isSymLink(const std::string& path);
72bool hasHardLink(const std::string& path);
73
74std::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
78std::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 */
85std::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 */
91std::vector<uint8_t> loadFile(const std::string& path, const std::string& default_dir = {});
92std::string loadTextFile(const std::string& path, const std::string& default_dir = {});
93
94void saveFile(const std::string& path, const uint8_t* data, size_t data_size, mode_t mode = 0644);
95inline void
96saveFile(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);
103std::string loadCacheTextFile(const std::string& path, std::chrono::system_clock::duration maxAge);
104
105std::vector<uint8_t> readArchive(const std::string& path, const std::string& password = {});
106void writeArchive(const std::string& data,
107 const std::string& path,
108 const std::string& password = {});*/
109
110std::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 */
122int 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 */
128void openStream(std::ifstream& file,
129 const std::string& path,
130 std::ios_base::openmode mode = std::ios_base::in);
131void openStream(std::ofstream& file,
132 const std::string& path,
133 std::ios_base::openmode mode = std::ios_base::out);
134std::ifstream ifstream(const std::string& path, std::ios_base::openmode mode = std::ios_base::in);
135std::ofstream ofstream(const std::string& path, std::ios_base::openmode mode = std::ios_base::out);
136
137int64_t size(const std::string& path);
138
139std::string sha3File(const std::string& path);
140std::string sha3sum(const std::vector<uint8_t>& buffer);
141
142/**
143 * Windows compatibility wrapper for checking read-only attribute
144 */
145int accessFile(const std::string& file, int mode);
146
147uint64_t lastWriteTime(const std::string& p);
148
149} // namespace fileutils
150} // namespace jami