Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c++ / fs.hpp
blobea220d28a4d0dc095613ce9094ac85a5db190efd
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
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_
33 extern "C" {
34 #include <sys/types.h>
37 #include <map>
38 #include <memory>
39 #include <ostream>
40 #include <set>
41 #include <stdexcept>
42 #include <string>
44 extern "C" {
45 #include "atf-c/fs.h"
48 namespace atf {
50 namespace io {
51 class systembuf;
52 } // namespace io
54 namespace fs {
56 // ------------------------------------------------------------------------
57 // The "path" class.
58 // ------------------------------------------------------------------------
60 //!
61 //! \brief A class to represent a path to a file.
62 //!
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.
67 //!
68 //! It is important to note that the file pointed to by a path need not
69 //! exist.
70 //!
71 class path {
72 //!
73 //! \brief Internal representation of a path.
74 //!
75 atf_fs_path_t m_path;
77 public:
78 //! \brief Constructs a new path from a user-provided string.
79 //!
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.
84 //!
85 //! The input string cannot be empty.
86 //!
87 explicit path(const std::string&);
89 //!
90 //! \brief Copy constructor.
91 //!
92 path(const path&);
94 //!
95 //! \brief Copy constructor.
96 //!
97 path(const atf_fs_path_t *);
99 //!
100 //! \brief Destructor for the path class.
102 ~path(void);
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 // ------------------------------------------------------------------------
206 class directory;
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.
214 class file_info {
215 atf_fs_stat_t m_stat;
217 public:
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
235 //! exist.
237 explicit file_info(const path&);
240 //! \brief The copy constructor.
242 file_info(const file_info&);
245 //! \brief The destructor.
247 ~file_info(void);
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 > {
337 public:
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 // ------------------------------------------------------------------------
357 // The "temp_dir" class.
358 // ------------------------------------------------------------------------
361 //! \brief A RAII model for temporary directories.
363 //! The temp_dir class provides a RAII model for temporary directories.
364 //! During construction, a safe temporary directory is created and during
365 //! destruction it is carefully removed by making use of the cleanup
366 //! function.
368 class temp_dir {
370 //! \brief The path to this temporary directory.
372 std::auto_ptr< path > m_path;
374 public:
376 //! \brief Creates a new temporary directory.
378 //! Creates a new temporary directory based on the provided name
379 //! template. The template must end with six X characters preceded
380 //! by a dot. These characters are replaced with a unique name on
381 //! the file system as described in mkdtemp(3).
383 temp_dir(const path&);
386 //! \brief Destroys the temporary directory.
388 //! Destroys this temporary directory object as well as its file
389 //! system representation.
391 ~temp_dir(void);
394 //! \brief Returns the path to this temporary directory.
396 const path& get_path(void) const;
399 // ------------------------------------------------------------------------
400 // The "temp_file" class.
401 // ------------------------------------------------------------------------
404 //! \brief A RAII model for temporary files.
406 //! The temp_file class provides a RAII model for temporary files.
407 //! During construction, a safe temporary file is created and during
408 //! destruction it is carefully removed by making use of the cleanup
409 //! function.
411 class temp_file : public std::ostream {
413 //! \brief The path to this temporary file.
415 std::auto_ptr< path > m_path;
418 //! \brief File descriptor of this temporary file.
420 int m_fd;
423 //! \brief Buffer backing this stream.
425 std::auto_ptr< io::systembuf > m_systembuf;
427 public:
429 //! \brief Creates a new temporary file.
431 //! Creates a new temporary file based on the provided name
432 //! template. The template must end with six X characters preceded
433 //! by a dot. These characters are replaced with a unique name on
434 //! the file system as described in mkdtemp(3).
436 temp_file(const path&);
439 //! \brief Destroys the temporary file.
441 //! Destroys this temporary file object as well as its file
442 //! system representation.
444 ~temp_file(void);
447 //! \brief Returns the path to this temporary file.
449 const path& get_path(void) const;
452 //! \brief Closes the file, but does not delete it.
454 void close(void);
457 //! \brief Gets the file descriptor.
459 //! XXX This is very ugly because, should we use this object as a
460 //! stream, accessing the underlying file descriptor is dangerous.
462 int fd(void);
465 // ------------------------------------------------------------------------
466 // Free functions.
467 // ------------------------------------------------------------------------
470 //! \brief Changes the current working directory.
472 //! Changes the current working directory to the given path. Returns the
473 //! path to the directory just left.
475 //! \throw system_error If ::chdir failed.
477 path change_directory(const path&);
480 //! \brief Checks if the given path exists.
482 bool exists(const path&);
485 //! \brief Looks for the given program in the PATH.
487 //! Given a program name (without slashes) looks for it in the path and
488 //! returns its full path name if found, otherwise an empty path.
490 bool have_prog_in_path(const std::string&);
493 //! \brief Returns the path to the current working directory.
495 //! Calculates and returns the path to the current working directory, which
496 //! is guessed using the ::getcwd function.
498 //! \throw system_error If ::getcwd failed.
500 path get_current_dir(void);
503 //! \brief Checks if the given path exists, is accessible and is executable.
505 bool is_executable(const path&);
508 //! \brief Removes a given file.
510 void remove(const path&);
513 //! \brief Recursively cleans up a directory.
515 //! This function cleans up a directory hierarchy. First of all, it looks
516 //! for any file system that may be mounted under the given path and, if
517 //! any is found, an attempt is made to unmount it. Later on, the
518 //! directory is removed alongside all of its contents.
520 void cleanup(const path&);
523 //! \brief Removes an empty directory.
525 void rmdir(const path&);
528 //! \brief Gets the current umask.
530 mode_t current_umask(void);
532 } // namespace fs
533 } // namespace atf
535 #endif // !defined(_ATF_CXX_FS_HPP_)