1 // Copyright (c) 2007 The NetBSD Foundation, Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
7 // 1. Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
13 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #if !defined(ATF_CXX_DETAIL_FS_HPP)
27 #define ATF_CXX_DETAIL_FS_HPP
30 #include <sys/types.h>
41 #include "atf-c/detail/fs.h"
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 //! \brief A class to represent a path to a file.
59 //! The path class represents the route to a file or directory in the
60 //! file system. All file manipulation operations use this class to
61 //! represent their arguments as it takes care of normalizing user-provided
62 //! strings and ensures they are valid.
64 //! It is important to note that the file pointed to by a path need not
69 //! \brief Internal representation of a path.
74 //! \brief Constructs a new path from a user-provided string.
76 //! This constructor takes a string, either provided by the program's
77 //! code or by the user and constructs a new path object. The string
78 //! is normalized to not contain multiple delimiters together and to
79 //! remove any trailing one.
81 //! The input string cannot be empty.
83 explicit path(const std::string&);
86 //! \brief Copy constructor.
91 //! \brief Copy constructor.
93 path(const atf_fs_path_t *);
96 //! \brief Destructor for the path class.
101 //! \brief Returns a pointer to a C-style string representing this path.
103 const char* c_str(void) const;
106 //! \brief Returns a pointer to the implementation data.
108 const atf_fs_path_t* c_path(void) const;
111 //! \brief Returns a string representing this path.
112 //! XXX Really needed?
114 std::string str(void) const;
117 //! \brief Returns the branch path of this path.
119 //! Calculates and returns the branch path of this path. In other
120 //! words, it returns what the standard ::dirname function would return.
122 path branch_path(void) const;
125 //! \brief Returns the leaf name of this path.
127 //! Calculates and returns the leaf name of this path. In other words,
128 //! it returns what the standard ::basename function would return.
130 std::string leaf_name(void) const;
133 //! \brief Checks whether this path is absolute or not.
135 //! Returns a boolean indicating if this is an absolute path or not;
136 //! i.e. if it starts with a slash.
138 bool is_absolute(void) const;
141 //! \brief Checks whether this path points to the root directory or not.
143 //! Returns a boolean indicating if this is path points to the root
144 //! directory or not. The checks made by this are extremely simple (so
145 //! the results cannot always be trusted) but they are enough for our
146 //! modest sanity-checking needs. I.e. "/../" could return false.
148 bool is_root(void) const;
151 //! \brief Converts the path to be absolute.
153 //! \pre The path was not absolute.
155 path to_absolute(void) const;
158 //! \brief Assignment operator.
160 path& operator=(const path&);
163 //! \brief Checks if two paths are equal.
165 bool operator==(const path&) const;
168 //! \brief Checks if two paths are different.
170 bool operator!=(const path&) const;
173 //! \brief Concatenates a path with a string.
175 //! Constructs a new path object that is the concatenation of the
176 //! left-hand path with the right-hand string. The string is normalized
177 //! before the concatenation, and a path delimiter is introduced between
178 //! the two components if needed.
180 path operator/(const std::string&) const;
183 //! \brief Concatenates a path with another path.
185 //! Constructs a new path object that is the concatenation of the
186 //! left-hand path with the right-hand one. A path delimiter is
187 //! introduced between the two components if needed.
189 path operator/(const path&) const;
192 //! \brief Checks if a path has to be sorted before another one
193 //! lexicographically.
195 bool operator<(const path&) const;
198 // ------------------------------------------------------------------------
199 // The "file_info" class.
200 // ------------------------------------------------------------------------
205 //! \brief A class that contains information about a file.
207 //! The file_info class holds information about an specific file that
208 //! exists in the file system.
211 atf_fs_stat_t m_stat;
215 //! \brief The file's type.
217 static const int blk_type;
218 static const int chr_type;
219 static const int dir_type;
220 static const int fifo_type;
221 static const int lnk_type;
222 static const int reg_type;
223 static const int sock_type;
224 static const int wht_type;
227 //! \brief Constructs a new file_info based on a given file.
229 //! This constructor creates a new file_info object and fills it with
230 //! the data returned by ::stat when run on the given file, which must
233 explicit file_info(const path&);
236 //! \brief The copy constructor.
238 file_info(const file_info&);
241 //! \brief The destructor.
246 //! \brief Returns the device containing the file.
248 dev_t get_device(void) const;
251 //! \brief Returns the file's inode.
253 ino_t get_inode(void) const;
256 //! \brief Returns the file's permissions.
258 mode_t get_mode(void) const;
261 //! \brief Returns the file's size.
263 off_t get_size(void) const;
266 //! \brief Returns the file's type.
268 int get_type(void) const;
271 //! \brief Returns whether the file is readable by its owner or not.
273 bool is_owner_readable(void) const;
276 //! \brief Returns whether the file is writable by its owner or not.
278 bool is_owner_writable(void) const;
281 //! \brief Returns whether the file is executable by its owner or not.
283 bool is_owner_executable(void) const;
286 //! \brief Returns whether the file is readable by the users belonging
287 //! to its group or not.
289 bool is_group_readable(void) const;
292 //! \brief Returns whether the file is writable the users belonging to
293 //! its group or not.
295 bool is_group_writable(void) const;
298 //! \brief Returns whether the file is executable by the users
299 //! belonging to its group or not.
301 bool is_group_executable(void) const;
304 //! \brief Returns whether the file is readable by people different
305 //! than the owner and those belonging to the group or not.
307 bool is_other_readable(void) const;
310 //! \brief Returns whether the file is write by people different
311 //! than the owner and those belonging to the group or not.
313 bool is_other_writable(void) const;
316 //! \brief Returns whether the file is executable by people different
317 //! than the owner and those belonging to the group or not.
319 bool is_other_executable(void) const;
322 // ------------------------------------------------------------------------
323 // The "directory" class.
324 // ------------------------------------------------------------------------
327 //! \brief A class representing a file system directory.
329 //! The directory class represents a group of files in the file system and
330 //! corresponds to exactly one directory.
332 class directory : public std::map< std::string, file_info > {
335 //! \brief Constructs a new directory.
337 //! Constructs a new directory object representing the given path.
338 //! The directory must exist at creation time as the contents of the
339 //! class are gathered from it.
341 directory(const path&);
344 //! \brief Returns the file names of the files in the directory.
346 //! Returns the leaf names of all files contained in the directory.
347 //! I.e. the keys of the directory map.
349 std::set< std::string > names(void) const;
352 // ------------------------------------------------------------------------
354 // ------------------------------------------------------------------------
357 //! \brief Checks if the given path exists.
359 bool exists(const path&);
362 //! \brief Looks for the given program in the PATH.
364 //! Given a program name (without slashes) looks for it in the path and
365 //! returns its full path name if found, otherwise an empty path.
367 bool have_prog_in_path(const std::string&);
370 //! \brief Checks if the given path exists, is accessible and is executable.
372 bool is_executable(const path&);
375 //! \brief Removes a given file.
377 void remove(const path&);
380 //! \brief Removes an empty directory.
382 void rmdir(const path&);
387 #endif // !defined(ATF_CXX_DETAIL_FS_HPP)