aboutsummaryrefslogtreecommitdiff
path: root/paludis/fs_entry.hh
blob: 800018031a62edee68bc19cdafde9be3e4508aa1 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@gentoo.org>
 * Copyright (c) 2006 Mark Loeser <halcy0n@gentoo.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
 */

#ifndef PALUDIS_GUARD_PALUDIS_FS_ENTRY_HH
#define PALUDIS_GUARD_PALUDIS_FS_ENTRY_HH 1

#include <string>
#include <ostream>
#include <paludis/comparison_policy.hh>
#include <paludis/exception.hh>
#include <paludis/counted_ptr.hh>
#include <sys/stat.h>

/** \file
 * Declarations for paludis::Filesystem.
 *
 * \ingroup Filesystem
 * \ingroup Exception
 */

namespace paludis
{
    /**
     * Generic filesystem error class.
     *
     * \ingroup Exception
     */
    class FSError : public Exception
    {
        public:
            /**
             * Constructor.
             */
            FSError(const std::string & message) throw ();
    };

    /**
     * File permissions used by FSEntry
     */
    enum FSPermission
    {
        fs_perm_read,       ///< read permission on file
        fs_perm_write,      ///< write permission on file
        fs_perm_execute     ///< execute permission on file
    };

    /**
     * User classes used by FSEntry
     */
    enum FSUserGroup
    {
        fs_ug_owner,         ///< owner permission
        fs_ug_group,        ///< group permission
        fs_ug_others        ///< others permission
    };

    /**
     * Represents an entry (which may or may not exist) in the filesystem.
     *
     * \ingroup Filesystem
     */
    class FSEntry : public ComparisonPolicy<
                        FSEntry,
                        comparison_mode::FullComparisonTag,
                        comparison_method::CompareByMemberTag<std::string> >
    {
        private:
            std::string _path;

            mutable CountedPtr<struct stat, count_policy::ExternalCountTag> _stat_info;

            mutable bool _exists;

            /**
             * Whether or not we have run _stat() on this location yet
             */
            mutable bool _checked;

            void _normalise();

            /**
             * Runs lstat() on the current path if we have not done so already
             * Note: lstat() will give information on the symbolic link itself, and not what
             * the link points to, which is how stat() works.
             */
            void _stat() const;

        public:
            /**
             * Constructor, from a path.
             */
            FSEntry(const std::string & path);

            /**
             * Copy constructor.
             */
            FSEntry(const FSEntry & other);

            /**
             * Destructor.
             */
            ~FSEntry();

            /**
             * Assignment, from another FSEntry.
             */
            const FSEntry & operator= (const FSEntry & other);

            /**
             * Join with another FSEntry.
             */
            FSEntry operator/ (const FSEntry & rhs) const;

            /**
             * Append another FSEntry.
             */
            const FSEntry & operator/= (const FSEntry & rhs);

            /**
             * Join with another path.
             */
            FSEntry operator/ (const std::string & rhs) const;

            /**
             * Append another path.
             */
            const FSEntry & operator/= (const std::string & rhs)
            {
                return operator/= (FSEntry(rhs));
            }

            /**
             * Fetch a string representation of our path.
             */
            operator std::string() const;

            /**
             * Does a filesystem entry exist at our location?
             */
            bool exists() const;

            /**
             * Does a filesystem entry exist at our location, and if it does,
             * is it a directory?
             */
            bool is_directory() const;

            /**
             * Does a filesystem entry exist at our location, and if it does,
             * is it a regular file?
             */
            bool is_regular_file() const;

            /**
             * Does a filesystem entry exist at our location, and if it does,
             * is it a symbolic link?
             */
            bool is_symbolic_link() const;

            /**
             * Check if filesystem entry has `perm` for `user_group`
             * \exception FSError if there was a problem accessing the filesystem entry
             */
            bool has_permission(const FSUserGroup & user_group, const FSPermission & fs_perm) const;

            /**
             * Return the last part of our path (eg '/foo/bar' => 'bar').
             */
            std::string basename() const;

            /**
             * Return the first part of our path (eg '/foo/bar' => '/foo').
             */
            FSEntry dirname() const;

            /**
             * Return the canonicalised version of our path.
             */
            FSEntry realpath() const;

            /**
             * Return the time the filesystem entry was created
             * \exception FSError if there was a problem accessing the filesystem entry
             */
            time_t ctime() const;

            /**
             * Return the time the filesystem entry was last modified
             * \exception FSError if there was a problem accessing the filesystem entry
             */
            time_t mtime() const;

            /**
             * Return the size of our file, in bytes.
             *
             * \exception FSError if we don't have a size.
             */
            off_t file_size() const;

            /**
             * Return the current working directory
             */
            static FSEntry cwd();
    };

    /**
     * An FSEntry can be written to an ostream.
     */
    std::ostream & operator<< (std::ostream & s, const FSEntry & f);
}

#endif