aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/accounts/installed_accounts_id.cc
blob: 56d22c8c87a18e157e6afea019659569df591b9f (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2009, 2010 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/installed_accounts_id.hh>
#include <paludis/repositories/accounts/accounts_dep_key.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/config_file.hh>
#include <paludis/util/options.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/simple_visitor_cast.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/log.hh>
#include <paludis/name.hh>
#include <paludis/version_spec.hh>
#include <paludis/literal_metadata_key.hh>
#include <paludis/repository.hh>
#include <paludis/action.hh>
#include <paludis/user_dep_spec.hh>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <list>
#include <vector>

using namespace paludis;
using namespace paludis::accounts_repository;

namespace paludis
{
    template <>
    struct Implementation<InstalledAccountsID>
    {
        const Environment * const env;

        const QualifiedPackageName name;
        const VersionSpec version;
        const std::tr1::shared_ptr<const Repository> repository;

        const std::tr1::shared_ptr<const LiteralMetadataValueKey<bool> > transient_key;

        mutable Mutex mutex;
        mutable std::tr1::shared_ptr<const AccountsDepKey> dependencies_key;

        const bool is_user;

        Implementation(const Environment * const e,
                const QualifiedPackageName & q, const std::tr1::shared_ptr<const Repository> & r,
                const bool u) :
            env(e),
            name(q),
            version("0", VersionSpecOptions()),
            repository(r),
            transient_key(new LiteralMetadataValueKey<bool>("transient", "Transient", mkt_internal, true)),
            is_user(u)
        {
        }
    };
}

InstalledAccountsID::InstalledAccountsID(const Environment * const e,
        const QualifiedPackageName & q, const std::tr1::shared_ptr<const Repository> & r, const bool u) :
    PrivateImplementationPattern<InstalledAccountsID>(new Implementation<InstalledAccountsID>(e, q, r, u)),
    _imp(PrivateImplementationPattern<InstalledAccountsID>::_imp)
{
    add_metadata_key(_imp->transient_key);
}

InstalledAccountsID::~InstalledAccountsID()
{
}

void
InstalledAccountsID::need_keys_added() const
{
    Lock lock(_imp->mutex);

    if (_imp->is_user && ! _imp->dependencies_key)
    {
        std::tr1::shared_ptr<Set<std::string> > groups(new Set<std::string>);

        /* depend upon our primary group */
        {
            int pwd_buf_sz(sysconf(_SC_GETPW_R_SIZE_MAX));
            if (-1 == pwd_buf_sz || pwd_buf_sz > 1024 * 128)
            {
                Log::get_instance()->message("accounts.getpw_r_size_max", ll_warning, lc_context) <<
                    "Got dodgy value " << pwd_buf_sz << " from sysconf(_SC_GETPW_R_SIZE_MAX)";
                pwd_buf_sz = 1024 * 128;
            }
            std::vector<char> pwd_buf(pwd_buf_sz);

            struct passwd pwd;
            struct passwd * pwd_result;

            if (0 == getpwnam_r(stringify(name().package()).c_str(), &pwd, &pwd_buf[0], pwd_buf_sz, &pwd_result))
            {
                int grp_buf_sz(sysconf(_SC_GETGR_R_SIZE_MAX));
                if (-1 == grp_buf_sz || grp_buf_sz > 1024 * 128)
                {
                    Log::get_instance()->message("accounts.getgr_r_size_max", ll_warning, lc_context) <<
                        "Got dodgy value " << grp_buf_sz << " from sysconf(_SC_GETPW_R_SIZE_MAX)";
                    grp_buf_sz = 1024 * 128;
                }
                std::vector<char> grp_buf(grp_buf_sz);

                struct group grp;
                struct group * grp_result;
                if (0 == getgrgid_r(pwd.pw_gid, &grp, &grp_buf[0], grp_buf_sz, &grp_result))
                {
                    /* really we should only do this if the group in question is managed by accounts. users
                     * might have accounts installed by hand with a group that's unmanaged. */
                    groups->insert(stringify(grp.gr_name));
                }
                else
                    Log::get_instance()->message("accounts.getgrgid_r", ll_warning, lc_context) <<
                        "getgrgid_r failed for " << name();
            }
            else
                Log::get_instance()->message("accounts.getpwnam_r", ll_warning, lc_context) <<
                    "getpwnam_r failed for " << name();
        }

        /* ...and our secondary groups */
        {
            /* first person who gets annoyed by this not existing gets to implement it. */
        }

        _imp->dependencies_key.reset(new AccountsDepKey(_imp->env, groups));
        add_metadata_key(_imp->dependencies_key);
    }
}

void
InstalledAccountsID::clear_metadata_keys() const
{
}

void
InstalledAccountsID::need_masks_added() const
{
}

const QualifiedPackageName
InstalledAccountsID::name() const
{
    return _imp->name;
}

const VersionSpec
InstalledAccountsID::version() const
{
    return _imp->version;
}

const std::tr1::shared_ptr<const Repository>
InstalledAccountsID::repository() const
{
    return _imp->repository;
}

const std::string
InstalledAccountsID::canonical_form(const PackageIDCanonicalForm f) const
{
    switch (f)
    {
        case idcf_full:
            return stringify(name()) + "-" + stringify(version()) + "::" + stringify(repository()->name());

        case idcf_no_version:
            return stringify(name()) + "::" + stringify(repository()->name());

        case idcf_version:
            return stringify(version());

        case idcf_no_name:
            return stringify(version()) + "::" + stringify(repository()->name());

        case last_idcf:
            break;
    }

    throw InternalError(PALUDIS_HERE, "Bad PackageIDCanonicalForm");
}

PackageDepSpec
InstalledAccountsID::uniquely_identifying_spec() const
{
    return parse_user_package_dep_spec(stringify(name()) + "::" + stringify(repository()->name()),
            _imp->env, UserPackageDepSpecOptions());
}

const std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const PackageID> > >
InstalledAccountsID::virtual_for_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataCollectionKey<KeywordNameSet> >
InstalledAccountsID::keywords_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<ProvideSpecTree> >
InstalledAccountsID::provide_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataCollectionKey<PackageIDSequence> >
InstalledAccountsID::contains_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const PackageID> > >
InstalledAccountsID::contained_in_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
InstalledAccountsID::build_dependencies_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
InstalledAccountsID::run_dependencies_key() const
{
    need_keys_added();
    return _imp->dependencies_key;
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
InstalledAccountsID::post_dependencies_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
InstalledAccountsID::dependencies_key() const
{
    need_keys_added();
    return _imp->dependencies_key;
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
InstalledAccountsID::suggested_dependencies_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<FetchableURISpecTree> >
InstalledAccountsID::fetches_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<SimpleURISpecTree> >
InstalledAccountsID::homepage_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataValueKey<std::string> >
InstalledAccountsID::short_description_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataValueKey<std::string> >
InstalledAccountsID::long_description_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const Contents> > >
InstalledAccountsID::contents_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataTimeKey>
InstalledAccountsID::installed_time_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataCollectionKey<Set<std::string> > >
InstalledAccountsID::from_repositories_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataValueKey<FSEntry> >
InstalledAccountsID::fs_location_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataValueKey<bool> >
InstalledAccountsID::transient_key() const
{
    return _imp->transient_key;
}

const std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const Choices> > >
InstalledAccountsID::choices_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataValueKey<SlotName> >
InstalledAccountsID::slot_key() const
{
    return make_null_shared_ptr();
}

std::tr1::shared_ptr<const Set<std::string> >
InstalledAccountsID::breaks_portage() const
{
    return make_shared_ptr(new Set<std::string>);
}

bool
InstalledAccountsID::arbitrary_less_than_comparison(const PackageID &) const
{
    return false;
}

std::size_t
InstalledAccountsID::extra_hash_value() const
{
    return 0;
}

bool
InstalledAccountsID::supports_action(const SupportsActionTestBase &) const
{
    return false;
}

void
InstalledAccountsID::perform_action(Action & action) const
{
    throw ActionFailedError("Unsupported action: " + action.simple_name());
}