Path: blob/main/contrib/atf/atf-c++/detail/fs.cpp
105152 views
// 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#include "atf-c++/detail/fs.hpp"2627#if defined(HAVE_CONFIG_H)28#include "config.h"29#endif3031extern "C" {32#include <sys/param.h>33#include <sys/types.h>34#include <sys/mount.h>35#include <sys/stat.h>36#include <sys/wait.h>37#include <dirent.h>38#include <libgen.h>39#include <unistd.h>40}4142#include <cerrno>43#include <cstdlib>44#include <cstring>4546extern "C" {47#include "atf-c/error.h"48}4950#include "atf-c++/detail/env.hpp"51#include "atf-c++/detail/exceptions.hpp"52#include "atf-c++/detail/process.hpp"53#include "atf-c++/detail/sanity.hpp"54#include "atf-c++/detail/text.hpp"55#include "atf-c++/utils.hpp"5657namespace impl = atf::fs;58#define IMPL_NAME "atf::fs"5960// ------------------------------------------------------------------------61// Auxiliary functions.62// ------------------------------------------------------------------------6364static bool safe_access(const impl::path&, int, int);6566//!67//! \brief A controlled version of access(2).68//!69//! This function reimplements the standard access(2) system call to70//! safely control its exit status and raise an exception in case of71//! failure.72//!73static74bool75safe_access(const impl::path& p, int mode, int experr)76{77bool ok;7879atf_error_t err = atf_fs_eaccess(p.c_path(), mode);80if (atf_is_error(err)) {81if (atf_error_is(err, "libc")) {82if (atf_libc_error_code(err) == experr) {83atf_error_free(err);84ok = false;85} else {86atf::throw_atf_error(err);87// XXX Silence warning; maybe throw_atf_error should be88// an exception and not a function.89ok = false;90}91} else {92atf::throw_atf_error(err);93// XXX Silence warning; maybe throw_atf_error should be94// an exception and not a function.95ok = false;96}97} else98ok = true;99100return ok;101}102103// ------------------------------------------------------------------------104// The "path" class.105// ------------------------------------------------------------------------106107impl::path::path(const std::string& s)108{109atf_error_t err = atf_fs_path_init_fmt(&m_path, "%s", s.c_str());110if (atf_is_error(err))111throw_atf_error(err);112}113114impl::path::path(const path& p)115{116atf_error_t err = atf_fs_path_copy(&m_path, &p.m_path);117if (atf_is_error(err))118throw_atf_error(err);119}120121impl::path::path(const atf_fs_path_t *p)122{123atf_error_t err = atf_fs_path_copy(&m_path, p);124if (atf_is_error(err))125throw_atf_error(err);126}127128impl::path::~path(void)129{130atf_fs_path_fini(&m_path);131}132133const char*134impl::path::c_str(void)135const136{137return atf_fs_path_cstring(&m_path);138}139140const atf_fs_path_t*141impl::path::c_path(void)142const143{144return &m_path;145}146147std::string148impl::path::str(void)149const150{151return c_str();152}153154bool155impl::path::is_absolute(void)156const157{158return atf_fs_path_is_absolute(&m_path);159}160161bool162impl::path::is_root(void)163const164{165return atf_fs_path_is_root(&m_path);166}167168impl::path169impl::path::branch_path(void)170const171{172atf_fs_path_t bp;173atf_error_t err;174175err = atf_fs_path_branch_path(&m_path, &bp);176if (atf_is_error(err))177throw_atf_error(err);178179path p(atf_fs_path_cstring(&bp));180atf_fs_path_fini(&bp);181return p;182}183184std::string185impl::path::leaf_name(void)186const187{188atf_dynstr_t ln;189atf_error_t err;190191err = atf_fs_path_leaf_name(&m_path, &ln);192if (atf_is_error(err))193throw_atf_error(err);194195std::string s(atf_dynstr_cstring(&ln));196atf_dynstr_fini(&ln);197return s;198}199200impl::path201impl::path::to_absolute(void)202const203{204atf_fs_path_t pa;205206atf_error_t err = atf_fs_path_to_absolute(&m_path, &pa);207if (atf_is_error(err))208throw_atf_error(err);209210path p(atf_fs_path_cstring(&pa));211atf_fs_path_fini(&pa);212return p;213}214215impl::path&216impl::path::operator=(const path& p)217{218atf_fs_path_t tmp;219220atf_error_t err = atf_fs_path_init_fmt(&tmp, "%s", p.c_str());221if (atf_is_error(err))222throw_atf_error(err);223else {224atf_fs_path_fini(&m_path);225m_path = tmp;226}227228return *this;229}230231bool232impl::path::operator==(const path& p)233const234{235return atf_equal_fs_path_fs_path(&m_path, &p.m_path);236}237238bool239impl::path::operator!=(const path& p)240const241{242return !atf_equal_fs_path_fs_path(&m_path, &p.m_path);243}244245impl::path246impl::path::operator/(const std::string& p)247const248{249path p2 = *this;250251atf_error_t err = atf_fs_path_append_fmt(&p2.m_path, "%s", p.c_str());252if (atf_is_error(err))253throw_atf_error(err);254255return p2;256}257258impl::path259impl::path::operator/(const path& p)260const261{262path p2 = *this;263264atf_error_t err = atf_fs_path_append_fmt(&p2.m_path, "%s",265atf_fs_path_cstring(&p.m_path));266if (atf_is_error(err))267throw_atf_error(err);268269return p2;270}271272bool273impl::path::operator<(const path& p)274const275{276const char *s1 = atf_fs_path_cstring(&m_path);277const char *s2 = atf_fs_path_cstring(&p.m_path);278return std::strcmp(s1, s2) < 0;279}280281// ------------------------------------------------------------------------282// The "file_info" class.283// ------------------------------------------------------------------------284285const int impl::file_info::blk_type = atf_fs_stat_blk_type;286const int impl::file_info::chr_type = atf_fs_stat_chr_type;287const int impl::file_info::dir_type = atf_fs_stat_dir_type;288const int impl::file_info::fifo_type = atf_fs_stat_fifo_type;289const int impl::file_info::lnk_type = atf_fs_stat_lnk_type;290const int impl::file_info::reg_type = atf_fs_stat_reg_type;291const int impl::file_info::sock_type = atf_fs_stat_sock_type;292const int impl::file_info::wht_type = atf_fs_stat_wht_type;293294impl::file_info::file_info(const path& p)295{296atf_error_t err;297298err = atf_fs_stat_init(&m_stat, p.c_path());299if (atf_is_error(err))300throw_atf_error(err);301}302303impl::file_info::file_info(const file_info& fi)304{305atf_fs_stat_copy(&m_stat, &fi.m_stat);306}307308impl::file_info::~file_info(void)309{310atf_fs_stat_fini(&m_stat);311}312313dev_t314impl::file_info::get_device(void)315const316{317return atf_fs_stat_get_device(&m_stat);318}319320ino_t321impl::file_info::get_inode(void)322const323{324return atf_fs_stat_get_inode(&m_stat);325}326327mode_t328impl::file_info::get_mode(void)329const330{331return atf_fs_stat_get_mode(&m_stat);332}333334off_t335impl::file_info::get_size(void)336const337{338return atf_fs_stat_get_size(&m_stat);339}340341int342impl::file_info::get_type(void)343const344{345return atf_fs_stat_get_type(&m_stat);346}347348bool349impl::file_info::is_owner_readable(void)350const351{352return atf_fs_stat_is_owner_readable(&m_stat);353}354355bool356impl::file_info::is_owner_writable(void)357const358{359return atf_fs_stat_is_owner_writable(&m_stat);360}361362bool363impl::file_info::is_owner_executable(void)364const365{366return atf_fs_stat_is_owner_executable(&m_stat);367}368369bool370impl::file_info::is_group_readable(void)371const372{373return atf_fs_stat_is_group_readable(&m_stat);374}375376bool377impl::file_info::is_group_writable(void)378const379{380return atf_fs_stat_is_group_writable(&m_stat);381}382383bool384impl::file_info::is_group_executable(void)385const386{387return atf_fs_stat_is_group_executable(&m_stat);388}389390bool391impl::file_info::is_other_readable(void)392const393{394return atf_fs_stat_is_other_readable(&m_stat);395}396397bool398impl::file_info::is_other_writable(void)399const400{401return atf_fs_stat_is_other_writable(&m_stat);402}403404bool405impl::file_info::is_other_executable(void)406const407{408return atf_fs_stat_is_other_executable(&m_stat);409}410411// ------------------------------------------------------------------------412// The "directory" class.413// ------------------------------------------------------------------------414415impl::directory::directory(const path& p)416{417DIR* dp = ::opendir(p.c_str());418if (dp == NULL)419throw system_error(IMPL_NAME "::directory::directory(" +420p.str() + ")", "opendir(3) failed", errno);421422struct dirent* dep;423while ((dep = ::readdir(dp)) != NULL) {424path entryp = p / dep->d_name;425insert(value_type(dep->d_name, file_info(entryp)));426}427428if (::closedir(dp) == -1)429throw system_error(IMPL_NAME "::directory::directory(" +430p.str() + ")", "closedir(3) failed", errno);431}432433std::set< std::string >434impl::directory::names(void)435const436{437std::set< std::string > ns;438439for (const_iterator iter = begin(); iter != end(); iter++)440ns.insert((*iter).first);441442return ns;443}444445// ------------------------------------------------------------------------446// Free functions.447// ------------------------------------------------------------------------448449bool450impl::exists(const path& p)451{452atf_error_t err;453bool b;454455err = atf_fs_exists(p.c_path(), &b);456if (atf_is_error(err))457throw_atf_error(err);458459return b;460}461462bool463impl::have_prog_in_path(const std::string& prog)464{465PRE(prog.find('/') == std::string::npos);466467// Do not bother to provide a default value for PATH. If it is not468// there something is broken in the user's environment.469if (!atf::env::has("PATH"))470throw std::runtime_error("PATH not defined in the environment");471std::vector< std::string > dirs =472atf::text::split(atf::env::get("PATH"), ":");473474bool found = false;475for (std::vector< std::string >::const_iterator iter = dirs.begin();476!found && iter != dirs.end(); iter++) {477const path& dir = path(*iter);478479if (is_executable(dir / prog))480found = true;481}482return found;483}484485bool486impl::is_executable(const path& p)487{488if (!exists(p))489return false;490return safe_access(p, atf_fs_access_x, EACCES);491}492493void494impl::remove(const path& p)495{496if (file_info(p).get_type() == file_info::dir_type)497throw atf::system_error(IMPL_NAME "::remove(" + p.str() + ")",498"Is a directory",499EPERM);500if (::unlink(p.c_str()) == -1)501throw atf::system_error(IMPL_NAME "::remove(" + p.str() + ")",502"unlink(" + p.str() + ") failed",503errno);504}505506void507impl::rmdir(const path& p)508{509atf_error_t err = atf_fs_rmdir(p.c_path());510if (atf_is_error(err))511throw_atf_error(err);512}513514515