2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #if defined(HAVE_CONFIG_H)
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/mount.h>
50 #include "../../atf-c/error.h"
53 #include "../utils.hpp"
55 #include "exceptions.hpp"
58 #include "process.hpp"
62 namespace impl
= atf::fs
;
63 #define IMPL_NAME "atf::fs"
65 // ------------------------------------------------------------------------
66 // Auxiliary functions.
67 // ------------------------------------------------------------------------
69 static bool safe_access(const impl::path
&, int, int);
72 //! \brief A controlled version of access(2).
74 //! This function reimplements the standard access(2) system call to
75 //! safely control its exit status and raise an exception in case of
80 safe_access(const impl::path
& p
, int mode
, int experr
)
84 atf_error_t err
= atf_fs_eaccess(p
.c_path(), mode
);
85 if (atf_is_error(err
)) {
86 if (atf_error_is(err
, "libc")) {
87 if (atf_libc_error_code(err
) == experr
) {
91 atf::throw_atf_error(err
);
92 // XXX Silence warning; maybe throw_atf_error should be
93 // an exception and not a function.
97 atf::throw_atf_error(err
);
98 // XXX Silence warning; maybe throw_atf_error should be
99 // an exception and not a function.
108 // ------------------------------------------------------------------------
110 // ------------------------------------------------------------------------
112 impl::path::path(const std::string
& s
)
114 atf_error_t err
= atf_fs_path_init_fmt(&m_path
, "%s", s
.c_str());
115 if (atf_is_error(err
))
116 throw_atf_error(err
);
119 impl::path::path(const path
& p
)
121 atf_error_t err
= atf_fs_path_copy(&m_path
, &p
.m_path
);
122 if (atf_is_error(err
))
123 throw_atf_error(err
);
126 impl::path::path(const atf_fs_path_t
*p
)
128 atf_error_t err
= atf_fs_path_copy(&m_path
, p
);
129 if (atf_is_error(err
))
130 throw_atf_error(err
);
133 impl::path::~path(void)
135 atf_fs_path_fini(&m_path
);
139 impl::path::c_str(void)
142 return atf_fs_path_cstring(&m_path
);
146 impl::path::c_path(void)
153 impl::path::str(void)
160 impl::path::is_absolute(void)
163 return atf_fs_path_is_absolute(&m_path
);
167 impl::path::is_root(void)
170 return atf_fs_path_is_root(&m_path
);
174 impl::path::branch_path(void)
180 err
= atf_fs_path_branch_path(&m_path
, &bp
);
181 if (atf_is_error(err
))
182 throw_atf_error(err
);
184 path
p(atf_fs_path_cstring(&bp
));
185 atf_fs_path_fini(&bp
);
190 impl::path::leaf_name(void)
196 err
= atf_fs_path_leaf_name(&m_path
, &ln
);
197 if (atf_is_error(err
))
198 throw_atf_error(err
);
200 std::string
s(atf_dynstr_cstring(&ln
));
201 atf_dynstr_fini(&ln
);
206 impl::path::to_absolute(void)
211 atf_error_t err
= atf_fs_path_to_absolute(&m_path
, &pa
);
212 if (atf_is_error(err
))
213 throw_atf_error(err
);
215 path
p(atf_fs_path_cstring(&pa
));
216 atf_fs_path_fini(&pa
);
221 impl::path::operator=(const path
& p
)
225 atf_error_t err
= atf_fs_path_init_fmt(&tmp
, "%s", p
.c_str());
226 if (atf_is_error(err
))
227 throw_atf_error(err
);
229 atf_fs_path_fini(&m_path
);
237 impl::path::operator==(const path
& p
)
240 return atf_equal_fs_path_fs_path(&m_path
, &p
.m_path
);
244 impl::path::operator!=(const path
& p
)
247 return !atf_equal_fs_path_fs_path(&m_path
, &p
.m_path
);
251 impl::path::operator/(const std::string
& p
)
256 atf_error_t err
= atf_fs_path_append_fmt(&p2
.m_path
, "%s", p
.c_str());
257 if (atf_is_error(err
))
258 throw_atf_error(err
);
264 impl::path::operator/(const path
& p
)
269 atf_error_t err
= atf_fs_path_append_fmt(&p2
.m_path
, "%s",
270 atf_fs_path_cstring(&p
.m_path
));
271 if (atf_is_error(err
))
272 throw_atf_error(err
);
278 impl::path::operator<(const path
& p
)
281 const char *s1
= atf_fs_path_cstring(&m_path
);
282 const char *s2
= atf_fs_path_cstring(&p
.m_path
);
283 return std::strcmp(s1
, s2
) < 0;
286 // ------------------------------------------------------------------------
287 // The "file_info" class.
288 // ------------------------------------------------------------------------
290 const int impl::file_info::blk_type
= atf_fs_stat_blk_type
;
291 const int impl::file_info::chr_type
= atf_fs_stat_chr_type
;
292 const int impl::file_info::dir_type
= atf_fs_stat_dir_type
;
293 const int impl::file_info::fifo_type
= atf_fs_stat_fifo_type
;
294 const int impl::file_info::lnk_type
= atf_fs_stat_lnk_type
;
295 const int impl::file_info::reg_type
= atf_fs_stat_reg_type
;
296 const int impl::file_info::sock_type
= atf_fs_stat_sock_type
;
297 const int impl::file_info::wht_type
= atf_fs_stat_wht_type
;
299 impl::file_info::file_info(const path
& p
)
303 err
= atf_fs_stat_init(&m_stat
, p
.c_path());
304 if (atf_is_error(err
))
305 throw_atf_error(err
);
308 impl::file_info::file_info(const file_info
& fi
)
310 atf_fs_stat_copy(&m_stat
, &fi
.m_stat
);
313 impl::file_info::~file_info(void)
315 atf_fs_stat_fini(&m_stat
);
319 impl::file_info::get_device(void)
322 return atf_fs_stat_get_device(&m_stat
);
326 impl::file_info::get_inode(void)
329 return atf_fs_stat_get_inode(&m_stat
);
333 impl::file_info::get_mode(void)
336 return atf_fs_stat_get_mode(&m_stat
);
340 impl::file_info::get_size(void)
343 return atf_fs_stat_get_size(&m_stat
);
347 impl::file_info::get_type(void)
350 return atf_fs_stat_get_type(&m_stat
);
354 impl::file_info::is_owner_readable(void)
357 return atf_fs_stat_is_owner_readable(&m_stat
);
361 impl::file_info::is_owner_writable(void)
364 return atf_fs_stat_is_owner_writable(&m_stat
);
368 impl::file_info::is_owner_executable(void)
371 return atf_fs_stat_is_owner_executable(&m_stat
);
375 impl::file_info::is_group_readable(void)
378 return atf_fs_stat_is_group_readable(&m_stat
);
382 impl::file_info::is_group_writable(void)
385 return atf_fs_stat_is_group_writable(&m_stat
);
389 impl::file_info::is_group_executable(void)
392 return atf_fs_stat_is_group_executable(&m_stat
);
396 impl::file_info::is_other_readable(void)
399 return atf_fs_stat_is_other_readable(&m_stat
);
403 impl::file_info::is_other_writable(void)
406 return atf_fs_stat_is_other_writable(&m_stat
);
410 impl::file_info::is_other_executable(void)
413 return atf_fs_stat_is_other_executable(&m_stat
);
416 // ------------------------------------------------------------------------
417 // The "directory" class.
418 // ------------------------------------------------------------------------
420 impl::directory::directory(const path
& p
)
422 DIR* dp
= ::opendir(p
.c_str());
424 throw system_error(IMPL_NAME
"::directory::directory(" +
425 p
.str() + ")", "opendir(3) failed", errno
);
428 while ((dep
= ::readdir(dp
)) != NULL
) {
429 path entryp
= p
/ dep
->d_name
;
430 insert(value_type(dep
->d_name
, file_info(entryp
)));
433 if (::closedir(dp
) == -1)
434 throw system_error(IMPL_NAME
"::directory::directory(" +
435 p
.str() + ")", "closedir(3) failed", errno
);
438 std::set
< std::string
>
439 impl::directory::names(void)
442 std::set
< std::string
> ns
;
444 for (const_iterator iter
= begin(); iter
!= end(); iter
++)
445 ns
.insert((*iter
).first
);
450 // ------------------------------------------------------------------------
452 // ------------------------------------------------------------------------
455 impl::exists(const path
& p
)
460 err
= atf_fs_exists(p
.c_path(), &b
);
461 if (atf_is_error(err
))
462 throw_atf_error(err
);
468 impl::have_prog_in_path(const std::string
& prog
)
470 PRE(prog
.find('/') == std::string::npos
);
472 // Do not bother to provide a default value for PATH. If it is not
473 // there something is broken in the user's environment.
474 if (!atf::env::has("PATH"))
475 throw std::runtime_error("PATH not defined in the environment");
476 std::vector
< std::string
> dirs
=
477 atf::text::split(atf::env::get("PATH"), ":");
480 for (std::vector
< std::string
>::const_iterator iter
= dirs
.begin();
481 !found
&& iter
!= dirs
.end(); iter
++) {
482 const path
& dir
= path(*iter
);
484 if (is_executable(dir
/ prog
))
491 impl::is_executable(const path
& p
)
495 return safe_access(p
, atf_fs_access_x
, EACCES
);
499 impl::remove(const path
& p
)
501 if (file_info(p
).get_type() == file_info::dir_type
)
502 throw atf::system_error(IMPL_NAME
"::remove(" + p
.str() + ")",
505 if (::unlink(p
.c_str()) == -1)
506 throw atf::system_error(IMPL_NAME
"::remove(" + p
.str() + ")",
507 "unlink(" + p
.str() + ") failed",
512 impl::rmdir(const path
& p
)
514 atf_error_t err
= atf_fs_rmdir(p
.c_path());
515 if (atf_is_error(err
))
516 throw_atf_error(err
);