Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c++ / fs.cpp
blobcbc097d66a13774f988af0c74a9e6e97c7d40245
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
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)
31 #include "bconfig.h"
32 #endif
34 extern "C" {
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/mount.h>
38 #include <sys/stat.h>
39 #include <sys/wait.h>
40 #include <dirent.h>
41 #include <libgen.h>
42 #include <unistd.h>
45 #include <cerrno>
46 #include <cstdlib>
47 #include <cstring>
49 extern "C" {
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 // ------------------------------------------------------------------------
69 //!
70 //! \brief A controlled version of access(2).
71 //!
72 //! This function reimplements the standard access(2) system call to
73 //! safely control its exit status and raise an exception in case of
74 //! failure.
75 //!
76 static
77 bool
78 safe_access(const impl::path& p, int mode, int experr)
80 bool ok;
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) {
86 atf_error_free(err);
87 ok = false;
88 } else {
89 atf::throw_atf_error(err);
90 // XXX Silence warning; maybe throw_atf_error should be
91 // an exception and not a function.
92 ok = false;
94 } else {
95 atf::throw_atf_error(err);
96 // XXX Silence warning; maybe throw_atf_error should be
97 // an exception and not a function.
98 ok = false;
100 } else
101 ok = true;
103 return ok;
106 // ------------------------------------------------------------------------
107 // The "path" class.
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);
136 const char*
137 impl::path::c_str(void)
138 const
140 return atf_fs_path_cstring(&m_path);
143 const atf_fs_path_t*
144 impl::path::c_path(void)
145 const
147 return &m_path;
150 std::string
151 impl::path::str(void)
152 const
154 return c_str();
157 bool
158 impl::path::is_absolute(void)
159 const
161 return atf_fs_path_is_absolute(&m_path);
164 bool
165 impl::path::is_root(void)
166 const
168 return atf_fs_path_is_root(&m_path);
171 impl::path
172 impl::path::branch_path(void)
173 const
175 atf_fs_path_t bp;
176 atf_error_t err;
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);
184 return p;
187 std::string
188 impl::path::leaf_name(void)
189 const
191 atf_dynstr_t ln;
192 atf_error_t err;
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);
200 return s;
203 impl::path
204 impl::path::to_absolute(void)
205 const
207 atf_fs_path_t pa;
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);
215 return p;
218 impl::path&
219 impl::path::operator=(const path& p)
221 atf_fs_path_t tmp;
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);
226 else {
227 atf_fs_path_fini(&m_path);
228 m_path = tmp;
231 return *this;
234 bool
235 impl::path::operator==(const path& p)
236 const
238 return atf_equal_fs_path_fs_path(&m_path, &p.m_path);
241 bool
242 impl::path::operator!=(const path& p)
243 const
245 return !atf_equal_fs_path_fs_path(&m_path, &p.m_path);
248 impl::path
249 impl::path::operator/(const std::string& p)
250 const
252 path p2 = *this;
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);
258 return p2;
261 impl::path
262 impl::path::operator/(const path& p)
263 const
265 path p2 = *this;
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);
272 return p2;
275 bool
276 impl::path::operator<(const path& p)
277 const
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)
299 atf_error_t err;
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);
316 dev_t
317 impl::file_info::get_device(void)
318 const
320 return atf_fs_stat_get_device(&m_stat);
323 ino_t
324 impl::file_info::get_inode(void)
325 const
327 return atf_fs_stat_get_inode(&m_stat);
330 mode_t
331 impl::file_info::get_mode(void)
332 const
334 return atf_fs_stat_get_mode(&m_stat);
337 off_t
338 impl::file_info::get_size(void)
339 const
341 return atf_fs_stat_get_size(&m_stat);
345 impl::file_info::get_type(void)
346 const
348 return atf_fs_stat_get_type(&m_stat);
351 bool
352 impl::file_info::is_owner_readable(void)
353 const
355 return atf_fs_stat_is_owner_readable(&m_stat);
358 bool
359 impl::file_info::is_owner_writable(void)
360 const
362 return atf_fs_stat_is_owner_writable(&m_stat);
365 bool
366 impl::file_info::is_owner_executable(void)
367 const
369 return atf_fs_stat_is_owner_executable(&m_stat);
372 bool
373 impl::file_info::is_group_readable(void)
374 const
376 return atf_fs_stat_is_group_readable(&m_stat);
379 bool
380 impl::file_info::is_group_writable(void)
381 const
383 return atf_fs_stat_is_group_writable(&m_stat);
386 bool
387 impl::file_info::is_group_executable(void)
388 const
390 return atf_fs_stat_is_group_executable(&m_stat);
393 bool
394 impl::file_info::is_other_readable(void)
395 const
397 return atf_fs_stat_is_other_readable(&m_stat);
400 bool
401 impl::file_info::is_other_writable(void)
402 const
404 return atf_fs_stat_is_other_writable(&m_stat);
407 bool
408 impl::file_info::is_other_executable(void)
409 const
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());
421 if (dp == NULL)
422 throw system_error(IMPL_NAME "::directory::directory(" +
423 p.str() + ")", "opendir(3) failed", errno);
425 struct dirent* dep;
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)
438 const
440 std::set< std::string > ns;
442 for (const_iterator iter = begin(); iter != end(); iter++)
443 ns.insert((*iter).first);
445 return ns;
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",
459 errno);
461 m_path.reset(new path(buf.get()));
464 impl::temp_dir::~temp_dir(void)
466 cleanup(*m_path);
469 const impl::path&
470 impl::temp_dir::get_path(void)
471 const
473 return *m_path;
476 // ------------------------------------------------------------------------
477 // The "temp_file" class.
478 // ------------------------------------------------------------------------
480 impl::temp_file::temp_file(const path& p) :
481 std::ostream(NULL),
482 m_fd(-1)
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());
488 if (m_fd == -1)
489 throw system_error(IMPL_NAME "::temp_file::temp_file(" +
490 p.str() + ")", "mkstemp(3) failed",
491 errno);
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)
500 close();
501 try {
502 remove(*m_path);
503 } catch (const atf::system_error& e) {
504 // Ignore deletion errors.
508 const impl::path&
509 impl::temp_file::get_path(void)
510 const
512 return *m_path;
515 void
516 impl::temp_file::close(void)
518 if (m_fd != -1) {
519 flush();
520 ::close(m_fd);
521 m_fd = -1;
526 impl::temp_file::fd(void)
528 INV(m_fd != -1);
529 return m_fd;
532 // ------------------------------------------------------------------------
533 // Free functions.
534 // ------------------------------------------------------------------------
536 impl::path
537 impl::change_directory(const path& dir)
539 path olddir = get_current_dir();
541 if (olddir != dir) {
542 if (::chdir(dir.c_str()) == -1)
543 throw system_error(IMPL_NAME "::chdir(" + dir.str() + ")",
544 "chdir(2) failed", errno);
547 return olddir;
550 bool
551 impl::exists(const path& p)
553 atf_error_t err;
554 bool b;
556 err = atf_fs_exists(p.c_path(), &b);
557 if (atf_is_error(err))
558 throw_atf_error(err);
560 return b;
563 bool
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"), ":");
575 bool found = false;
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))
581 found = true;
583 return found;
586 impl::path
587 impl::get_current_dir(void)
589 atf_fs_path_t cwd;
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);
597 return p;
600 bool
601 impl::is_executable(const path& p)
603 if (!exists(p))
604 return false;
605 return safe_access(p, atf_fs_access_x, EACCES);
608 void
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() + ")",
613 "Is a directory",
614 EPERM);
615 if (::unlink(p.c_str()) == -1)
616 throw atf::system_error(IMPL_NAME "::remove(" + p.str() + ")",
617 "unlink(" + p.str() + ") failed",
618 errno);
621 void
622 impl::cleanup(const path& p)
624 atf_error_t err;
626 err = atf_fs_cleanup(p.c_path());
627 if (atf_is_error(err))
628 throw_atf_error(err);
631 void
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.
642 mode_t
643 impl::current_umask(void)
645 const mode_t current = umask(0);
646 (void)umask(current);
647 return current;