Line data Source code
1 : /* 2 : Zipios -- a small C++ library that provides easy access to .zip files. 3 : 4 : Copyright (C) 2000-2007 Thomas Sondergaard 5 : Copyright (c) 2015-2022 Made to Order Software Corp. All Rights Reserved 6 : 7 : This library is free software; you can redistribute it and/or 8 : modify it under the terms of the GNU Lesser General Public 9 : License as published by the Free Software Foundation; either 10 : version 2.1 of the License, or (at your option) any later version. 11 : 12 : This library is distributed in the hope that it will be useful, 13 : but WITHOUT ANY WARRANTY; without even the implied warranty of 14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 : Lesser General Public License for more details. 16 : 17 : You should have received a copy of the GNU Lesser General Public 18 : License along with this library; if not, write to the Free Software 19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 : */ 21 : 22 : /** \file 23 : * 24 : * Zipios RAII objects used in various unit tests. 25 : */ 26 : 27 : #include "catch_main.hpp" 28 : 29 : #include <unistd.h> 30 : 31 : 32 : namespace zipios_test 33 : { 34 : 35 : 36 627 : auto_unlink_t::auto_unlink_t(std::string const & filename, bool delete_on_creation) 37 627 : : m_filename(filename) 38 : { 39 627 : if(delete_on_creation) 40 : { 41 622 : unlink(); 42 : } 43 627 : } 44 : 45 1254 : auto_unlink_t::~auto_unlink_t() 46 : { 47 627 : unlink(); 48 627 : } 49 : 50 : 51 1249 : void auto_unlink_t::unlink() 52 : { 53 1249 : system(("rm -rf " + m_filename).c_str()); 54 1249 : } 55 : 56 : 57 : 58 86 : safe_chdir::safe_chdir(std::string const & path) 59 86 : : m_original_path(get_current_dir_name()) 60 : { 61 86 : if(m_original_path == nullptr) 62 : { 63 0 : int const e(errno); 64 0 : throw std::logic_error( 65 : "the current working directory could not be queried (err: " 66 0 : + std::to_string(e) 67 0 : + ", " 68 0 : + strerror(e) 69 0 : + ")."); 70 : } 71 : 72 86 : if(chdir(path.c_str()) != 0) 73 : { 74 0 : if(errno == EFAULT) 75 : { 76 0 : throw std::logic_error("the path variable is invalid."); 77 : } 78 0 : if(errno == ENOMEM) 79 : { 80 0 : throw std::bad_alloc(); 81 : } 82 0 : int const e(errno); 83 0 : throw std::runtime_error( 84 : "chdir() generated error: " 85 0 : + std::to_string(e) 86 0 : + ", " 87 0 : + strerror(e)); 88 : } 89 86 : } 90 : 91 : 92 86 : safe_chdir::~safe_chdir() 93 : { 94 86 : if(chdir(m_original_path.get()) != 0) 95 : { 96 0 : int const e(errno); 97 : std::cerr 98 : << "error: chdir(" 99 0 : << m_original_path.get() 100 : << ") generated error: " 101 0 : << std::to_string(e) 102 : << ", " 103 0 : << strerror(e) 104 0 : << "\n"; 105 0 : std::terminate(); 106 : } 107 86 : } 108 : 109 : 110 : 111 : } // zipios_tests namespace 112 : 113 : // Local Variables: 114 : // mode: cpp 115 : // indent-tabs-mode: nil 116 : // c-basic-offset: 4 117 : // tab-width: 4 118 : // End: 119 : 120 : // vim: ts=4 sw=4 et