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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
|
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2023 Best Practical Solutions, LLC
# <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work 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., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
package RT::Shredder;
use strict;
use warnings;
=head1 NAME
RT::Shredder - Permanently wipeout data from RT
=head1 SYNOPSIS
=head2 CLI
rt-shredder --force --plugin 'Tickets=query,Queue="General" and Status="deleted"'
=head1 DESCRIPTION
RT::Shredder is extension to RT which allows you to permanently wipeout
data from the RT database. Shredder supports the wiping of almost
all RT objects (Tickets, Transactions, Attachments, Users...).
=head2 "Delete" vs "Wipeout"
RT uses the term "delete" to mean "deactivate". To avoid confusion,
RT::Shredder uses the term "Wipeout" to mean "permanently erase" (or
what most people would think of as "delete").
=head2 Why do you want this?
Normally in RT, "deleting" an item simply deactivates it and makes it
invisible from view. This is done to retain full history and
auditability of your tickets. For most RT users this is fine and they
have no need of RT::Shredder.
But in some large and heavily used RT instances the database can get
clogged up with junk, particularly spam. This can slow down searches
and bloat the size of the database. For these users, RT::Shredder
allows them to completely clear the database of this unwanted junk.
An additional use of Shredder is to obliterate sensitive information
(passwords, credit card numbers, ...) which might have made their way
into RT.
=head2 Command line tools (CLI)
L<rt-shredder> is a program which allows you to wipe objects from
command line or with system tasks scheduler (cron, for example).
See also 'rt-shredder --help'.
=head2 Web based interface (WebUI)
Shredder's WebUI integrates into RT's WebUI. You can find it in the
Admin->Tools->Shredder tab. The interface is similar to the
CLI and gives you the same functionality. You can find 'Shredder' link
at the bottom of tickets search results, so you could wipeout tickets
in the way similar to the bulk update.
=head1 DATA STORAGE AND BACKUPS
Shredder allows you to store data you wiped in files as scripts with SQL
commands.
=head3 Restoring from backup
Should you wipeout something you did not intend to the objects can be
restored by using the storage files. These files are a simple set of
SQL commands to re-insert your objects into the RT database.
1) Locate the appropriate shredder SQL dump file. In the WebUI, when
you use shredder, the path to the dump file is displayed. It also
gives the option to download the dump file after each wipeout. Or
it can be found in your C<$ShredderStoragePath>.
2) Load the shredder SQL dump into your RT database. The details will
be different for each database and RT configuration, consult your
database manual and RT config. For example, in MySQL...
mysql -u your_rt_user -p your_rt_database < /path/to/rt/var/data/shredder/dump.sql
That's it.i This will restore everything you'd deleted during a
shredding session when the file had been created.
=head1 CONFIGURATION
=head2 $DependenciesLimit
Shredder stops with an error if the object has more than
C<$DependenciesLimit> dependencies. For example: a ticket has 1000
transactions or a transaction has 1000 attachments. This is protection
from bugs in shredder from wiping out your whole database, but
sometimes when you have big mail loops you may hit it.
Defaults to 1000. To change this (for example, to 10000) add the
following to your F<RT_SiteConfig.pm>:
Set( $DependenciesLimit, 10_000 );>
=head2 $ShredderStoragePath
Directory containing Shredder backup dumps; defaults to
F</opt/rt4/var/data/RT-Shredder> (assuming an /opt/rt4 installation).
To change this (for example, to /some/backup/path) add the following to
your F<RT_SiteConfig.pm>:
Set( $ShredderStoragePath, "/some/backup/path" );>
Be sure to specify an absolute path.
=head1 Database Indexes
We have found that the following indexes significantly speed up
shredding on most databases. However they are intended to be deployed
only for the duration of your shredding, as they may have an adverse
effect on the performance of ordinary RT operations.
CREATE INDEX SHREDDER_CGM1 ON CachedGroupMembers(MemberId, GroupId, Disabled);
CREATE INDEX SHREDDER_CGM2 ON CachedGroupMembers(ImmediateParentId,MemberId);
CREATE INDEX SHREDDER_CGM3 on CachedGroupMembers (Via, Id);
CREATE UNIQUE INDEX SHREDDER_GM1 ON GroupMembers(MemberId, GroupId);
CREATE INDEX SHREDDER_TXN1 ON Transactions(ReferenceType, OldReference);
CREATE INDEX SHREDDER_TXN2 ON Transactions(ReferenceType, NewReference);
CREATE INDEX SHREDDER_TXN3 ON Transactions(Type, OldValue);
CREATE INDEX SHREDDER_TXN4 ON Transactions(Type, NewValue);
CREATE INDEX SHREDDER_ATTACHMENTS1 ON Attachments(Creator);
CREATE INDEX SHREDDER_LINKS1 ON Links(Target);
CREATE INDEX SHREDDER_ACL1 ON ACL(ObjectType, ObjectId);
CREATE INDEX SHREDDER_OCFV1 ON ObjectCustomFieldValues(ObjectType, ObjectId);
=head1 INFORMATION FOR DEVELOPERS
=head2 General API
L<RT::Shredder> is an extension to RT which adds shredder methods to
RT objects and classes. The API is not well documented yet, but you
can find usage examples in L<rt-shredder> and the
F<lib/t/regression/shredder/*.t> test files.
However, here is a small example that do the same action as in CLI
example from L</SYNOPSIS>:
use RT::Shredder;
RT::Shredder::Init( force => 1 );
my $deleted = RT::Tickets->new( RT->SystemUser );
$deleted->{'allow_deleted_search'} = 1;
$deleted->LimitQueue( VALUE => 'general' );
$deleted->LimitStatus( VALUE => 'deleted' );
while( my $t = $deleted->Next ) {
$t->Wipeout;
}
=head2 RT::Shredder class' API
L<RT::Shredder> implements interfaces to objects cache, actions on the
objects in the cache and backups storage.
=cut
use File::Spec ();
BEGIN {
# I can't use 'use lib' here since it breakes tests
# because test suite uses old RT::Shredder setup from
# RT lib path
### after: push @INC, qw(@RT_LIB_PATH@);
use RT::Shredder::Constants;
use RT::Shredder::Exceptions;
}
our @SUPPORTED_OBJECTS = qw(
ACE
Attachment
CachedGroupMember
CustomField
CustomFieldValue
GroupMember
Group
Link
Principal
Queue
Scrip
ScripAction
ScripCondition
Template
ObjectCustomFieldValue
Ticket
Transaction
User
);
=head3 GENERIC
=head4 Init
RT::Shredder::Init( %default_options );
C<RT::Shredder::Init()> should be called before creating an
RT::Shredder object. It iniitalizes RT and loads the RT
configuration.
%default_options are passed to every C<<RT::Shredder->new>> call.
=cut
our %opt = ();
sub Init
{
%opt = @_;
RT::LoadConfig();
RT::Init();
return;
}
=head4 new
my $shredder = RT::Shredder->new(%options);
Construct a new RT::Shredder object.
There currently are no %options.
=cut
sub new
{
my $proto = shift;
my $self = bless( {}, ref $proto || $proto );
return $self->_Init( @_ );
}
sub _Init
{
my $self = shift;
$self->{'opt'} = { %opt, @_ };
$self->{'cache'} = {};
$self->{'resolver'} = {};
$self->{'dump_plugins'} = [];
return $self;
}
=head4 CastObjectsToRecords( Objects => undef )
Cast objects to the C<RT::Record> objects or its ancesstors.
Objects can be passed as SCALAR (format C<< <class>-<id> >>),
ARRAY, C<RT::Record> ancesstors or C<RT::SearchBuilder> ancesstor.
Most methods that takes C<Objects> argument use this method to
cast argument value to list of records.
Returns an array of records.
For example:
my @objs = $shredder->CastObjectsToRecords(
Objects => [ # ARRAY reference
'RT::Attachment-10', # SCALAR or SCALAR reference
$tickets, # RT::Tickets object (isa RT::SearchBuilder)
$user, # RT::User object (isa RT::Record)
],
);
=cut
sub CastObjectsToRecords
{
my $self = shift;
my %args = ( Objects => undef, @_ );
my @res;
my $targets = delete $args{'Objects'};
unless( $targets ) {
RT::Shredder::Exception->throw( "Undefined Objects argument" );
}
if( UNIVERSAL::isa( $targets, 'RT::SearchBuilder' ) ) {
#XXX: try to use ->_DoSearch + ->ItemsArrayRef in feature
# like we do in Record with links, but change only when
# more tests would be available
while( my $tmp = $targets->Next ) { push @res, $tmp };
} elsif ( UNIVERSAL::isa( $targets, 'RT::Record' ) ) {
push @res, $targets;
} elsif ( UNIVERSAL::isa( $targets, 'ARRAY' ) ) {
foreach( @$targets ) {
push @res, $self->CastObjectsToRecords( Objects => $_ );
}
} elsif ( UNIVERSAL::isa( $targets, 'SCALAR' ) || !ref $targets ) {
$targets = $$targets if ref $targets;
my $Organization = RT->Config->Get('Organization');
my ($class, $id);
if ($targets =~ /^([\w:]+)-\Q$Organization\E-(.+)$/) {
($class, $id) = ($1, $2);
} elsif ($targets =~ /^(RT::User)-(.*)$/) {
($class, $id) = ($1, $2);
} elsif ($targets =~ /-.*-/) {
RT::Shredder::Exception->throw( "Can't wipeout remote object $targets" );
} else {
($class, $id) = split /-/, $targets;
}
RT::Shredder::Exception->throw( "Unsupported class $class" )
unless $class =~ /^\w+(::\w+)*$/;
$class = 'RT::'. $class unless $class =~ /^RTx?::/i;
$class->require or die "Failed to load $class: $@";
my $obj = $class->new( RT->SystemUser );
die "Couldn't construct new '$class' object" unless $obj;
$obj->Load( $id );
unless ( $obj->id ) {
$RT::Logger->error( "Couldn't load '$class' object with id '$id'" );
RT::Shredder::Exception::Info->throw( 'CouldntLoadObject' );
}
if ( $id =~ /^\d+$/ ) {
if ( $id ne $obj->Id ) {
die 'Loaded object id ' . $obj->Id . " is different from passed id $id";
}
}
else {
if ( $obj->_Accessible( 'Name', 'read' ) && $id ne $obj->Name ) {
die 'Loaded object name ' . $obj->Name . " is different from passed name $id";
}
}
push @res, $obj;
} else {
RT::Shredder::Exception->throw( "Unsupported type ". ref $targets );
}
return @res;
}
=head3 OBJECTS CACHE
=head4 PutObjects( Objects => undef )
Puts objects into cache.
Returns array of the cache entries.
See C<CastObjectsToRecords> method for supported types of the C<Objects>
argument.
=cut
sub PutObjects
{
my $self = shift;
my %args = ( Objects => undef, @_ );
my @res;
for( $self->CastObjectsToRecords( Objects => delete $args{'Objects'} ) ) {
push @res, $self->PutObject( %args, Object => $_ )
}
return @res;
}
=head4 PutObject( Object => undef )
Puts record object into cache and returns its cache entry.
B<NOTE> that this method support B<only C<RT::Record> object or its ancesstor
objects>, if you want put mutliple objects or objects represented by different
classes then use C<PutObjects> method instead.
=cut
sub PutObject
{
my $self = shift;
my %args = ( Object => undef, @_ );
my $obj = $args{'Object'};
unless( UNIVERSAL::isa( $obj, 'RT::Record' ) ) {
RT::Shredder::Exception->throw( "Unsupported type '". (ref $obj || $obj || '(undef)')."'" );
}
my $str = $obj->UID;
return ($self->{'cache'}->{ $str } ||= {
State => RT::Shredder::Constants::ON_STACK,
Object => $obj
} );
}
=head4 GetObject, GetState, GetRecord( String => ''| Object => '' )
Returns record object from cache, cache entry state or cache entry accordingly.
All three methods takes C<String> (format C<< <class>-<id> >>) or C<Object> argument.
C<String> argument has more priority than C<Object> so if it's not empty then methods
leave C<Object> argument unchecked.
You can read about possible states and their meanings in L<RT::Shredder::Constants> docs.
=cut
sub _ParseRefStrArgs
{
my $self = shift;
my %args = (
String => '',
Object => undef,
@_
);
if( $args{'String'} && $args{'Object'} ) {
require Carp;
Carp::croak( "both String and Object args passed" );
}
return $args{'String'} if $args{'String'};
return $args{'Object'}->UID if UNIVERSAL::can($args{'Object'}, 'UID' );
return '';
}
sub GetObject { return (shift)->GetRecord( @_ )->{'Object'} }
sub GetState { return (shift)->GetRecord( @_ )->{'State'} }
sub GetRecord
{
my $self = shift;
my $str = $self->_ParseRefStrArgs( @_ );
return $self->{'cache'}->{ $str };
}
=head3 Dependencies resolvers
=head4 PutResolver, GetResolvers and ApplyResolvers
TODO: These methods have no documentation.
=cut
sub PutResolver
{
my $self = shift;
my %args = (
BaseClass => '',
TargetClass => '',
Code => undef,
@_,
);
unless( UNIVERSAL::isa( $args{'Code'} => 'CODE' ) ) {
die "Resolver '$args{Code}' is not code reference";
}
my $resolvers = (
(
$self->{'resolver'}->{ $args{'BaseClass'} } ||= {}
)->{ $args{'TargetClass'} || '' } ||= []
);
unshift @$resolvers, $args{'Code'};
return;
}
sub GetResolvers
{
my $self = shift;
my %args = (
BaseClass => '',
TargetClass => '',
@_,
);
my @res;
if( $args{'TargetClass'} && exists $self->{'resolver'}->{ $args{'BaseClass'} }->{ $args{'TargetClass'} } ) {
push @res, @{ $self->{'resolver'}->{ $args{'BaseClass'} }->{ $args{'TargetClass'} || '' } };
}
if( exists $self->{'resolver'}->{ $args{'BaseClass'} }->{ '' } ) {
push @res, @{ $self->{'resolver'}->{ $args{'BaseClass'} }->{''} };
}
return @res;
}
sub ApplyResolvers
{
my $self = shift;
my %args = ( Dependency => undef, @_ );
my $dep = $args{'Dependency'};
my @resolvers = $self->GetResolvers(
BaseClass => $dep->BaseClass,
TargetClass => $dep->TargetClass,
);
unless( @resolvers ) {
RT::Shredder::Exception::Info->throw(
tag => 'NoResolver',
error => "Couldn't find resolver for dependency '". $dep->AsString ."'",
);
}
$_->(
Shredder => $self,
BaseObject => $dep->BaseObject,
TargetObject => $dep->TargetObject,
) foreach @resolvers;
return;
}
sub WipeoutAll
{
my $self = $_[0];
foreach my $cache_val ( values %{ $self->{'cache'} } ) {
next if $cache_val->{'State'} & (RT::Shredder::Constants::WIPED | RT::Shredder::Constants::IN_WIPING);
$self->Wipeout( Object => $cache_val->{'Object'} );
}
return;
}
sub Wipeout
{
my $self = shift;
my $mark;
eval {
die "Couldn't begin transaction" unless $RT::Handle->BeginTransaction;
$mark = $self->PushDumpMark or die "Couldn't get dump mark";
$self->_Wipeout( @_ );
$self->PopDumpMark( Mark => $mark );
die "Couldn't commit transaction" unless $RT::Handle->Commit;
};
if( $@ ) {
my $error = $@;
$RT::Handle->Rollback('force');
$self->RollbackDumpTo( Mark => $mark ) if $mark;
die $error if RT::Shredder::Exception::Info->caught;
die "Couldn't wipeout object: $error";
}
return;
}
sub _Wipeout
{
my $self = shift;
my %args = ( CacheRecord => undef, Object => undef, @_ );
my $record = $args{'CacheRecord'};
$record = $self->PutObject( Object => $args{'Object'} ) unless $record;
return if $record->{'State'} & (RT::Shredder::Constants::WIPED | RT::Shredder::Constants::IN_WIPING);
$record->{'State'} |= RT::Shredder::Constants::IN_WIPING;
my $object = $record->{'Object'};
$self->DumpObject( Object => $object, State => 'before any action' );
unless( $object->BeforeWipeout ) {
RT::Shredder::Exception->throw( "BeforeWipeout check returned error" );
}
my $deps = $object->Dependencies( Shredder => $self );
$deps->List(
WithFlags => RT::Shredder::Constants::DEPENDS_ON | RT::Shredder::Constants::VARIABLE,
Callback => sub { $self->ApplyResolvers( Dependency => $_[0] ) },
);
$self->DumpObject( Object => $object, State => 'after resolvers' );
$deps->List(
WithFlags => RT::Shredder::Constants::DEPENDS_ON,
WithoutFlags => RT::Shredder::Constants::WIPE_AFTER | RT::Shredder::Constants::VARIABLE,
Callback => sub { $self->_Wipeout( Object => $_[0]->TargetObject ) },
);
$self->DumpObject( Object => $object, State => 'after wiping dependencies' );
$object->__Wipeout;
$record->{'State'} |= RT::Shredder::Constants::WIPED; delete $record->{'Object'};
$self->DumpObject( Object => $object, State => 'after wipeout' );
$deps->List(
WithFlags => RT::Shredder::Constants::DEPENDS_ON | RT::Shredder::Constants::WIPE_AFTER,
WithoutFlags => RT::Shredder::Constants::VARIABLE,
Callback => sub { $self->_Wipeout( Object => $_[0]->TargetObject ) },
);
$self->DumpObject( Object => $object, State => 'after late dependencies' );
return;
}
=head3 Data storage and backups
=head4 GetFileName( FileName => '<ISO DATETIME>-XXXX.sql', FromStorage => 1 )
Takes desired C<FileName> and flag C<FromStorage> then translate file name to absolute
path by next rules:
* Default value of the C<FileName> option is C<< <ISO DATETIME>-XXXX.sql >>;
* if C<FileName> has C<XXXX> (exactly four uppercase C<X> letters) then it would be changed with digits from 0000 to 9999 range, with first one free value;
* if C<FileName> has C<%T> then it would be replaced with the current date and time in the C<YYYY-MM-DDTHH:MM:SS> format. Note that using C<%t> may still generate not unique names, using C<XXXX> recomended.
* if C<FromStorage> argument is true (default behaviour) then result path would always be relative to C<StoragePath>;
* if C<FromStorage> argument is false then result would be relative to the current dir unless it's already absolute path.
Returns an absolute path of the file.
Examples:
# file from storage with default name format
my $fname = $shredder->GetFileName;
# file from storage with custom name format
my $fname = $shredder->GetFileName( FileName => 'shredder-XXXX.backup' );
# file with path relative to the current dir
my $fname = $shredder->GetFileName(
FromStorage => 0,
FileName => 'backups/shredder.sql',
);
# file with absolute path
my $fname = $shredder->GetFileName(
FromStorage => 0,
FileName => '/var/backups/shredder-XXXX.sql'
);
=cut
sub GetFileName
{
my $self = shift;
my %args = ( FileName => '', FromStorage => 1, @_ );
# default value
my $file = $args{'FileName'} || '%t-XXXX.sql';
if( $file =~ /\%t/i ) {
require POSIX;
my $date_time = POSIX::strftime( "%Y%m%dT%H%M%S", gmtime );
$file =~ s/\%t/$date_time/gi;
}
# convert to absolute path
if( $args{'FromStorage'} ) {
$file = File::Spec->catfile( $self->StoragePath, $file );
} elsif( !File::Spec->file_name_is_absolute( $file ) ) {
$file = File::Spec->rel2abs( $file );
}
# check mask
if( $file =~ /XXXX[^\/\\]*$/ ) {
my( $tmp, $i ) = ( $file, 0 );
do {
$i++;
$tmp = $file;
$tmp =~ s/XXXX([^\/\\]*)$/sprintf("%04d", $i).$1/e;
} while( -e $tmp && $i < 9999 );
$file = $tmp;
}
if( -f $file ) {
unless( -w _ ) {
die "File '$file' exists, but is read-only";
}
} elsif( !-e _ ) {
unless( File::Spec->file_name_is_absolute( $file ) ) {
$file = File::Spec->rel2abs( $file );
}
# check base dir
my $dir = File::Spec->join( (File::Spec->splitpath( $file ))[0,1] );
unless( -e $dir && -d _) {
die "Base directory '$dir' for file '$file' doesn't exist";
}
unless( -w $dir ) {
die "Base directory '$dir' is not writable";
}
} else {
die "'$file' is not regular file";
}
return $file;
}
=head4 StoragePath
Returns an absolute path to the storage dir. See
L</$ShredderStoragePath>.
See also description of the L</GetFileName> method.
=cut
sub StoragePath
{
return scalar( RT->Config->Get('ShredderStoragePath') )
|| File::Spec->catdir( $RT::VarPath, qw(data RT-Shredder) );
}
my %active_dump_state = ();
sub AddDumpPlugin {
my $self = shift;
my %args = ( Object => undef, Name => 'SQLDump', Arguments => undef, @_ );
my $plugin = $args{'Object'};
unless ( $plugin ) {
require RT::Shredder::Plugin;
$plugin = RT::Shredder::Plugin->new;
my( $status, $msg ) = $plugin->LoadByName( $args{'Name'} );
die "Couldn't load dump plugin: $msg\n" unless $status;
}
die "Plugin is not of correct type" unless lc $plugin->Type eq 'dump';
if ( my $pargs = $args{'Arguments'} ) {
my ($status, $msg) = $plugin->TestArgs( %$pargs );
die "Couldn't set plugin args: $msg\n" unless $status;
}
my @applies_to = $plugin->AppliesToStates;
die "Plugin doesn't apply to any state" unless @applies_to;
$active_dump_state{ lc $_ } = 1 foreach @applies_to;
push @{ $self->{'dump_plugins'} }, $plugin;
return $plugin;
}
sub DumpObject {
my $self = shift;
my %args = (Object => undef, State => undef, @_);
die "No state passed" unless $args{'State'};
return unless $active_dump_state{ lc $args{'State'} };
foreach (@{ $self->{'dump_plugins'} }) {
next unless grep lc $args{'State'} eq lc $_, $_->AppliesToStates;
my ($state, $msg) = $_->Run( %args );
die "Couldn't run plugin: $msg" unless $state;
}
return;
}
{ my $mark = 1; # XXX: integer overflows?
sub PushDumpMark {
my $self = shift;
$mark++;
foreach (@{ $self->{'dump_plugins'} }) {
my ($state, $msg) = $_->PushMark( Mark => $mark );
die "Couldn't push mark: $msg" unless $state;
}
return $mark;
}
sub PopDumpMark {
my $self = shift;
foreach (@{ $self->{'dump_plugins'} }) {
my ($state, $msg) = $_->PopMark( @_ );
die "Couldn't pop mark: $msg" unless $state;
}
return;
}
sub RollbackDumpTo {
my $self = shift;
foreach (@{ $self->{'dump_plugins'} }) {
my ($state, $msg) = $_->RollbackTo( @_ );
die "Couldn't rollback to mark: $msg" unless $state;
}
return;
}
}
1;
__END__
=head1 SEE ALSO
L<rt-shredder>, L<rt-validator>
=cut
|