head	1.2;
access;
symbols
	perseant-exfatfs-base-20250801:1.2
	perseant-exfatfs-base-20240630:1.2
	perseant-exfatfs:1.2.0.24
	perseant-exfatfs-base:1.2
	cjep_sun2x:1.2.0.22
	cjep_sun2x-base:1.2
	cjep_staticlib_x-base1:1.2
	cjep_staticlib_x:1.2.0.20
	cjep_staticlib_x-base:1.2
	phil-wifi-20200421:1.2
	phil-wifi-20200411:1.2
	phil-wifi-20200406:1.2
	pgoyette-compat-merge-20190127:1.2
	pgoyette-compat-20190127:1.2
	pgoyette-compat-20190118:1.2
	pgoyette-compat-1226:1.2
	pgoyette-compat-1126:1.2
	pgoyette-compat-1020:1.2
	pgoyette-compat-0930:1.2
	pgoyette-compat-0906:1.2
	pgoyette-compat-0728:1.2
	pgoyette-compat-0625:1.2
	pgoyette-compat-0521:1.2
	pgoyette-compat-0502:1.2
	pgoyette-compat-0422:1.2
	pgoyette-compat-0415:1.2
	pgoyette-compat-0407:1.2
	pgoyette-compat-0330:1.2
	pgoyette-compat-0322:1.2
	pgoyette-compat-0315:1.2
	pgoyette-compat:1.2.0.18
	pgoyette-compat-base:1.2
	prg-localcount2-base3:1.2
	prg-localcount2-base2:1.2
	prg-localcount2-base1:1.2
	prg-localcount2:1.2.0.16
	prg-localcount2-base:1.2
	pgoyette-localcount-20170426:1.2
	bouyer-socketcan-base1:1.2
	pgoyette-localcount-20170320:1.2
	bouyer-socketcan:1.2.0.14
	bouyer-socketcan-base:1.2
	pgoyette-localcount-20170107:1.2
	pgoyette-localcount-20161104:1.2
	localcount-20160914:1.2
	pgoyette-localcount-20160806:1.2
	pgoyette-localcount-20160726:1.2
	pgoyette-localcount:1.2.0.12
	pgoyette-localcount-base:1.2
	netbsd-5-2-3-RELEASE:1.1.1.1
	netbsd-5-1-5-RELEASE:1.1.1.1
	yamt-pagecache-base9:1.2
	yamt-pagecache-tag8:1.2
	tls-earlyentropy:1.2.0.8
	tls-earlyentropy-base:1.2
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.2
	riastradh-drm2-base3:1.2
	netbsd-5-2-2-RELEASE:1.1.1.1
	netbsd-5-1-4-RELEASE:1.1.1.1
	netbsd-5-2-1-RELEASE:1.1.1.1
	netbsd-5-1-3-RELEASE:1.1.1.1
	agc-symver:1.2.0.10
	agc-symver-base:1.2
	tls-maxphys-base:1.2
	yamt-pagecache-base8:1.2
	netbsd-5-2:1.1.1.1.0.6
	yamt-pagecache-base7:1.2
	netbsd-5-2-RELEASE:1.1.1.1
	netbsd-5-2-RC1:1.1.1.1
	yamt-pagecache-base6:1.2
	yamt-pagecache-base5:1.2
	yamt-pagecache-base4:1.2
	netbsd-5-1-2-RELEASE:1.1.1.1
	netbsd-5-1-1-RELEASE:1.1.1.1
	yamt-pagecache-base3:1.2
	yamt-pagecache-base2:1.2
	yamt-pagecache:1.2.0.6
	yamt-pagecache-base:1.2
	bouyer-quota2-nbase:1.2
	bouyer-quota2:1.2.0.4
	bouyer-quota2-base:1.2
	matt-nb5-mips64-premerge-20101231:1.1.1.1
	matt-nb5-pq3:1.1.1.1.0.16
	matt-nb5-pq3-base:1.1.1.1
	netbsd-5-1:1.1.1.1.0.14
	netbsd-5-1-RELEASE:1.1.1.1
	netbsd-5-1-RC4:1.1.1.1
	matt-nb5-mips64-k15:1.1.1.1
	netbsd-5-1-RC3:1.1.1.1
	netbsd-5-1-RC2:1.1.1.1
	netbsd-5-1-RC1:1.1.1.1
	netbsd-5-0-2-RELEASE:1.1.1.1
	matt-nb5-mips64-premerge-20091211:1.1.1.1
	matt-nb5-mips64-u2-k2-k4-k7-k8-k9:1.1.1.1
	matt-nb4-mips64-k7-u2a-k9b:1.1.1.1
	matt-nb5-mips64-u1-k1-k5:1.1.1.1
	matt-nb5-mips64:1.1.1.1.0.12
	netbsd-5-0-1-RELEASE:1.1.1.1
	jym-xensuspend-nbase:1.2
	netbsd-5-0:1.1.1.1.0.10
	netbsd-5-0-RELEASE:1.1.1.1
	netbsd-5-0-RC4:1.1.1.1
	netbsd-5-0-RC3:1.1.1.1
	netbsd-5-0-RC2:1.1.1.1
	jym-xensuspend:1.2.0.2
	jym-xensuspend-base:1.2
	netbsd-5-0-RC1:1.1.1.1
	netbsd-5:1.1.1.1.0.8
	netbsd-5-base:1.1.1.1
	matt-mips64-base2:1.1.1.1
	wrstuden-revivesa-base-3:1.1.1.1
	wrstuden-revivesa-base-2:1.1.1.1
	wrstuden-revivesa-base-1:1.1.1.1
	yamt-pf42-base4:1.1.1.1
	yamt-pf42-base3:1.1.1.1
	hpcarm-cleanup-nbase:1.1.1.1
	yamt-pf42:1.1.1.1.0.4
	yamt-pf42-base2:1.1.1.1
	yamt-pf42-base:1.1.1.1
	wrstuden-revivesa:1.1.1.1.0.2
	wrstuden-revivesa-base:1.1.1.1
	atf-0-5:1.1.1.1
	TNF:1.1.1;
