/usr/include/dar/mask.hpp is in libdar-dev 2.4.8-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | /*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2052 Denis Corbin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// to contact the author : http://dar.linux.free.fr/email.html
/*********************************************************************/
/// \file mask.hpp
/// \brief here lies a collection of mask classes
///
/// The mask classes defined here are to be used to filter files
/// in the libdar API calls.
/// \ingroup API
#ifndef MASK_HPP
#define MASK_HPP
#include "/usr/include/dar/libdar_my_config.h"
extern "C"
{
#if LIBDAR_HAS_UNISTD_H
#include <unistd.h>
#endif
#if LIBDAR_HAS_REGEX_H
#include <regex.h>
#endif
} // end extern "C"
#include <string>
#include <vector>
#include <new>
#include "/usr/include/dar/path.hpp"
namespace libdar
{
/// \addtogroup API
/// @{
/// the generic class, parent of all masks
/// this is a pure virtual class that is used in API call
/// any of the following mask classes inherit from this class
class mask
{
public :
virtual ~mask() {};
/// check wether the given string is covered by the mask
/// \param[in] expression is the filename to check
/// \return true if the given filename is covered by the mask
/// \note only libdar internally needs to call this method
virtual bool is_covered(const std::string &expression) const = 0;
/// check whether the given path is covered by the mask
/// \param[in] chemin is the path to check
/// \return true if the given path is covered by the mask
/// \note only libdar internally needs to call this method
/// \note this is an optional method to the previous one, it can be overwritten
virtual bool is_covered(const path & chemin) const { return is_covered(chemin.display()); };
/// this is to be able to copy a mask without knowing its
/// exact class and without loosing its specialized data
virtual mask *clone() const = 0;
};
/// boolean mask, either always true or false
/// it matches all files or no files at all
class bool_mask : public mask
{
public :
/// the constructor
/// \param[in] always is the value that this mask will always return
/// when the is_covered method will be used
/// \note once initialized an object cannot change its behavior
bool_mask(bool always) { val = always; };
/// inherited from the mask class
bool is_covered(const std::string & expression) const { return val; };
bool is_covered(const path & chemin) const { return val; };
/// inherited from the mask class
mask *clone() const { return new (std::nothrow) bool_mask(val); };
private :
bool val;
};
/// matches as done on shell command lines (see "man 7 glob")
class simple_mask : public mask
{
public :
/// the constructor to use by libdar external programs
/// \param[in] wilde_card_expression is the glob expression that defines the mask
/// \param[in] case_sensit whether the mask is case sensitive or not
simple_mask(const std::string & wilde_card_expression, bool case_sensit);
/// copy constructor
simple_mask(const simple_mask & m) : mask(m) { copy_from(m); };
/// assignment operator
const simple_mask & operator = (const simple_mask & m);
/// inherited from the mask class
bool is_covered(const std::string &expression) const;
/// inherited from the mask class
mask *clone() const { return new (std::nothrow) simple_mask(*this); };
private :
std::string the_mask;
bool case_s;
void copy_from(const simple_mask & m);
};
/// matches regular expressions (see "man 7 regex")
class regular_mask : public mask
{
public :
/// the constructor to be used by libdar external programs
/// \param[in] wilde_card_expression is the regular expression that defines the mask
/// \param[in] x_case_sensit whether the mask is case sensitive or not
regular_mask(const std::string & wilde_card_expression,
bool x_case_sensit);
/// the copy constructor
regular_mask(const regular_mask & ref);
/// the assignment operator
regular_mask & operator= (const regular_mask & ref);
/// destructor
virtual ~regular_mask() { regfree(&preg); };
/// inherited from the mask class
bool is_covered(const std::string & expression) const;
/// inherited from the mask class
mask *clone() const { return new (std::nothrow) regular_mask(*this); };
private :
regex_t preg;
std::string mask_exp; //< used only by the copy constructor
bool case_sensit; //< used only by the copy constructor
void set_preg(const std::string & wilde_card_expression,
bool x_case_sensit);
};
/// negation of another mask
/// this is the first mask built over masks
/// it realizes the negation of the given mask
class not_mask : public mask
{
public :
/// the constructor to be used by libdar external programs
/// \param[in] m is the mask to negate
/// \note the mask used as argument need not to survive the just created not_mask object
/// as an internal copy of the mask given in argument has been done.
not_mask(const mask &m) { copy_from(m); };
/// copy constructor
not_mask(const not_mask & m) : mask(m) { copy_from(m); };
/// assignment operator
const not_mask & operator = (const not_mask & m);
/// destructor
~not_mask() { detruit(); };
/// inherited from the mask class
bool is_covered(const std::string &expression) const { return !ref->is_covered(expression); };
bool is_covered(const path & chemin) const { return !ref->is_covered(chemin); };
/// inherited from the mask class
mask *clone() const { return new (std::nothrow) not_mask(*this); };
private :
mask *ref;
void copy_from(const not_mask &m);
void copy_from(const mask &m);
void detruit();
};
/// makes an *AND* operator between two or more masks
class et_mask : public mask
{
public :
/// the constructor to be used by libdar external programs
/// \note at this stage the mask is not usable and will
/// throw an exception until some mask are added to the *AND*
/// thanks to the add_mask() method
et_mask() {};
/// copy constructor
et_mask(const et_mask &m) : mask(m) { copy_from(m); };
/// assignment operator
const et_mask & operator = (const et_mask &m);
/// destructor
~et_mask() { detruit(); };
/// add a mask to the operator
/// \param[in] toadd a mask to add to the *AND* operator
/// \note the mask given in argument has not to survive the et_mask to which it has been added
/// a internal copy of the mask has been done.
void add_mask(const mask & toadd);
/// inherited from the mask class
bool is_covered(const std::string & expression) const { return t_is_covered(expression); };
bool is_covered(const path & chemin) const { return t_is_covered(chemin); };
/// inherited from the mask class
mask *clone() const { return new (std::nothrow) et_mask(*this); };
/// the number of mask on which is done the *AND* operator
/// \return the number of mask that has been added thanks to the add_mask() method
/// \note there is no mean to remove a given mask once it has been added (see the clear method)
U_I size() const { return lst.size(); };
/// clear the mask
/// remove all previously added masks
/// \note that after this call the mask is no more usable as the *AND* operator cannot be done
/// on any mask
void clear() { detruit(); };
protected :
std::vector<mask *> lst;
private :
void copy_from(const et_mask & m);
void detruit();
template<class T> bool t_is_covered(const T & expression) const
{
std::vector<mask *>::const_iterator it = lst.begin();
if(lst.empty())
throw Erange("et_mask::is_covered", dar_gettext("No mask in the list of mask to operate on"));
while(it != lst.end() && (*it)->is_covered(expression))
++it;
return it == lst.end();
}
};
/// makes the *OR* operator between two or more masks
/// this mask has exactly the same use as the et_mask
/// please see the et_mask documentation. The only difference
/// is that it makes an *OR* operation rather than an *AND*
/// with the masks added thanks to the add_mask method
class ou_mask : public et_mask
{
public:
/// inherited from the mask class
bool is_covered(const std::string & expression) const { return t_is_covered(expression); };
bool is_covered(const path & chemin) const { return t_is_covered(chemin); }
;
/// inherited from the mask class
mask *clone() const { return new (std::nothrow) ou_mask(*this); };
private:
template<class T> bool t_is_covered(const T & expression) const
{
std::vector<mask *>::const_iterator it = lst.begin();
if(lst.empty())
throw Erange("et_mask::is_covered", dar_gettext("No mask to operate on in the list of mask"));
while(it != lst.end() && ! (*it)->is_covered(expression))
it++;
return it != lst.end();
}
};
/// string matches if it is subdir of mask or mask is a subdir of expression
class simple_path_mask : public mask
{
public :
/// the constructor to be used by libdar external programs
/// \param[in] p the path the compare with
/// \param[in] case_sensit whether the mask is case sensitive or not
/// \note p must be a valid path
simple_path_mask(const path &p, bool case_sensit) : chemin(p) { case_s = case_sensit; };
/// inherited from the mask class
bool is_covered(const std::string & expression) const { throw SRC_BUG; };
bool is_covered(const path & chemin) const;
/// inherited from the mask class
mask *clone() const { return new (std::nothrow) simple_path_mask(*this); };
private :
path chemin;
bool case_s;
};
/// matches if string is exactly the given mask (no wilde card expression)
class same_path_mask : public mask
{
public :
/// the constructor to be used by libdar external programs
/// \param[in] p is the path to compare with
/// \param[in] case_sensit whether the mask is case sensitive or not
same_path_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit; };
/// inherited from the mask class
bool is_covered(const std::string &chemin) const;
/// inherited from the mask class
mask *clone() const { return new (std::nothrow) same_path_mask(*this); };
private :
std::string chemin;
bool case_s;
};
/// matches if string is the given constructor string or a sub directory of it
class exclude_dir_mask : public mask
{
public:
/// the constructor to be used by libdar external programs
/// \param[in] p is the path to compare with
/// \param[in] case_sensit whether the mask is case sensitive or not
exclude_dir_mask(const std::string &p, bool case_sensit) { chemin = p; case_s = case_sensit;};
/// inherited from the mask class
bool is_covered(const std::string &expression) const { throw SRC_BUG; }
bool is_covered(const path &chemin) const { return chemin.is_subdir_of(chemin, case_s); };
/// inherited from the mask class
mask *clone() const { return new (std::nothrow) exclude_dir_mask(*this); };
private:
std::string chemin;
bool case_s;
};
/// @}
} // end of namespace
#endif
|