-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathubhmv
More file actions
executable file
·163 lines (129 loc) · 3.66 KB
/
ubhmv
File metadata and controls
executable file
·163 lines (129 loc) · 3.66 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
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
#!/usr/bin/perl -w
use strict;
# $Id: ubhmv,v 1.1.1.1 2003/11/12 20:51:59 jepace Exp $
# ubhmv
#
# Usage: ubhmv [-v] [-f | -i] file [file...] destination
#
# This program moves the specified file(s) to the destination and
# updates the ubhpal database to reflect their new location(s).
use File::Copy;
use File::Basename;
use Getopt::Std;
use vars qw/ $opt_f $opt_i $opt_v /;
# Source in ubh library
my $lib = $ENV{HOME} . "/usr/bin/ubhlib.pm";
require "$lib";
my $MD5 = "/sbin/md5 -q";
# Files we will operate on
# XXX: These should be in an rc file
my $rcfile = $ENV{HOME} . "/.ubhrc";
my $dbfile = $ENV{HOME} . "/.paldb";
my @srcs; # Args passed from user
my $src; # Source file (as passed from user)
my $palsrc; # Path to src relative to $startdir
my $srcfile; # Just the name of the source file
my $dest; # Destination from the user
my $paldest; # Path to dest relative to $startdir
my $paldestfile;
my $sum; # MD5 sum of current file
my $char; # User's interactive input
getopts("fiv");
die "Usage: ubhmv [-v] [-f|-i] file [...] destination\n" unless $ARGV[1];
# Last arg is destination
$dest = splice (@ARGV, -1);
@srcs = @ARGV;
die "Usage: ubhmv [-v] [-f|-i] file [...] destination\n" unless @srcs;
die "Usage: ubhmv [-v] [-f|-i] file [...] destination\n" unless $dest;
die "Only one of -f and -i can be specified\n" if ($opt_f && $opt_i);
if (defined $srcs[1] && ! -d $dest)
{
print "Destination must be a directory with multiple sources.\n";
exit 1;
}
# Parse rc file to get the DATADIR.
my %rc = &parse_rc ($rcfile);
my $start_dir = $rc{"DATADIR"};
die "Must set DATADIR in ~/.ubhrc\n" unless $start_dir;
# Learn more about the destination
unless ($paldest = ubhname ($dest, $start_dir))
{
print "Cannot ubhname '$dest'\n";
exit 1;
}
# Load the existing database
my %db = &open_database ($dbfile);
my $hit = 0;
my $number = 0;
foreach $src (@srcs)
{
$number++;
# Get other names for the files
$srcfile = basename ($src, "");
unless ($palsrc = ubhname ($src, $start_dir))
{
print "Cannot ubhname '$src'\n";
next;
}
$paldestfile = $paldest;
$paldestfile = "$paldest/$srcfile" if ( -d $dest);
if ($opt_v)
{
print "Src: '$src' PalSrc: '$palsrc'\n";
print "Dst: '$dest' PalDstFile: '$paldestfile'\n";
}
unless (-e $src)
{
print "$src: No such file or directory\n";
next;
}
# Get the key for this file and ensure that it exists in the
# database
$sum = `$MD5 "$src"`;
chomp $sum;
unless ($sum)
{
print "ERROR: $src: No sum?!\n";
next;
}
unless (exists $db{$sum})
{
print "$src: Unknown file\n";
next;
}
# XXX: Should we check to see if the database has the same path as
# what we thing it is?
unless ($db{$sum} eq $palsrc)
{
print "$src: Database sez '$db{$sum}'\n";
next;
}
# Get user's confirmation. Then we are ready to change the
# database and move the file
if ($opt_i)
{
print "move '$src' to '$dest'? ";
$char = getc;
getc if ($char eq '\n');
next unless ($char eq 'y');
}
elsif (-e $dest && ! -d $dest )
{
print "'$dest' exists. Overwrite? ";
$char = getc;
getc if ($char eq '\n');
next unless ($char eq 'y');
}
# Do the move
# FIXME: This doesn't work?
$db{$sum} = $paldestfile;
unless (move($src, $dest))
{
print "ubhmv '$src' to '$dest' failed: $!\n";
next;
}
$hit++;
print "ubhmv: File '$src' moved to '$dest'\n" if ($opt_v);
}
&close_database (%db);
exit ($number - $hit);