Debian Codegraph @

io/archive.h

raw symbols SCIP
1 #ifndef CPP_UTILITIES_ARCHIVE_H
2 #define CPP_UTILITIES_ARCHIVE_H
3
4 #include "../chrono/datetime.h"
5 #include "../global.h"
6
7 #include <functional>
8 #include <map>
9 #include <stdexcept>
10 #include <string>
11 #include <string_view>
12 #include <vector>
13
14 namespace CppUtilities {
15
16 /*!
17 * \class ArchiveException
18 * \brief The ArchiveException class is thrown by the various archiving-related
19 * functions of this library when a conversion error occurs.
20 */
21 class CPP_UTILITIES_EXPORT ArchiveException : public std::runtime_error {
22 public:
23 explicit ArchiveException() noexcept;
24 explicit ArchiveException(std::string_view what) noexcept;
25 ~ArchiveException() override;
26 };
27
28 /*!
29 * \brief Constructs a new ArchiveException.
30 */
31 inline ArchiveException::ArchiveException() noexcept
32 : std::runtime_error("unable to convert")
33 {
34 }
35
36 /*!
37 * \brief Constructs a new ArchiveException.
38 */
39 inline ArchiveException::ArchiveException(std::string_view what) noexcept
40 : std::runtime_error(what.data())
41 {
42 }
43
44 /*!
45 * \brief The ArchiveFileType enum specifies the type of a file within an archive.
46 */
47 enum class ArchiveFileType { Regular, Link };
48
49 /*!
50 * \brief The ArchiveFile class holds data about a file within an archive.
51 * \remarks
52 * The timezone of the timestamps is unfortunately unspecified at this point because libarchive
53 * does not provide timezone information (as functions like `archive_entry_mtime` are ambiguous
54 * about the timezone, see https://github.com/libarchive/libarchive/issues/945). Note that for
55 * Zip files the local timezone seems to be incorporated by libarchive (at least with version
56 * 3.8.4). So one can get the UTC time via e.g.
57 * `DateTime::fromTimeStamp(modificationTime.toTimeStamp())`. There is no guarantee that future
58 * versions of libarchive behave the same way.
59 */
60 struct CPP_UTILITIES_EXPORT ArchiveFile {
61 explicit ArchiveFile(
62 std::string &&name, std::string &&content, ArchiveFileType type, CppUtilities::DateTime creationTime, CppUtilities::DateTime modificationTime)
63 : name(name)
64 , content(content)
65 , creationTime(creationTime)
66 , modificationTime(modificationTime)
67 , type(type)
68 {
69 }
70 std::string name;
71 std::string content;
72 CppUtilities::DateTime creationTime;
73 CppUtilities::DateTime modificationTime;
74 ArchiveFileType type;
75 };
76
77 /// \brief A map of files extracted from an archive. Keys represent directories and values files within those directories.
78 using FileMap = std::map<std::string, std::vector<ArchiveFile>>;
79 /// \brief A function that is invoked for each file within an archive. If it returns true, the file is considered; otherwise the file is ignored.
80 using FilePredicate = std::function<bool(const char *, const char *, mode_t)>;
81 /// \brief A function that is invoked by the walk-through-functions to return a directory.
82 using DirectoryHandler = std::function<bool(std::string_view path)>;
83 /// \brief A function that is invoked by the walk-through-functions to return a file.
84 using FileHandler = std::function<bool(std::string_view path, ArchiveFile &&file)>;
85
86 CPP_UTILITIES_EXPORT FileMap extractFiles(std::string_view archivePath, const FilePredicate &isFileRelevant = FilePredicate());
87 CPP_UTILITIES_EXPORT void walkThroughArchive(std::string_view archivePath, const FilePredicate &isFileRelevant = FilePredicate(),
88 FileHandler &&fileHandler = FileHandler(), DirectoryHandler &&directoryHandler = DirectoryHandler());
89 CPP_UTILITIES_EXPORT FileMap extractFilesFromBuffer(
90 std::string_view archiveData, std::string_view archiveName, const FilePredicate &isFileRelevant = FilePredicate());
91 CPP_UTILITIES_EXPORT void walkThroughArchiveFromBuffer(std::string_view archiveData, std::string_view archiveName,
92 const FilePredicate &isFileRelevant = FilePredicate(), FileHandler &&fileHandler = FileHandler(),
93 DirectoryHandler &&directoryHandler = DirectoryHandler());
94
95 } // namespace CppUtilities
96
97 #endif // CPP_UTILITIES_ARCHIVE_H