LCOV - code coverage report
Current view: top level - src - fileutils.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 3 3 100.0 %
Date: 2024-04-23 08:02:50 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 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             : std::filesystem::path get_config_dir(const char* pkg);
      49             : std::filesystem::path get_data_dir(const char* pkg);
      50             : std::filesystem::path get_cache_dir(const char* pkg);
      51             : 
      52             : const std::filesystem::path& get_home_dir();
      53             : const std::filesystem::path& get_config_dir();
      54             : const std::filesystem::path& get_data_dir();
      55             : const std::filesystem::path& get_cache_dir();
      56             : 
      57             : /**
      58             :  * Check directory existence and create it with given mode if it doesn't.
      59             :  * @param path to check, relative or absolute
      60             :  * @param dir last directory creation mode
      61             :  * @param parents default mode for all created directories except the last
      62             :  */
      63             : LIBJAMI_PUBLIC void set_program_dir(char* program_path); // public because bin/main.cpp uses it
      64             : std::string expand_path(const std::string& path);
      65             : 
      66             : bool isPathRelative(const std::filesystem::path& path);
      67             : 
      68             : /**
      69             :  * If path is contained in base, return the suffix, otherwise return the full path.
      70             :  * @param base must not finish with DIR_SEPARATOR_STR, can be empty
      71             :  * @param path the path
      72             :  */
      73             : std::string getCleanPath(const std::string& base, const std::string& path);
      74             : /**
      75             :  * If path is relative, it is appended to base.
      76             :  */
      77             : std::filesystem::path getFullPath(const std::filesystem::path& base,
      78             :                                   const std::filesystem::path& path);
      79             : 
      80             : bool createFileLink(const std::filesystem::path& src, const std::filesystem::path& dest, bool hard = false);
      81             : 
      82             : std::string_view getFileExtension(std::string_view filename);
      83             : 
      84             : bool isDirectoryWritable(const std::string& directory);
      85             : 
      86             : /**
      87             :  * Read the full content of a file at path.
      88             :  * If path is relative, it is appended to default_dir.
      89             :  */
      90             : std::vector<uint8_t> loadFile(const std::filesystem::path& path,
      91             :                               const std::filesystem::path& default_dir = {});
      92             : std::string loadTextFile(const std::filesystem::path& path,
      93             :                          const std::filesystem::path& default_dir = {});
      94             : 
      95             : void saveFile(const std::filesystem::path& path, const uint8_t* data, size_t data_size, mode_t mode = 0644);
      96             : inline void
      97        1568 : saveFile(const std::filesystem::path& path, const std::vector<uint8_t>& data, mode_t mode = 0644)
      98             : {
      99        1568 :     saveFile(path, data.data(), data.size(), mode);
     100        1568 : }
     101             : 
     102             : std::vector<uint8_t> loadCacheFile(const std::filesystem::path& path,
     103             :                                    std::chrono::system_clock::duration maxAge);
     104             : std::string loadCacheTextFile(const std::filesystem::path& path, std::chrono::system_clock::duration maxAge);
     105             : 
     106             : static constexpr auto ARCHIVE_AUTH_SCHEME_NONE = ""sv;
     107             : static constexpr auto ARCHIVE_AUTH_SCHEME_PASSWORD = "password"sv;
     108             : static constexpr auto ARCHIVE_AUTH_SCHEME_KEY = "key"sv;
     109             : 
     110             : struct ArchiveStorageData {
     111             :     std::vector<uint8_t> data;
     112             :     std::vector<uint8_t> salt;
     113             : };
     114             : ArchiveStorageData readArchive(const std::filesystem::path& path, std::string_view scheme, const std::string& pwd);
     115             : 
     116             : void writeArchive(const std::string& data,
     117             :                   const std::filesystem::path& path,
     118             :                   std::string_view scheme,
     119             :                   const std::string& password = {}, const std::vector<uint8_t>& password_salt = {});
     120             : 
     121             : int64_t size(const std::filesystem::path& path);
     122             : 
     123             : std::string sha3File(const std::filesystem::path& path);
     124             : std::string sha3sum(const std::vector<uint8_t>& buffer);
     125             : 
     126             : /**
     127             :  * Windows compatibility wrapper for checking read-only attribute
     128             :  */
     129             : int accessFile(const std::string& file, int mode);
     130             : 
     131             : /**
     132             :  * Return the last write time (epoch time) of a given file path (in seconds).
     133             :  */
     134             : uint64_t lastWriteTimeInSeconds(const std::filesystem::path& filePath);
     135             : 
     136             : } // namespace fileutils
     137             : } // namespace jami

Generated by: LCOV version 1.14