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(TOOLS_FS_HPP)
34 #include <sys/types.h>
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
53 //! \brief A class to represent a path to a file.
55 //! The path class represents the route to a file or directory in the
56 //! file system. All file manipulation operations use this class to
57 //! represent their arguments as it takes care of normalizing user-provided
58 //! strings and ensures they are valid.
60 //! It is important to note that the file pointed to by a path need not
65 //! \brief Internal representation of a path.
70 //! \brief Constructs a new path from a user-provided string.
72 //! This constructor takes a string, either provided by the program's
73 //! code or by the user and constructs a new path object. The string
74 //! is normalized to not contain multiple delimiters together and to
75 //! remove any trailing one.
77 //! The input string cannot be empty.
79 explicit path(const std::string
&);
82 //! \brief Destructor for the path class.
87 //! \brief Returns a pointer to a C-style string representing this path.
89 const char* c_str(void) const;
92 //! \brief Returns a string representing this path.
93 //! XXX Really needed?
95 std::string
str(void) const;
98 //! \brief Returns the branch path of this path.
100 //! Calculates and returns the branch path of this path. In other
101 //! words, it returns what the standard ::dirname function would return.
103 path
branch_path(void) const;
106 //! \brief Returns the leaf name of this path.
108 //! Calculates and returns the leaf name of this path. In other words,
109 //! it returns what the standard ::basename function would return.
111 std::string
leaf_name(void) const;
114 //! \brief Checks whether this path is absolute or not.
116 //! Returns a boolean indicating if this is an absolute path or not;
117 //! i.e. if it starts with a slash.
119 bool is_absolute(void) const;
122 //! \brief Checks whether this path points to the root directory or not.
124 //! Returns a boolean indicating if this is path points to the root
125 //! directory or not. The checks made by this are extremely simple (so
126 //! the results cannot always be trusted) but they are enough for our
127 //! modest sanity-checking needs. I.e. "/../" could return false.
129 bool is_root(void) const;
132 //! \brief Converts the path to be absolute.
134 //! \pre The path was not absolute.
136 path
to_absolute(void) const;
139 //! \brief Checks if two paths are equal.
141 bool operator==(const path
&) const;
144 //! \brief Checks if two paths are different.
146 bool operator!=(const path
&) const;
149 //! \brief Concatenates a path with a string.
151 //! Constructs a new path object that is the concatenation of the
152 //! left-hand path with the right-hand string. The string is normalized
153 //! before the concatenation, and a path delimiter is introduced between
154 //! the two components if needed.
156 path
operator/(const std::string
&) const;
159 //! \brief Concatenates a path with another path.
161 //! Constructs a new path object that is the concatenation of the
162 //! left-hand path with the right-hand one. A path delimiter is
163 //! introduced between the two components if needed.
165 path
operator/(const path
&) const;
168 //! \brief Checks if a path has to be sorted before another one
169 //! lexicographically.
171 bool operator<(const path
&) const;
174 // ------------------------------------------------------------------------
175 // The "file_info" class.
176 // ------------------------------------------------------------------------
181 //! \brief A class that contains information about a file.
183 //! The file_info class holds information about an specific file that
184 //! exists in the file system.
192 //! \brief The file's type.
194 static const int blk_type
;
195 static const int chr_type
;
196 static const int dir_type
;
197 static const int fifo_type
;
198 static const int lnk_type
;
199 static const int reg_type
;
200 static const int sock_type
;
201 static const int wht_type
;
204 //! \brief Constructs a new file_info based on a given file.
206 //! This constructor creates a new file_info object and fills it with
207 //! the data returned by ::stat when run on the given file, which must
210 explicit file_info(const path
&);
213 //! \brief The destructor.
218 //! \brief Returns the device containing the file.
220 dev_t
get_device(void) const;
223 //! \brief Returns the file's inode.
225 ino_t
get_inode(void) const;
228 //! \brief Returns the file's permissions.
230 mode_t
get_mode(void) const;
233 //! \brief Returns the file's size.
235 off_t
get_size(void) const;
238 //! \brief Returns the file's type.
240 int get_type(void) const;
243 //! \brief Returns whether the file is readable by its owner or not.
245 bool is_owner_readable(void) const;
248 //! \brief Returns whether the file is writable by its owner or not.
250 bool is_owner_writable(void) const;
253 //! \brief Returns whether the file is executable by its owner or not.
255 bool is_owner_executable(void) const;
258 //! \brief Returns whether the file is readable by the users belonging
259 //! to its group or not.
261 bool is_group_readable(void) const;
264 //! \brief Returns whether the file is writable the users belonging to
265 //! its group or not.
267 bool is_group_writable(void) const;
270 //! \brief Returns whether the file is executable by the users
271 //! belonging to its group or not.
273 bool is_group_executable(void) const;
276 //! \brief Returns whether the file is readable by people different
277 //! than the owner and those belonging to the group or not.
279 bool is_other_readable(void) const;
282 //! \brief Returns whether the file is write by people different
283 //! than the owner and those belonging to the group or not.
285 bool is_other_writable(void) const;
288 //! \brief Returns whether the file is executable by people different
289 //! than the owner and those belonging to the group or not.
291 bool is_other_executable(void) const;
294 // ------------------------------------------------------------------------
295 // The "directory" class.
296 // ------------------------------------------------------------------------
299 //! \brief A class representing a file system directory.
301 //! The directory class represents a group of files in the file system and
302 //! corresponds to exactly one directory.
304 class directory
: public std::map
< std::string
, file_info
> {
307 //! \brief Constructs a new directory.
309 //! Constructs a new directory object representing the given path.
310 //! The directory must exist at creation time as the contents of the
311 //! class are gathered from it.
313 directory(const path
&);
316 //! \brief Returns the file names of the files in the directory.
318 //! Returns the leaf names of all files contained in the directory.
319 //! I.e. the keys of the directory map.
321 std::set
< std::string
> names(void) const;
324 // ------------------------------------------------------------------------
325 // The "temp_dir" class.
326 // ------------------------------------------------------------------------
329 std::auto_ptr
< tools::fs::path
> m_path
;
332 temp_dir(const tools::fs::path
&);
335 const tools::fs::path
& get_path(void) const;
338 // ------------------------------------------------------------------------
340 // ------------------------------------------------------------------------
343 //! \brief Checks if the given path exists.
345 bool exists(const path
&);
348 //! \brief Looks for the given program in the PATH.
350 //! Given a program name (without slashes) looks for it in the path and
351 //! returns its full path name if found, otherwise an empty path.
353 bool have_prog_in_path(const std::string
&);
356 //! \brief Checks if the given path exists, is accessible and is executable.
358 bool is_executable(const path
&);
361 //! \brief Removes a given file.
363 void remove(const path
&);
366 //! \brief Removes an empty directory.
368 void rmdir(const path
&);
370 tools::fs::path
change_directory(const tools::fs::path
&);
371 void cleanup(const tools::fs::path
&);
372 tools::fs::path
get_current_dir(void);
377 #endif // !defined(TOOLS_FS_HPP)