Ring Daemon 16.0.0
Loading...
Searching...
No Matches
fileutils.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18#include "jami/def.h"
19
20#include <dhtnet/fileutils.h>
21
22#include <string>
23#include <vector>
24#include <chrono>
25#include <mutex>
26#include <cstdio>
27#include <ios>
28#include <filesystem>
29#include <string_view>
30
31#ifndef _WIN32
32#include <sys/stat.h> // mode_t
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#else
37#define mode_t unsigned
38#define DIR_SEPARATOR_STR "\\" // Directory separator string
39#define DIR_SEPARATOR_CH '\\' // Directory separator char
40#define DIR_SEPARATOR_STR_ESC "//*" // Escaped directory separator string
41#endif
42
43namespace jami {
44namespace fileutils {
45
46using namespace std::literals;
47
48std::filesystem::path get_config_dir(const char* pkg);
49std::filesystem::path get_data_dir(const char* pkg);
50std::filesystem::path get_cache_dir(const char* pkg);
51
52const std::filesystem::path& get_home_dir();
53const std::filesystem::path& get_config_dir();
54const std::filesystem::path& get_data_dir();
55const std::filesystem::path& get_cache_dir();
56
63LIBJAMI_PUBLIC void set_resource_dir_path(const std::filesystem::path& resourceDirPath);
64
69const std::filesystem::path& get_resource_dir_path();
70
76std::string expand_path(const std::string& path);
77
78bool isPathRelative(const std::filesystem::path& path);
79
85std::string getCleanPath(const std::string& base, const std::string& path);
89std::filesystem::path getFullPath(const std::filesystem::path& base,
90 const std::filesystem::path& path);
91
92bool createFileLink(const std::filesystem::path& src,
93 const std::filesystem::path& dest,
94 bool hard = false);
95
96std::string_view getFileExtension(std::string_view filename);
97
98bool isDirectoryWritable(const std::string& directory);
99
104std::vector<uint8_t> loadFile(const std::filesystem::path& path,
105 const std::filesystem::path& default_dir = {});
106std::string loadTextFile(const std::filesystem::path& path,
107 const std::filesystem::path& default_dir = {});
108
109void saveFile(const std::filesystem::path& path,
110 const uint8_t* data,
111 size_t data_size,
112 mode_t mode = 0644);
113inline void
114saveFile(const std::filesystem::path& path, const std::vector<uint8_t>& data, mode_t mode = 0644)
115{
116 saveFile(path, data.data(), data.size(), mode);
117}
118
119std::vector<uint8_t> loadCacheFile(const std::filesystem::path& path,
120 std::chrono::system_clock::duration maxAge);
121std::string loadCacheTextFile(const std::filesystem::path& path,
122 std::chrono::system_clock::duration maxAge);
123
124static constexpr auto ARCHIVE_AUTH_SCHEME_NONE = ""sv;
125static constexpr auto ARCHIVE_AUTH_SCHEME_PASSWORD = "password"sv;
126static constexpr auto ARCHIVE_AUTH_SCHEME_KEY = "key"sv;
127
129{
130 std::string data;
131 std::vector<uint8_t> salt;
132};
133ArchiveStorageData readArchive(const std::filesystem::path& path,
134 std::string_view scheme,
135 const std::string& pwd);
136
137bool writeArchive(const std::string& data,
138 const std::filesystem::path& path,
139 std::string_view scheme,
140 const std::string& password = {},
141 const std::vector<uint8_t>& password_salt = {});
142
143int64_t size(const std::filesystem::path& path);
144
145std::string sha3File(const std::filesystem::path& path);
146std::string sha3sum(const std::vector<uint8_t>& buffer);
147
151int accessFile(const std::string& file, int mode);
152
156uint64_t lastWriteTimeInSeconds(const std::filesystem::path& filePath);
157
158} // namespace fileutils
159} // namespace jami
#define LIBJAMI_PUBLIC
Definition def.h:42
const std::filesystem::path & get_data_dir()
void set_resource_dir_path(const std::filesystem::path &resourceDirPath)
Set the program's resource directory path.
std::vector< uint8_t > loadCacheFile(const std::filesystem::path &path, std::chrono::system_clock::duration maxAge)
static constexpr auto ARCHIVE_AUTH_SCHEME_PASSWORD
Definition fileutils.h:125
int accessFile(const std::string &file, int mode)
Windows compatibility wrapper for checking read-only attribute.
std::string sha3File(const std::filesystem::path &path)
bool createFileLink(const std::filesystem::path &linkFile, const std::filesystem::path &target, bool hard)
void saveFile(const std::filesystem::path &path, const uint8_t *data, size_t data_size, mode_t UNUSED mode)
static constexpr auto ARCHIVE_AUTH_SCHEME_NONE
Definition fileutils.h:124
std::string getCleanPath(const std::string &base, const std::string &path)
If path is contained in base, return the suffix, otherwise return the full path.
int64_t size(const std::filesystem::path &path)
ArchiveStorageData readArchive(const std::filesystem::path &path, std::string_view scheme, const std::string &pwd)
const std::filesystem::path & get_config_dir()
std::string loadCacheTextFile(const std::filesystem::path &path, std::chrono::system_clock::duration maxAge)
std::vector< uint8_t > loadFile(const std::filesystem::path &path, const std::filesystem::path &default_dir)
Read the full content of a file at path.
uint64_t lastWriteTimeInSeconds(const std::filesystem::path &filePath)
Return the last write time (epoch time) of a given file path (in seconds).
static constexpr auto ARCHIVE_AUTH_SCHEME_KEY
Definition fileutils.h:126
bool isPathRelative(const std::filesystem::path &path)
std::string sha3sum(const uint8_t *data, size_t size)
std::string_view getFileExtension(std::string_view filename)
const std::filesystem::path & get_cache_dir()
bool isDirectoryWritable(const std::string &directory)
std::filesystem::path getFullPath(const std::filesystem::path &base, const std::filesystem::path &path)
If path is relative, it is appended to base.
bool writeArchive(const std::string &archive_str, const std::filesystem::path &path, std::string_view scheme, const std::string &password, const std::vector< uint8_t > &password_salt)
const std::filesystem::path & get_resource_dir_path()
Get the resource directory path that was set with set_resource_dir_path.
std::string expand_path(const std::string &path)
Expand the given path.
const std::filesystem::path & get_home_dir()
std::string loadTextFile(const std::filesystem::path &path, const std::filesystem::path &default_dir)
void emitSignal(Args... args)
Definition ring_signal.h:64
std::vector< uint8_t > salt
Definition fileutils.h:131