locks; strict;
comment	@// @;


1.2
date	2009.01.19.07.08.16;	author jmmv;	state dead;
branches;
next	1.1;

1.1
date	2008.05.01.15.22.30;	author jmmv;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	2008.05.01.15.22.30;	author jmmv;	state Exp;
branches
	1.1.1.1.4.1;
next	;

1.1.1.1.4.1
date	2008.05.01.15.22.30;	author yamt;	state dead;
branches;
next	1.1.1.1.4.2;

1.1.1.1.4.2
date	2008.05.18.12.29.12;	author yamt;	state Exp;
branches;
next	;


desc
@@


1.2
log
@Remove ATF 0.5 from dist/atf and all of the reachover Makefiles used to
build it.  0.6 is going to be imported in external/bsd/atf, with all the
necessary Makefiles in that same hierarchy.
@
text
@//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#if defined(HAVE_CONFIG_H)
#include "bconfig.h"
#endif

extern "C" {
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dirent.h>
#include <libgen.h>
#include <unistd.h>
}

#include <cerrno>
#include <cstdlib>
#include <cstring>

#include "atf-c++/exceptions.hpp"
#include "atf-c++/env.hpp"
#include "atf-c++/fs.hpp"
#include "atf-c++/sanity.hpp"
#include "atf-c++/text.hpp"
#include "atf-c++/user.hpp"
#include "atf-c++/utils.hpp"

namespace impl = atf::fs;
#define IMPL_NAME "atf::fs"

// ------------------------------------------------------------------------
// Auxiliary functions.
// ------------------------------------------------------------------------

