// Copyright (c) 2007 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#if !defined(ATF_CXX_DETAIL_FS_HPP)26#define ATF_CXX_DETAIL_FS_HPP2728extern "C" {29#include <sys/types.h>30}3132#include <map>33#include <memory>34#include <ostream>35#include <set>36#include <stdexcept>37#include <string>3839extern "C" {40#include "atf-c/detail/fs.h"41}4243namespace atf {4445namespace io {46class systembuf;47} // namespace io4849namespace fs {5051// ------------------------------------------------------------------------52// The "path" class.53// ------------------------------------------------------------------------5455//!56//! \brief A class to represent a path to a file.57//!58//! The path class represents the route to a file or directory in the59//! file system. All file manipulation operations use this class to60//! represent their arguments as it takes care of normalizing user-provided61//! strings and ensures they are valid.62//!63//! It is important to note that the file pointed to by a path need not64//! exist.65//!66class path {67//!68//! \brief Internal representation of a path.69//!70atf_fs_path_t m_path;7172public:73//! \brief Constructs a new path from a user-provided string.74//!75//! This constructor takes a string, either provided by the program's76//! code or by the user and constructs a new path object. The string77//! is normalized to not contain multiple delimiters together and to78//! remove any trailing one.79//!80//! The input string cannot be empty.81//!82explicit path(const std::string&);8384//!85//! \brief Copy constructor.86//!87path(const path&);8889//!90//! \brief Copy constructor.91//!92path(const atf_fs_path_t *);9394//!95//! \brief Destructor for the path class.96//!97~path(void);9899//!100//! \brief Returns a pointer to a C-style string representing this path.101//!102const char* c_str(void) const;103104//!105//! \brief Returns a pointer to the implementation data.106//!107const atf_fs_path_t* c_path(void) const;108109//!110//! \brief Returns a string representing this path.111//! XXX Really needed?112//!113std::string str(void) const;114115//!116//! \brief Returns the branch path of this path.117//!118//! Calculates and returns the branch path of this path. In other119//! words, it returns what the standard ::dirname function would return.120//!121path branch_path(void) const;122123//!124//! \brief Returns the leaf name of this path.125//!126//! Calculates and returns the leaf name of this path. In other words,127//! it returns what the standard ::basename function would return.128//!129std::string leaf_name(void) const;130131//!132//! \brief Checks whether this path is absolute or not.133//!134//! Returns a boolean indicating if this is an absolute path or not;135//! i.e. if it starts with a slash.136//!137bool is_absolute(void) const;138139//!140//! \brief Checks whether this path points to the root directory or not.141//!142//! Returns a boolean indicating if this is path points to the root143//! directory or not. The checks made by this are extremely simple (so144//! the results cannot always be trusted) but they are enough for our145//! modest sanity-checking needs. I.e. "/../" could return false.146//!147bool is_root(void) const;148149//!150//! \brief Converts the path to be absolute.151//!152//! \pre The path was not absolute.153//!154path to_absolute(void) const;155156//!157//! \brief Assignment operator.158//!159path& operator=(const path&);160161//!162//! \brief Checks if two paths are equal.163//!164bool operator==(const path&) const;165166//!167//! \brief Checks if two paths are different.168//!169bool operator!=(const path&) const;170171//!172//! \brief Concatenates a path with a string.173//!174//! Constructs a new path object that is the concatenation of the175//! left-hand path with the right-hand string. The string is normalized176//! before the concatenation, and a path delimiter is introduced between177//! the two components if needed.178//!179path operator/(const std::string&) const;180181//!182//! \brief Concatenates a path with another path.183//!184//! Constructs a new path object that is the concatenation of the185//! left-hand path with the right-hand one. A path delimiter is186//! introduced between the two components if needed.187//!188path operator/(const path&) const;189190//!191//! \brief Checks if a path has to be sorted before another one192//! lexicographically.193//!194bool operator<(const path&) const;195};196197// ------------------------------------------------------------------------198// The "file_info" class.199// ------------------------------------------------------------------------200201class directory;202203//!204//! \brief A class that contains information about a file.205//!206//! The file_info class holds information about an specific file that207//! exists in the file system.208//!209class file_info {210atf_fs_stat_t m_stat;211212public:213//!214//! \brief The file's type.215//!216static const int blk_type;217static const int chr_type;218static const int dir_type;219static const int fifo_type;220static const int lnk_type;221static const int reg_type;222static const int sock_type;223static const int wht_type;224225//!226//! \brief Constructs a new file_info based on a given file.227//!228//! This constructor creates a new file_info object and fills it with229//! the data returned by ::stat when run on the given file, which must230//! exist.231//!232explicit file_info(const path&);233234//!235//! \brief The copy constructor.236//!237file_info(const file_info&);238239//!240//! \brief The destructor.241//!242~file_info(void);243244//!245//! \brief Returns the device containing the file.246//!247dev_t get_device(void) const;248249//!250//! \brief Returns the file's inode.251//!252ino_t get_inode(void) const;253254//!255//! \brief Returns the file's permissions.256//!257mode_t get_mode(void) const;258259//!260//! \brief Returns the file's size.261//!262off_t get_size(void) const;263264//!265//! \brief Returns the file's type.266//!267int get_type(void) const;268269//!270//! \brief Returns whether the file is readable by its owner or not.271//!272bool is_owner_readable(void) const;273274//!275//! \brief Returns whether the file is writable by its owner or not.276//!277bool is_owner_writable(void) const;278279//!280//! \brief Returns whether the file is executable by its owner or not.281//!282bool is_owner_executable(void) const;283284//!285//! \brief Returns whether the file is readable by the users belonging286//! to its group or not.287//!288bool is_group_readable(void) const;289290//!291//! \brief Returns whether the file is writable the users belonging to292//! its group or not.293//!294bool is_group_writable(void) const;295296//!297//! \brief Returns whether the file is executable by the users298//! belonging to its group or not.299//!300bool is_group_executable(void) const;301302//!303//! \brief Returns whether the file is readable by people different304//! than the owner and those belonging to the group or not.305//!306bool is_other_readable(void) const;307308//!309//! \brief Returns whether the file is write by people different310//! than the owner and those belonging to the group or not.311//!312bool is_other_writable(void) const;313314//!315//! \brief Returns whether the file is executable by people different316//! than the owner and those belonging to the group or not.317//!318bool is_other_executable(void) const;319};320321// ------------------------------------------------------------------------322// The "directory" class.323// ------------------------------------------------------------------------324325//!326//! \brief A class representing a file system directory.327//!328//! The directory class represents a group of files in the file system and329//! corresponds to exactly one directory.330//!331class directory : public std::map< std::string, file_info > {332public:333//!334//! \brief Constructs a new directory.335//!336//! Constructs a new directory object representing the given path.337//! The directory must exist at creation time as the contents of the338//! class are gathered from it.339//!340directory(const path&);341342//!343//! \brief Returns the file names of the files in the directory.344//!345//! Returns the leaf names of all files contained in the directory.346//! I.e. the keys of the directory map.347//!348std::set< std::string > names(void) const;349};350351// ------------------------------------------------------------------------352// Free functions.353// ------------------------------------------------------------------------354355//!356//! \brief Checks if the given path exists.357//!358bool exists(const path&);359360//!361//! \brief Looks for the given program in the PATH.362//!363//! Given a program name (without slashes) looks for it in the path and364//! returns its full path name if found, otherwise an empty path.365//!366bool have_prog_in_path(const std::string&);367368//!369//! \brief Checks if the given path exists, is accessible and is executable.370//!371bool is_executable(const path&);372373//!374//! \brief Removes a given file.375//!376void remove(const path&);377378//!379//! \brief Removes an empty directory.380//!381void rmdir(const path&);382383} // namespace fs384} // namespace atf385386#endif // !defined(ATF_CXX_DETAIL_FS_HPP)387388389