summaryrefslogtreecommitdiff
path: root/backend/pkgtools.cpp
blob: f6c9b00133f3aee2ba8a5ace8cf411cffd1ae999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */
module;

#include "config.h"
#include <curl/curl.h>
#include <sqlite3.h>
#include <cstdint>
#include <glib-object.h>
#include <string>
#include <regex>
#include <forward_list>
#include <filesystem>

export module katja.pkgtools;

import katja.utils;

export namespace katja
{
using cache_entry = std::pair<std::string, std::filesystem::path>;

class Pkgtools
{
public:
    /**
     * Repository name.
     */
    const std::string name;

    /**
     * Repository mirror.
     */
    const std::string mirror;

    /**
     * Repository order.
     */
    const std::uint8_t order;

    /**
     * katja::Pkgtools::Pkgtools:
     * @name: Repository name.
     * @mirror: Repository mirror.
     * @order: Repository order.
     */
    Pkgtools(const std::string& name, const std::string& mirror, std::uint8_t order)
        : name(name), mirror(mirror), order(order)
    {
    }

    /**
     * katja::Pkgtools:is_blacklisted:
     * @pkg: Package name to check for.
     *
     * Checks whether a package is blacklisted.
     *
     * Returns: %TRUE if the package is blacklisted, %FALSE otherwise.
     **/
    bool is_blacklisted(const std::string_view& pkg) const noexcept
    {
        return std::regex_match(std::cbegin(pkg), std::cend(pkg), this->blacklist);
    }

    virtual ~Pkgtools() noexcept
    {
    }

    /**
     * katja::Pkgtools::download:
     * @job_data: A #JobData.
     * @dest_dir_name: Destination directory.
     * @pkg_name: Package name.
     *
     * Download a package.
     *
     * Returns: %TRUE on success, %FALSE otherwise.
     **/
    bool download(JobData *job_data, const char *dest_dir_name, const char *pkg_name) noexcept
    {
        std::filesystem::path dest_filename;
        std::string source_url;
        bool ret = false;
        sqlite3_stmt *statement = nullptr;
        CURL *curl = nullptr;

        if ((sqlite3_prepare_v2(job_data->db,
                                "SELECT location, (full_name || '.' || ext) FROM pkglist "
                                "WHERE name LIKE @name AND repo_order = @repo_order",
                                -1,
                                &statement,
                                nullptr) != SQLITE_OK))
        {
            return false;
        }
        sqlite3_bind_text(statement, 1, pkg_name, -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(statement, 2, this->order);

        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            dest_filename = std::filesystem::path(dest_dir_name)
                / reinterpret_cast<const char *>(sqlite3_column_text(statement, 1));
            source_url = this->mirror
                + reinterpret_cast<const char *>(sqlite3_column_text(statement, 0))
                + "/"
                + reinterpret_cast<const char *>(sqlite3_column_text(statement, 1));

            ret = std::filesystem::exists(dest_filename)
                || get_file(&curl, source_url.c_str(), dest_filename.native().c_str()) == CURLE_OK;

            if (curl)
            {
                curl_easy_cleanup(curl);
            }
        }
        sqlite3_finalize(statement);

        return ret;
    }

    /**
     * katja::Pkgtools::install:
     * @job_data: A #JobData.
     * @pkg_name: Package name.
     *
     * Install a package.
     **/
    void install(JobData *job_data, const char *pkg_name) noexcept
    {
        std::filesystem::path pkg_filename;
        sqlite3_stmt *statement = nullptr;

        if ((sqlite3_prepare_v2(job_data->db,
                                "SELECT (full_name || '.' || ext) FROM pkglist "
                                "WHERE name LIKE @name AND repo_order = @repo_order",
                                -1,
                                &statement,
                                nullptr) != SQLITE_OK))
        {
            return;
        }

        sqlite3_bind_text(statement, 1, pkg_name, -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(statement, 2, this->order);

        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            pkg_filename = std::filesystem::path(LOCALSTATEDIR)
                / "cache" / "katja" / "downloads"
                / reinterpret_cast<const char*>(sqlite3_column_text(statement, 0));

            std::string cmd_line = "/sbin/upgradepkg --install-new " + pkg_filename.native();
            g_spawn_command_line_sync(cmd_line.c_str(), nullptr, nullptr, nullptr, nullptr);
        }
        sqlite3_finalize(statement);
    }

    virtual std::forward_list<cache_entry> collect_cache_info(const std::filesystem::path& tmpl) noexcept = 0;
    virtual void generate_cache(JobData *job_data, const std::filesystem::path& tmpl) noexcept = 0;

protected:
    std::regex blacklist;
};

/**
 * katja::cmp_repo:
 **/
template<class InputIt>
InputIt cmp_repo(InputIt first, InputIt last, const char *a)
{
    return std::find_if(first, last, [a](std::unique_ptr<Pkgtools>& repo) {
        return repo->name == a;
    });
}
}