//!
//! \brief A controlled version of access(2).
//!
//! This function reimplements the standard access(2) system call to
//! safely control its exit status and raise an exception in case of
//! failure.
//!
static
bool
safe_access(const impl::path& p, int mode, int experr)
{
    bool ok;

    atf_error_t err = atf_fs_eaccess(p.c_path(), mode);
    if (atf_is_error(err)) {
        if (atf_error_is(err, "libc")) {
            if (atf_libc_error_code(err) == experr)
                ok = false;
            else {
                atf::throw_atf_error(err);
                // XXX Silence warning; maybe throw_atf_error should be
                // an exception and not a function.
                ok = false;
            }
        } else {
            atf::throw_atf_error(err);
            // XXX Silence warning; maybe throw_atf_error should be
            // an exception and not a function.
            ok = false;
        }
    } else
        ok = true;

    return ok;
}

// ------------------------------------------------------------------------
// The "path" class.
// ------------------------------------------------------------------------

impl::path::path(const std::string& s)
{
    atf_error_t err = atf_fs_path_init_fmt(&m_path, "%s", s.c_str());
    if (atf_is_error(err))
        throw_atf_error(err);
}

impl::path::path(const path& p)
{
    atf_error_t err = atf_fs_path_copy(&m_path, &p.m_path);
    if (atf_is_error(err))
        throw_atf_error(err);
}

impl::path::~path(void)
{
    atf_fs_path_fini(&m_path);
}

const char*
impl::path::c_str(void)
    const
{
    return atf_fs_path_cstring(&m_path);
}

const atf_fs_path_t*
impl::path::c_path(void)
    const
{
    return &m_path;
}

std::string
impl::path::str(void)
    const
{
    return c_str();
}

bool
impl::path::is_absolute(void)
    const
{
    return atf_fs_path_is_absolute(&m_path);
}

bool
impl::path::is_root(void)
    const
{
    return atf_fs_path_is_root(&m_path);
}

impl::path
impl::path::branch_path(void)
    const
{
    atf_fs_path_t bp;
    atf_error_t err;

    err = atf_fs_path_branch_path(&m_path, &bp);
    if (atf_is_error(err))
        throw_atf_error(err);

    path p(atf_fs_path_cstring(&bp));
    atf_fs_path_fini(&bp);
    return p;
}

std::string
impl::path::leaf_name(void)
    const
{
    atf_dynstr_t ln;
    atf_error_t err;

    err = atf_fs_path_leaf_name(&m_path, &ln);
    if (atf_is_error(err))
        throw_atf_error(err);

    std::string s(atf_dynstr_cstring(&ln));
    atf_dynstr_fini(&ln);
    return s;
}

impl::path
impl::path::to_absolute(void)
    const
{
    atf_fs_path_t pa;

    atf_error_t err = atf_fs_path_to_absolute(&m_path, &pa);
    if (atf_is_error(err))
        throw_atf_error(err);

    path p(atf_fs_path_cstring(&pa));
    atf_fs_path_fini(&pa);
    return p;
}

impl::path&
impl::path::operator=(const path& p)
{
    atf_fs_path_t tmp;

    atf_error_t err = atf_fs_path_init_fmt(&tmp, "%s", p.c_str());
    if (atf_is_error(err))
        throw_atf_error(err);
    else {
        atf_fs_path_fini(&m_path);
        m_path = tmp;
    }

    return *this;
}

bool
impl::path::operator==(const path& p)
    const
{
    return atf_equal_fs_path_fs_path(&m_path, &p.m_path);
}

bool
impl::path::operator!=(const path& p)
    const
{
    return !atf_equal_fs_path_fs_path(&m_path, &p.m_path);
}

impl::path
impl::path::operator/(const std::string& p)
    const
{
    path p2 = *this;

    atf_error_t err = atf_fs_path_append_fmt(&p2.m_path, "%s", p.c_str());
    if (atf_is_error(err))
        throw_atf_error(err);

    return p2;
}

impl::path
impl::path::operator/(const path& p)
    const
{
    path p2 = *this;

    atf_error_t err = atf_fs_path_append_fmt(&p2.m_path, "%s",
                                             atf_fs_path_cstring(&p.m_path));
    if (atf_is_error(err))
        throw_atf_error(err);

    return p2;
}

bool
impl::path::operator<(const path& p)
    const
{
    const char *s1 = atf_fs_path_cstring(&m_path);
    const char *s2 = atf_fs_path_cstring(&p.m_path);
    return std::strcmp(s1, s2) < 0;
}

