blob: 7539e11c62c7c03e9341f66fdbe217b04562c449 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraud612b55b2023-05-29 10:42:04 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraud612b55b2023-05-29 10:42:04 -04007 * (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éraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraud612b55b2023-05-29 10:42:04 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Adrien Béraudcb753622023-07-17 22:32:49 -040015 * along with this program. If not, see <https://www.gnu.org/licenses/>.
Adrien Béraud612b55b2023-05-29 10:42:04 -040016 */
Adrien Béraud612b55b2023-05-29 10:42:04 -040017#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éraud1ae60aa2023-07-07 09:55:09 -040038namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040039namespace 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 */
47bool check_dir(const char* path, mode_t dir = 0755, mode_t parents = 0755);
48/*std::string expand_path(const std::string& path);*/
49bool isDirectoryWritable(const std::string& directory);
50
51bool recursive_mkdir(const std::string& path, mode_t mode = 0755);
52
53bool 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
65bool isFile(const std::string& path, bool resolveSymlink = true);
66bool isDirectory(const std::string& path);
67bool isSymLink(const std::string& path);
68bool hasHardLink(const std::string& path);
69
70std::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
74std::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 */
81std::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 */
87std::vector<uint8_t> loadFile(const std::string& path, const std::string& default_dir = {});
88std::string loadTextFile(const std::string& path, const std::string& default_dir = {});
89
90void saveFile(const std::string& path, const uint8_t* data, size_t data_size, mode_t mode = 0644);
91inline void
92saveFile(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);
99std::string loadCacheTextFile(const std::string& path, std::chrono::system_clock::duration maxAge);
100
101std::vector<uint8_t> readArchive(const std::string& path, const std::string& password = {});
102void writeArchive(const std::string& data,
103 const std::string& path,
104 const std::string& password = {});*/
105
106std::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éraudcb753622023-07-17 22:32:49 -0400112int remove(const std::string& path, bool erase = false);
Adrien Béraud612b55b2023-05-29 10:42:04 -0400113
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 */
118int 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 */
124void openStream(std::ifstream& file,
125 const std::string& path,
126 std::ios_base::openmode mode = std::ios_base::in);
127void openStream(std::ofstream& file,
128 const std::string& path,
129 std::ios_base::openmode mode = std::ios_base::out);
130std::ifstream ifstream(const std::string& path, std::ios_base::openmode mode = std::ios_base::in);
131std::ofstream ofstream(const std::string& path, std::ios_base::openmode mode = std::ios_base::out);
132
133int64_t size(const std::string& path);
134
Adrien Béraud612b55b2023-05-29 10:42:04 -0400135/**
136 * Windows compatibility wrapper for checking read-only attribute
137 */
138int accessFile(const std::string& file, int mode);
139
140uint64_t lastWriteTime(const std::string& p);
141
142} // namespace fileutils
Sébastien Blin464bdff2023-07-19 08:02:53 -0400143} // namespace dhtnet