LCOV - code coverage report
Current view: top level - src - fileutils.h (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 100.0 % 3 3
Test Date: 2026-09-13 09:08:58 Functions: 100.0 % 1 1

            Line data    Source code
       1              : /*
       2              :  *  Copyright (C) 2004-2026 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              : 
      43              : namespace jami {
      44              : namespace fileutils {
      45              : 
      46              : using namespace std::literals;
      47              : 
      48              : constexpr size_t MAX_EXTENSION_SIZE = 7;
      49              : 
      50              : std::filesystem::path get_config_dir(const char* pkg);
      51              : std::filesystem::path get_data_dir(const char* pkg);
      52              : std::filesystem::path get_cache_dir(const char* pkg);
      53              : 
      54              : const std::filesystem::path& get_home_dir();
      55              : const std::filesystem::path& get_config_dir();
      56              : const std::filesystem::path& get_data_dir();
      57              : const std::filesystem::path& get_cache_dir();
      58              : 
      59              : /**
      60              :  * Set the program's resource directory path. This is used for clients that may be installed in
      61              :  * different locations and are deployed with ringtones and other resources in an application
      62              :  * relative directory.
      63              :  * @param resource_dir_path The path to the ringtone directory.
      64              :  */
      65              : LIBJAMI_PUBLIC void set_resource_dir_path(const std::filesystem::path& resourceDirPath);
      66              : 
      67              : /**
      68              :  * Get the resource directory path that was set with set_resource_dir_path.
      69              :  * @return The resource directory path.
      70              :  */
      71              : const std::filesystem::path& get_resource_dir_path();
      72              : 
      73              : /**
      74              :  * Expand the given path.
      75              :  * @param path The path to be expanded.
      76              :  * @return The expanded path as a string.
      77              :  */
      78              : std::string expand_path(const std::string& path);
      79              : 
      80              : bool isPathRelative(const std::filesystem::path& path);
      81              : 
      82              : /**
      83              :  * If path is contained in base, return the suffix, otherwise return the full path.
      84              :  * @param base must not finish with DIR_SEPARATOR_STR, can be empty
      85              :  * @param path the path
      86              :  */
      87              : std::string getCleanPath(const std::string& base, const std::string& path);
      88              : /**
      89              :  * If path is relative, it is appended to base.
      90              :  */
      91              : std::filesystem::path getFullPath(const std::filesystem::path& base, const std::filesystem::path& path);
      92              : 
      93              : bool createFileLink(const std::filesystem::path& src, const std::filesystem::path& dest, bool hard = false);
      94              : 
      95              : std::string_view getFileExtension(std::string_view filename);
      96              : 
      97              : bool isDirectoryWritable(const std::string& directory);
      98              : 
      99              : /**
     100              :  * Read the full content of a file at path.
     101              :  * If path is relative, it is appended to default_dir.
     102              :  */
     103              : std::vector<uint8_t> loadFile(const std::filesystem::path& path, const std::filesystem::path& default_dir = {});
     104              : std::string loadTextFile(const std::filesystem::path& path, const std::filesystem::path& default_dir = {});
     105              : 
     106              : void saveFile(const std::filesystem::path& path, const uint8_t* data, size_t data_size, mode_t mode = 0644);
     107              : inline void
     108         1864 : saveFile(const std::filesystem::path& path, const std::vector<uint8_t>& data, mode_t mode = 0644)
     109              : {
     110         1864 :     saveFile(path, data.data(), data.size(), mode);
     111         1864 : }
     112              : 
     113              : std::vector<uint8_t> loadCacheFile(const std::filesystem::path& path, std::chrono::system_clock::duration maxAge);
     114              : std::string loadCacheTextFile(const std::filesystem::path& path, std::chrono::system_clock::duration maxAge);
     115              : 
     116              : static constexpr auto ARCHIVE_AUTH_SCHEME_NONE = ""sv;
     117              : static constexpr auto ARCHIVE_AUTH_SCHEME_PASSWORD = "password"sv;
     118              : static constexpr auto ARCHIVE_AUTH_SCHEME_KEY = "key"sv;
     119              : 
     120              : struct ArchiveStorageData
     121              : {
     122              :     std::string data;
     123              :     std::vector<uint8_t> salt;
     124              : };
     125              : ArchiveStorageData readArchive(const std::filesystem::path& path, std::string_view scheme, const std::string& pwd);
     126              : 
     127              : bool writeArchive(const std::string& data,
     128              :                   const std::filesystem::path& path,
     129              :                   std::string_view scheme,
     130              :                   const std::string& password = {},
     131              :                   const std::vector<uint8_t>& password_salt = {});
     132              : 
     133              : std::string sha3File(const std::filesystem::path& path);
     134              : std::string sha3sum(const std::vector<uint8_t>& buffer);
     135              : 
     136              : /**
     137              :  * Windows compatibility wrapper for checking read-only attribute
     138              :  */
     139              : int accessFile(const std::string& file, int mode);
     140              : 
     141              : /**
     142              :  * Return the last write time (epoch time) of a given file path (in seconds).
     143              :  */
     144              : uint64_t lastWriteTimeInSeconds(const std::filesystem::path& filePath);
     145              : 
     146              : std::string getOrCreateLocalDeviceId();
     147              : 
     148              : } // namespace fileutils
     149              : } // namespace jami
        

Generated by: LCOV version 2.0-1