-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathundear.pl
More file actions
125 lines (105 loc) · 2.78 KB
/
undear.pl
File metadata and controls
125 lines (105 loc) · 2.78 KB
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
use strict;
use feature 'switch';
use feature 'say';
use File::Basename;
use Cwd;
#removes warning for experimental switch function on later versions
no if ($] >= 5.018), 'warnings' => 'experimental';
#decompresses file based on extension
sub decompress {
my ($file) = @_;
my ($name,$path,$extension) = fileparse($file, qr"\..[^.]*$");
my @arguments;
if ($extension eq '.gz') {
push @arguments, ("gunzip", "-c", $file);
$file = substr($file, 0, -3);
} elsif ($extension eq '.Z') {
push @arguments, ("uncompress", "-k", $file);
$file = substr($file, 0, -2);
} elsif ($extension eq '.bz2') {
push @arguments, ("bzip2", "-d", "-k", $file);
$file = substr($file, 0, -4);
}
system(@arguments);
return $file;
}
#extracts file with duplicates deleted
sub deleteDups {
my ($file) = @_;
my @arguments =("tar", "-xvf", $file);
system(@arguments);
}
#creates a soft link to duplicate files
sub softLink {
my ($file) = @_;
say $file;
my @arguments = ("tar", "-xvf", $file);
system(@arguments);
@arguments = ();
my @duplicates = getDuplicates();
my $pair;
foreach $pair (@duplicates) {
push @arguments, ("ln", "-s", cwd() . "/" . $pair->[0], $pair->[1], "&&");
}
pop @arguments;
my $command = join(" ", @arguments), "\n";
system($command);
}
#copies duplicate files as they were originally
sub copyDups {
my ($file) = @_;
my @arguments = ("tar", "-xvf", $file);
system(@arguments);
@arguments = ();
my @duplicates = getDuplicates();
my $pair;
foreach $pair (@duplicates) {
push @arguments, ("cp", $pair->[0], $pair->[1], "&&");
}
pop @arguments;
system(@arguments);
}
#fetches duplicate list from duplicates.txt
sub getDuplicates {
open my $dupFile, "<", "duplicates.txt";
my @pair;
my @duplicates;
my $count = 0;
while (my $line = <$dupFile>) {
@pair = split ' ', $line;
push @{$duplicates[$count]}, @pair;
$count++;
}
close ($dupFile);
return @duplicates;
}
my $argCount = $#ARGV + 1; #number of arguments provided
if ($argCount > 0) {
my $method = 'default';
#checks for option and removes from arguments once retrieved
if (substr($ARGV[0], 0, 1) eq '-') {
$method = substr($ARGV[0], 1, 1);
splice @ARGV, 0, 1;
$argCount--;
}
#ensures correct amount of arguments
if ($argCount < 1) {
die "Not enough arguments provided.\n Command should be in format \"undear [options] file\"";
} elsif ($argCount > 1) {
die "Too many arguments provided.\n Command should be in format \"undear [options] file\"";
}
my $file = $ARGV[0];
$file = decompress($file);
#determines how duplicates should be treated
given($method){
when('d') {deleteDups($file);}
when('l') {softLink($file);}
when('c') {copyDups($file);}
default {die "No option specified.";}
}
system("rm duplicates.txt");
system("rm test.tar");
}
else {
die "No arguments provided.\n";
}