~thomas-voss/trust-store/add-click-based-package-name-query

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
/*
 * Copyright © 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#include <core/trust/click/index.h>

#include <core/trust/click/deb822.h>

#include <boost/filesystem.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>

// Constructs a new instance, querying filesystem path setups
click::Index::Index(const click::Index::Configuration& configuration)
{
    // Iterate over all known frameworks.
    boost::filesystem::directory_iterator it{configuration.paths->default_frameworks_config_dir()}, itE;
    for (; it != itE; ++it)
    {
        std::ifstream in{it->path().string().c_str()};
        auto kv = click::deb822::parse_from_stream(in);

        if (kv.count(click::Framework::Keys::base_name) == 0 || kv.count(click::Framework::Keys::base_version) == 0)
            continue;

        framework_registry->register_framework(Framework::Ptr
        {
            new Framework
            {
                kv.at(click::Framework::Keys::base_name),
                kv.at(click::Framework::Keys::base_version)
            }
        });
    }

    // Iterate over all configured databases.
    boost::filesystem::directory_iterator itd{configuration.paths->default_database_config_dir()}, itdE;
    for (; itd != itdE; ++itd)
    {
        boost::property_tree::ptree ptree;
        boost::property_tree::ini_parser::read_ini(itd->path().string(), ptree);

        auto root = ptree.get_child("Click Database").get<std::string>("root");
        databases.push_back(std::make_shared<click::FileSystemDatabase>(
            click::FileSystemDatabase::Configuration{root, framework_registry}
        ));
    }
}

// From Database
void click::Index::enumerate_packages(const click::Database::PackageEnumerator& enumerator) const
{
    for(auto database : databases)
    {
        database->enumerate_packages(enumerator);
    }
}

click::Package::Ptr click::Index::find_package_for_name(const std::string& name) const
{
    for(auto database : databases)
    {
        try
        {
            return database->find_package_for_name(name);
        } catch(...)
        {
        }
    }
    return click::Package::Ptr{};
}