blob: 8362cacbfd637935cd5e344f4f22b77423aa21eb [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>
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040025#include <filesystem>
Adrien Béraud612b55b2023-05-29 10:42:04 -040026
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040027namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040028namespace fileutils {
29
30/**
31 * Check directory existence and create it with given mode if it doesn't.
32 * @param path to check, relative or absolute
33 * @param dir last directory creation mode
34 * @param parents default mode for all created directories except the last
35 */
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040036bool check_dir(const std::filesystem::path& path, mode_t dir = 0755, mode_t parents = 0755);
Adrien Béraud612b55b2023-05-29 10:42:04 -040037
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040038bool recursive_mkdir(const std::filesystem::path& path, mode_t mode = 0755);
Adrien Béraud612b55b2023-05-29 10:42:04 -040039
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040040inline bool isPathRelative(const std::filesystem::path& path) {
41 return path.is_relative();
42}
Adrien Béraud612b55b2023-05-29 10:42:04 -040043
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040044bool isFile(const std::filesystem::path& path, bool resolveSymlink = true);
45bool isDirectory(const std::filesystem::path& path);
46bool isSymLink(const std::filesystem::path& path);
47bool hasHardLink(const std::filesystem::path& path);
Adrien Béraud612b55b2023-05-29 10:42:04 -040048
49/**
50 * Read content of the directory.
51 * The result is a list of relative (to @param dir) paths of all entries
52 * in the directory, without "." and "..".
53 */
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040054std::vector<std::string> readDirectory(const std::filesystem::path& dir);
Adrien Béraud612b55b2023-05-29 10:42:04 -040055
56/**
57 * Read the full content of a file at path.
58 * If path is relative, it is appended to default_dir.
59 */
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040060std::vector<uint8_t> loadFile(const std::filesystem::path& path);
Adrien Béraud612b55b2023-05-29 10:42:04 -040061
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040062void saveFile(const std::filesystem::path& path, const uint8_t* data, size_t data_size, mode_t mode = 0644);
Adrien Béraud612b55b2023-05-29 10:42:04 -040063inline void
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040064saveFile(const std::filesystem::path& path, const std::vector<uint8_t>& data, mode_t mode = 0644)
Adrien Béraud612b55b2023-05-29 10:42:04 -040065{
66 saveFile(path, data.data(), data.size(), mode);
67}
68
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040069std::mutex& getFileLock(const std::filesystem::path& path);
Adrien Béraud612b55b2023-05-29 10:42:04 -040070
71/**
72 * Remove a file with optional erasing of content.
73 * Return the same value as std::remove().
74 */
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040075int remove(const std::filesystem::path& path, bool erase = false);
Adrien Béraud612b55b2023-05-29 10:42:04 -040076
77/**
78 * Prune given directory's content and remove it, symlinks are not followed.
79 * Return 0 if succeed, -1 if directory is not removed (content can be removed partially).
80 */
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040081int removeAll(const std::filesystem::path& path, bool erase = false);
Adrien Béraud612b55b2023-05-29 10:42:04 -040082
83/**
84 * Wrappers for fstream opening that will convert paths to wstring
85 * on windows
86 */
87void openStream(std::ifstream& file,
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040088 const std::filesystem::path& path,
Adrien Béraud612b55b2023-05-29 10:42:04 -040089 std::ios_base::openmode mode = std::ios_base::in);
90void openStream(std::ofstream& file,
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040091 const std::filesystem::path& path,
Adrien Béraud612b55b2023-05-29 10:42:04 -040092 std::ios_base::openmode mode = std::ios_base::out);
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040093std::ifstream ifstream(const std::filesystem::path& path, std::ios_base::openmode mode = std::ios_base::in);
94std::ofstream ofstream(const std::filesystem::path& path, std::ios_base::openmode mode = std::ios_base::out);
Adrien Béraud612b55b2023-05-29 10:42:04 -040095
Adrien Béraud612b55b2023-05-29 10:42:04 -040096/**
97 * Windows compatibility wrapper for checking read-only attribute
98 */
99int accessFile(const std::string& file, int mode);
100
Adrien Béraud612b55b2023-05-29 10:42:04 -0400101} // namespace fileutils
Sébastien Blin464bdff2023-07-19 08:02:53 -0400102} // namespace dhtnet