aboutsummaryrefslogtreecommitdiff
path: root/paludis/repository_name_cache.cc
blob: 26ae537eb0f8c7fe19923449bdb2cc6d4f170166 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007, 2008, 2009, 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 "repository_name_cache.hh"
#include <paludis/repository.hh>
#include <paludis/util/log.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/set.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/wrapped_output_iterator.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/safe_ofstream.hh>
#include <paludis/util/safe_ifstream.hh>
#include <paludis/util/fs_iterator.hh>
#include <paludis/util/fs_stat.hh>
#include <paludis/util/fs_error.hh>
#include <unordered_map>
#include <memory>
#include <set>
#include <cstring>
#include <cerrno>

using namespace paludis;

namespace paludis
{
    typedef std::unordered_map<PackageNamePart, std::set<CategoryNamePart>, Hash<PackageNamePart> > NameCacheMap;

    template<>
    struct Imp<RepositoryNameCache>
    {
        mutable Mutex mutex;

        mutable bool usable;
        mutable FSPath location;
        const Repository * const repo;

        mutable NameCacheMap name_cache_map;
        mutable bool checked_name_cache_map;

        Imp(const FSPath & l, const Repository * const r) :
            usable(l != FSPath("/var/empty")),
            location(l == FSPath("/var/empty") ? l : l / stringify(r->name())),
            repo(r),
            checked_name_cache_map(false)
        {
        }

        NameCacheMap::iterator find(const PackageNamePart &) const;
        void update(const PackageNamePart & p, NameCacheMap::iterator r);
    };
}

NameCacheMap::iterator
Imp<RepositoryNameCache>::find(const PackageNamePart & p) const
{
    NameCacheMap::iterator r(name_cache_map.find(p));

    location = FSPath(stringify(location));

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

        if (! checked_name_cache_map)
        {
            if (location.stat().is_directory() && (location / "_VERSION_").stat().exists())
            {
                SafeIFStream vvf(location / "_VERSION_");
                std::string line;
                std::getline(vvf, line);
                if (line != "paludis-2")
                {
                    Log::get_instance()->message("repository.names_cache.unsupported", ll_warning, lc_context)
                        << "Names cache for '" << repo->name() << "' has version string '" << line
                        << "', which is not supported. Was it generated using a different Paludis version? Perhaps you need to regenerate "
                        "the cache using 'cave fix-cache'?";
                    usable = false;
                    return name_cache_map.end();
                }
                std::getline(vvf, line);
                if (line != stringify(repo->name()))
                {
                    Log::get_instance()->message("repository.names_cache.different", ll_warning, lc_context)
                        << "Names cache for '" << 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 name_cache_map.end();
                }
                checked_name_cache_map = true;
            }
            else if ((location.dirname() / "_VERSION_").stat().exists())
            {
                Log::get_instance()->message("repository.names_cache.old", ll_warning, lc_context)
                    << "Names cache for '" << repo->name() << "' does not exist at '" << location
                    << "', but a names cache exists at '" << 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 '" << location.dirname() <<
                    "' and then regenerate the cache.";
                usable = false;
                return name_cache_map.end();
            }
            else
            {
                Log::get_instance()->message("repository.names_cache.unversioned", ll_warning, lc_context)
                    << "Names cache for '" << 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. Perhaps you need to regenerate "
                    "the cache using 'cave fix-cache'?";
                usable = false;
                return name_cache_map.end();
            }
        }

        FSPath ff(location / stringify(p));
        if (ff.stat().exists())
        {
            SafeIFStream f(ff);
            std::string line;
            while (std::getline(f, line))
                r->second.insert(CategoryNamePart(line));
        }
    }

    return r;
}

void
Imp<RepositoryNameCache>::update(const PackageNamePart & p, NameCacheMap::iterator r)
{
    FSPath ff(location / stringify(p));
    if (r->second.empty() && ff.stat().exists())
    {
        try
        {
            ff.unlink();
        }
        catch (const FSError & e)
        {
            Log::get_instance()->message("repository.names_cache.unlink_failed", ll_warning, lc_context)
                << "Cannot unlink '" << ff << "': " << e.message() << " (" << e.what() << ")";
        }
        return;
    }

    try
    {
        SafeOFStream f(ff, -1, true);

        for (std::set<CategoryNamePart>::const_iterator it(r->second.begin()),
                 it_end(r->second.end()); it_end != it; ++it)
            f << *it << std::endl;
    }
    catch (const SafeOFStreamError & e)
    {
        Log::get_instance()->message("repository.names_cache.write_failed", ll_warning, lc_context)
            << "Cannot write '" << ff << "': '" << e.message() << "' (" << e.what() << ")";
        return;
    }
}

