aboutsummaryrefslogtreecommitdiff
path: root/paludis/repository_name_cache.cc
blob: 28ff6f4d066cc93df11e9ff3d9c139d7ba645e1a (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * 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 "repository_name_cache.hh"
#include <paludis/repository.hh>
#include <paludis/hashed_containers.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/tr1_memory.hh>
#include <paludis/util/log.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/set.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <libwrapiter/libwrapiter_forward_iterator.hh>
#include <libwrapiter/libwrapiter_output_iterator.hh>
#include <list>
#include <fstream>

using namespace paludis;

namespace paludis
{
    typedef MakeHashedMap<PackageNamePart, std::list<CategoryNamePart> >::Type NameCacheMap;

    template<>
    struct Implementation<RepositoryNameCache>
    {
        mutable FSEntry location;
        const Repository * const repo;

        mutable NameCacheMap name_cache_map;
        mutable bool checked_name_cache_map;

        Implementation(const FSEntry & l, const Repository * const r) :
            location(l == FSEntry("/var/empty") ? l : l / stringify(r->name())),
            repo(r),
            checked_name_cache_map(false)
        {
        }
    };
}

RepositoryNameCache::RepositoryNameCache(
        const FSEntry & location,
        const Repository * const repo) :
    PrivateImplementationPattern<RepositoryNameCache>(new Implementation<RepositoryNameCache>(
                location, repo)),
    _usable(_imp->location != FSEntry("/var/empty"))
{
}

RepositoryNameCache::~RepositoryNameCache()
{
}

tr1::shared_ptr<const CategoryNamePartSet>
RepositoryNameCache::category_names_containing_package(const PackageNamePart & p) const
{
    if (! usable())
        return tr1::shared_ptr<const CategoryNamePartSet>();

    Context context("When using name cache at '" + stringify(_imp->location) + "':");

    tr1::shared_ptr<CategoryNamePartSet> result(new CategoryNamePartSet);
    NameCacheMap::iterator r(_imp->name_cache_map.find(p));

    _imp->location = FSEntry(stringify(_imp->location));

    if (_imp->name_cache_map.end() == r)
    {
        r = _imp->name_cache_map.insert(std::make_pair(p, std::list<CategoryNamePart>())).first;

        if (! _imp->checked_name_cache_map)
        {
            if (_imp->location.is_directory() && (_imp->location / "_VERSION_").exists())
            {
                std::ifstream vvf(stringify(_imp->location / "_VERSION_").c_str());
                std::string line;
                std::getline(vvf, line);
                if (line != "paludis-2")
                {
                    Log::get_instance()->message(ll_warning, lc_context, "Names cache for '" + stringify(_imp->repo->name())
                            + "' has version string '" + line + "', which is not supported. Was it generated using "
                            "a different Paludis version?");
                    _usable = false;
                    return tr1::shared_ptr<const CategoryNamePartSet>();
                }
                std::getline(vvf, line);
                if (line != stringify(_imp->repo->name()))
                {
                    Log::get_instance()->message(ll_warning, lc_context, "Names cache for '" + stringify(_imp->repo->name())
                            + "' was generated for repository '" + line + "', so it cannot be used. You must not "
                            "have multiple name caches at the same location.");
                    _usable = false;
                    return tr1::shared_ptr<const CategoryNamePartSet>();
                }
                _imp->checked_name_cache_map = true;
            }
            else if ((_imp->location.dirname() / "_VERSION_").exists())
            {
                Log::get_instance()->message(ll_warning, lc_context, "Names cache for '" + stringify(_imp->repo->name())
                        + "' does not exist at '" + stringify(_imp->location) + "', but a names cache exists at '" +
                        stringify(_imp->location.dirname()) + "'. This was probably generated by a Paludis version "
                        "older than 0.18.0. The names cache now automatically appends the repository name to the "
                        "directory. You probably want to manually remove '" + stringify(_imp->location.dirname()) +
                        "' and then regenerate the cache.");
                _usable = false;
                return tr1::shared_ptr<const CategoryNamePartSet>();
            }
            else
            {
                Log::get_instance()->message(ll_warning, lc_context, "Names cache for '" + stringify(_imp->repo->name())
                        + "' has no version information, so cannot be used. Either it was generated using "
                        "an older Paludis version or it has not yet been generated.");
                _usable = false;
                return tr1::shared_ptr<const CategoryNamePartSet>();
            }
        }

        FSEntry ff(_imp->location / stringify(p));
        if (ff.exists())
        {
            std::ifstream f(stringify(ff).c_str());
            if (! f)
                Log::get_instance()->message(ll_warning, lc_context, "Cannot read '" + stringify(ff) + "'");
            std::string line;
            while (std::getline(f, line))
                r->second.push_back(CategoryNamePart(line));
        }
    }

    std::copy(r->second.begin(), r->second.end(), result->inserter());

    return result;
}

void
RepositoryNameCache::regenerate_cache() const
{
    if (_imp->location == FSEntry("/var/empty"))
        return;

    Context context("When generating repository names cache at '"
            + stringify(_imp->location) + "':");

    if (_imp->location.is_directory())
        for (DirIterator i(_imp->location, true), i_end ; i != i_end ; ++i)
            FSEntry(*i).unlink();

    _imp->location.dirname().mkdir();
    if (_imp->location.exists() && ! _imp->location.is_directory())
        FSEntry(_imp->location).unlink();
    FSEntry(_imp->location).mkdir();

    MakeHashedMap<std::string, std::string>::Type m;

    tr1::shared_ptr<const CategoryNamePartSet> cats(_imp->repo->category_names());
    for (CategoryNamePartSet::Iterator c(cats->begin()), c_end(cats->end()) ;
            c != c_end ; ++c)
    {
        tr1::shared_ptr<const QualifiedPackageNameSet> pkgs(_imp->repo->package_names(*c));
        for (QualifiedPackageNameSet::Iterator p(pkgs->begin()), p_end(pkgs->end()) ;
                p != p_end ; ++p)
            m[stringify(p->package)].append(stringify(*c) + "\n");
    }

    for (MakeHashedMap<std::string, std::string>::Type::const_iterator e(m.begin()), e_end(m.end()) ;
            e != e_end ; ++e)
    {
        std::ofstream f(stringify(_imp->location / stringify(e->first)).c_str());
        if (! f)
        {
            Log::get_instance()->message(ll_warning, lc_context, "Cannot write to '"
                    + stringify(_imp->location) + "'");
            continue;
        }
        f << e->second;
    }

    std::ofstream f(stringify(_imp->location / "_VERSION_").c_str());
    if (f)
    {
        f << "paludis-2" << std::endl;
        f << _imp->repo->name() << std::endl;
    }
    else
        Log::get_instance()->message(ll_warning, lc_context, "Cannot write to '"
                + stringify(_imp->location) + "'");
}