// ------------------------------------------------------------------------
// The "file_info" class.
// ------------------------------------------------------------------------

const int impl::file_info::blk_type = atf_fs_stat_blk_type;
const int impl::file_info::chr_type = atf_fs_stat_chr_type;
const int impl::file_info::dir_type = atf_fs_stat_dir_type;
const int impl::file_info::fifo_type = atf_fs_stat_fifo_type;
const int impl::file_info::lnk_type = atf_fs_stat_lnk_type;
const int impl::file_info::reg_type = atf_fs_stat_reg_type;
const int impl::file_info::sock_type = atf_fs_stat_sock_type;
const int impl::file_info::wht_type = atf_fs_stat_wht_type;

impl::file_info::file_info(const path& p)
{
    atf_error_t err;

    err = atf_fs_stat_init(&m_stat, p.c_path());
    if (atf_is_error(err))
        throw_atf_error(err);
}

impl::file_info::file_info(const file_info& fi)
{
    atf_fs_stat_copy(&m_stat, &fi.m_stat);
}

impl::file_info::~file_info(void)
{
    atf_fs_stat_fini(&m_stat);
}

dev_t
impl::file_info::get_device(void)
    const
{
    return atf_fs_stat_get_device(&m_stat);
}

ino_t
impl::file_info::get_inode(void)
    const
{
    return atf_fs_stat_get_inode(&m_stat);
}

int
impl::file_info::get_type(void)
    const
{
    return atf_fs_stat_get_type(&m_stat);
}

bool
impl::file_info::is_owner_readable(void)
    const
{
    return atf_fs_stat_is_owner_readable(&m_stat);
}

bool
impl::file_info::is_owner_writable(void)
    const
{
    return atf_fs_stat_is_owner_writable(&m_stat);
}

bool
impl::file_info::is_owner_executable(void)
    const
{
    return atf_fs_stat_is_owner_executable(&m_stat);
}

bool
impl::file_info::is_group_readable(void)
    const
{
    return atf_fs_stat_is_group_readable(&m_stat);
}

bool
impl::file_info::is_group_writable(void)
    const
{
    return atf_fs_stat_is_group_writable(&m_stat);
}

bool
impl::file_info::is_group_executable(void)
    const
{
    return atf_fs_stat_is_group_executable(&m_stat);
}

bool
impl::file_info::is_other_readable(void)
    const
{
    return atf_fs_stat_is_other_readable(&m_stat);
}

bool
impl::file_info::is_other_writable(void)
    const
{
    return atf_fs_stat_is_other_writable(&m_stat);
}

bool
impl::file_info::is_other_executable(void)
    const
{
    return atf_fs_stat_is_other_executable(&m_stat);
}

// ------------------------------------------------------------------------
// The "directory" class.
// ------------------------------------------------------------------------

impl::directory::directory(const path& p)
{
    DIR* dp = ::opendir(p.c_str());
    if (dp == NULL)
        throw system_error(IMPL_NAME "::directory::directory(" +
                           p.str() + ")", "opendir(3) failed", errno);

    struct dirent* dep;
    while ((dep = ::readdir(dp)) != NULL) {
        path entryp = p / dep->d_name;
        insert(value_type(dep->d_name, file_info(entryp)));
    }

    if (::closedir(dp) == -1)
        throw system_error(IMPL_NAME "::directory::directory(" +
                           p.str() + ")", "closedir(3) failed", errno);
}

std::set< std::string >
impl::directory::names(void)
    const
{
    std::set< std::string > ns;

    for (const_iterator iter = begin(); iter != end(); iter++)
        ns.insert((*iter).first);

    return ns;
}

// ------------------------------------------------------------------------
// The "temp_dir" class.
// ------------------------------------------------------------------------

