aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/metadata_xml.cc
blob: 73d22ba638b0053d011b923f9ad54ced7e025a7e (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2008, 2010, 2011 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis 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 General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/repositories/e/metadata_xml.hh>
#include <paludis/repositories/e/xml_things_handle.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/singleton-impl.hh>
#include <paludis/util/map-impl.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/log.hh>
#include <paludis/util/fs_path.hh>
#include <paludis/util/fs_stat.hh>
#include <paludis/literal_metadata_key.hh>
#include <paludis/choice.hh>
#include <unordered_map>

using namespace paludis;
using namespace paludis::erepository;

typedef std::unordered_map<FSPath, std::shared_ptr<MetadataXML>, Hash<FSPath> > Store;

namespace paludis
{
    template <>
    struct Imp<MetadataXMLPool>
    {
        mutable Mutex mutex;
        mutable Store store;
    };
}

MetadataXMLPool::MetadataXMLPool() :
    _imp()
{
}

MetadataXMLPool::~MetadataXMLPool()
{
}

const std::shared_ptr<const MetadataXML>
MetadataXMLPool::metadata_if_exists(const FSPath & f) const
{
    Context context("When handling metadata.xml file '" + stringify(f) + "':");

    FSPath f_real(f.realpath_if_exists());
    Lock lock(_imp->mutex);
    Store::const_iterator i(_imp->store.find(f_real));
    if (i != _imp->store.end())
        return i->second;
    else
    {
        std::shared_ptr<MetadataXML> metadata_xml;
        if (f_real.stat().is_regular_file_or_symlink_to_regular_file())
        {
            try
            {
                if (XMLThingsHandle::get_instance()->create_metadata_xml_from_xml_file())
                    metadata_xml = XMLThingsHandle::get_instance()->create_metadata_xml_from_xml_file()(f);
            }
            catch (const Exception & e)
            {
                Log::get_instance()->message("e.metadata_xml.bad", ll_warning, lc_context) << "Got exception '"
                    << e.message() << "' (" << e.what() << "), ignoring metadata.xml file '" << f_real << "'";
            }
        }
        return _imp->store.insert(std::make_pair(f_real, metadata_xml)).first->second;
    }
}

namespace paludis
{
    template class PALUDIS_VISIBLE Map<ChoiceNameWithPrefix, std::string>;
    template class Pimp<MetadataXMLPool>;
    template class Singleton<MetadataXMLPool>;
}