aboutsummaryrefslogtreecommitdiff
path: root/src/clients/reconcilio/broken_linkage_finder/configuration.cc
blob: 621a844025ee554ffb993507cabf182feefec1ec (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 David Leverton
 *
 * 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 "configuration.hh"

#include <src/clients/reconcilio/util/realpath.hh>
#include <src/clients/reconcilio/util/wildcard_expander.hh>

#include <paludis/util/config_file.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/join.hh>
#include <paludis/util/log.hh>
#include <paludis/util/options.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/system.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/tr1_functional.hh>

#include <libwrapiter/libwrapiter_forward_iterator-impl.hh>

#include <algorithm>
#include <functional>
#include <iterator>
#include <vector>

using namespace paludis;
using namespace broken_linkage_finder;

namespace paludis
{
    template <>
    struct Implementation<Configuration>
    {
        std::vector<std::string> ld_library_mask;
        std::vector<FSEntry> search_dirs;
        std::vector<FSEntry> search_dirs_mask;
        std::vector<FSEntry> ld_so_conf;

        void load_from_environment();
        void load_from_etc_revdep_rebuild(const FSEntry &);
        void load_from_etc_profile_env(const FSEntry &);
        void load_from_etc_ld_so_conf(const FSEntry &);
        void add_defaults();
    };
}

namespace
{
    struct IsGarbageFile : std::unary_function<const FSEntry &, bool>
    {
        bool operator() (const FSEntry & file)
        {
            std::string basename(file.basename());
            return '#' == basename[0] || '~' == basename[basename.length() - 1];
        }
    };

    template <typename Tokeniser_, typename T_>
    void
    from_string(const tr1::function<std::string (const std::string &)> & source,
                const std::string & varname, std::vector<T_> & vec, const std::string & delims)
    {
        std::string str(source(varname));
        if (! str.empty())
        {
            Log::get_instance()->message(ll_debug, lc_context, "Got " + varname + "=\"" + str + "\"");
            Tokeniser_::tokenise(str, delims, std::back_inserter(vec));
        }
    }

    template <typename T_>
    void
    from_string(const tr1::function<std::string (const std::string &)> & source,
                const std::string & varname, std::vector<T_> & vec)
    {
        std::string str(source(varname));
        if (! str.empty())
        {
            Log::get_instance()->message(ll_debug, lc_context, "Got " + varname + "=\"" + str + "\"");
            WhitespaceTokeniser::tokenise(str, std::back_inserter(vec));
        }
    }

    inline void
    do_wildcards(std::vector<std::string> &, const FSEntry &)
    {
    }

    inline void
    do_wildcards(std::vector<FSEntry> & vec, const FSEntry & root)
    {
        std::vector<FSEntry> scratch;

        for (std::vector<FSEntry>::const_iterator it(vec.begin()), it_end(vec.end()); it_end != it; ++it)
            std::copy(WildcardExpander(stringify(*it), root), WildcardExpander(),
                      std::back_inserter(scratch));

        using std::swap;
        swap(vec, scratch);
    }

    template <typename T_>
    void
    cleanup(const std::string & varname, std::vector<T_> & vec, const FSEntry & root)
    {
        vec.erase(std::find(vec.begin(), vec.end(), T_("-*")), vec.end());

        do_wildcards(vec, root);

        std::sort(vec.begin(), vec.end());
        vec.erase(std::unique(vec.begin(), vec.end()), vec.end());

        Log::get_instance()->message(
            ll_debug, lc_context, "Final " + varname + "=\""  +
            join(vec.begin(), vec.end(),  " ") + "\"");
    }
}

Configuration::Configuration(const FSEntry & root) :
    PrivateImplementationPattern<Configuration>(new Implementation<Configuration>)
{
    Context ctx("When loading broken linkage checker configuration for '" + stringify(root) + "':");

    _imp->load_from_environment();
    _imp->load_from_etc_revdep_rebuild(root);
    _imp->load_from_etc_profile_env(root);
    _imp->load_from_etc_ld_so_conf(root);
    _imp->add_defaults();

    cleanup("LD_LIBRARY_MASK",  _imp->ld_library_mask,  root);
    cleanup("SEARCH_DIRS",      _imp->search_dirs,      root);
    cleanup("SEARCH_DIRS_MASK", _imp->search_dirs_mask, root);

    // don't need the extra cleanup here
    std::sort(_imp->ld_so_conf.begin(), _imp->ld_so_conf.end());
    _imp->ld_so_conf.erase(std::unique(_imp->ld_so_conf.begin(), _imp->ld_so_conf.end()),
                           _imp->ld_so_conf.end());
    Log::get_instance()->message(
        ll_debug, lc_context, "Final ld.so.conf contents is \"" +
        join(_imp->ld_so_conf.begin(), _imp->ld_so_conf.end(), " ") + "\"");
}

Configuration::~Configuration()
{
}

void
Implementation<Configuration>::load_from_environment()
{
    using namespace tr1::placeholders;

    Context ctx("When checking environment variables:");

    tr1::function<std::string (const std::string &)> fromenv(
        tr1::bind(getenv_with_default, _1, ""));

    from_string(fromenv, "LD_LIBRARY_MASK",  ld_library_mask);
    from_string(fromenv, "SEARCH_DIRS",      search_dirs);
    from_string(fromenv, "SEARCH_DIRS_MASK", search_dirs_mask);
}

void
Implementation<Configuration>::load_from_etc_revdep_rebuild(const FSEntry & root)
{
    using namespace tr1::placeholders;

    FSEntry etc_revdep_rebuild(root / "etc" / "revdep-rebuild");
    Context ctx("When reading '" + stringify(etc_revdep_rebuild) + "':");

    if (etc_revdep_rebuild.is_directory_or_symlink_to_directory())
    {
        std::vector<FSEntry> conf_files = std::vector<FSEntry>(
            DirIterator(etc_revdep_rebuild), DirIterator());
        conf_files.erase(std::remove_if(conf_files.begin(), conf_files.end(),
                                        IsGarbageFile()),
                         conf_files.end());
        std::sort(conf_files.begin(), conf_files.end());

        KeyValueConfigFileOptions opts;
        opts += kvcfo_disallow_space_around_equals;
        opts += kvcfo_disallow_space_inside_unquoted_values;

        for (std::vector<FSEntry>::iterator it(conf_files.begin()),
                 it_end(conf_files.end()); it_end != it; ++it)
        {
            Context ctx_file("When reading '" + stringify(*it) + "':");

            if (it->is_regular_file_or_symlink_to_regular_file())
            {
                KeyValueConfigFile kvs(*it, opts);

                tr1::function<std::string (const std::string &)> fromfile(
                    tr1::bind(&KeyValueConfigFile::get, tr1::cref(kvs), _1));

                from_string(fromfile, "LD_LIBRARY_MASK",  ld_library_mask);
                from_string(fromfile, "SEARCH_DIRS",      search_dirs);
                from_string(fromfile, "SEARCH_DIRS_MASK", search_dirs_mask);
            }
            else
                Log::get_instance()->message(ll_warning, lc_context, "'" + stringify(*it) + "' is not a regular file");
        }
    }
    else if (etc_revdep_rebuild.exists())
        Log::get_instance()->message(ll_warning, lc_context, "'" + stringify(etc_revdep_rebuild) + "' exists but is not a directory");
}

void
Implementation<Configuration>::load_from_etc_profile_env(const FSEntry & root)
{
    using namespace tr1::placeholders;

    FSEntry etc_profile_env(root / "etc" / "profile.env");
    Context ctx("When reading '" + stringify(etc_profile_env) + "':");

    if (etc_profile_env.is_regular_file_or_symlink_to_regular_file())
    {
        KeyValueConfigFileOptions opts;
        opts += kvcfo_disallow_space_around_equals;
        opts += kvcfo_disallow_space_inside_unquoted_values;
        opts += kvcfo_ignore_export;

        KeyValueConfigFile kvs(etc_profile_env, opts);
        typedef Tokeniser<delim_kind::AnyOfTag> Tokeniser;
        const std::string delims(":");

        tr1::function<std::string (const std::string &)> fromfile(
            tr1::bind(&KeyValueConfigFile::get, tr1::cref(kvs), _1));

        from_string<Tokeniser>(fromfile, "PATH",     search_dirs, delims);
        from_string<Tokeniser>(fromfile, "ROOTPATH", search_dirs, delims);
    }
    else if (etc_profile_env.exists())
        Log::get_instance()->message(ll_warning, lc_context, "'" + stringify(etc_profile_env) + "' exists but is not a regular file");
}

void
Implementation<Configuration>::load_from_etc_ld_so_conf(const FSEntry & root)
{
    FSEntry etc_ld_so_conf(root / "etc" / "ld.so.conf");
    Context ctx("When reading '" + stringify(etc_ld_so_conf) + "':");

    if (etc_ld_so_conf.is_regular_file_or_symlink_to_regular_file())
    {
        LineConfigFileOptions opts;
        opts += lcfo_disallow_continuations;

        LineConfigFile lines(etc_ld_so_conf, opts);
        if (lines.begin() != lines.end())
        {
            Log::get_instance()->message(ll_debug, lc_context, "Got " + join(lines.begin(), lines.end(), " "));
            std::copy(lines.begin(), lines.end(), std::back_inserter(search_dirs));
            std::copy(lines.begin(), lines.end(), std::back_inserter(ld_so_conf));
        }
    }
    else if (etc_ld_so_conf.exists())
        Log::get_instance()->message(ll_warning, lc_context, "'" + stringify(etc_ld_so_conf) + "' exists but is not a regular file");
}

void
Implementation<Configuration>::add_defaults()
{
    Context ctx("When adding default settings:");

    static const std::string default_ld_library_mask(
        "libodbcinst.so libodbc.so libjava.so libjvm.so");
    static const std::string default_search_dirs(
        "/bin /sbin /usr/bin /usr/sbin /lib* /usr/lib*");
    static const std::string default_search_dirs_mask(
        "/opt/OpenOffice /usr/lib*/openoffice /lib*/modules");
    static const std::string default_ld_so_conf("/lib /usr/lib");

    Log::get_instance()->message(ll_debug, lc_context, "Got LD_LIBRARY_MASK=\"" + default_ld_library_mask + "\"");
    WhitespaceTokeniser::tokenise(
            default_ld_library_mask, std::back_inserter(ld_library_mask));

    Log::get_instance()->message(ll_debug, lc_context, "Got SEARCH_DIRS=\"" + default_search_dirs + "\"");
    WhitespaceTokeniser::tokenise(
            default_search_dirs, std::back_inserter(search_dirs));

    Log::get_instance()->message(ll_debug, lc_context, "Got SEARCH_DIRS_MASK=\"" + default_search_dirs_mask + "\"");
    WhitespaceTokeniser::tokenise(
            default_search_dirs_mask, std::back_inserter(search_dirs_mask));

    Log::get_instance()->message(ll_debug, lc_context, "Default ld.so.conf contents is \"" + default_ld_so_conf + "\"");
    WhitespaceTokeniser::tokenise(
            default_ld_so_conf, std::back_inserter(ld_so_conf));
}

Configuration::DirsIterator
Configuration::begin_search_dirs() const
{
    return DirsIterator(_imp->search_dirs.begin());
}

Configuration::DirsIterator
Configuration::end_search_dirs() const
{
    return DirsIterator(_imp->search_dirs.end());
}

Configuration::DirsIterator
Configuration::begin_ld_so_conf() const
{
    return DirsIterator(_imp->ld_so_conf.begin());
}

Configuration::DirsIterator
Configuration::end_ld_so_conf() const
{
    return DirsIterator(_imp->ld_so_conf.end());
}

bool
Configuration::dir_is_masked(const FSEntry & dir) const
{
    return std::binary_search(_imp->search_dirs_mask.begin(), _imp->search_dirs_mask.end(), dir);
}

bool
Configuration::lib_is_masked(const std::string & lib) const
{
    return std::binary_search(_imp->ld_library_mask.begin(), _imp->ld_library_mask.end(), lib);
}