fs.hpp revision 240116
1240116Smarcel//
2240116Smarcel// Automated Testing Framework (atf)
3240116Smarcel//
4240116Smarcel// Copyright (c) 2007 The NetBSD Foundation, Inc.
5240116Smarcel// All rights reserved.
6240116Smarcel//
7240116Smarcel// Redistribution and use in source and binary forms, with or without
8240116Smarcel// modification, are permitted provided that the following conditions
9240116Smarcel// are met:
10240116Smarcel// 1. Redistributions of source code must retain the above copyright
11240116Smarcel//    notice, this list of conditions and the following disclaimer.
12240116Smarcel// 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel//    notice, this list of conditions and the following disclaimer in the
14240116Smarcel//    documentation and/or other materials provided with the distribution.
15240116Smarcel//
16240116Smarcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17240116Smarcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18240116Smarcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19240116Smarcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20240116Smarcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21240116Smarcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23240116Smarcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24240116Smarcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25240116Smarcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26240116Smarcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27240116Smarcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28240116Smarcel//
29240116Smarcel
30240116Smarcel#if !defined(_ATF_CXX_FS_HPP_)
31240116Smarcel#define _ATF_CXX_FS_HPP_
32240116Smarcel
33240116Smarcelextern "C" {
34240116Smarcel#include <sys/types.h>
35240116Smarcel}
36240116Smarcel
37240116Smarcel#include <map>
38240116Smarcel#include <memory>
39240116Smarcel#include <ostream>
40240116Smarcel#include <set>
41240116Smarcel#include <stdexcept>
42240116Smarcel#include <string>
43240116Smarcel
44240116Smarcelextern "C" {
45240116Smarcel#include "../../atf-c/detail/fs.h"
46240116Smarcel}
47240116Smarcel
48240116Smarcelnamespace atf {
49240116Smarcel
50240116Smarcelnamespace io {
51240116Smarcelclass systembuf;
52240116Smarcel} // namespace io
53240116Smarcel
54240116Smarcelnamespace fs {
55240116Smarcel
56240116Smarcel// ------------------------------------------------------------------------
57240116Smarcel// The "path" class.
58240116Smarcel// ------------------------------------------------------------------------
59240116Smarcel
60240116Smarcel//!
61240116Smarcel//! \brief A class to represent a path to a file.
62240116Smarcel//!
63240116Smarcel//! The path class represents the route to a file or directory in the
64240116Smarcel//! file system.  All file manipulation operations use this class to
65240116Smarcel//! represent their arguments as it takes care of normalizing user-provided
66240116Smarcel//! strings and ensures they are valid.
67240116Smarcel//!
68240116Smarcel//! It is important to note that the file pointed to by a path need not
69240116Smarcel//! exist.
70240116Smarcel//!
71240116Smarcelclass path {
72240116Smarcel    //!
73240116Smarcel    //! \brief Internal representation of a path.
74240116Smarcel    //!
75240116Smarcel    atf_fs_path_t m_path;
76240116Smarcel
77240116Smarcelpublic:
78240116Smarcel    //! \brief Constructs a new path from a user-provided string.
79240116Smarcel    //!
80240116Smarcel    //! This constructor takes a string, either provided by the program's
81240116Smarcel    //! code or by the user and constructs a new path object.  The string
82240116Smarcel    //! is normalized to not contain multiple delimiters together and to
83240116Smarcel    //! remove any trailing one.
84240116Smarcel    //!
85240116Smarcel    //! The input string cannot be empty.
86240116Smarcel    //!
87240116Smarcel    explicit path(const std::string&);
88240116Smarcel
89240116Smarcel    //!
90240116Smarcel    //! \brief Copy constructor.
91240116Smarcel    //!
92240116Smarcel    path(const path&);
93240116Smarcel
94240116Smarcel    //!
95240116Smarcel    //! \brief Copy constructor.
96240116Smarcel    //!
97240116Smarcel    path(const atf_fs_path_t *);
98240116Smarcel
99240116Smarcel    //!
100240116Smarcel    //! \brief Destructor for the path class.
101240116Smarcel    //!
102240116Smarcel    ~path(void);
103240116Smarcel
104240116Smarcel    //!
105240116Smarcel    //! \brief Returns a pointer to a C-style string representing this path.
106240116Smarcel    //!
107240116Smarcel    const char* c_str(void) const;
108240116Smarcel
109240116Smarcel    //!
110240116Smarcel    //! \brief Returns a pointer to the implementation data.
111240116Smarcel    //!
112240116Smarcel    const atf_fs_path_t* c_path(void) const;
113240116Smarcel
114240116Smarcel    //!
115240116Smarcel    //! \brief Returns a string representing this path.
116240116Smarcel    //! XXX Really needed?
117240116Smarcel    //!
118240116Smarcel    std::string str(void) const;
119240116Smarcel
120240116Smarcel    //!
121240116Smarcel    //! \brief Returns the branch path of this path.
122240116Smarcel    //!
123240116Smarcel    //! Calculates and returns the branch path of this path.  In other
124240116Smarcel    //! words, it returns what the standard ::dirname function would return.
125240116Smarcel    //!
126240116Smarcel    path branch_path(void) const;
127240116Smarcel
128240116Smarcel    //!
129240116Smarcel    //! \brief Returns the leaf name of this path.
130240116Smarcel    //!
131240116Smarcel    //! Calculates and returns the leaf name of this path.  In other words,
132240116Smarcel    //! it returns what the standard ::basename function would return.
133240116Smarcel    //!
134240116Smarcel    std::string leaf_name(void) const;
135240116Smarcel
136240116Smarcel    //!
137240116Smarcel    //! \brief Checks whether this path is absolute or not.
138240116Smarcel    //!
139240116Smarcel    //! Returns a boolean indicating if this is an absolute path or not;
140240116Smarcel    //! i.e. if it starts with a slash.
141240116Smarcel    //!
142240116Smarcel    bool is_absolute(void) const;
143240116Smarcel
144240116Smarcel    //!
145240116Smarcel    //! \brief Checks whether this path points to the root directory or not.
146240116Smarcel    //!
147240116Smarcel    //! Returns a boolean indicating if this is path points to the root
148240116Smarcel    //! directory or not.  The checks made by this are extremely simple (so
149240116Smarcel    //! the results cannot always be trusted) but they are enough for our
150240116Smarcel    //! modest sanity-checking needs.  I.e. "/../" could return false.
151240116Smarcel    //!
152240116Smarcel    bool is_root(void) const;
153240116Smarcel
154240116Smarcel    //!
155240116Smarcel    //! \brief Converts the path to be absolute.
156240116Smarcel    //!
157240116Smarcel    //! \pre The path was not absolute.
158240116Smarcel    //!
159240116Smarcel    path to_absolute(void) const;
160240116Smarcel
161240116Smarcel    //!
162240116Smarcel    //! \brief Assignment operator.
163240116Smarcel    //!
164240116Smarcel    path& operator=(const path&);
165240116Smarcel
166240116Smarcel    //!
167240116Smarcel    //! \brief Checks if two paths are equal.
168240116Smarcel    //!
169240116Smarcel    bool operator==(const path&) const;
170240116Smarcel
171240116Smarcel    //!
172240116Smarcel    //! \brief Checks if two paths are different.
173240116Smarcel    //!
174240116Smarcel    bool operator!=(const path&) const;
175240116Smarcel
176240116Smarcel    //!
177240116Smarcel    //! \brief Concatenates a path with a string.
178240116Smarcel    //!
179240116Smarcel    //! Constructs a new path object that is the concatenation of the
180240116Smarcel    //! left-hand path with the right-hand string.  The string is normalized
181240116Smarcel    //! before the concatenation, and a path delimiter is introduced between
182240116Smarcel    //! the two components if needed.
183240116Smarcel    //!
184240116Smarcel    path operator/(const std::string&) const;
185240116Smarcel
186240116Smarcel    //!
187240116Smarcel    //! \brief Concatenates a path with another path.
188240116Smarcel    //!
189240116Smarcel    //! Constructs a new path object that is the concatenation of the
190240116Smarcel    //! left-hand path with the right-hand one. A path delimiter is
191240116Smarcel    //! introduced between the two components if needed.
192240116Smarcel    //!
193240116Smarcel    path operator/(const path&) const;
194240116Smarcel
195240116Smarcel    //!
196240116Smarcel    //! \brief Checks if a path has to be sorted before another one
197240116Smarcel    //!        lexicographically.
198240116Smarcel    //!
199240116Smarcel    bool operator<(const path&) const;
200240116Smarcel};
201240116Smarcel
202240116Smarcel// ------------------------------------------------------------------------
203240116Smarcel// The "file_info" class.
204240116Smarcel// ------------------------------------------------------------------------
205240116Smarcel
206240116Smarcelclass directory;
207240116Smarcel
208240116Smarcel//!
209240116Smarcel//! \brief A class that contains information about a file.
210240116Smarcel//!
211240116Smarcel//! The file_info class holds information about an specific file that
212240116Smarcel//! exists in the file system.
213240116Smarcel//!
214240116Smarcelclass file_info {
215240116Smarcel    atf_fs_stat_t m_stat;
216240116Smarcel
217240116Smarcelpublic:
218240116Smarcel    //!
219240116Smarcel    //! \brief The file's type.
220240116Smarcel    //!
221240116Smarcel    static const int blk_type;
222240116Smarcel    static const int chr_type;
223240116Smarcel    static const int dir_type;
224240116Smarcel    static const int fifo_type;
225240116Smarcel    static const int lnk_type;
226240116Smarcel    static const int reg_type;
227240116Smarcel    static const int sock_type;
228240116Smarcel    static const int wht_type;
229240116Smarcel
230240116Smarcel    //!
231240116Smarcel    //! \brief Constructs a new file_info based on a given file.
232240116Smarcel    //!
233240116Smarcel    //! This constructor creates a new file_info object and fills it with
234240116Smarcel    //! the data returned by ::stat when run on the given file, which must
235240116Smarcel    //! exist.
236240116Smarcel    //!
237240116Smarcel    explicit file_info(const path&);
238240116Smarcel
239240116Smarcel    //!
240240116Smarcel    //! \brief The copy constructor.
241240116Smarcel    //!
242240116Smarcel    file_info(const file_info&);
243240116Smarcel
244240116Smarcel    //!
245240116Smarcel    //! \brief The destructor.
246240116Smarcel    //!
247240116Smarcel    ~file_info(void);
248240116Smarcel
249240116Smarcel    //!
250240116Smarcel    //! \brief Returns the device containing the file.
251240116Smarcel    //!
252240116Smarcel    dev_t get_device(void) const;
253240116Smarcel
254240116Smarcel    //!
255240116Smarcel    //! \brief Returns the file's inode.
256240116Smarcel    //!
257240116Smarcel    ino_t get_inode(void) const;
258240116Smarcel
259240116Smarcel    //!
260240116Smarcel    //! \brief Returns the file's permissions.
261240116Smarcel    //!
262240116Smarcel    mode_t get_mode(void) const;
263240116Smarcel
264240116Smarcel    //!
265240116Smarcel    //! \brief Returns the file's size.
266240116Smarcel    //!
267240116Smarcel    off_t get_size(void) const;
268240116Smarcel
269240116Smarcel    //!
270240116Smarcel    //! \brief Returns the file's type.
271240116Smarcel    //!
272240116Smarcel    int get_type(void) const;
273240116Smarcel
274240116Smarcel    //!
275240116Smarcel    //! \brief Returns whether the file is readable by its owner or not.
276240116Smarcel    //!
277240116Smarcel    bool is_owner_readable(void) const;
278240116Smarcel
279240116Smarcel    //!
280240116Smarcel    //! \brief Returns whether the file is writable by its owner or not.
281240116Smarcel    //!
282240116Smarcel    bool is_owner_writable(void) const;
283240116Smarcel
284240116Smarcel    //!
285240116Smarcel    //! \brief Returns whether the file is executable by its owner or not.
286240116Smarcel    //!
287240116Smarcel    bool is_owner_executable(void) const;
288240116Smarcel
289240116Smarcel    //!
290240116Smarcel    //! \brief Returns whether the file is readable by the users belonging
291240116Smarcel    //! to its group or not.
292240116Smarcel    //!
293240116Smarcel    bool is_group_readable(void) const;
294240116Smarcel
295240116Smarcel    //!
296240116Smarcel    //! \brief Returns whether the file is writable the users belonging to
297240116Smarcel    //! its group or not.
298240116Smarcel    //!
299240116Smarcel    bool is_group_writable(void) const;
300240116Smarcel
301240116Smarcel    //!
302240116Smarcel    //! \brief Returns whether the file is executable by the users
303240116Smarcel    //! belonging to its group or not.
304240116Smarcel    //!
305240116Smarcel    bool is_group_executable(void) const;
306240116Smarcel
307240116Smarcel    //!
308240116Smarcel    //! \brief Returns whether the file is readable by people different
309240116Smarcel    //! than the owner and those belonging to the group or not.
310240116Smarcel    //!
311240116Smarcel    bool is_other_readable(void) const;
312240116Smarcel
313240116Smarcel    //!
314240116Smarcel    //! \brief Returns whether the file is write by people different
315240116Smarcel    //! than the owner and those belonging to the group or not.
316240116Smarcel    //!
317240116Smarcel    bool is_other_writable(void) const;
318240116Smarcel
319240116Smarcel    //!
320240116Smarcel    //! \brief Returns whether the file is executable by people different
321240116Smarcel    //! than the owner and those belonging to the group or not.
322240116Smarcel    //!
323240116Smarcel    bool is_other_executable(void) const;
324240116Smarcel};
325240116Smarcel
326240116Smarcel// ------------------------------------------------------------------------
327240116Smarcel// The "directory" class.
328240116Smarcel// ------------------------------------------------------------------------
329240116Smarcel
330240116Smarcel//!
331240116Smarcel//! \brief A class representing a file system directory.
332240116Smarcel//!
333240116Smarcel//! The directory class represents a group of files in the file system and
334240116Smarcel//! corresponds to exactly one directory.
335240116Smarcel//!
336240116Smarcelclass directory : public std::map< std::string, file_info > {
337240116Smarcelpublic:
338240116Smarcel    //!
339240116Smarcel    //! \brief Constructs a new directory.
340240116Smarcel    //!
341240116Smarcel    //! Constructs a new directory object representing the given path.
342240116Smarcel    //! The directory must exist at creation time as the contents of the
343240116Smarcel    //! class are gathered from it.
344240116Smarcel    //!
345240116Smarcel    directory(const path&);
346240116Smarcel
347240116Smarcel    //!
348240116Smarcel    //! \brief Returns the file names of the files in the directory.
349240116Smarcel    //!
350240116Smarcel    //! Returns the leaf names of all files contained in the directory.
351240116Smarcel    //! I.e. the keys of the directory map.
352240116Smarcel    //!
353240116Smarcel    std::set< std::string > names(void) const;
354240116Smarcel};
355240116Smarcel
356240116Smarcel// ------------------------------------------------------------------------
357240116Smarcel// Free functions.
358240116Smarcel// ------------------------------------------------------------------------
359240116Smarcel
360240116Smarcel//!
361240116Smarcel//! \brief Checks if the given path exists.
362240116Smarcel//!
363240116Smarcelbool exists(const path&);
364240116Smarcel
365240116Smarcel//!
366240116Smarcel//! \brief Looks for the given program in the PATH.
367240116Smarcel//!
368240116Smarcel//! Given a program name (without slashes) looks for it in the path and
369240116Smarcel//! returns its full path name if found, otherwise an empty path.
370240116Smarcel//!
371240116Smarcelbool have_prog_in_path(const std::string&);
372240116Smarcel
373240116Smarcel//!
374240116Smarcel//! \brief Checks if the given path exists, is accessible and is executable.
375240116Smarcel//!
376240116Smarcelbool is_executable(const path&);
377240116Smarcel
378240116Smarcel//!
379240116Smarcel//! \brief Removes a given file.
380240116Smarcel//!
381240116Smarcelvoid remove(const path&);
382240116Smarcel
383240116Smarcel//!
384240116Smarcel//! \brief Removes an empty directory.
385240116Smarcel//!
386240116Smarcelvoid rmdir(const path&);
387240116Smarcel
388240116Smarcel} // namespace fs
389240116Smarcel} // namespace atf
390240116Smarcel
391240116Smarcel#endif // !defined(_ATF_CXX_FS_HPP_)
392