impl::temp_dir::temp_dir(const path& p)
{
    atf::utils::auto_array< char > buf(new char[p.str().length() + 1]);
    std::strcpy(buf.get(), p.c_str());
    if (::mkdtemp(buf.get()) == NULL)
        throw system_error(IMPL_NAME "::temp_dir::temp_dir(" +
                           p.str() + ")", "mkdtemp(3) failed",
                           errno);
    m_path = new path(buf.get());
}

impl::temp_dir::~temp_dir(void)
{
    cleanup(*m_path);
    delete m_path;
}

const impl::path&
impl::temp_dir::get_path(void)
    const
{
    return *m_path;
}

// ------------------------------------------------------------------------
// Free functions.
// ------------------------------------------------------------------------

impl::path
impl::change_directory(const path& dir)
{
    path olddir = get_current_dir();

    if (olddir != dir) {
        if (::chdir(dir.c_str()) == -1)
            throw system_error(IMPL_NAME "::chdir(" + dir.str() + ")",
                               "chdir(2) failed", errno);
    }

    return olddir;
}

bool
impl::exists(const path& p)
{
    atf_error_t err;
    bool b;

    err = atf_fs_exists(p.c_path(), &b);
    if (atf_is_error(err))
        throw_atf_error(err);

    return b;
}

bool
impl::have_prog_in_path(const std::string& prog)
{
    PRE(prog.find('/') == std::string::npos);

    // Do not bother to provide a default value for PATH.  If it is not
    // there something is broken in the user's environment.
    if (!atf::env::has("PATH"))
        throw std::runtime_error("PATH not defined in the environment");
    std::vector< std::string > dirs = \
        atf::text::split(atf::env::get("PATH"), ":");

    bool found = false;
    for (std::vector< std::string >::const_iterator iter = dirs.begin();
         !found && iter != dirs.end(); iter++) {
        const path& dir = path(*iter);

        if (is_executable(dir / prog))
            found = true;
    }
    return found;
}

impl::path
impl::get_current_dir(void)
{
    atf_fs_path_t cwd;

    atf_error_t err = atf_fs_getcwd(&cwd);
    if (atf_is_error(err))
        throw_atf_error(err);

    path p(atf_fs_path_cstring(&cwd));
    atf_fs_path_fini(&cwd);
    return p;
}

bool
impl::is_executable(const path& p)
{
    if (!exists(p))
        return false;
    return safe_access(p, atf_fs_access_x, EACCES);
}

void
impl::remove(const path& p)
{
    if (file_info(p).get_type() == file_info::dir_type)
        throw atf::system_error(IMPL_NAME "::remove(" + p.str() + ")",
                                "Is a directory",
                                EPERM);
    if (::unlink(p.c_str()) == -1)
        throw atf::system_error(IMPL_NAME "::remove(" + p.str() + ")",
                                "unlink(" + p.str() + ") failed",
                                errno);
}

void
impl::cleanup(const path& p)
{
    atf_error_t err;

    err = atf_fs_cleanup(p.c_path());
    if (atf_is_error(err))
        throw_atf_error(err);
}
@


1.1
log
@Initial revision
@
text
@@


1.1.1.1
log
@Import ATF 0.5

Changes in this release:

* Clauses 3 and 4 of the BSD license used by the project were dropped.
  All the code is now under a 2-clause BSD license compatible with the
  GNU General Public License (GPL).

* Added a C-only binding so that binary test programs do not need to be
  tied to C++ at all.  This binding is now known as the atf-c library.

* Renamed the C++ binding to atf-c++ for consistency with the new atf-c.

* Renamed the POSIX shell binding to atf-sh for consistency with the new
  atf-c and atf-c++.

* Added a -w flag to test programs through which it is possible to specify
  the work directory to be used.  This was possible in prior releases by
  defining the workdir configuration variable (-v workdir=...), but was a
  conceptually incorrect mechanism.

* Test programs now preserve the execution order of test cases when they
  are given in the command line.  Even those mentioned more than once are
  executed multiple times to comply with the user's requests.
@
text
@@


1.1.1.1.4.1
log
@file fs.cpp was added on branch yamt-pf42 on 2008-05-18 12:29:12 +0000
@
text
@d1 546
@


