Home | History | Annotate | Line # | Download | only in tools
      1  1.1      jmmv //
      2  1.1      jmmv // Automated Testing Framework (atf)
      3  1.1      jmmv //
      4  1.1      jmmv // Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.1      jmmv // All rights reserved.
      6  1.1      jmmv //
      7  1.1      jmmv // Redistribution and use in source and binary forms, with or without
      8  1.1      jmmv // modification, are permitted provided that the following conditions
      9  1.1      jmmv // are met:
     10  1.1      jmmv // 1. Redistributions of source code must retain the above copyright
     11  1.1      jmmv //    notice, this list of conditions and the following disclaimer.
     12  1.1      jmmv // 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      jmmv //    notice, this list of conditions and the following disclaimer in the
     14  1.1      jmmv //    documentation and/or other materials provided with the distribution.
     15  1.1      jmmv //
     16  1.1      jmmv // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17  1.1      jmmv // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  1.1      jmmv // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  1.1      jmmv // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1      jmmv // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21  1.1      jmmv // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1      jmmv // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  1.1      jmmv // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1      jmmv // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  1.1      jmmv // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.1      jmmv // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  1.1      jmmv // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1      jmmv //
     29  1.1      jmmv 
     30  1.1      jmmv #if !defined(TOOLS_FS_HPP)
     31  1.1      jmmv #define TOOLS_FS_HPP
     32  1.1      jmmv 
     33  1.1      jmmv extern "C" {
     34  1.1      jmmv #include <sys/types.h>
     35  1.1      jmmv #include <sys/stat.h>
     36  1.1      jmmv }
     37  1.1      jmmv 
     38  1.1      jmmv #include <map>
     39  1.1      jmmv #include <memory>
     40  1.1      jmmv #include <ostream>
     41  1.1      jmmv #include <set>
     42  1.1      jmmv #include <stdexcept>
     43  1.1      jmmv #include <string>
     44  1.1      jmmv 
     45  1.1      jmmv namespace tools {
     46  1.1      jmmv namespace fs {
     47  1.1      jmmv 
     48  1.1      jmmv // ------------------------------------------------------------------------
     49  1.1      jmmv // The "path" class.
     50  1.1      jmmv // ------------------------------------------------------------------------
     51  1.1      jmmv 
     52  1.1      jmmv //!
     53  1.1      jmmv //! \brief A class to represent a path to a file.
     54  1.1      jmmv //!
     55  1.1      jmmv //! The path class represents the route to a file or directory in the
     56  1.1      jmmv //! file system.  All file manipulation operations use this class to
     57  1.1      jmmv //! represent their arguments as it takes care of normalizing user-provided
     58  1.1      jmmv //! strings and ensures they are valid.
     59  1.1      jmmv //!
     60  1.1      jmmv //! It is important to note that the file pointed to by a path need not
     61  1.1      jmmv //! exist.
     62  1.1      jmmv //!
     63  1.1      jmmv class path {
     64  1.1      jmmv     //!
     65  1.1      jmmv     //! \brief Internal representation of a path.
     66  1.1      jmmv     //!
     67  1.1      jmmv     std::string m_data;
     68  1.1      jmmv 
     69  1.1      jmmv public:
     70  1.1      jmmv     //! \brief Constructs a new path from a user-provided string.
     71  1.1      jmmv     //!
     72  1.1      jmmv     //! This constructor takes a string, either provided by the program's
     73  1.1      jmmv     //! code or by the user and constructs a new path object.  The string
     74  1.1      jmmv     //! is normalized to not contain multiple delimiters together and to
     75  1.1      jmmv     //! remove any trailing one.
     76  1.1      jmmv     //!
     77  1.1      jmmv     //! The input string cannot be empty.
     78  1.1      jmmv     //!
     79  1.1      jmmv     explicit path(const std::string&);
     80  1.1      jmmv 
     81  1.1      jmmv     //!
     82  1.1      jmmv     //! \brief Destructor for the path class.
     83  1.1      jmmv     //!
     84  1.1      jmmv     ~path(void);
     85  1.1      jmmv 
     86  1.1      jmmv     //!
     87  1.1      jmmv     //! \brief Returns a pointer to a C-style string representing this path.
     88  1.1      jmmv     //!
     89  1.1      jmmv     const char* c_str(void) const;
     90  1.1      jmmv 
     91  1.1      jmmv     //!
     92  1.1      jmmv     //! \brief Returns a string representing this path.
     93  1.1      jmmv     //! XXX Really needed?
     94  1.1      jmmv     //!
     95  1.1      jmmv     std::string str(void) const;
     96  1.1      jmmv 
     97  1.1      jmmv     //!
     98  1.1      jmmv     //! \brief Returns the branch path of this path.
     99  1.1      jmmv     //!
    100  1.1      jmmv     //! Calculates and returns the branch path of this path.  In other
    101  1.1      jmmv     //! words, it returns what the standard ::dirname function would return.
    102  1.1      jmmv     //!
    103  1.1      jmmv     path branch_path(void) const;
    104  1.1      jmmv 
    105  1.1      jmmv     //!
    106  1.1      jmmv     //! \brief Returns the leaf name of this path.
    107  1.1      jmmv     //!
    108  1.1      jmmv     //! Calculates and returns the leaf name of this path.  In other words,
    109  1.1      jmmv     //! it returns what the standard ::basename function would return.
    110  1.1      jmmv     //!
    111  1.1      jmmv     std::string leaf_name(void) const;
    112  1.1      jmmv 
    113  1.1      jmmv     //!
    114  1.1      jmmv     //! \brief Checks whether this path is absolute or not.
    115  1.1      jmmv     //!
    116  1.1      jmmv     //! Returns a boolean indicating if this is an absolute path or not;
    117  1.1      jmmv     //! i.e. if it starts with a slash.
    118  1.1      jmmv     //!
    119  1.1      jmmv     bool is_absolute(void) const;
    120  1.1      jmmv 
    121  1.1      jmmv     //!
    122  1.1      jmmv     //! \brief Checks whether this path points to the root directory or not.
    123  1.1      jmmv     //!
    124  1.1      jmmv     //! Returns a boolean indicating if this is path points to the root
    125  1.1      jmmv     //! directory or not.  The checks made by this are extremely simple (so
    126  1.1      jmmv     //! the results cannot always be trusted) but they are enough for our
    127  1.1      jmmv     //! modest sanity-checking needs.  I.e. "/../" could return false.
    128  1.1      jmmv     //!
    129  1.1      jmmv     bool is_root(void) const;
    130  1.1      jmmv 
    131  1.1      jmmv     //!
    132  1.1      jmmv     //! \brief Converts the path to be absolute.
    133  1.1      jmmv     //!
    134  1.1      jmmv     //! \pre The path was not absolute.
    135  1.1      jmmv     //!
    136  1.1      jmmv     path to_absolute(void) const;
    137  1.1      jmmv 
    138  1.1      jmmv     //!
    139  1.1      jmmv     //! \brief Checks if two paths are equal.
    140  1.1      jmmv     //!
    141  1.1      jmmv     bool operator==(const path&) const;
    142  1.1      jmmv 
    143  1.1      jmmv     //!
    144  1.1      jmmv     //! \brief Checks if two paths are different.
    145  1.1      jmmv     //!
    146  1.1      jmmv     bool operator!=(const path&) const;
    147  1.1      jmmv 
    148  1.1      jmmv     //!
    149  1.1      jmmv     //! \brief Concatenates a path with a string.
    150  1.1      jmmv     //!
    151  1.1      jmmv     //! Constructs a new path object that is the concatenation of the
    152  1.1      jmmv     //! left-hand path with the right-hand string.  The string is normalized
    153  1.1      jmmv     //! before the concatenation, and a path delimiter is introduced between
    154  1.1      jmmv     //! the two components if needed.
    155  1.1      jmmv     //!
    156  1.1      jmmv     path operator/(const std::string&) const;
    157  1.1      jmmv 
    158  1.1      jmmv     //!
    159  1.1      jmmv     //! \brief Concatenates a path with another path.
    160  1.1      jmmv     //!
    161  1.1      jmmv     //! Constructs a new path object that is the concatenation of the
    162  1.1      jmmv     //! left-hand path with the right-hand one. A path delimiter is
    163  1.1      jmmv     //! introduced between the two components if needed.
    164  1.1      jmmv     //!
    165  1.1      jmmv     path operator/(const path&) const;
    166  1.1      jmmv 
    167  1.1      jmmv     //!
    168  1.1      jmmv     //! \brief Checks if a path has to be sorted before another one
    169  1.1      jmmv     //!        lexicographically.
    170  1.1      jmmv     //!
    171  1.1      jmmv     bool operator<(const path&) const;
    172  1.1      jmmv };
    173  1.1      jmmv 
    174  1.1      jmmv // ------------------------------------------------------------------------
    175  1.1      jmmv // The "file_info" class.
    176  1.1      jmmv // ------------------------------------------------------------------------
    177  1.1      jmmv 
    178  1.1      jmmv class directory;
    179  1.1      jmmv 
    180  1.1      jmmv //!
    181  1.1      jmmv //! \brief A class that contains information about a file.
    182  1.1      jmmv //!
    183  1.1      jmmv //! The file_info class holds information about an specific file that
    184  1.1      jmmv //! exists in the file system.
    185  1.1      jmmv //!
    186  1.1      jmmv class file_info {
    187  1.1      jmmv     int m_type;
    188  1.1      jmmv     struct stat m_sb;
    189  1.1      jmmv 
    190  1.1      jmmv public:
    191  1.1      jmmv     //!
    192  1.1      jmmv     //! \brief The file's type.
    193  1.1      jmmv     //!
    194  1.1      jmmv     static const int blk_type;
    195  1.1      jmmv     static const int chr_type;
    196  1.1      jmmv     static const int dir_type;
    197  1.1      jmmv     static const int fifo_type;
    198  1.1      jmmv     static const int lnk_type;
    199  1.1      jmmv     static const int reg_type;
    200  1.1      jmmv     static const int sock_type;
    201  1.1      jmmv     static const int wht_type;
    202  1.1      jmmv 
    203  1.1      jmmv     //!
    204  1.1      jmmv     //! \brief Constructs a new file_info based on a given file.
    205  1.1      jmmv     //!
    206  1.1      jmmv     //! This constructor creates a new file_info object and fills it with
    207  1.1      jmmv     //! the data returned by ::stat when run on the given file, which must
    208  1.1      jmmv     //! exist.
    209  1.1      jmmv     //!
    210  1.1      jmmv     explicit file_info(const path&);
    211  1.1      jmmv 
    212  1.1      jmmv     //!
    213  1.1      jmmv     //! \brief The destructor.
    214  1.1      jmmv     //!
    215  1.1      jmmv     ~file_info(void);
    216  1.1      jmmv 
    217  1.1      jmmv     //!
    218  1.1      jmmv     //! \brief Returns the device containing the file.
    219  1.1      jmmv     //!
    220  1.1      jmmv     dev_t get_device(void) const;
    221  1.1      jmmv 
    222  1.1      jmmv     //!
    223  1.1      jmmv     //! \brief Returns the file's inode.
    224  1.1      jmmv     //!
    225  1.1      jmmv     ino_t get_inode(void) const;
    226  1.1      jmmv 
    227  1.1      jmmv     //!
    228  1.1      jmmv     //! \brief Returns the file's permissions.
    229  1.1      jmmv     //!
    230  1.1      jmmv     mode_t get_mode(void) const;
    231  1.1      jmmv 
    232  1.1      jmmv     //!
    233  1.1      jmmv     //! \brief Returns the file's size.
    234  1.1      jmmv     //!
    235  1.1      jmmv     off_t get_size(void) const;
    236  1.1      jmmv 
    237  1.1      jmmv     //!
    238  1.1      jmmv     //! \brief Returns the file's type.
    239  1.1      jmmv     //!
    240  1.1      jmmv     int get_type(void) const;
    241  1.1      jmmv 
    242  1.1      jmmv     //!
    243  1.1      jmmv     //! \brief Returns whether the file is readable by its owner or not.
    244  1.1      jmmv     //!
    245  1.1      jmmv     bool is_owner_readable(void) const;
    246  1.1      jmmv 
    247  1.1      jmmv     //!
    248  1.1      jmmv     //! \brief Returns whether the file is writable by its owner or not.
    249  1.1      jmmv     //!
    250  1.1      jmmv     bool is_owner_writable(void) const;
    251  1.1      jmmv 
    252  1.1      jmmv     //!
    253  1.1      jmmv     //! \brief Returns whether the file is executable by its owner or not.
    254  1.1      jmmv     //!
    255  1.1      jmmv     bool is_owner_executable(void) const;
    256  1.1      jmmv 
    257  1.1      jmmv     //!
    258  1.1      jmmv     //! \brief Returns whether the file is readable by the users belonging
    259  1.1      jmmv     //! to its group or not.
    260  1.1      jmmv     //!
    261  1.1      jmmv     bool is_group_readable(void) const;
    262  1.1      jmmv 
    263  1.1      jmmv     //!
    264  1.1      jmmv     //! \brief Returns whether the file is writable the users belonging to
    265  1.1      jmmv     //! its group or not.
    266  1.1      jmmv     //!
    267  1.1      jmmv     bool is_group_writable(void) const;
    268  1.1      jmmv 
    269  1.1      jmmv     //!
    270  1.1      jmmv     //! \brief Returns whether the file is executable by the users
    271  1.1      jmmv     //! belonging to its group or not.
    272  1.1      jmmv     //!
    273  1.1      jmmv     bool is_group_executable(void) const;
    274  1.1      jmmv 
    275  1.1      jmmv     //!
    276  1.1      jmmv     //! \brief Returns whether the file is readable by people different
    277  1.1      jmmv     //! than the owner and those belonging to the group or not.
    278  1.1      jmmv     //!
    279  1.1      jmmv     bool is_other_readable(void) const;
    280  1.1      jmmv 
    281  1.1      jmmv     //!
    282  1.1      jmmv     //! \brief Returns whether the file is write by people different
    283  1.1      jmmv     //! than the owner and those belonging to the group or not.
    284  1.1      jmmv     //!
    285  1.1      jmmv     bool is_other_writable(void) const;
    286  1.1      jmmv 
    287  1.1      jmmv     //!
    288  1.1      jmmv     //! \brief Returns whether the file is executable by people different
    289  1.1      jmmv     //! than the owner and those belonging to the group or not.
    290  1.1      jmmv     //!
    291  1.1      jmmv     bool is_other_executable(void) const;
    292  1.1      jmmv };
    293  1.1      jmmv 
    294  1.1      jmmv // ------------------------------------------------------------------------
    295  1.1      jmmv // The "directory" class.
    296  1.1      jmmv // ------------------------------------------------------------------------
    297  1.1      jmmv 
    298  1.1      jmmv //!
    299  1.1      jmmv //! \brief A class representing a file system directory.
    300  1.1      jmmv //!
    301  1.1      jmmv //! The directory class represents a group of files in the file system and
    302  1.1      jmmv //! corresponds to exactly one directory.
    303  1.1      jmmv //!
    304  1.1      jmmv class directory : public std::map< std::string, file_info > {
    305  1.1      jmmv public:
    306  1.1      jmmv     //!
    307  1.1      jmmv     //! \brief Constructs a new directory.
    308  1.1      jmmv     //!
    309  1.1      jmmv     //! Constructs a new directory object representing the given path.
    310  1.1      jmmv     //! The directory must exist at creation time as the contents of the
    311  1.1      jmmv     //! class are gathered from it.
    312  1.1      jmmv     //!
    313  1.1      jmmv     directory(const path&);
    314  1.1      jmmv 
    315  1.1      jmmv     //!
    316  1.1      jmmv     //! \brief Returns the file names of the files in the directory.
    317  1.1      jmmv     //!
    318  1.1      jmmv     //! Returns the leaf names of all files contained in the directory.
    319  1.1      jmmv     //! I.e. the keys of the directory map.
    320  1.1      jmmv     //!
    321  1.1      jmmv     std::set< std::string > names(void) const;
    322  1.1      jmmv };
    323  1.1      jmmv 
    324  1.1      jmmv // ------------------------------------------------------------------------
    325  1.1      jmmv // The "temp_dir" class.
    326  1.1      jmmv // ------------------------------------------------------------------------
    327  1.1      jmmv 
    328  1.1      jmmv class temp_dir {
    329  1.2  christos     std::unique_ptr< tools::fs::path > m_path;
    330  1.1      jmmv 
    331  1.1      jmmv public:
    332  1.1      jmmv     temp_dir(const tools::fs::path&);
    333  1.1      jmmv     ~temp_dir(void);
    334  1.1      jmmv 
    335  1.1      jmmv     const tools::fs::path& get_path(void) const;
    336  1.1      jmmv };
    337  1.1      jmmv 
    338  1.1      jmmv // ------------------------------------------------------------------------
    339  1.1      jmmv // Free functions.
    340  1.1      jmmv // ------------------------------------------------------------------------
    341  1.1      jmmv 
    342  1.1      jmmv //!
    343  1.1      jmmv //! \brief Checks if the given path exists.
    344  1.1      jmmv //!
    345  1.1      jmmv bool exists(const path&);
    346  1.1      jmmv 
    347  1.1      jmmv //!
    348  1.1      jmmv //! \brief Looks for the given program in the PATH.
    349  1.1      jmmv //!
    350  1.1      jmmv //! Given a program name (without slashes) looks for it in the path and
    351  1.1      jmmv //! returns its full path name if found, otherwise an empty path.
    352  1.1      jmmv //!
    353  1.1      jmmv bool have_prog_in_path(const std::string&);
    354  1.1      jmmv 
    355  1.1      jmmv //!
    356  1.1      jmmv //! \brief Checks if the given path exists, is accessible and is executable.
    357  1.1      jmmv //!
    358  1.1      jmmv bool is_executable(const path&);
    359  1.1      jmmv 
    360  1.1      jmmv //!
    361  1.1      jmmv //! \brief Removes a given file.
    362  1.1      jmmv //!
    363  1.1      jmmv void remove(const path&);
    364  1.1      jmmv 
    365  1.1      jmmv //!
    366  1.1      jmmv //! \brief Removes an empty directory.
    367  1.1      jmmv //!
    368  1.1      jmmv void rmdir(const path&);
    369  1.1      jmmv 
    370  1.1      jmmv tools::fs::path change_directory(const tools::fs::path&);
    371  1.3  christos void change_ownership(const tools::fs::path&, const std::pair< int, int >&);
    372  1.1      jmmv void cleanup(const tools::fs::path&);
    373  1.1      jmmv tools::fs::path get_current_dir(void);
    374  1.1      jmmv 
    375  1.1      jmmv } // namespace fs
    376  1.1      jmmv } // namespace tools
    377  1.1      jmmv 
    378  1.1      jmmv #endif // !defined(TOOLS_FS_HPP)
    379