2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007, 2008, 2009 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 "atf-c++/exceptions.hpp"
54 #include "atf-c++/env.hpp"
55 #include "atf-c++/fs.hpp"
56 #include "atf-c++/io.hpp"
57 #include "atf-c++/sanity.hpp"
58 #include "atf-c++/text.hpp"
59 #include "atf-c++/user.hpp"
60 #include "atf-c++/utils.hpp"
62 namespace impl
= atf::fs
;
63 #define IMPL_NAME "atf::fs"
65 // ------------------------------------------------------------------------
66 // Auxiliary functions.
67 // ------------------------------------------------------------------------
70 //! \brief A controlled version of access(2).
72 //! This function reimplements the standard access(2) system call to
73 //! safely control its exit status and raise an exception in case of
78 safe_access(const impl::path
& p
, int mode
, int experr
)
82 atf_error_t err
= atf_fs_eaccess(p
.c_path(), mode
);
83 if (atf_is_error(err
)) {
84 if (atf_error_is(err
, "libc")) {
85 if (atf_libc_error_code(err
) == experr
) {
89 atf::throw_atf_error(err
);
90 // XXX Silence warning; maybe throw_atf_error should be
91 // an exception and not a function.
95 atf::throw_atf_error(err
);
96 // XXX Silence warning; maybe throw_atf_error should be
97 // an exception and not a function.
106 // ------------------------------------------------------------------------
108 // ------------------------------------------------------------------------
110 impl::path::path(const std::string
& s
)
112 atf_error_t err
= atf_fs_path_init_fmt(&m_path
, "%s", s
.c_str());
113 if (atf_is_error(err
))
114 throw_atf_error(err
);
117 impl::path::path(const path
& p
)
119 atf_error_t err
= atf_fs_path_copy(&m_path
, &p
.m_path
);
120 if (atf_is_error(err
))
121 throw_atf_error(err
);
124 impl::path::path(const atf_fs_path_t
*p
)
126 atf_error_t err
= atf_fs_path_copy(&m_path
, p
);
127 if (atf_is_error(err
))
128 throw_atf_error(err
);
131 impl::path::~path(void)
133 atf_fs_path_fini(&m_path
);
137 impl::path::c_str(void)
140 return atf_fs_path_cstring(&m_path
);
144 impl::path::c_path(void)
151 impl::path::str(void)
158 impl::path::is_absolute(void)
161 return atf_fs_path_is_absolute(&m_path
);
165 impl::path::is_root(void)
168 return atf_fs_path_is_root(&m_path
);
172 impl::path::branch_path(void)
178 err
= atf_fs_path_branch_path(&m_path
, &bp
);
179 if (atf_is_error(err
))
180 throw_atf_error(err
);
182 path
p(atf_fs_path_cstring(&bp
));
183 atf_fs_path_fini(&bp
);
188 impl::path::leaf_name(void)
194 err
= atf_fs_path_leaf_name(&m_path
, &ln
);
195 if (atf_is_error(err
))
196 throw_atf_error(err
);
198 std::string
s(atf_dynstr_cstring(&ln
));
199 atf_dynstr_fini(&ln
);
204 impl::path::to_absolute(void)
209 atf_error_t err
= atf_fs_path_to_absolute(&m_path
, &pa
);
210 if (atf_is_error(err
))
211 throw_atf_error(err
);
213 path
p(atf_fs_path_cstring(&pa
));
214 atf_fs_path_fini(&pa
);
219 impl::path::operator=(const path
& p
)
223 atf_error_t err
= atf_fs_path_init_fmt(&tmp
, "%s", p
.c_str());
224 if (atf_is_error(err
))
225 throw_atf_error(err
);
227 atf_fs_path_fini(&m_path
);
235 impl::path::operator==(const path
& p
)
238 return atf_equal_fs_path_fs_path(&m_path
, &p
.m_path
);
242 impl::path::operator!=(const path
& p
)
245 return !atf_equal_fs_path_fs_path(&m_path
, &p
.m_path
);
249 impl::path::operator/(const std::string
& p
)
254 atf_error_t err
= atf_fs_path_append_fmt(&p2
.m_path
, "%s", p
.c_str());
255 if (atf_is_error(err
))
256 throw_atf_error(err
);
262 impl::path::operator/(const path
& p
)
267 atf_error_t err
= atf_fs_path_append_fmt(&p2
.m_path
, "%s",
268 atf_fs_path_cstring(&p
.m_path
));
269 if (atf_is_error(err
))
270 throw_atf_error(err
);
276 impl::path::operator<(const path
& p
)
279 const char *s1
= atf_fs_path_cstring(&m_path
);
280 const char *s2
= atf_fs_path_cstring(&p
.m_path
);
281 return std::strcmp(s1
, s2
) < 0;
284 // ------------------------------------------------------------------------
285 // The "file_info" class.
286 // ------------------------------------------------------------------------
288 const int impl::file_info::blk_type
= atf_fs_stat_blk_type
;
289 const int impl::file_info::chr_type
= atf_fs_stat_chr_type
;
290 const int impl::file_info::dir_type
= atf_fs_stat_dir_type
;
291 const int impl::file_info::fifo_type
= atf_fs_stat_fifo_type
;
292 const int impl::file_info::lnk_type
= atf_fs_stat_lnk_type
;
293 const int impl::file_info::reg_type
= atf_fs_stat_reg_type
;
294 const int impl::file_info::sock_type
= atf_fs_stat_sock_type
;
295 const int impl::file_info::wht_type
= atf_fs_stat_wht_type
;
297 impl::file_info::file_info(const path
& p
)
301 err
= atf_fs_stat_init(&m_stat
, p
.c_path());
302 if (atf_is_error(err
))
303 throw_atf_error(err
);
306 impl::file_info::file_info(const file_info
& fi
)
308 atf_fs_stat_copy(&m_stat
, &fi
.m_stat
);
311 impl::file_info::~file_info(void)
313 atf_fs_stat_fini(&m_stat
);
317 impl::file_info::get_device(void)
320 return atf_fs_stat_get_device(&m_stat
);
324 impl::file_info::get_inode(void)
327 return atf_fs_stat_get_inode(&m_stat
);
331 impl::file_info::get_mode(void)
334 return atf_fs_stat_get_mode(&m_stat
);
338 impl::file_info::get_size(void)
341 return atf_fs_stat_get_size(&m_stat
);
345 impl::file_info::get_type(void)
348 return atf_fs_stat_get_type(&m_stat
);
352 impl::file_info::is_owner_readable(void)
355 return atf_fs_stat_is_owner_readable(&m_stat
);
359 impl::file_info::is_owner_writable(void)
362 return atf_fs_stat_is_owner_writable(&m_stat
);
366 impl::file_info::is_owner_executable(void)
369 return atf_fs_stat_is_owner_executable(&m_stat
);
373 impl::file_info::is_group_readable(void)
376 return atf_fs_stat_is_group_readable(&m_stat
);
380 impl::file_info::is_group_writable(void)
383 return atf_fs_stat_is_group_writable(&m_stat
);
387 impl::file_info::is_group_executable(void)
390 return atf_fs_stat_is_group_executable(&m_stat
);
394 impl::file_info::is_other_readable(void)
397 return atf_fs_stat_is_other_readable(&m_stat
);
401 impl::file_info::is_other_writable(void)
404 return atf_fs_stat_is_other_writable(&m_stat
);
408 impl::file_info::is_other_executable(void)
411 return atf_fs_stat_is_other_executable(&m_stat
);
414 // ------------------------------------------------------------------------
415 // The "directory" class.
416 // ------------------------------------------------------------------------
418 impl::directory::directory(const path
& p
)
420 DIR* dp
= ::opendir(p
.c_str());
422 throw system_error(IMPL_NAME
"::directory::directory(" +
423 p
.str() + ")", "opendir(3) failed", errno
);
426 while ((dep
= ::readdir(dp
)) != NULL
) {
427 path entryp
= p
/ dep
->d_name
;
428 insert(value_type(dep
->d_name
, file_info(entryp
)));
431 if (::closedir(dp
) == -1)
432 throw system_error(IMPL_NAME
"::directory::directory(" +
433 p
.str() + ")", "closedir(3) failed", errno
);
436 std::set
< std::string
>
437 impl::directory::names(void)
440 std::set
< std::string
> ns
;
442 for (const_iterator iter
= begin(); iter
!= end(); iter
++)
443 ns
.insert((*iter
).first
);
448 // ------------------------------------------------------------------------
449 // The "temp_dir" class.
450 // ------------------------------------------------------------------------
452 impl::temp_dir::temp_dir(const path
& p
)
454 atf::utils::auto_array
< char > buf(new char[p
.str().length() + 1]);
455 std::strcpy(buf
.get(), p
.c_str());
456 if (::mkdtemp(buf
.get()) == NULL
)
457 throw system_error(IMPL_NAME
"::temp_dir::temp_dir(" +
458 p
.str() + ")", "mkdtemp(3) failed",
461 m_path
.reset(new path(buf
.get()));
464 impl::temp_dir::~temp_dir(void)
470 impl::temp_dir::get_path(void)
476 // ------------------------------------------------------------------------
477 // The "temp_file" class.
478 // ------------------------------------------------------------------------
480 impl::temp_file::temp_file(const path
& p
) :
484 atf::utils::auto_array
< char > buf(new char[p
.str().length() + 1]);
485 std::strcpy(buf
.get(), p
.c_str());
487 m_fd
= ::mkstemp(buf
.get());
489 throw system_error(IMPL_NAME
"::temp_file::temp_file(" +
490 p
.str() + ")", "mkstemp(3) failed",
492 m_systembuf
.reset(new io::systembuf(m_fd
));
493 rdbuf(m_systembuf
.get());
495 m_path
.reset(new path(buf
.get()));
498 impl::temp_file::~temp_file(void)
503 } catch (const atf::system_error
& e
) {
504 // Ignore deletion errors.
509 impl::temp_file::get_path(void)
516 impl::temp_file::close(void)
526 impl::temp_file::fd(void)
532 // ------------------------------------------------------------------------
534 // ------------------------------------------------------------------------
537 impl::change_directory(const path
& dir
)
539 path olddir
= get_current_dir();
542 if (::chdir(dir
.c_str()) == -1)
543 throw system_error(IMPL_NAME
"::chdir(" + dir
.str() + ")",
544 "chdir(2) failed", errno
);
551 impl::exists(const path
& p
)
556 err
= atf_fs_exists(p
.c_path(), &b
);
557 if (atf_is_error(err
))
558 throw_atf_error(err
);
564 impl::have_prog_in_path(const std::string
& prog
)
566 PRE(prog
.find('/') == std::string::npos
);
568 // Do not bother to provide a default value for PATH. If it is not
569 // there something is broken in the user's environment.
570 if (!atf::env::has("PATH"))
571 throw std::runtime_error("PATH not defined in the environment");
572 std::vector
< std::string
> dirs
=
573 atf::text::split(atf::env::get("PATH"), ":");
576 for (std::vector
< std::string
>::const_iterator iter
= dirs
.begin();
577 !found
&& iter
!= dirs
.end(); iter
++) {
578 const path
& dir
= path(*iter
);
580 if (is_executable(dir
/ prog
))
587 impl::get_current_dir(void)
591 atf_error_t err
= atf_fs_getcwd(&cwd
);
592 if (atf_is_error(err
))
593 throw_atf_error(err
);
595 path
p(atf_fs_path_cstring(&cwd
));
596 atf_fs_path_fini(&cwd
);
601 impl::is_executable(const path
& p
)
605 return safe_access(p
, atf_fs_access_x
, EACCES
);
609 impl::remove(const path
& p
)
611 if (file_info(p
).get_type() == file_info::dir_type
)
612 throw atf::system_error(IMPL_NAME
"::remove(" + p
.str() + ")",
615 if (::unlink(p
.c_str()) == -1)
616 throw atf::system_error(IMPL_NAME
"::remove(" + p
.str() + ")",
617 "unlink(" + p
.str() + ") failed",
622 impl::cleanup(const path
& p
)
626 err
= atf_fs_cleanup(p
.c_path());
627 if (atf_is_error(err
))
628 throw_atf_error(err
);
632 impl::rmdir(const path
& p
)
634 atf_error_t err
= atf_fs_rmdir(p
.c_path());
635 if (atf_is_error(err
))
636 throw_atf_error(err
);
639 // XXX: This is racy if we have other threads. We should grab the current
640 // umask durent program initialization and never query it again, provided
641 // that we don't modify it, of course.
643 impl::current_umask(void)
645 const mode_t current
= umask(0);
646 (void)umask(current
);