aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/accounts/accounts_repository_store.cc
blob: fb782e3168233c23f03dd6ba7911a7331b6f9755 (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
321
322
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 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 <paludis/repositories/accounts/accounts_repository_store.hh>
#include <paludis/repositories/accounts/accounts_repository.hh>
#include <paludis/repositories/accounts/accounts_id.hh>
#include <paludis/repositories/accounts/installed_accounts_id.hh>
#include <paludis/repositories/accounts/accounts_exceptions.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/set.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/log.hh>
#include <paludis/util/visitor_cast.hh>
#include <paludis/util/fs_iterator.hh>
#include <paludis/util/is_file_with_extension.hh>
#include <paludis/util/options.hh>
#include <paludis/util/strip.hh>
#include <paludis/util/fs_stat.hh>
#include <paludis/name.hh>
#include <paludis/package_database.hh>
#include <paludis/metadata_key.hh>
#include <paludis/environment.hh>
#include <paludis/literal_metadata_key.hh>
#include <functional>
#include <unordered_map>
#include <algorithm>
#include <set>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>

using namespace paludis;
using namespace paludis::accounts_repository;

typedef std::unordered_map<CategoryNamePart,
        std::shared_ptr<QualifiedPackageNameSet>,
        Hash<CategoryNamePart> > PackageNames;

typedef std::unordered_map<QualifiedPackageName,
        std::shared_ptr<PackageIDSequence>,
        Hash<QualifiedPackageName> > IDs;

namespace paludis
{
    template <>
    struct Imp<AccountsRepositoryStore>
    {
        const Environment * const env;
        const RepositoryName repository_name;
        const bool installed;

        std::shared_ptr<CategoryNamePartSet> categories;
        mutable PackageNames package_names;
        mutable IDs ids;

        Imp(const Environment * const e, const RepositoryName & r, const bool i) :
            env(e),
            repository_name(r),
            installed(i),
            categories(std::make_shared<CategoryNamePartSet>())
        {
            categories->insert(CategoryNamePart("user"));
            categories->insert(CategoryNamePart("group"));
        }
    };
}

AccountsRepositoryStore::AccountsRepositoryStore(
        const Environment * const env,
        const RepositoryName & r,
        const bool installed) :
    _imp(env, r, installed)
{
    _load(r);
}

AccountsRepositoryStore::~AccountsRepositoryStore()
{
}

void
AccountsRepositoryStore::_load(const RepositoryName & repository_name)
{
    Context context("When loading data for AccountsRepository:");

    for (PackageDatabase::RepositoryConstIterator r(_imp->env->package_database()->begin_repositories()),
            r_end(_imp->env->package_database()->end_repositories()) ;
            r != r_end ; ++r)
    {
        Context r_context("When loading data for repository '" + stringify((*r)->name()) + ":");

        Repository::MetadataConstIterator k_iter((*r)->find_metadata("accounts_repository_data_location"));
        if (k_iter == (*r)->end_metadata())
        {
            Log::get_instance()->message("accounts.no_key_from_repository", ll_debug, lc_context) <<
                "Repository " << (*r)->name() << " defines no accounts_repository_data_location key";
            continue;
        }

        const MetadataValueKey<FSPath> * k(visitor_cast<const MetadataValueKey<FSPath> >(**k_iter));
        if (! k)
        {
            Log::get_instance()->message("accounts.bad_key_from_repository", ll_warning, lc_context) <<
                "Repository " << (*r)->name() << " defines an accounts_repository_data_location key, but it is not an FSPath key";
            continue;
        }

        FSPath dir(k->value());
        if (! dir.stat().is_directory_or_symlink_to_directory())
        {
            Log::get_instance()->message("accounts.empty_key_from_repository", ll_warning, lc_context) <<
                "Repository " << (*r)->name() << " has accounts_repository_data_location " << dir << ", but this is not a directory";
            continue;
        }

        std::shared_ptr<Set<std::string> > r_set(std::make_shared<Set<std::string>>());
        r_set->insert(stringify((*r)->name()));
        std::shared_ptr<LiteralMetadataStringSetKey> r_key(std::make_shared<LiteralMetadataStringSetKey>("defined_by", "Defined by repository", mkt_internal, r_set));
        _load_one(repository_name, r_key, dir);
    }
}

void
AccountsRepositoryStore::_load_one(
        const RepositoryName & repository_name,
        const std::shared_ptr<const MetadataCollectionKey<Set<std::string> > > & from_repo,
        const FSPath & dir)
{
    Context context("When loading accounts from directory '" + stringify(dir) + "':");

    FSPath users_dir(dir / "users");
    if (users_dir.stat().is_directory_or_symlink_to_directory())
        _load_one_users(repository_name, from_repo, users_dir);
    else
        Log::get_instance()->message("accounts.no_users", ll_debug, lc_context) <<
            "Repository " << repository_name << " has accounts_repository_data_location " << dir << ", but no users subdirectory";

    FSPath groups_dir(dir / "groups");
    if (groups_dir.stat().is_directory_or_symlink_to_directory())
        _load_one_groups(repository_name, from_repo, groups_dir);
    else
        Log::get_instance()->message("accounts.no_groups", ll_debug, lc_context) <<
            "Repository " << repository_name << " has accounts_repository_data_location " << dir << ", but no groups subdirectory";

}

void
AccountsRepositoryStore::_load_one_users(
        const RepositoryName & repository_name,
        const std::shared_ptr<const MetadataCollectionKey<Set<std::string> > > & from_repo,
        const FSPath & dir)
{
    for (FSIterator d(dir, { fsio_inode_sort }), d_end ; d != d_end ; ++d)
        if (is_file_with_extension(*d, ".conf", { }))
            _load_one_user(repository_name, from_repo, *d);
        else
            Log::get_instance()->message("accounts.unknown_file", ll_debug, lc_context) <<
                "Don't know what to do with '" << *d << "'";
}

void
AccountsRepositoryStore::_load_one_user(
        const RepositoryName & repository_name,
        const std::shared_ptr<const MetadataCollectionKey<Set<std::string> > > & from_repo,
        const FSPath & filename)
{
    CategoryNamePart cat("user");
    PackageNamePart pkg(strip_trailing_string(filename.basename(), ".conf"));
    QualifiedPackageName qpn(cat + pkg);
    std::string username(stringify(pkg));

    bool masked(false);

    if (_imp->installed)
    {
        if (! getpwnam(username.c_str()))
            return;
    }
    else
    {
        if (getpwnam(username.c_str()))
            masked = true;
    }

    PackageNames::iterator p(_imp->package_names.find(cat));
    if (p == _imp->package_names.end())
        p = _imp->package_names.insert(std::make_pair(cat, std::make_shared<QualifiedPackageNameSet>())).first;

    p->second->insert(qpn);

    IDs::iterator q(_imp->ids.find(qpn));
    if (q == _imp->ids.end())
        q = _imp->ids.insert(std::make_pair(qpn, std::make_shared<PackageIDSequence>())).first;
    else
        q->second = std::make_shared<PackageIDSequence>();

    if (_imp->installed)
        q->second->push_back(std::make_shared<InstalledAccountsID>(_imp->env, qpn, repository_name, true));
    else
        q->second->push_back(std::make_shared<AccountsID>(_imp->env, qpn, repository_name, from_repo, filename, true, masked));
}

void
AccountsRepositoryStore::_load_one_groups(
        const RepositoryName & repository_name,
        const std::shared_ptr<const MetadataCollectionKey<Set<std::string> > > & from_repo,
        const FSPath & dir)
{
    for (FSIterator d(dir, { fsio_inode_sort }), d_end ; d != d_end ; ++d)
        if (is_file_with_extension(*d, ".conf", { }))
            _load_one_group(repository_name, from_repo, *d);
        else
            Log::get_instance()->message("accounts.unknown_file", ll_debug, lc_context) <<
                "Don't know what to do with '" << *d << "'";
}

void
AccountsRepositoryStore::_load_one_group(
        const RepositoryName & repository_name,
        const std::shared_ptr<const MetadataCollectionKey<Set<std::string> > > & from_repo,
        const FSPath & filename)
{
    CategoryNamePart cat("group");
    PackageNamePart pkg(strip_trailing_string(filename.basename(), ".conf"));
    QualifiedPackageName qpn(cat + pkg);
    std::string groupname(stringify(pkg));

    bool masked(false);

    if (_imp->installed)
    {
        if (! getgrnam(groupname.c_str()))
            return;
    }
    else
    {
        if (getgrnam(groupname.c_str()))
            masked = true;
    }

    PackageNames::iterator p(_imp->package_names.find(cat));
    if (p == _imp->package_names.end())
        p = _imp->package_names.insert(std::make_pair(cat, std::make_shared<QualifiedPackageNameSet>())).first;

    p->second->insert(qpn);

    IDs::iterator q(_imp->ids.find(qpn));
    if (q == _imp->ids.end())
        q = _imp->ids.insert(std::make_pair(qpn, std::make_shared<PackageIDSequence>())).first;
    else
        q->second = std::make_shared<PackageIDSequence>();

    if (_imp->installed)
        q->second->push_back(std::make_shared<InstalledAccountsID>(_imp->env, qpn, repository_name, false));
    else
        q->second->push_back(std::make_shared<AccountsID>(_imp->env, qpn, repository_name, from_repo, filename, false, masked));
}

bool
AccountsRepositoryStore::has_category_named(const CategoryNamePart & c) const
{
    return _imp->categories->end() != _imp->categories->find(c);
}

bool
AccountsRepositoryStore::has_package_named(const QualifiedPackageName & q) const
{
    return _imp->ids.end() != _imp->ids.find(q);
}

std::shared_ptr<const CategoryNamePartSet>
AccountsRepositoryStore::category_names() const
{
    return _imp->categories;
}

std::shared_ptr<const CategoryNamePartSet>
AccountsRepositoryStore::unimportant_category_names() const
{
    return _imp->categories;
}

std::shared_ptr<const QualifiedPackageNameSet>
AccountsRepositoryStore::package_names(const CategoryNamePart & c) const
{
    PackageNames::iterator p(_imp->package_names.find(c));
    if (_imp->package_names.end() == p)
        return std::make_shared<QualifiedPackageNameSet>();
    else
        return p->second;
}

std::shared_ptr<const PackageIDSequence>
AccountsRepositoryStore::package_ids(const QualifiedPackageName & p) const
{
    IDs::iterator i(_imp->ids.find(p));
    if (_imp->ids.end() == i)
        return std::make_shared<PackageIDSequence>();
    else
        return i->second;
}

template class Pimp<accounts_repository::AccountsRepositoryStore>;