aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/fs_iterator.cc
blob: 5a21bf209bd538e34ba2a28b92e63f982c1104de (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006, 2007, 2008, 2010, 2011 Ciaran McCreesh
 * Copyright (c) 2008 Fernando J. Pereda
 *
 * 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/util/fs_iterator.hh>
#include <paludis/util/fs_path.hh>
#include <paludis/util/fs_error.hh>
#include <paludis/util/fs_stat.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/options.hh>
#include <paludis/util/tribool.hh>

#include <dirent.h>
#include <sys/types.h>
#include <functional>
#include <set>
#include <cstring>
#include <cerrno>

#include "config.h"

using namespace paludis;

#include <paludis/util/fs_iterator-se.cc>

typedef std::multiset<std::pair<ino_t, FSPath>, std::function<bool (std::pair<ino_t, FSPath>, std::pair<ino_t, FSPath>)> > EntrySet;

namespace paludis
{
    template<>
    struct Imp<FSIterator>
    {
        std::shared_ptr<EntrySet> items;
        EntrySet::iterator iter;

        Imp(const std::shared_ptr<EntrySet> & ii) :
            items(ii)
        {
        }
    };

    bool compare_inode(const std::pair<ino_t, FSPath> & a, const std::pair<ino_t, FSPath> & b)
    {
        return a.first < b.first;
    }

    bool compare_name(const std::pair<ino_t, FSPath> & a, const std::pair<ino_t, FSPath> & b)
    {
        return FSPathComparator()(a.second, b.second);
    }
}

FSIterator::FSIterator(const FSPath & base, const FSIteratorOptions & options) :
    _imp(std::shared_ptr<EntrySet>())
{
    using namespace std::placeholders;

    if (options[fsio_inode_sort])
        _imp->items = std::make_shared<EntrySet>(&compare_inode);
    else
        _imp->items = std::make_shared<EntrySet>(&compare_name);

    DIR * d(opendir(stringify(base).c_str()));
    if (0 == d)
        throw FSError("Error opening directory '" + stringify(base) + "'");

    bool have_any_special_wants(options[fsio_want_directories] || options[fsio_want_regular_files]);

    struct dirent * de;
    bool done(false);
    while (0 != ((de = readdir(d))))
    {
        if (done)
            break;

        bool want(false);

        if (! options[fsio_include_dotfiles])
        {
            if ('.' != de->d_name[0])
                want = true;
        }
        else if (! (de->d_name[0] == '.' &&
                    (de->d_name[1] == '\0' || (de->d_name[1] == '.' && de->d_name[2] == '\0'))))
            want = true;

        if (want)
        {
            FSPath f(stringify(base / std::string(de->d_name)));

            if (have_any_special_wants)
            {
                Tribool still_want(indeterminate);

#ifdef HAVE_DIRENT_DTYPE
                if (DT_UNKNOWN != de->d_type)
                {
                    int s(DTTOIF(de->d_type));

                    if (S_ISLNK(s))
                    {
                        if (! options[fsio_deref_symlinks_for_wants])
                            still_want = false;
                    }
                    else if (S_ISREG(s))
                        still_want = options[fsio_want_regular_files];
                    else if (S_ISDIR(s))
                        still_want = options[fsio_want_directories];
                    else
                        still_want = false;
                }
#endif

                if (still_want.is_indeterminate())
                {
                    FSStat f_stat(f);

                    if (f_stat.is_regular_file())
                        still_want = options[fsio_want_regular_files];
                    else if (f_stat.is_directory())
                        still_want = options[fsio_want_directories];
                    else if (f_stat.is_symlink())
                    {
                        if (options[fsio_deref_symlinks_for_wants])
                        {
                            if (f_stat.is_regular_file_or_symlink_to_regular_file())
                                still_want = options[fsio_want_regular_files];
                            else if (f_stat.is_directory_or_symlink_to_directory())
                                still_want = options[fsio_want_directories];
                            else
                                still_want = false;
                        }
                        else
                            still_want = false;
                    }
                    else
                        still_want = false;
                }

                want = still_want.is_true();
            }

            if (want)
            {
                _imp->items->insert(std::make_pair(de->d_ino, f));
                if (options[fsio_first_only])
                    done = true;
            }
        }
    }

    _imp->iter = _imp->items->begin();

    closedir(d);
}

FSIterator::FSIterator(const FSIterator & other) :
    _imp(other._imp->items)
{
    _imp->iter = other._imp->iter;
}

FSIterator::FSIterator() :
    _imp(std::shared_ptr<EntrySet>(std::make_shared<EntrySet>(&compare_name)))
{
    _imp->iter = _imp->items->end();
}

FSIterator::~FSIterator()
{
}

FSIterator &
FSIterator::operator= (const FSIterator & other)
{
    if (this != &other)
    {
        _imp->items = other._imp->items;
        _imp->iter = other._imp->iter;
    }
    return *this;
}

const FSPath &
FSIterator::operator* () const
{
    return _imp->iter->second;
}

const FSPath *
FSIterator::operator-> () const
{
    return &_imp->iter->second;
}

FSIterator &
FSIterator::operator++ ()
{
    ++_imp->iter;
    return *this;
}

FSIterator
FSIterator::operator++ (int)
{
    FSIterator c(*this);
    _imp->iter++;
    return c;
}

bool
paludis::operator== (const FSIterator & me, const FSIterator & other)
{
    if (other._imp->iter == other._imp->items->end())
        return me._imp->iter == me._imp->items->end();

    if (me._imp->iter == me._imp->items->end())
        return other._imp->iter == other._imp->items->end();

    if (other._imp->items != me._imp->items)
        throw InternalError(PALUDIS_HERE, "comparing two different FSIterators.");

    return other._imp->iter == me._imp->iter;
}

bool
paludis::operator!= (const FSIterator & me, const FSIterator & other)
{
    return ! operator== (me, other);
}

namespace paludis
{
    template class Pimp<FSIterator>;
}