RepositoryNameCache::RepositoryNameCache(
        const FSPath & location,
        const Repository * const repo) :
    _imp(location, repo)
{
}

RepositoryNameCache::~RepositoryNameCache()
{
}

std::shared_ptr<const CategoryNamePartSet>
RepositoryNameCache::category_names_containing_package(const PackageNamePart & p) const
{
    Lock l(_imp->mutex);

    if (! usable())
        return std::shared_ptr<const CategoryNamePartSet>();

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

    std::shared_ptr<CategoryNamePartSet> result(std::make_shared<CategoryNamePartSet>());
    NameCacheMap::iterator r(_imp->find(p));
    if (_imp->name_cache_map.end() == r)
        return std::shared_ptr<const CategoryNamePartSet>();

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

void
RepositoryNameCache::regenerate_cache() const
{
    Lock l(_imp->mutex);

    if (_imp->location == FSPath("/var/empty"))
        return;

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

    if (_imp->location.stat().is_directory())
        for (FSIterator i(_imp->location, { fsio_inode_sort }), i_end ; i != i_end ; ++i)
            i->unlink();

    FSPath main_cache_dir(_imp->location.dirname());
    FSStat main_cache_dir_stat(main_cache_dir);
    if (! main_cache_dir_stat.exists())
        Log::get_instance()->message("repository.names_cache.no_dir", ll_warning, lc_context)
            << "Names cache directory '" << main_cache_dir << "' does not exist "
            << "(see the faq for why this directory will not be created automatically)";

    if (_imp->location.mkdir(main_cache_dir_stat.permissions(), { fspmkdo_ok_if_exists }))
        _imp->location.chmod(main_cache_dir_stat.permissions());

    std::unordered_map<std::string, std::string, Hash<std::string> > m;

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

    for (std::unordered_map<std::string, std::string, Hash<std::string> >::const_iterator e(m.begin()), e_end(m.end()) ;
            e != e_end ; ++e)
    {
        try
        {
            SafeOFStream f(_imp->location / stringify(e->first), -1, true);
            f << e->second;
        }
        catch (const SafeOFStreamError & ee)
        {
            Log::get_instance()->message("repository.names_cache.write_failed", ll_warning, lc_context)
                << "Cannot write to '" << _imp->location << "': '" << ee.message() << "' (" << ee.what() << ")";
            continue;
        }
    }

    try
    {
        SafeOFStream f(_imp->location / "_VERSION_", -1, true);;
        f << "paludis-2" << std::endl;
        f << _imp->repo->name() << std::endl;
    }
    catch (const SafeOFStreamError & e)
    {
        Log::get_instance()->message("repository.names_cache.write_failed", ll_warning, lc_context)
            << "Cannot write to '" << _imp->location << "': '" << e.message() << "' (" << e.what() << ")";
    }
}

void
RepositoryNameCache::add(const QualifiedPackageName & q)
{
    Lock l(_imp->mutex);

    if (! usable())
        return;

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

    std::shared_ptr<CategoryNamePartSet> result(std::make_shared<CategoryNamePartSet>());
    NameCacheMap::iterator r(_imp->find(q.package()));
    if (_imp->name_cache_map.end() == r)
        return;

    r->second.insert(q.category());
    _imp->update(q.package(), r);
}

void
RepositoryNameCache::remove(const QualifiedPackageName & q)
{
    Lock l(_imp->mutex);

    if (! usable())
        return;

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

    std::shared_ptr<CategoryNamePartSet> result(std::make_shared<CategoryNamePartSet>());
    NameCacheMap::iterator r(_imp->find(q.package()));
    if (_imp->name_cache_map.end() == r)
        return;

    r->second.erase(q.category());
    _imp->update(q.package(), r);
}

bool
RepositoryNameCache::usable() const
{
    return _imp->usable;
}