Debian Codegraph @

bin/autofex

raw
1 #!/usr/bin/perl -w
2
3 use Getopt::Std;
4 use File::Basename;
5 use Cwd qw(abs_path getcwd);
6
7 our $prg = $0;
8
9 $prg =~ s:(.*)/::;
10 $0 = "$prg @ARGV";
11 $| = 1;
12
13 $usage = "$prg: fex new created or modified files
14 usage: $prg [-v] [-n] [-a] [-A] [-t NAME] [-k DAYS] [-w SECONDS] [RECIPIENT]
15 options:
16 -v verbose mode
17 -n do not send notification email
18 -a also fex files where attributes have been modified
19 -A fex ALL files, not only new ones
20 -t use transport NAME, must have suffix .tar .tgz or .zip
21 -k keep DAYS on server (default: 1)
22 -w wait SECONDS to collect new files (default: 10)
23 example:
24 $prg -v -a framstag\@rus.uni-stuttgart.de
25 ";
26
27 $ENV{FUA} = 'autofex';
28
29 $opt_h = $opt_v = $opt_n = $opt_a = $opt_A = 0;
30 $opt_k = 1;
31 $opt_w = 10;
32 $opt_t = basename(getcwd()).'.tar';
33 getopts('hvnaAt:w:k:') or die $usage;
34
35 if ($opt_h) {
36 print $usage;
37 exit;
38 }
39
40 $wait = 10;
41 $wait = $1 if $opt_w =~ /^(\d+)$/;
42 $keep = 1;
43 $keep = $1 if $opt_k =~ /^(\d+)$/;
44
45 $lsof = searchpath('lsof') or die "$prg: lsof not installed\n";
46 $inotify = searchpath('inotifywait') or die "$prg: inotifywait not installed\n";
47
48 $inotify .= ' -qmr -e close_write -e moved_to -e create -e modify';
49 $inotify .= ' -e attrib' if $opt_a;
50
51 @fexsend = ('fexsend');
52 push @fexsend,'-k',$keep;
53 push @fexsend,'-n' if $opt_n;
54 $recipient = join(',',@ARGV) || '.';
55
56 if ($opt_t !~ /\.(tar|tgz|zip)$/) {
57 die "$prg: transport name must have suffix .tar .tgz or .zip\n";
58 }
59
60 print "\$ $inotify .\n" if $opt_v;
61 open $inotify,"$inotify . |" or die "$prg: cannot run inotifywait - $!\n";
62
63 $SIG{ALRM} = \&fexsend;
64 %files = ();
65
66 while (<$inotify>) {
67 alarm($wait);
68 print if $opt_v;
69 chomp;
70 if (s/ (CREATE|CLOSE|MOVE|ATTRIB)[_,A-Z]* //) {
71 unless ($files{$_}) {
72 if ((@s = lstat) and $s[3]>1) {
73 my $find = "find . -xdev -type f -inum $s[1]";
74 print "\$ $find\n" if $opt_v;
75 if (open my $find,"$find 2>/dev/null|") {
76 while (<$find>) {
77 chomp;
78 $files{$_}++;
79 }
80 close $find;
81 }
82 } else {
83 $files{$_}++;
84 }
85 }
86 }
87 if ($opt_v and %files) {
88 kill 9,$spid if $spid;
89 local $SIG{CHLD} = 'IGNORE';
90 $spid = fork();
91 if (defined $spid and $spid == 0) {
92 for (my $i = $wait; $i; $i--) {
93 printf "%3d\r",$i;
94 sleep 1;
95 }
96 exit;
97 }
98 }
99 }
100
101
102 sub fexsend {
103 local $_;
104 my @files = keys %files;
105
106 if (@files and @files = grep { -r or -l } sort(@files)) {
107 print "\r";
108 my @fexsend = @fexsend;
109 my $archive = $opt_t;
110 if ($opt_A) {
111 my @wof = ();
112 if ($lsof) {
113 if (open my $lsof,"$lsof +D . 2>/dev/null|") {
114 while (<$lsof>) {
115 if (/^\S+\s+\d+\s+\S+\s+\d+w\w*\s+(\S+\s+){4}(.+)/) {
116 push @wof,$2;
117 }
118 }
119 close $lsof;
120 }
121 }
122 push @fexsend,('-#',join('#',@wof)) if @wof;
123 @files = ('.');
124 } else {
125 my @d = localtime time;
126 my $t = sprintf('%d%02d%02d_%02d%02d%02d',
127 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
128 $archive =~ s/(\.\w+)$/_$t$1/;
129 @files = map { s:^\./::;$_ } @files;
130 }
131 push @fexsend,('-A',$archive,@files,$recipient);
132 print "\$ @fexsend\n" if $opt_v;
133 system(@fexsend);
134 print "\n";
135 }
136 %files = ();
137 };
138
139
140 sub searchpath {
141 my ($prg,$path,@PATH);
142
143 @PATH = split(':',$ENV{PATH});
144 foreach $prg (@_) {
145 foreach $path (@PATH) {
146 return $prg if -x "$path/$prg";
147 }
148 }
149 return '';
150 }