1 // distribution boxbackup-0.11_trunk_2979 (svn version: 2979)
2 // Box Backup, http://www.boxbackup.org/
3 //
4 // Copyright (c) 2003-2010, Ben Summers and contributors.
5 // All rights reserved.
6 //
7 // Note that this project uses mixed licensing. Any file with this license
8 // attached, or where the code LICENSE-DUAL appears on the first line, falls
9 // under this license. See the file COPYING.txt for more information.
10 //
11 // This file is dual licensed. You may use and distribute it providing that you
12 // comply EITHER with the terms of the BSD license, OR the GPL license. It is
13 // not necessary to comply with both licenses, only one.
14 //
15 // The BSD license option follows:
16 //
17 // Redistribution and use in source and binary forms, with or without
18 // modification, are permitted provided that the following conditions are met:
19 //
20 // 1. Redistributions of source code must retain the above copyright
21 //    notice, this list of conditions and the following disclaimer.
22 //
23 // 2. Redistributions in binary form must reproduce the above copyright
24 //    notice, this list of conditions and the following disclaimer in the
25 //    documentation and/or other materials provided with the distribution.
26 //
27 // 3. Neither the name of the Box Backup nor the names of its contributors may
28 //    be used to endorse or promote products derived from this software without
29 //    specific prior written permission.
30 //
31 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
35 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 //
42 // [http://en.wikipedia.org/wiki/BSD_licenses#3-clause_license_.28.22New_BSD_License.22.29]
43 //
44 // The GPL license option follows:
45 //
46 // This program is free software; you can redistribute it and/or
47 // modify it under the terms of the GNU General Public License
48 // as published by the Free Software Foundation; either version 2
49 // of the License, or (at your option) any later version.
50 //
51 // This program is distributed in the hope that it will be useful,
52 // but WITHOUT ANY WARRANTY; without even the implied warranty of
53 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
54 // GNU General Public License for more details.
55 //
56 // You should have received a copy of the GNU General Public License
57 // along with this program; if not, write to the Free Software
58 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
59 //
60 // [http://www.gnu.org/licenses/old-licenses/gpl-2.0.html#SEC4]
61 // --------------------------------------------------------------------------
62 //
63 // File
64 //		Name:    Guards.h
65 //		Purpose: Classes which ensure things are closed/deleted properly when
66 //				 going out of scope. Easy exception proof code, etc
67 //		Created: 2003/07/12
68 //
69 // --------------------------------------------------------------------------
70 
71 #ifndef GUARDS__H
72 #define GUARDS__H
73 
74 #ifdef HAVE_UNISTD_H
75 	#include <unistd.h>
76 #endif
77 
78 #include <errno.h>
79 #include <fcntl.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <sys/stat.h>
83 
84 #include <new>
85 
86 #include "CommonException.h"
87 #include "Logging.h"
88 
89 #include "MemLeakFindOn.h"
90 
91 template <int flags = O_RDONLY | O_BINARY, int mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)>
92 class FileHandleGuard
93 {
94 public:
FileHandleGuard(const std::string & rFilename)95 	FileHandleGuard(const std::string& rFilename)
96 		: mOSFileHandle(::open(rFilename.c_str(), flags, mode))
97 	{
98 		if(mOSFileHandle < 0)
99 		{
100 			BOX_LOG_SYS_ERROR("FileHandleGuard: failed to open "
101 				"file '" << rFilename << "'");
102 			THROW_EXCEPTION(CommonException, OSFileOpenError)
103 		}
104 	}
105 
~FileHandleGuard()106 	~FileHandleGuard()
107 	{
108 		if(mOSFileHandle >= 0)
109 		{
110 			Close();
111 		}
112 	}
113 
Close()114 	void Close()
115 	{
116 		if(mOSFileHandle < 0)
117 		{
118 			THROW_EXCEPTION(CommonException, FileAlreadyClosed)
119 		}
120 		if(::close(mOSFileHandle) != 0)
121 		{
122 			THROW_EXCEPTION(CommonException, OSFileCloseError)
123 		}
124 		mOSFileHandle = -1;
125 	}
126 
127 	operator int() const
128 	{
129 		return mOSFileHandle;
130 	}
131 
132 private:
133 	int mOSFileHandle;
134 };
135 
136 template<typename type>
137 class MemoryBlockGuard
138 {
139 public:
MemoryBlockGuard(int BlockSize)140 	MemoryBlockGuard(int BlockSize)
141 		: mpBlock(::malloc(BlockSize))
142 	{
143 		if(mpBlock == 0)
144 		{
145 			throw std::bad_alloc();
146 		}
147 	}
148 
~MemoryBlockGuard()149 	~MemoryBlockGuard()
150 	{
151 		free(mpBlock);
152 	}
153 
type()154 	operator type() const
155 	{
156 		return (type)mpBlock;
157 	}
158 
GetPtr()159 	type GetPtr() const
160 	{
161 		return (type)mpBlock;
162 	}
163 
Resize(int NewSize)164 	void Resize(int NewSize)
165 	{
166 		void *ptrn = ::realloc(mpBlock, NewSize);
167 		if(ptrn == 0)
168 		{
169 			throw std::bad_alloc();
170 		}
171 		mpBlock = ptrn;
172 	}
173 
174 private:
175 	void *mpBlock;
176 };
177 
178 #include "MemLeakFindOff.h"
179 
180 #endif // GUARDS__H
181 
182