1.1.1.1.4.2
log
@sync with head.
@
text
@a0 546
//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#if defined(HAVE_CONFIG_H)
#include "bconfig.h"
#endif

extern "C" {
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dirent.h>
#include <libgen.h>
#include <unistd.h>
}

#include <cerrno>
#include <cstdlib>
#include <cstring>

#include "atf-c++/exceptions.hpp"
#include "atf-c++/env.hpp"
#include "atf-c++/fs.hpp"
#include "atf-c++/sanity.hpp"
#include "atf-c++/text.hpp"
#include "atf-c++/user.hpp"
#include "atf-c++/utils.hpp"

namespace impl = atf::fs;
#define IMPL_NAME "atf::fs"

// ------------------------------------------------------------------------
// Auxiliary functions.
// ------------------------------------------------------------------------

//!
//! \brief A controlled version of access(2).
//!
//! This function reimplements the standard access(2) system call to
//! safely control its exit status and raise an exception in case of
//! failure.
//!
static
bool
safe_access(const impl::path& p, int mode, int experr)
{
    bool ok;

    atf_error_t err = atf_fs_eaccess(p.c_path(), mode);
    if (atf_is_error(err)) {
        if (atf_error_is(err, "libc")) {
            if (atf_libc_error_code(err) == experr)
                ok = false;
            else {
                atf::throw_atf_error(err);
                // XXX Silence warning; maybe throw_atf_error should be
                // an exception and not a function.
                ok = false;
            }
        } else {
            atf::throw_atf_error(err);
            // XXX Silence warning; maybe throw_atf_error should be
            // an exception and not a function.
            ok = false;
        }
    } else
        ok = true;

    return ok;
}

// ------------------------------------------------------------------------
// The "path" class.
// ------------------------------------------------------------------------

impl::path::path(const std::string& s)
{
    atf_error_t err = atf_fs_path_init_fmt(&m_path, "%s", s.c_str());
    if (atf_is_error(err))
        throw_atf_error(err);
}

impl::path::path(const path& p)
{
    atf_error_t err = atf_fs_path_copy(&m_path, &p.m_path);
    if (atf_is_error(err))
        throw_atf_error(err);
}

impl::path::~path(void)
{
    atf_fs_path_fini(&m_path);
}

const char*
impl::path::c_str(void)
    const
{
    return atf_fs_path_cstring(&m_path);
}

const atf_fs_path_t*
impl::path::c_path(void)
    const
{
    return &m_path;
}

std::string
impl::path::str(void)
    const
{
    return c_str();
}

bool
impl::path::is_absolute(void)
    const
{
    return atf_fs_path_is_absolute(&m_path);
}

bool
impl::path::is_root(void)
    const
{
    return atf_fs_path_is_root(&m_path);
}

impl::path
impl::path::branch_path(void)
    const
{
    atf_fs_path_t bp;
    atf_error_t err;

    err = atf_fs_path_branch_path(&m_path, &bp);
    if (atf_is_error(err))
        throw_atf_error(err);

    path p(atf_fs_path_cstring(&bp));
    atf_fs_path_fini(&bp);
    return p;
}

std::string
impl::path::leaf_name(void)
    const
{
    atf_dynstr_t ln;
    atf_error_t err;

    err = atf_fs_path_leaf_name(&m_path, &ln);
    if (atf_is_error(err))
        throw_atf_error(err);

    std::string s(atf_dynstr_cstring(&ln));
    atf_dynstr_fini(&ln);
    return s;
}

impl::path
impl::path::to_absolute(void)
    const
{
    atf_fs_path_t pa;

    atf_error_t err = atf_fs_path_to_absolute(&m_path, &pa);
    if (atf_is_error(err))
        throw_atf_error(err);

    path p(atf_fs_path_cstring(&pa));
    atf_fs_path_fini(&pa);
    return p;
}

impl::path&
impl::path::operator=(const path& p)
{
    atf_fs_path_t tmp;

    atf_error_t err = atf_fs_path_init_fmt(&tmp, "%s", p.c_str());
    if (atf_is_error(err))
        throw_atf_error(err);
    else {
        atf_fs_path_fini(&m_path);
        m_path = tmp;
    }

