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

/*
 * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * 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 <dirent.h>
#include <errno.h>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/stringify.hh>
#include <sys/types.h>
#include <set>

/** \file
 * Implementation of paludis::DirIterator.
 *
 * \ingroup grpfilesystem
 */

using namespace paludis;

namespace paludis
{
    /**
     * Implementation data for DirIterator.
     *
     * \ingroup grpfilesystem
     */
    template<>
    struct Implementation<DirIterator> :
        InternalCounted<Implementation<DirIterator> >
    {
        FSEntry base;
        bool ignore_dotfiles;
        CountedPtr<std::set<FSEntry>, count_policy::ExternalCountTag> items;
        std::set<FSEntry>::iterator iter;

        Implementation(const FSEntry & b, bool i, CountedPtr<std::set<FSEntry>,
                count_policy::ExternalCountTag> ii) :
            base(b),
            ignore_dotfiles(i),
            items(ii)
        {
        }
    };
}

DirOpenError::DirOpenError(const FSEntry & location, const int errno_value) throw () :
    FSError("Error opening directory '" + stringify(location) + "': " + strerror(errno_value))
{
}

DirIterator::DirIterator(const FSEntry & base, bool ignore_dotfiles) :
    PrivateImplementationPattern<DirIterator>(new Implementation<DirIterator>(
                base, ignore_dotfiles, CountedPtr<std::set<FSEntry>,
                count_policy::ExternalCountTag>(new std::set<FSEntry>)))
{
    DIR * d(opendir(stringify(_imp->base).c_str()));
    if (0 == d)
        throw DirOpenError(_imp->base, errno);

    struct dirent * de;
    while (0 != ((de = readdir(d))))
        if (_imp->ignore_dotfiles)
        {
            if ('.' != de->d_name[0])
                _imp->items->insert(_imp->base / std::string(de->d_name));
        }
        else if (! (de->d_name[0] == '.' &&
                    (de->d_name[1] == '\0' || (de->d_name[1] == '.' && de->d_name[2] == '\0'))))
            _imp->items->insert(_imp->base / std::string(de->d_name));

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

    closedir(d);
}

DirIterator::DirIterator(const DirIterator & other) :
    PrivateImplementationPattern<DirIterator>(new Implementation<DirIterator>(
                other._imp->base, other._imp->ignore_dotfiles, other._imp->items))
{
    _imp->iter = other._imp->iter;
}

DirIterator::DirIterator() :
    PrivateImplementationPattern<DirIterator>(new Implementation<DirIterator>(
                FSEntry(""), true, CountedPtr<std::set<FSEntry>,
                count_policy::ExternalCountTag>(new std::set<FSEntry>)))
{
    _imp->iter = _imp->items->end();
}

DirIterator::~DirIterator()
{
}

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

const FSEntry &
DirIterator::operator* () const
{
    return *_imp->iter;
}

const FSEntry *
DirIterator::operator-> () const
{
    return &*_imp->iter;
}

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

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

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

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

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

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

bool
DirIterator::operator!= (const DirIterator & other) const
{
    return ! operator== (other);
}