paludis Version 3.0.2
The one true package mangler
fs_path.hh
1/* vim: set sw=4 sts=4 et foldmethod=syntax : */
2
3/*
4 * Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Ciaran McCreesh
5 * Copyright (c) 2006 Mark Loeser
6 * Copyright (c) 2008 Fernando J. Pereda
7 *
8 * This file is part of the Paludis package manager. Paludis is free software;
9 * you can redistribute it and/or modify it under the terms of the GNU General
10 * Public License version 2, as published by the Free Software Foundation.
11 *
12 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#ifndef PALUDIS_GUARD_PALUDIS_UTIL_FS_PATH_HH
23#define PALUDIS_GUARD_PALUDIS_UTIL_FS_PATH_HH 1
24
25#include <paludis/util/fs_path-fwd.hh>
26#include <paludis/util/fs_stat-fwd.hh>
27#include <paludis/util/timestamp-fwd.hh>
28#include <paludis/util/pimp.hh>
29#include <sys/stat.h>
30
31namespace paludis
32{
33 class PALUDIS_VISIBLE FSPath
34 {
35 friend std::ostream & paludis::operator<< (std::ostream & s, const FSPath & f);
36 friend bool operator== (const FSPath &, const FSPath &);
37
38 private:
39 Pimp<FSPath> _imp;
40
41 void _normalise();
42
43 public:
44 explicit FSPath(const std::string &);
45
46 FSPath(const FSPath &);
47
48 FSPath & operator= (const FSPath &);
49
50 ~FSPath();
51
52 FSStat stat() const PALUDIS_ATTRIBUTE((warn_unused_result));
53
54 FSPath operator/ (const std::string &) const PALUDIS_ATTRIBUTE((warn_unused_result));
55 FSPath operator/ (const FSPath &) const PALUDIS_ATTRIBUTE((warn_unused_result));
56
57 FSPath & operator/= (const std::string &);
58 FSPath & operator/= (const FSPath &);
59
60 /**
61 * Return the last part of our path (eg '/foo/bar' => 'bar').
62 */
63 const std::string basename() const PALUDIS_ATTRIBUTE((warn_unused_result));
64
65 /**
66 * Return the first part of our path (eg '/foo/bar' => '/foo').
67 */
68 FSPath dirname() const
69 PALUDIS_ATTRIBUTE((warn_unused_result));
70
71 /**
72 * Return the canonicalised version of our path.
73 */
74 FSPath realpath() const PALUDIS_ATTRIBUTE((warn_unused_result));
75
76 /**
77 * Return the canonicalised version of our path, if it exists, or
78 * ourself if it doesn't.
79 */
80 FSPath realpath_if_exists() const PALUDIS_ATTRIBUTE((warn_unused_result));
81
82 /**
83 * Return the path without a given prefix (eg '/foo/bar/baz'->strip_leading('/foo') => '/bar/baz').
84 */
85 FSPath strip_leading(const FSPath & prefix) const;
86
87 /**
88 * Do we start with a given path (eg '/foo/bar' starts with '/foo'
89 * but not '/fo')?
90 */
91 bool starts_with(const FSPath &) const;
92
93 /**
94 * Try to make a directory.
95 *
96 * \return True, if we succeeded, and false if the directory
97 * already exists and is a directory and options includes
98 * fspmkdo_ok_if_exists.
99 *
100 * \exception FSError If an error other than the directory already
101 * existing occurs, or if the directory already exists and
102 * options does not include fspmkdo_ok_if_exists.
103 */
104 bool mkdir(const mode_t mode, const FSPathMkdirOptions &) const;
105
106 /**
107 * Try to make a symlink.
108 *
109 * \return True, if we succeeded, and false if the target already
110 * exists and is a symlink to the same target.
111 *
112 * \exception FSError If an error other than the symlink already
113 * existing occurs, or if the symlink exists and points elsewhere.
114 */
115 bool symlink(const std::string & target) const;
116
117 /**
118 * Try to unlink.
119 *
120 * \return True, if we succeeded, and false if we don't exist
121 * already.
122 *
123 * \exception FSError If an error other than us already not
124 * existing occurs.
125 */
126 bool unlink() const;
127
128 /**
129 * Try to rmdir.
130 *
131 * \return True, if we succeeded, and false if we don't exist
132 * already.
133 *
134 * \exception FSError If an error other than us already not
135 * existing occurs.
136 */
137 bool rmdir() const;
138
139 /**
140 * Change our ownership, following symlinks.
141 *
142 * \exception FSError If the chown failed.
143 */
144 void chown(const uid_t owner, const gid_t group) const;
145
146 /**
147 * Change our ownership, not following symlinks.
148 *
149 * \exception FSError If the lchown failed.
150 */
151 void lchown(const uid_t owner, const gid_t group) const;
152
153 /**
154 * Change our permissions.
155 *
156 * \exception FSError If the chmod failed.
157 */
158 void chmod(const mode_t mode) const;
159
160 /**
161 * Try to set atime and mtime
162 *
163 * \return True, if we succeeded, and false if we don't exist
164 * already.
165 *
166 * \exception FSError If an error other than us already not
167 * existing ocurrs.
168 */
169 bool utime(const Timestamp &) const;
170
171 /**
172 * Return our destination, if we are a symlink.
173 *
174 * \exception FSError if we are not a symlink, or if the system call
175 * fails.
176 */
177 std::string readlink() const PALUDIS_ATTRIBUTE((warn_unused_result));
178
179 /**
180 * Rename ourself (will not work across mount points).
181 *
182 * \exception FSError If the rename failed.
183 */
184 void rename(const FSPath & new_name) const;
185
186 /**
187 * Return the current working directory
188 */
189 static FSPath cwd() PALUDIS_ATTRIBUTE((warn_unused_result));
190 };
191
193 {
194 public:
195 bool operator() (const FSPath &, const FSPath &) const;
196 };
197
198 extern template class Pimp<FSPath>;
199}
200
201#endif
Definition fs_path.hh:193
Definition fs_path.hh:34
bool unlink() const
FSPath realpath() const PALUDIS_ATTRIBUTE((warn_unused_result))
std::string readlink() const PALUDIS_ATTRIBUTE((warn_unused_result))
FSPath dirname() const PALUDIS_ATTRIBUTE((warn_unused_result))
void lchown(const uid_t owner, const gid_t group) const
bool mkdir(const mode_t mode, const FSPathMkdirOptions &) const
bool utime(const Timestamp &) const
FSPath strip_leading(const FSPath &prefix) const
bool symlink(const std::string &target) const
void chmod(const mode_t mode) const
bool starts_with(const FSPath &) const
void chown(const uid_t owner, const gid_t group) const
const std::string basename() const PALUDIS_ATTRIBUTE((warn_unused_result))
void rename(const FSPath &new_name) const
static FSPath cwd() PALUDIS_ATTRIBUTE((warn_unused_result))
FSPath realpath_if_exists() const PALUDIS_ATTRIBUTE((warn_unused_result))
bool rmdir() const
Definition fs_stat.hh:36
Definition pimp.hh:52
Definition timestamp.hh:42
std::ostream & operator<<(std::ostream &, const AdditionalPackageDepSpecRequirement &) PALUDIS_VISIBLE
#define PALUDIS_ATTRIBUTE(x)
Definition attributes.hh:53
#define PALUDIS_VISIBLE
Definition attributes.hh:59
Definition about_metadata-fwd.hh:24