    return *this;
}

bool
impl::path::operator==(const path& p)
    const
{
    return atf_equal_fs_path_fs_path(&m_path, &p.m_path);
}

bool
impl::path::operator!=(const path& p)
    const
{
    return !atf_equal_fs_path_fs_path(&m_path, &p.m_path);
}

impl::path
impl::path::operator/(const std::string& p)
    const
{
    path p2 = *this;

    atf_error_t err = atf_fs_path_append_fmt(&p2.m_path, "%s", p.c_str());
    if (atf_is_error(err))
        throw_atf_error(err);

    return p2;
}

impl::path
impl::path::operator/(const path& p)
    const
{
    path p2 = *this;

    atf_error_t err = atf_fs_path_append_fmt(&p2.m_path, "%s",
                                             atf_fs_path_cstring(&p.m_path));
    if (atf_is_error(err))
        throw_atf_error(err);

    return p2;
}

bool
impl::path::operator<(const path& p)
    const
{
    const char *s1 = atf_fs_path_cstring(&m_path);
    const char *s2 = atf_fs_path_cstring(&p.m_path);
    return std::strcmp(s1, s2) < 0;
}

// ------------------------------------------------------------------------
// The "file_info" class.
// ------------------------------------------------------------------------

const int impl::file_info::blk_type = atf_fs_stat_blk_type;
const int impl::file_info::chr_type = atf_fs_stat_chr_type;
const int impl::file_info::dir_type = atf_fs_stat_dir_type;
const int impl::file_info::fifo_type = atf_fs_stat_fifo_type;
const int impl::file_info::lnk_type = atf_fs_stat_lnk_type;
const int impl::file_info::reg_type = atf_fs_stat_reg_type;
const int impl::file_info::sock_type = atf_fs_stat_sock_type;
const int impl::file_info::wht_type = atf_fs_stat_wht_type;

impl::file_info::file_info(const path& p)
{
    atf_error_t err;

    err = atf_fs_stat_init(&m_stat, p.c_path());
    if (atf_is_error(err))
        throw_atf_error(err);
}

impl::file_info::file_info(const file_info& fi)
{
    atf_fs_stat_copy(&m_stat, &fi.m_stat);
}

impl::file_info::~file_info(void)
{
    atf_fs_stat_fini(&m_stat);
}

dev_t
impl::file_info::get_device(void)
    const
{
    return atf_fs_stat_get_device(&m_stat);
}

ino_t
impl::file_info::get_inode(void)
    const
{
    return atf_fs_stat_get_inode(&m_stat);
}

int
impl::file_info::get_type(void)
    const
{
    return atf_fs_stat_get_type(&m_stat);
}

bool
impl::file_info::is_owner_readable(void)
    const
{
    return atf_fs_stat_is_owner_readable(&m_stat);
}

bool
impl::file_info::is_owner_writable(void)
    const
{
    return atf_fs_stat_is_owner_writable(&m_stat);
}

bool
impl::file_info::is_owner_executable(void)
    const
{
    return atf_fs_stat_is_owner_executable(&m_stat);
}

bool
impl::file_info::is_group_readable(void)
    const
{
    return atf_fs_stat_is_group_readable(&m_stat);
}

bool
impl::file_info::is_group_writable(void)
    const
{
    return atf_fs_stat_is_group_writable(&m_stat);
}

bool
impl::file_info::is_group_executable(void)
    const
{
    return atf_fs_stat_is_group_executable(&m_stat);
}

bool
impl::file_info::is_other_readable(void)
    const
{
    return atf_fs_stat_is_other_readable(&m_stat);
}

bool
impl::file_info::is_other_writable(void)
    const
{
    return atf_fs_stat_is_other_writable(&m_stat);
}

bool
impl::file_info::is_other_executable(void)
    const
{
    return atf_fs_stat_is_other_executable(&m_stat);
}

// ------------------------------------------------------------------------
// The "directory" class.
// ------------------------------------------------------------------------

