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