tombstones-expunge: Add a test for deleting links to recycled objects
[Samba.git] / source3 / script / tests / fake_snap.pl
blobd1233f375b000ca06a961855a73e0dfa8b9ced57
1 #!/usr/bin/perl -w
3 use strict;
5 use File::Path qw(rmtree);
6 use POSIX ();
8 sub _create_snapshot
10 my ($base_path) = @_;
11 my $time_str = POSIX::strftime("%Y.%m.%d-%H.%M.%S" , localtime());
12 my $snap_path = $base_path . "/.snapshots/\@GMT-" . $time_str;
13 my $ret;
15 POSIX::mkdir($base_path . "/.snapshots", 0777);
17 # add trailing slash to src path to ensure that only contents is copied
18 $ret = system("rsync", "-a", "--exclude=.snapshots/", "${base_path}/",
19 $snap_path);
20 if ($ret != 0) {
21 print STDERR "rsync failed with $ret\n";
22 } else {
23 print "$snap_path\n";
26 return $ret;
29 sub _delete_snapshot
31 my ($base_path, $snap_path) = @_;
33 # we're doing a recursive delete, so do some sanity checks
34 if ((index($snap_path, $base_path) != 0) || (index($snap_path, ".snapshots") == -1)) {
35 print STDERR "invalid snap_path: $snap_path\n";
36 return -1;
39 rmtree($snap_path, {error => \my $err});
40 if (@$err) {
41 for my $diag (@$err) {
42 my ($file, $message) = %$diag;
43 if ($file eq '') {
44 print STDERR "rmtree error: $message\n";
45 } else {
46 print STDERR "rmtree error $file: $message\n";
49 return -1;
52 return 0;
55 my $ret;
56 my $num_args = $#ARGV + 1;
57 my $cmd = shift;
59 if (($num_args == 2) && ($cmd eq "--check")) {
60 $ret = 0;
61 } elsif (($num_args == 2) && ($cmd eq "--create")) {
62 $ret = _create_snapshot($ARGV[0]);
63 } elsif (($num_args == 3) && ($cmd eq "--delete")) {
64 $ret = _delete_snapshot($ARGV[0], $ARGV[1]);
65 } else {
66 print STDERR "invalid script argument\n";
67 $ret = -1;
70 exit $ret;