2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #if !defined(_ATF_CXX_FS_HPP_)
31 #define _ATF_CXX_FS_HPP_
34 #include <sys/types.h>
45 #include "../../atf-c/detail/fs.h"
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 //! \brief A class to represent a path to a file.
63 //! The path class represents the route to a file or directory in the
64 //! file system. All file manipulation operations use this class to
65 //! represent their arguments as it takes care of normalizing user-provided
66 //! strings and ensures they are valid.
68 //! It is important to note that the file pointed to by a path need not
73 //! \brief Internal representation of a path.
78 //! \brief Constructs a new path from a user-provided string.
80 //! This constructor takes a string, either provided by the program's
81 //! code or by the user and constructs a new path object. The string
82 //! is normalized to not contain multiple delimiters together and to
83 //! remove any trailing one.
85 //! The input string cannot be empty.
87 explicit path(const std::string
&);
90 //! \brief Copy constructor.
95 //! \brief Copy constructor.
97 path(const atf_fs_path_t
*);
100 //! \brief Destructor for the path class.
105 //! \brief Returns a pointer to a C-style string representing this path.
107 const char* c_str(void) const;
110 //! \brief Returns a pointer to the implementation data.
112 const atf_fs_path_t
* c_path(void) const;
115 //! \brief Returns a string representing this path.
116 //! XXX Really needed?
118 std::string
str(void) const;
121 //! \brief Returns the branch path of this path.
123 //! Calculates and returns the branch path of this path. In other
124 //! words, it returns what the standard ::dirname function would return.
126 path
branch_path(void) const;
129 //! \brief Returns the leaf name of this path.
131 //! Calculates and returns the leaf name of this path. In other words,
132 //! it returns what the standard ::basename function would return.
134 std::string
leaf_name(void) const;
137 //! \brief Checks whether this path is absolute or not.
139 //! Returns a boolean indicating if this is an absolute path or not;
140 //! i.e. if it starts with a slash.
142 bool is_absolute(void) const;
145 //! \brief Checks whether this path points to the root directory or not.
147 //! Returns a boolean indicating if this is path points to the root
148 //! directory or not. The checks made by this are extremely simple (so
149 //! the results cannot always be trusted) but they are enough for our
150 //! modest sanity-checking needs. I.e. "/../" could return false.
152 bool is_root(void) const;
155 //! \brief Converts the path to be absolute.
157 //! \pre The path was not absolute.
159 path
to_absolute(void) const;
162 //! \brief Assignment operator.
164 path
& operator=(const path
&);
167 //! \brief Checks if two paths are equal.
169 bool operator==(const path
&) const;
172 //! \brief Checks if two paths are different.
174 bool operator!=(const path
&) const;
177 //! \brief Concatenates a path with a string.
179 //! Constructs a new path object that is the concatenation of the
180 //! left-hand path with the right-hand string. The string is normalized
181 //! before the concatenation, and a path delimiter is introduced between
182 //! the two components if needed.
184 path
operator/(const std::string
&) const;
187 //! \brief Concatenates a path with another path.
189 //! Constructs a new path object that is the concatenation of the
190 //! left-hand path with the right-hand one. A path delimiter is
191 //! introduced between the two components if needed.
193 path
operator/(const path
&) const;
196 //! \brief Checks if a path has to be sorted before another one
197 //! lexicographically.
199 bool operator<(const path
&) const;
202 // ------------------------------------------------------------------------
203 // The "file_info" class.
204 // ------------------------------------------------------------------------
209 //! \brief A class that contains information about a file.
211 //! The file_info class holds information about an specific file that
212 //! exists in the file system.
215 atf_fs_stat_t m_stat
;
219 //! \brief The file's type.
221 static const int blk_type
;
222 static const int chr_type
;
223 static const int dir_type
;
224 static const int fifo_type
;
225 static const int lnk_type
;
226 static const int reg_type
;
227 static const int sock_type
;
228 static const int wht_type
;
231 //! \brief Constructs a new file_info based on a given file.
233 //! This constructor creates a new file_info object and fills it with
234 //! the data returned by ::stat when run on the given file, which must
237 explicit file_info(const path
&);
240 //! \brief The copy constructor.
242 file_info(const file_info
&);
245 //! \brief The destructor.
250 //! \brief Returns the device containing the file.
252 dev_t
get_device(void) const;
255 //! \brief Returns the file's inode.
257 ino_t
get_inode(void) const;
260 //! \brief Returns the file's permissions.
262 mode_t
get_mode(void) const;
265 //! \brief Returns the file's size.
267 off_t
get_size(void) const;
270 //! \brief Returns the file's type.
272 int get_type(void) const;
275 //! \brief Returns whether the file is readable by its owner or not.
277 bool is_owner_readable(void) const;
280 //! \brief Returns whether the file is writable by its owner or not.
282 bool is_owner_writable(void) const;
285 //! \brief Returns whether the file is executable by its owner or not.
287 bool is_owner_executable(void) const;
290 //! \brief Returns whether the file is readable by the users belonging
291 //! to its group or not.
293 bool is_group_readable(void) const;
296 //! \brief Returns whether the file is writable the users belonging to
297 //! its group or not.
299 bool is_group_writable(void) const;
302 //! \brief Returns whether the file is executable by the users
303 //! belonging to its group or not.
305 bool is_group_executable(void) const;
308 //! \brief Returns whether the file is readable by people different
309 //! than the owner and those belonging to the group or not.
311 bool is_other_readable(void) const;
314 //! \brief Returns whether the file is write by people different
315 //! than the owner and those belonging to the group or not.
317 bool is_other_writable(void) const;
320 //! \brief Returns whether the file is executable by people different
321 //! than the owner and those belonging to the group or not.
323 bool is_other_executable(void) const;
326 // ------------------------------------------------------------------------
327 // The "directory" class.
328 // ------------------------------------------------------------------------
331 //! \brief A class representing a file system directory.
333 //! The directory class represents a group of files in the file system and
334 //! corresponds to exactly one directory.
336 class directory
: public std::map
< std::string
, file_info
> {
339 //! \brief Constructs a new directory.
341 //! Constructs a new directory object representing the given path.
342 //! The directory must exist at creation time as the contents of the
343 //! class are gathered from it.
345 directory(const path
&);
348 //! \brief Returns the file names of the files in the directory.
350 //! Returns the leaf names of all files contained in the directory.
351 //! I.e. the keys of the directory map.
353 std::set
< std::string
> names(void) const;
356 // ------------------------------------------------------------------------
358 // ------------------------------------------------------------------------
361 //! \brief Checks if the given path exists.
363 bool exists(const path
&);
366 //! \brief Looks for the given program in the PATH.
368 //! Given a program name (without slashes) looks for it in the path and
369 //! returns its full path name if found, otherwise an empty path.
371 bool have_prog_in_path(const std::string
&);
374 //! \brief Checks if the given path exists, is accessible and is executable.
376 bool is_executable(const path
&);
379 //! \brief Removes a given file.
381 void remove(const path
&);
384 //! \brief Removes an empty directory.
386 void rmdir(const path
&);
391 #endif // !defined(_ATF_CXX_FS_HPP_)