impl::directory::directory(const path& p)
{
    DIR* dp = ::opendir(p.c_str());
    if (dp == NULL)
        throw system_error(IMPL_NAME "::directory::directory(" +
                           p.str() + ")", "opendir(3) failed", errno);

    struct dirent* dep;
    while ((dep = ::readdir(dp)) != NULL) {
        path entryp = p / dep->d_name;
        insert(value_type(dep->d_name, file_info(entryp)));
    }

    if (::closedir(dp) == -1)
        throw system_error(IMPL_NAME "::directory::directory(" +
                           p.str() + ")", "closedir(3) failed", errno);
}

std::set< std::string >
impl::directory::names(void)
    const
{
    std::set< std::string > ns;

    for (const_iterator iter = begin(); iter != end(); iter++)
        ns.insert((*iter).first);

    return ns;
}

// ------------------------------------------------------------------------
// The "temp_dir" class.
// ------------------------------------------------------------------------

impl::temp_dir::temp_dir(const path& p)
{
    atf::utils::auto_array< char > buf(new char[p.str().length() + 1]);
    std::strcpy(buf.get(), p.c_str());
    if (::mkdtemp(buf.get()) == NULL)
        throw system_error(IMPL_NAME "::temp_dir::temp_dir(" +
                           p.str() + ")", "mkdtemp(3) failed",
                           errno);
    m_path = new path(buf.get());
}

impl::temp_dir::~temp_dir(void)
{
    cleanup(*m_path);
    delete m_path;
}

const impl::path&
impl::temp_dir::get_path(void)
    const
{
    return *m_path;
}

// ------------------------------------------------------------------------
// Free functions.
// ------------------------------------------------------------------------

impl::path
impl::change_directory(const path& dir)
{
    path olddir = get_current_dir();

    if (olddir != dir) {
        if (::chdir(dir.c_str()) == -1)
            throw system_error(IMPL_NAME "::chdir(" + dir.str() + ")",
                               "chdir(2) failed", errno);
    }

    return olddir;
}

bool
impl::exists(const path& p)
{
    atf_error_t err;
    bool b;

    err = atf_fs_exists(p.c_path(), &b);
    if (atf_is_error(err))
        throw_atf_error(err);

    return b;
}

bool
impl::have_prog_in_path(const std::string& prog)
{
    PRE(prog.find('/') == std::string::npos);

    // Do not bother to provide a default value for PATH.  If it is not
    // there something is broken in the user's environment.
    if (!atf::env::has("PATH"))
        throw std::runtime_error("PATH not defined in the environment");
    std::vector< std::string > dirs = \
        atf::text::split(atf::env::get("PATH"), ":");

    bool found = false;
    for (std::vector< std::string >::const_iterator iter = dirs.begin();
         !found && iter != dirs.end(); iter++) {
        const path& dir = path(*iter);

        if (is_executable(dir / prog))
            found = true;
    }
    return found;
}

impl::path
impl::get_current_dir(void)
{
    atf_fs_path_t cwd;

    atf_error_t err = atf_fs_getcwd(&cwd);
    if (atf_is_error(err))
        throw_atf_error(err);

    path p(atf_fs_path_cstring(&cwd));
    atf_fs_path_fini(&cwd);
    return p;
}

bool
impl::is_executable(const path& p)
{
    if (!exists(p))
        return false;
    return safe_access(p, atf_fs_access_x, EACCES);
}

void
impl::remove(const path& p)
{
    if (file_info(p).get_type() == file_info::dir_type)
        throw atf::system_error(IMPL_NAME "::remove(" + p.str() + ")",
                                "Is a directory",
                                EPERM);
    if (::unlink(p.c_str()) == -1)
        throw atf::system_error(IMPL_NAME "::remove(" + p.str() + ")",
                                "unlink(" + p.str() + ") failed",
                                errno);
}

void
impl::cleanup(const path& p)
{
    atf_error_t err;

    err = atf_fs_cleanup(p.c_path());
    if (atf_is_error(err))
        throw_atf_error(err);
}
@
