/usr/include/dar/criterium.hpp is in libdar-dev 2.4.15-1.
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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | /*********************************************************************/
// 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 criterium.hpp
/// \brief contains classes that let the user define the policy for overwriting files
/// \ingroup API
#ifndef CRITERIUM_HPP
#define CRITERIUM_HPP
#include "/usr/include/dar/libdar_my_config.h"
#include <new>
#include "/usr/include/dar/catalogue.hpp"
namespace libdar
{
/// \addtogroup API
/// @{
/// the possible actions for overwriting data
enum over_action_data
{
data_preserve, //< do not overwrite (keep the 'in place' entry)
data_overwrite, //< overwirte the 'in place' entry by the 'to be added' one
data_preserve_mark_already_saved, //< keep the 'in place' but mark it as already saved in the archive of reference
data_overwrite_mark_already_saved, //< overwrite the 'in place' but mark the 'to be added' as already saved in the archive of reference
data_remove, //< remove the original data/EA (file is completely deleted)
data_undefined, //< action still undefined at this step of the evaluation
data_ask //< ask for user decision about file's data
};
/// the possible action for overwriting EA
enum over_action_ea //< define the action to apply to each EA entry (not to the EA set of a particular inode)
{
EA_preserve, //< keep the EA of the 'in place' entry
EA_overwrite, //< keep the EA of the 'to be added' entry
EA_clear, //< drop the EA for the elected entry
EA_preserve_mark_already_saved, //< drop any EA but mark them as already saved in the archive of reference (ctime is the one of the 'in place' inode)
EA_overwrite_mark_already_saved, //< drop any EA but mark them as already saved in the archive of reference (ctime is the one of the 'to be added' inode)
EA_merge_preserve, //< merge EA but do not overwrite existing EA of 'in place' by one of the same name of 'to be added' inode
EA_merge_overwrite, //< merge EA but if both inode share an EA with the same name, take keep the one of the 'to be added' inode
EA_undefined, //< action still undefined at this step of the evaluation
EA_ask //< ask for user decision about EA
};
/// the global action for overwriting
/// this class is a generic interface to handle what action to perform on both EA and Data
/// based on two files to evaluate.
class crit_action
{
public:
/// the destructor
virtual ~crit_action() {};
/// the action to take based on the files to compare
/// \param[in] first is the 'in place' inode
/// \param[in] second is the 'to be added' inode
/// \param[out] data is the action to perform with file's data
/// \param[out] ea is the action to perform with file's EA
virtual void get_action(const nomme & first, const nomme & second, over_action_data & data, over_action_ea & ea) const = 0;
/// clone construction method
/// \return a new object of the same type,
/// \note this method must be implemented in all the leaf classes of the
/// action hierarchy class
virtual crit_action *clone() const = 0;
};
/// the basic constant action
/// the resulting action is not dependant on the files to compare
/// it always returns the action provided through its constructor
class crit_constant_action : public crit_action
{
public:
/// the constuctor
/// \param[in] data the action to perform on data
/// \param[in] ea the action to perform on EA
crit_constant_action(over_action_data data, over_action_ea ea) { x_data = data; x_ea = ea; };
/// the inherited pure virtual methods from class action that must be implemented
void get_action(const nomme & first, const nomme & second, over_action_data & data, over_action_ea & ea) const { data = x_data; ea = x_ea; };
crit_action *clone() const { return new (std::nothrow) crit_constant_action(*this); };
private:
over_action_data x_data;
over_action_ea x_ea;
};
/// the generic criterium class, parent of all criterium
/// this is a pure virtual class that is used in API call
/// it is used to federate under a single type all the
/// criterium classes defined below. It defines a common
/// interface for all of them.
class criterium
{
public:
virtual ~criterium() {};
/// criterum interface method
/// \param[in] first entry to compare with the following (this is the original or 'in place' entry)
/// \param[in] second the other entry to compare with the previous one (this is the new entry to add)
/// \return the result of the criterium evaluation (true or false)
virtual bool evaluate(const nomme &first, const nomme &second) const = 0;
/// clone construction method
/// \return a new object of the same type,
/// \note this method must be implemented in all the leaf classes of the
/// class hierarchy rooted at the criterium class
virtual criterium *clone() const = 0;
protected:
static const inode *get_inode(const nomme * arg);
};
/// the testing class binds criterium to actions
/// a testing class is also an action that let the user build complex
/// testing. It is thus possible to recursively use testing inside testing
class testing : public crit_action
{
public:
/// the constructor
/// \param[in] input is the criterium to base the evaluation on
/// \param[in] go_true is the action to use for evaluation if the criterium states true
/// \param[in] go_false is the action to use for evaluation if the criterium states false
testing(const criterium & input, const crit_action & go_true, const crit_action & go_false);
testing(const testing & ref) : crit_action(ref) { copy_from(ref); if(!check()) throw Ememory("testing::testing(const testing &)"); };
const testing & operator = (const testing & ref) { free(); copy_from(ref); if(!check()) throw Ememory("testing::testing(const testing &)"); return *this; };
~testing() { free(); };
/// the inherited pure virtual method from class action that must be gimplemented
void get_action(const nomme & first, const nomme & second, over_action_data & data, over_action_ea & ea) const
{
if(x_input->evaluate(first, second))
x_go_true->get_action(first, second, data, ea);
else
x_go_false->get_action(first, second, data, ea);
};
crit_action *clone() const { return new (std::nothrow) testing(*this); };
private:
criterium *x_input;
crit_action *x_go_true;
crit_action *x_go_false;
void free();
void copy_from(const testing & ref);
bool check() const; //< returns false if an field is NULL
};
/// the crit_chain class sequences crit_actions up to full definition of the action
/// several expressions must be added. The first is evaluated, then the second, up to the last
/// or up to the step the data_action and ea_action are both fully defined (no data_undefined nor ea_undefined)
class crit_chain : public crit_action
{
public:
crit_chain() { sequence.clear(); };
crit_chain(const crit_chain & ref) : crit_action(ref) { copy_from(ref); };
const crit_chain & operator = (const crit_chain & ref) { destroy(); copy_from(ref); return *this; };
~crit_chain() { destroy(); };
void add(const crit_action & act);
void clear() { destroy(); };
void gobe(crit_chain & to_be_voided);
void get_action(const nomme & first, const nomme & second, over_action_data & data, over_action_ea & ea) const;
crit_action *clone() const { return new (std::nothrow) crit_chain(*this); };
private:
std::vector<crit_action *> sequence;
void destroy();
void copy_from(const crit_chain & ref);
};
////////////////////////////////////////////////////////////
//////////// a set of criterium classes follows ////////////
////////////////////////////////////////////////////////////
/// returns true if the first entry is an inode (whatever is the second)
/// \note the current only entry that can be found in an archive which is not an inode, is an entry
/// signaling that a file has been destroyed since the archive of reference.
class crit_in_place_is_inode : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const { return dynamic_cast<const inode *>(&first) != NULL || dynamic_cast<const mirage *>(&first) != NULL; };
criterium *clone() const { return new (std::nothrow) crit_in_place_is_inode(*this); };
};
/// returns true if the first entry is a directory (whatever is the second)
class crit_in_place_is_dir : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const { return dynamic_cast<const directory *>(&first) != NULL; };
criterium *clone() const { return new (std::nothrow) crit_in_place_is_dir(*this); };
};
/// returns true if the first entry is a plain file (whatever is the second)
class crit_in_place_is_file : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_is_file(*this); };
};
/// returns true if the first entry is a inode with several hard links (whatever is the second entry)
/// it may be a plain file, a symlink a char device, a block device or a named pipe for example
class crit_in_place_is_hardlinked_inode : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const { return dynamic_cast<const mirage *>(&first) != NULL; };
criterium *clone() const { return new (std::nothrow) crit_in_place_is_hardlinked_inode(*this); };
};
/// returns true if the first entry is a inode with several hard links (whatever is the second entry) and also if this first entry is the first we meet that points to this hard linked inode
class crit_in_place_is_new_hardlinked_inode : public criterium
{
bool evaluate(const nomme &first, const nomme &second) const
{
const mirage * tmp = dynamic_cast<const mirage *>(&first);
return tmp != NULL && tmp->is_first_mirage();
};
criterium *clone() const { return new (std::nothrow) crit_in_place_is_new_hardlinked_inode(*this); };
};
/// returns true if the data of the first entry is more recent or of the same date of the one of the second entry
/// this class always returns false if both entry are not inode. Comparison is done on mtime
class crit_in_place_data_more_recent : public criterium
{
public:
crit_in_place_data_more_recent(const infinint & hourshift = 0) : x_hourshift(hourshift) {};
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_data_more_recent(*this); };
private:
infinint x_hourshift;
};
/// returns true if the data of the first entry is more recent or of the same date as the fixed date given in argument to the constructor
/// If the in_place entry is not an inode its date is considered equal to zero. Comparison is done on mtime
class crit_in_place_data_more_recent_or_equal_to : public criterium
{
public:
crit_in_place_data_more_recent_or_equal_to(const infinint & date, const infinint & hourshift = 0) : x_hourshift(hourshift), x_date(date) {};
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_data_more_recent_or_equal_to(*this); };
private:
infinint x_hourshift;
infinint x_date;
};
/// returns true if the data of the first entry is bigger or equal to the one of the second entry
/// this class always returns false if both entries are not plain files
class crit_in_place_data_bigger : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_data_bigger(*this); };
};
/// returns true if the data of the first entry is saved int the archive (not marked as unchanged since the archive of reference)
/// if the entry is not an inode the result is also true
class crit_in_place_data_saved : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_data_saved(*this); };
};
/// return true if the entry is a dirty file (or hard linked dirty file)
class crit_in_place_data_dirty : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_data_dirty(*this); };
};
/// return true if the entry is a sparse file (or hard linked sparse file)
class crit_in_place_data_sparse : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_data_sparse(*this); };
};
/// returns true if the first entry is first an inode, and has some EA (EA may be saved
/// or just recorded as existing).
class crit_in_place_EA_present : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const
{
const inode *tmp = dynamic_cast<const inode *>(&first);
return tmp != NULL && tmp->ea_get_saved_status() != inode::ea_none && tmp->ea_get_saved_status() != inode::ea_removed;
};
criterium *clone() const { return new (std::nothrow) crit_in_place_EA_present(*this); };
};
/// returns true if the EA of the first entry is more recent or equal to the one of the second entry
/// if no EA are present in 'to be added' or if it even not an inode true is returned. If 'in place'
/// does not have EA or is even not an inode true is returned unless 'to be added' has EA present.
/// \note that the comparison is done on the ctime, EA may be just marked as saved in the archive of
/// reference or be saved in the current archive, this does not have any impact on the comparison.
class crit_in_place_EA_more_recent : public criterium
{
public:
crit_in_place_EA_more_recent(const infinint & hourshift = 0) : x_hourshift(hourshift) {};
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_EA_more_recent(*this); };
private:
infinint x_hourshift;
};
/// returns true if the EA of the first entry is more recent or equal to the fixed date given in argument to the constructor
/// comparison using ctime of the "in place" object. If no ctime is available (not an inode for example)
/// the date is considered equal to zero.
class crit_in_place_EA_more_recent_or_equal_to : public criterium
{
public:
crit_in_place_EA_more_recent_or_equal_to(const infinint & date, const infinint & hourshift = 0) : x_hourshift(hourshift), x_date(date) {};
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_EA_more_recent_or_equal_to(*this); };
private:
infinint x_hourshift;
infinint x_date;
};
/// returns true if the first entry has more or even EA (in number not in size) than the second entry
/// if an entry is not an inode or has no EA it is assumed it has zero EA, the comparison is done on that number.
class crit_in_place_more_EA : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_more_EA(*this); };
};
/// returns true if the space used by EA of the first entry is greater or equal to the space used by the EA of the second entry (no EA means 0 byte for EA storage)
/// this criterium does not have any consideration for the second entry
class crit_in_place_EA_bigger : public crit_in_place_more_EA
{
public:
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_EA_bigger(*this); };
};
/// returns true if the in place entry has its EA saved (not just marked as saved) in the archve of reference
/// this criterium does not have any consideration for the second entry
class crit_in_place_EA_saved : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_in_place_EA_saved(*this); };
};
/// returns true if the two entries are of the same type (plain-file/char dev/block dev/named pipe/symlink/directory/unix socket)
/// two plain files are considered of same type even if one is hard linked while the other is not
/// same thing whether one entry has EA while the other has not, they are still considered of the same type.
class crit_same_type : public criterium
{
public:
bool evaluate(const nomme &first, const nomme &second) const;
criterium *clone() const { return new (std::nothrow) crit_same_type(*this); };
};
/// realises the negation of the criterium given in argument to its constructor
class crit_not : public criterium
{
public:
crit_not(const criterium & crit) { x_crit = crit.clone(); if(x_crit == NULL) throw Ememory("crit_not::crit_not"); };
crit_not(const crit_not & ref) : criterium (ref) { copy_from(ref); };
const crit_not & operator = (const crit_not & ref) { destroy(); copy_from(ref); return *this; };
~crit_not() { destroy(); };
bool evaluate(const nomme & first, const nomme & second) const { return ! x_crit->evaluate(first, second); };
criterium *clone() const { return new (std::nothrow) crit_not(*this); };
protected:
const criterium *x_crit;
private:
void copy_from(const crit_not & ref);
void destroy() { if(x_crit != NULL) { delete x_crit; x_crit = NULL; } };
};
/// realises the *AND* operator
class crit_and : public criterium
{
public:
crit_and() { clear(); };
crit_and(const crit_and & ref) : criterium(ref) { copy_from(ref); };
const crit_and & operator = (const crit_and & ref) { detruit(); copy_from(ref); return *this; };
~crit_and() { detruit(); };
void add_crit(const criterium & ref);
void clear() { detruit(); };
/// this call merges to the current call the arguments of another "crit_and", the given argument is cleared of its arguments.
void gobe(crit_and & to_be_voided);
virtual bool evaluate(const nomme & first, const nomme & second) const;
criterium *clone() const { return new (std::nothrow) crit_and(*this); };
protected:
std::vector<criterium *> operand;
private:
void copy_from(const crit_and & ref);
void detruit();
};
class crit_or : public crit_and
{
public:
crit_or() { clear(); };
bool evaluate(const nomme & first, const nomme & second) const;
criterium *clone() const { return new (std::nothrow) crit_or(*this); };
};
class crit_invert : public crit_not
{
public:
crit_invert(const criterium & crit) : crit_not(crit) {};
bool evaluate(const nomme & first, const nomme & second) const { return x_crit->evaluate(second, first); };
criterium *clone() const { return new (std::nothrow) crit_invert(*this); };
};
/// ask user for EA action
/// \param[in] dialog for user interaction
/// \param[in] full_name full path to the entry do ask decision for
/// \param[in] already_here pointer to the object 'in place'
/// \param[in] dolly pointer to the object 'to be added'
/// \return the action decided by the user. The user may also choose to abort, which will throw an Euser_abort exception
extern over_action_ea crit_ask_user_for_EA_action(user_interaction & dialog, const std::string & full_name, const entree *already_here, const entree *dolly);
/// ask user for Data action
/// \param[in] dialog for user interaction
/// \param[in] full_name full path to the entry do ask decision for
/// \param[in] already_here pointer to the object 'in place'
/// \param[in] dolly pointer to the object 'to be added'
/// \return the action decided by the user. The user may also choose to abort, which will throw an Euser_abort exception
extern over_action_data crit_ask_user_for_data_action(user_interaction & dialog, const std::string & full_name, const entree *already_here, const entree *dolly);
/// show information suited for user comparison and decision for entry in conflict
/// \param[in] dialog for user interaction
/// \param[in] full_name path to the entry of the entry to display information
/// \param[in] already_here pointer to the object 'in place'
/// \param[in] dolly pointer to the object 'to be added'
extern void crit_show_entry_info(user_interaction & dialog, const std::string & full_name, const entree *already_here, const entree *dolly);
/// @}
} // end of namespace
#endif
|