aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/fs_entry.hh
blob: 370868cca055ef9bc96555c9a9f6cf9f8d3a9d3b (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010 Ciaran McCreesh
 * Copyright (c) 2006 Mark Loeser
 *
 * 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 <paludis/util/fs_entry-fwd.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/operators.hh>
#include <paludis/util/pimp.hh>
#include <paludis/util/timestamp-fwd.hh>
#include <string>
#include <memory>
#include <iosfwd>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctime>

/** \file
 * Declarations for FSEntry.
 *
 * \ingroup g_fs
 *
 * \section Examples
 *
 * - None at this time.
 */

struct stat;
struct utimbuf;

namespace paludis
{
    /**
     * Generic filesystem error class.
     *
     * \ingroup g_exceptions
     * \ingroup g_fs
     * \nosubgrouping
     */
    class PALUDIS_VISIBLE FSError :
        public Exception
    {
        public:
            ///\name Basic operations
            ///\{

            FSError(const std::string & message) throw ();

            ///\}
    };

    /**
     * Represents an entry (which may or may not exist) in the filesystem.
     *
     * \ingroup g_fs
     */
    class PALUDIS_VISIBLE FSEntry :
        public relational_operators::HasRelationalOperators,
        public arithmetic_operators::HasArithmeticOperators,
        private Pimp<FSEntry>
    {
        friend std::ostream & operator<< (std::ostream & s, const FSEntry & f);
        friend class DirIterator;

        private:
            void _normalise();

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

            FSEntry(const std::string & path, unsigned char d_type);

        public:
            ///\name Basic operations
            ///\{

            FSEntry(const std::string & path);

            FSEntry(const FSEntry & other);

            ~FSEntry();

            const FSEntry & operator= (const FSEntry & other);

            ///\}

            ///\name Modification operations
            ///\{

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

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

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

            ///\}


            ///\name Comparison operators
            ///\{

            bool operator< (const FSEntry &) const;
            bool operator== (const FSEntry &) const;

            ///\}

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

            /**
             * Return the path without a given prefix (eg '/foo/bar/baz'->strip_leading('/foo') => '/bar/baz').
             */
            FSEntry strip_leading(const FSEntry & prefix) const;

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


            ///\}

            ///\name Filesystem queries
            ///\{

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

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

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

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

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

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

            /**
             * Whether we exist and are a device file.
             */
            bool is_device() const
                PALUDIS_ATTRIBUTE((warn_unused_result));

            /**
             * Whether we exist and are a fifo.
             */
            bool is_fifo() const
                PALUDIS_ATTRIBUTE((warn_unused_result));

            /**
             * 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
                PALUDIS_ATTRIBUTE((warn_unused_result));

            /**
             * Return the permissions for our item.
             *
             * \exception FSError if there was a problem accessing the filesystem entry
             */
            mode_t permissions() const
                PALUDIS_ATTRIBUTE((warn_unused_result));

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

            /**
             * Return the canonicalised version of our path, if it exists, or
             * ourself if it doesn't.
             */
            FSEntry realpath_if_exists() const
                PALUDIS_ATTRIBUTE((warn_unused_result));

            /**
             * Return our destination, if we are a symlink.
             *
             * \exception FSError if we are not a symlink, or if the system call
             * fails.
             */
            std::string readlink() const
                PALUDIS_ATTRIBUTE((warn_unused_result));

            /**
             * Return the time the inode for the filesystem entry was last modified
             *
             * \exception FSError if there was a problem accessing the filesystem entry
             * \since 0.43
             */
            Timestamp ctim() const
                PALUDIS_ATTRIBUTE((warn_unused_result));

            /**
             * Return the time the filesystem entry was last modified
             *
             * \exception FSError if there was a problem accessing the filesystem entry
             * \since 0.43
             */
            Timestamp mtim() const
                PALUDIS_ATTRIBUTE((warn_unused_result));

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

            /**
             * Fetch our owner.
             *
             * \exception FSError If we don't exist or the stat call fails.
             */
            uid_t owner() const
                PALUDIS_ATTRIBUTE((warn_unused_result));

            /**
             * Fetch our group.
             *
             * \exception FSError If we don't exist or the stat call fails.
             */
            gid_t group() const
                PALUDIS_ATTRIBUTE((warn_unused_result));

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

            /**
             * Return an unique low-level id for this entry
             */
            std::pair<dev_t, ino_t> lowlevel_id() const
                PALUDIS_ATTRIBUTE((warn_unused_result));

            ///\}

            ///\name Filesystem operations
            ///\{

            /**
             * Try to make a directory.
             *
             * \return True, if we succeeded, and false if the directory
             *   already exists and is a directory.
             *
             * \exception FSError If an error other than the directory already
             *   existing occurs.
             */
            bool mkdir(const mode_t mode = 0755);

            /**
             * Try to make a symlink.
             *
             * \return True, if we succeeded, and false if the target already
             *   exists and is a symlink to the same target.
             *
             * \exception FSError If an error other than the symlink already
             *   existing occurs, or if the symlink exists and points elsewhere.
             */
            bool symlink(const std::string & target);

            /**
             * Try to unlink.
             *
             * \return True, if we succeeded, and false if we don't exist
             *   already.
             *
             * \exception FSError If an error other than us already not
             *   existing occurs.
             */
            bool unlink();

            /**
             * Try to rmdir.
             *
             * \return True, if we succeeded, and false if we don't exist
             *   already.
             *
             * \exception FSError If an error other than us already not
             *   existing occurs.
             */
            bool rmdir();

            /**
             * Try to set atime and mtime
             *
             * \return True, if we succeeded, and false if we don't exist
             *   already.
             *
             * \exception FSError If an error other than us already not
             *   existing ocurrs.
             *
             * \since 0.44 takes a Timestamp
             */
            bool utime(const Timestamp &);

            /**
             * Change our ownership, following symlinks.
             *
             * \exception FSError If the chown failed.
             */
            void chown(const uid_t owner, const gid_t group = static_cast<gid_t>(-1));

            /**
             * Change our ownership, not following symlinks.
             *
             * \exception FSError If the lchown failed.
             */
            void lchown(const uid_t owner, const gid_t group = static_cast<gid_t>(-1));

            /**
             * Change our permissions.
             *
             * \exception FSError If the chmod failed.
             */
            void chmod(const mode_t mode);

            /**
             * Rename ourself (will not work across mount points).
             *
             * \exception FSError If the rename failed.
             */
            void rename(const FSEntry & new_name);

            ///\}
    };
}

#endif