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

« back to all changes in this revision

Viewing changes to src/core/trust/click/index.cpp

  • Committer: thomas-voss
  • Date: 2014-08-21 08:07:29 UTC
  • Revision ID: thomas.voss@canonical.com-20140821080729-sihlhaifeibhicqt
Add an interface to installed click packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2014 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
 
 
19
#include <core/trust/click/index.h>
 
20
 
 
21
#include <core/trust/click/deb822.h>
 
22
 
 
23
#include <boost/filesystem.hpp>
 
24
#include <boost/property_tree/ini_parser.hpp>
 
25
#include <boost/property_tree/ptree.hpp>
 
26
 
 
27
// Constructs a new instance, querying filesystem path setups
 
28
click::Index::Index(const click::Index::Configuration& configuration)
 
29
{
 
30
    // Iterate over all known frameworks.
 
31
    boost::filesystem::directory_iterator it{configuration.paths->default_frameworks_config_dir()}, itE;
 
32
    for (; it != itE; ++it)
 
33
    {
 
34
        std::ifstream in{it->path().string().c_str()};
 
35
        auto kv = click::deb822::parse_from_stream(in);
 
36
 
 
37
        if (kv.count(click::Framework::Keys::base_name) == 0 || kv.count(click::Framework::Keys::base_version) == 0)
 
38
            continue;
 
39
 
 
40
        framework_registry->register_framework(Framework::Ptr
 
41
        {
 
42
            new Framework
 
43
            {
 
44
                kv.at(click::Framework::Keys::base_name),
 
45
                kv.at(click::Framework::Keys::base_version)
 
46
            }
 
47
        });
 
48
    }
 
49
 
 
50
    // Iterate over all configured databases.
 
51
    boost::filesystem::directory_iterator itd{configuration.paths->default_database_config_dir()}, itdE;
 
52
    for (; itd != itdE; ++itd)
 
53
    {
 
54
        boost::property_tree::ptree ptree;
 
55
        boost::property_tree::ini_parser::read_ini(itd->path().string(), ptree);
 
56
 
 
57
        auto root = ptree.get_child("Click Database").get<std::string>("root");
 
58
        databases.push_back(std::make_shared<click::FileSystemDatabase>(
 
59
            click::FileSystemDatabase::Configuration{root, framework_registry}
 
60
        ));
 
61
    }
 
62
}
 
63
 
 
64
// From Database
 
65
void click::Index::enumerate_packages(const click::Database::PackageEnumerator& enumerator) const
 
66
{
 
67
    for(auto database : databases)
 
68
    {
 
69
        database->enumerate_packages(enumerator);
 
70
    }
 
71
}
 
72
 
 
73
click::Package::Ptr click::Index::find_package_for_name(const std::string& name) const
 
74
{
 
75
    for(auto database : databases)
 
76
    {
 
77
        try
 
78
        {
 
79
            return database->find_package_for_name(name);
 
80
        } catch(...)
 
81
        {
 
82
        }
 
83
    }
 
84
    return click::Package::Ptr{};
 
85
}
 
86