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