Debian Codegraph @

bin/fexpush

raw
1 #!/usr/bin/perl -w
2
3 $help = <<'';
4 # F*EX push&pull archive sharing client with versioning.
5 #
6 # Needs fexsend, fexget, tar, zip, unzip and optional vv.
7 #
8 # Author: Ulli Horlacher <[email protected]>
9 #
10 # Source: http://fex.belwue.de/download/fex.pl
11 #
12 # Perl Artistic Licence
13 #
14 # fexpush puts files and directories into tar or zip archives and saves them
15 # on a F*EX server share. Pipe data (STDIN/STDOUT) can also stored.
16 #
17 # A share is a storage object on a F*EX server and comprises owner and user(s)
18 # and contains archives.
19 # You can obtain a share-URL from a share owner. For more information see
20 # http://fex.rus.uni-stuttgart.de/sharing.html
21 #
22 # A version date string is appended to the archive name.
23 # The version date is determinded by the newest file in the archive.
24 # The version date is always in UTC (gmtime) to allow international
25 # collaboration.
26 #
27 # With fexpull you can list your shares and archives and download them.
28 # The newest archive version will be autoselected when you omit the
29 # version string. When you name it, then exactly this archive version will
30 # be downloaded and a directory with this version name will be created.
31 # So you can have several versions in your local directory at the same time
32 # without interference.
33 #
34 # The archive will be written to STDOUT when you add argument "-"
35 #
36 # If you have owner or manage access right, you also can delete archives.
37 #
38 # fexpush ignores files or directories with a leading . unless you use option -.
39 # fexpush always ignores files or directories named .del and .snapshot*
40 #
41 # fexpull does not overwrite files which are newer.
42 #
43 # STDIN/STDOUT archives (pipe data) are prefixed with "_IO_"
44 #
45 # fexpush calls fexsend and fexpull calls fexget.
46 # You need to have them installed, too. Use the installer
47 # http://fex.belwue.de/download/fex.pl
48 #
49 # To use the special personal share "_" you need a F*EX account.
50 # Initialize it with "fexsend -I", for the very first time.
51 # This share is used as the default for push&pull.
52 #
53 # You can call fexpush as fexstore (via symbolic link) to use the special archive
54 # "_:_" as your extended internet clipboard.
55 #
56 # If you are a registered F*EX user, then use http://YOURFEXSERVER/fas
57 # to create or manage your share users and archives.
58 #
59 # Additional options:
60 #
61 # -v # verbose output
62 # -n # no real delete action, just show what would happen
63 # -i ID # use F*EX ID
64 #
65 # fexpush and fexpull respect the environment variable FEXID (see: fexsend -I)
66 # fexstore respects the environment variable FEXXX (see: xx -I)
67 #
68 # See also:
69 # http://fex.rus.uni-stuttgart.de/sharing.html
70 # http://fex.rus.uni-stuttgart.de/usecases/fexpush.html
71
72 # perl -MDebug::Trace=sharedata,queryshare,listshares ./fexpush -l blubb
73
74 use 5.010;
75 use Config;
76 use Cwd qw(abs_path getcwd);
77 use Digest::MD5 qw(md5_hex);
78 use Getopt::Std;
79 use File::Basename;
80 use Term::ReadLine;
81 use Sys::Hostname;
82
83 $| = 1;
84 $prg = $0;
85 $prg =~ s:(.*)/::;
86 $ENV{PATH} = $1.':'.$ENV{PATH};
87
88 $version = 20200429;
89
90 unless ($version) {
91 my @d = gmtime((stat $0)[9]);
92 $version = sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3])
93 }
94
95 $ENV{FUA} = "fexpush-$version";
96
97 # chomp($hostname = `hostname -f`);
98 $host = $hostname = hostname();
99 $host =~ s/\..*//;
100 @pwu = getpwuid($<) or die "$prg: user unknown - $!\n";
101 $hostuser = $pwu[0];
102 $HOME = $pwu[7];
103 $sharedir = "$HOME/.fex/share";
104 $fid = "$HOME/.fex/id";
105 $MB = 2**20;
106 $fexid = $ENV{FEXID} || -s $fid;
107
108 if ($prg =~ /push/) {
109 if ($fexid) {
110 $usage = strip("
111 $prg ($version): push files or named STDOUT to F*EX share archive
112 help: $prg -H
113 see also: fexpull, fexsend
114
115 usage: $prg [-l] [-L] [SHARE]
116 usage: $prg [options] [SHARE:][ARCHIVE] FILE_OR_DIRECTORY...
117 usage: $prg SHARE-URL
118 usage: $prg -U SHARE:USER:ACCESS
119 usage: ... | $prg [options] [[SHARE:]NAME]
120
121 options:
122 -l list known shares or archives in SHARE
123 -L list known shares in long format
124 -C 'COMMENT' add COMMENT (with %u=user %h=host %H=host.domain)
125 -# 'EXLIST' exclude file pattern list (use # as list separator)
126 -x exclude directories on different file systems (tar archive only)
127 -. include .files
128 -m MAX limit upload to MAX kB/s
129 # -F use newest file date as archive version (default)
130 -T use current time instead of file date as archive version
131 -s keep symbolic links (default: dereference), arguments only
132 -o overwrite (delete all old versions)
133 -U add USER to SHARE with ACCESS right
134
135 arguments:
136 SHARE use SHARE on server (default is share _)
137 ARCHIVE name of ARCHIVE (may end with .tgz .tar .zip)
138 VERSION select VERSION of ARCHIVE
139 SHARE-URL save share configuration
140 NAME NAME of special STDOUT archive
141 ACCESS user ACCESS right read, write, add or manage
142
143 examples:
144 $prg /usr/local/bin/usbmount
145 $prg tux:images.tar *.jpg
146 lshw | $prg -C 'for host %h' hardware-list
147 ");
148 } else {
149 $usage = strip("
150 $prg ($version): push files or named STDOUT to F*EX share archive
151 help: $prg -H
152 see also: fexpull, fexsend
153
154 usage: $prg [-l] [-L] [SHARE]
155 usage: $prg [options] SHARE:ARCHIVE FILE_OR_DIRECTORY...
156 usage: $prg SHARE-URL
157 usage: ... | $prg [options] SHARE:NAME
158
159 options:
160 -l list known shares or archives in SHARE
161 -L list known shares in long format
162 -C 'COMMENT' add COMMENT (with %u=user %h=host %H=host.domain)
163 -# 'EXLIST' exclude file pattern list (use # as list separator)
164 -. include .files
165 -m MAX limit upload to MAX kB/s
166 # -F use newest file date as archive version (default)
167 -T use current time instead of file date as archive version
168 -s keep symbolic links (default: dereference), arguments only
169
170 arguments:
171 SHARE use SHARE on server
172 ARCHIVE name of ARCHIVE (may end with .tgz .tar .zip)
173 SHARE-URL save share configuration
174 NAME NAME of special STDOUT archive
175
176 examples:
177 $prg -l
178 $prg -l tux
179 $prg tux:images.tar *.jpg
180 lshw | $prg -C 'for host %h' lab:hardware-list
181 ");
182 }
183 $opt_h = $opt_H = $opt_o = $opt_v = $opt_l = $opt_L = $opt_s = $opt_u = 0;
184 $opt_T = $opt_F = $opt_U = $opt_c = $opt_n = $opt_x = 0;
185 ${'opt_.'} = 0;
186 $opt_i = $opt_m = $opt_C = '';
187 ${'opt_#'} = '';
188 getopts('hHlLovsucnxTFU.i:k:C:m:#:') or die $usage;
189 $opt_l ||= $opt_L;
190
191 if ($opt_T and $opt_F) {
192 die "$prg: you cannot mix options -T and -F\n";
193 }
194 if (-t STDIN and not
195 ($opt_l||$opt_L||$opt_h||$opt_H||$opt_U||$opt_u||$opt_o or @ARGV)) {
196 die $usage;
197 }
198
199 if ($opt_x) {
200 $ENV{TAR_OPTIONS} = '--one-file-system';
201 }
202 } elsif ($prg =~ /pull/) {
203 if ($fexid) {
204 $usage = strip("
205 $prg ($version): pull files or named STDIN from F*EX share archive
206 help: $prg -H
207 see also: fexpush, fexget
208
209 usage: $prg [-u] [-l]
210 usage: $prg -l SHARE[:ARCHIVE[:VERSION]]
211 usage: $prg -L [SHARE]
212 usage: $prg -U SHARE
213 usage: $prg [-m MAX] [SHARE:]NUMBER [FILE...]
214 usage: $prg [-m MAX] [SHARE:][DIRECTORY/]ARCHIVE[:VERSION] [FILE...]
215 usage: $prg [-m MAX] [SHARE:]ARCHIVE[:VERSION] - | ...
216 usage: $prg [-m MAX] [[SHARE:]_IO_NAME] [-] [| ...]
217 usage: $prg [-d] NUMBER
218 usage: $prg -d [SHARE:]ARCHIVE[:VERSION]
219 usage: $prg -o SHARE:ARCHIVE
220 usage: $prg -c NUMBER [SHARE:]ARCHIVE
221 usage: $prg SHARE-URL
222
223 options:
224 -l list available shares, archives or archive content
225 -L list available shares or archives in long format
226 -u list URLs from share _
227 -U list SHARE users URLs
228 -m limit download to MAX kB/s
229 -d delete ARCHIVE (NUMBER)
230 -o delete all versions but one (keep latest)
231 -c copy archive version to other ARCHIVE in SHARE
232
233 arguments:
234 SHARE select SHARE on server (default is share _)
235 ARCHIVE select ARCHIVE from server
236 VERSION select VERSION of ARCHIVE
237 NUMBER select archive version by NUMBER
238 DIRECTORY extract ARCHIVE in DIRECTORY (default is ./)
239 FILE extract only FILE from ARCHIVE
240 - uncompress ARCHIVE and write to STDOUT/pipe
241 _IO_NAME name of special STDOUT archive
242 SHARE-URL save share configuration
243
244 examples:
245 $prg # list archives in share _
246 $prg -l # list known shares
247 $prg -l lab # list archives in share lab
248 $prg -l 3 # show content of archive number 3
249 $prg bunnies # pull latest bunnies archive from share _
250 $prg lab:data # pull latest data archive from share lab
251 $prg 3 # pull archive number 3
252 $prg | less # show STDOUT _IO_ from share _
253 ");
254 } else {
255 $usage = strip("
256 $prg ($version): pull files from F*EX share archive
257 help: $prg -H
258 see also: fexpush, fexget
259
260 usage: $prg -l [SHARE[:ARCHIVE[:VERSION]]]
261 usage: $prg -L [SHARE]
262 usage: $prg [-m MAX] [SHARE:]NUMBER [FILE...]
263 usage: $prg [-m MAX] SHARE:ARCHIVE[:VERSION] [FILE...]
264 usage: $prg [-m MAX] SHARE:ARCHIVE[:VERSION] - | ...
265 usage: $prg [-m MAX] SHARE:_IO_NAME [-] [| ...]
266 usage: $prg SHARE-URL
267
268 options:
269 -l list available shares, archives or archive content
270 -L list available shares or archives in long format
271 -m MAX limit download to MAX kB/s
272
273 arguments:
274 SHARE select SHARE on server
275 ARCHIVE select ARCHIVE from server
276 VERSION select VERSION of ARCHIVE
277 NUMBER select archive version by NUMBER
278 FILE extract only FILE from ARCHIVE
279 - write ARCHIVE to pipe
280 _IO_NAME name of special STDOUT archive
281 SHARE-URL save share configuration
282
283 examples:
284 $prg -l # list known shares
285 $prg -l lab # list share lab
286 $prg -l 3 # show content of archive number 3
287 $prg lab:data # pull latest data archive from share lab
288 $prg 3 # pull archive number 3
289 $prg lab:_IO_blubb | less # show STDOUT _IO_blubb from share lab
290 ");
291 }
292
293 $opt_h = $opt_H = $opt_l = $opt_L = $opt_d = $opt_o = $opt_v = $opt_u = 0;
294 $opt_c = $opt_n = 0;
295 $opt_i = $opt_m = $opt_U = '';
296 getopts('hHlLudovcni:m:U:') or die $usage;
297 # $opt_l = -1 unless $opt_l||$opt_L||$opt_H||$opt_S or @ARGV or not -t STDOUT;
298 $opt_l ||= $opt_L;
299
300 die $usage if $opt_d and ($opt_l||$opt_u||$opt_U);
301 die $usage if not (@ARGV or $fexid or $opt_U||$opt_o||$opt_l);
302 } elsif ($prg eq 'fexstore') {
303 $fexstore = $prg;
304 $usage = strip("
305 $prg ($version): store/retrieve data to/from your F*EX default share archive
306 see also: fexpush, fexpull, xxx
307
308 usage: $prg [-l] [-u] [-o]
309 usage: $prg FILE(s)
310 usage: $prg NUMBER
311 usage: $prg -d NUMBER(s)
312 usage: $prg -d 'REGEXP'
313 usage: $prg -D [DAYS]
314 usage: ... | $prg [-C 'COMMENT']
315 usage: $prg [NUMBER] | ...
316 options:
317 -l list available archives
318 -u show download URLs
319 -d delete archive(s)
320 -D delete all archives older than DAYS
321 -o delete all archives but one (keep latest)
322 -C add COMMENT
323 arguments:
324 NUMBER select archive by NUMBER
325 REGEXP select archive with REGular EXPression uploader or arguments
326 FILE store FILE(s) (and also directories)
327 examples:
328 $prg *png # store *png files
329 lshw | $prg # store hardware list
330 $prg -l # list archives
331 $prg 3 # retrieve archive number 3
332 $prg - # retrieve last archive
333 $prg -d test # delete all \"test\" archives
334 $prg -D 2 # delete all archives older than 2 days
335 ");
336 $opt_h = $opt_H = $opt_v = $opt_l = $opt_u = $opt_o = $opt_D = $opt_d = 0;
337 $opt_c = $opt_n = 0;
338 $opt_m = $opt_C = '';
339 getopts('hHvluoDdcnm:C:') or die $usage;
340 } else {
341 die "$prg must be named fexpush or fexpull\n";
342 }
343
344 $opt_v ||= $opt_n;
345
346 if ($opt_h) {
347 print $usage;
348 exit;
349 }
350
351 if ($opt_H) {
352 $help =~ s/^# ?//mg;
353 print $help;
354 exit;
355 }
356
357 $m_opt = $opt_m ? "-m $opt_m" : '';
358 $i_opt = $opt_i ? "-i $opt_i" : '';
359
360 $vp = '\d{8}_\d{6}'; # version pattern
361 $ap = '[\w_.+-]+'; # archive pattern
362
363 $fexid = $ENV{FEXID} || $opt_i || -s $fid;
364
365 if ($fexstore) {
366 &fexstore;
367 exit;
368 }
369
370 mkdirp($sharedir) if -s $fid;
371
372 if ($opt_c) {
373 if ($fexid) {
374 getfid();
375 } else {
376 die "$prg: option -c only possible with FEXID, run: fexsend -I\n";
377 }
378 my $share = readlink "$sharedir/.list" or die "usage: fexpull -l SHARE\n";
379 $share =~ s/.list$//;
380 my ($number,$nshare,$narchive);
381 if ("@ARGV" =~ /^(\d+) (\S+):(\S+)$/) {
382 $number = $1;
383 $nshare = $2;
384 $narchive = $3;
385 } elsif ("@ARGV" =~ /^(\d+) (\S+)$/) {
386 $number = $1;
387 $nshare = '_';
388 $narchive = $2;
389 } else {
390 die "usage: $prg NUMBER [SHARE:]ARCHIVE\n";
391 }
392 my $list = "$sharedir/$share.list";
393 open $list,$list or die "$prg: cannot open $list - $!\n";
394 my $n = 0;
395 while (<$list>) {
396 if (/^(\S+) ($vp\.\w+)/) {
397 my $archive = $1;
398 my $avt = $2;
399 $n++;
400 if ($number == $n) {
401 $ENV{FEXID} = "$url $owner $id";
402 vsystem("fexsend -~ ".
403 "COPYARCHIVE:$share:$archive:$avt:$nshare:$narchive $owner");
404 exit $?;
405 }
406 }
407 }
408 die "$prg: $number not found in $list\n";
409 }
410
411 if ("@ARGV" =~ /^https?:\/\//) {
412 if (scalar(@ARGV) > 1 ) {
413 die "usage: $prg SHARE-URL\n";
414 }
415 saveconfig(@ARGV);
416 exit;
417 }
418
419 # @ARGV = map { s:/+$::;$_ } @ARGV;
420
421 if ($opt_l and not @ARGV) {
422 getfid() if $fexid;
423 &listshares;
424 unless ($opt_L) {
425 $prg =~ s/push/pull/;
426 warn "\nusage: $prg -l SHARE\n";
427 }
428 exit;
429 }
430
431 if ($opt_u and not $opt_L) {
432 die "$prg: no \$FEXID\n" unless $fexid;
433 my $a = "@ARGV";
434 if ($a =~ /^\d*$/) {
435 queryshare('_') unless $a;
436 } else {
437 die "usage: $prg -u\n";
438 }
439 my $list = "$sharedir/_.list";
440 open $list,$list or die "$prg: cannot open $list - $!\n";
441 my $n = 0;
442 while (<$list>) {
443 s/(\.gz)$/$1 /;
444 if (/"(.*)" (http.+)$/) {
445 $n++;
446 if ($a and $a == $n) {
447 print "$2\n";
448 } elsif (not $a) {
449 if (length($1)) { print qq'$2 "$1"\n' }
450 else { print qq'$2\n' }
451 }
452 }
453 }
454 exit;
455 }
456
457 if ($opt_o and scalar(@ARGV) == 0) {
458 die "$prg -o SHARE:ARCHIVE\n"
459 }
460
461 # push or pull ?
462
463 if ($prg =~ /push/) {
464 my $status = 0;
465 my $sa = '';
466
467 if ($opt_d) {
468 die "$prg: not implemented";
469 }
470
471 if ($opt_l) {
472 die "usage: $prg $i_opt -l SHARE\n" if scalar(@ARGV) != 1;
473 die "usage: fexpull $i_opt -l @ARGV\n" if "@ARGV" =~ /:/;
474 my $share = shift;
475 ($access,@list) = queryshare($share,'archives');
476 if ($access =~ /none|read/ or $access eq 'write' and not @list) {
477 die "$prg: no writable archives in share $share\n";
478 }
479 print "writable archives in share $share:\n";
480 print @list;
481 print "(any)\n" if $access =~ /manage|owner/;
482 exit;
483 }
484
485 if ($opt_U) {
486 if ("@ARGV" =~ /^([\w-]+):(\S+\@[\w.-]+):([a-z]+)$/) {
487 $share = $1;
488 $suser = $2;
489 $access = $3;
490 } else {
491 die "usage: $prg -U SHARE:USER:ACCESS\n";
492 }
493 ($sshare,$server,$owner,$user,$pkey) = sharedata($share);
494 if ($owner eq $user and not $pkey) {
495 getfid();
496 } else {
497 $ENV{FEXID} = "$server $user PKEY:$pkey";
498 }
499 vsystem("fexsend -~ SHAREUSER:$suser:$access $owner:share=$sshare");
500 exit $?;
501 }
502
503 unless (@ARGV) {
504 if ($fexid) {
505 @ARGV = ('_:_');
506 } else {
507 die $usage;
508 }
509 }
510 my $a = $ARGV[0];
511 if (-t STDIN or not -t STDOUT and -e $ARGV[-1]) {
512 if ($a =~ /:$/) {
513 if (scalar(@ARGV) == 2) {
514 $a .= $ARGV[1];
515 } else {
516 die "usage: $prg SHARE:ARCHIVE FILE_OR_DIRECTORY...\n";
517 }
518 }
519 if ($a eq "@ARGV") {
520 die $usage unless $fexid;
521 if (-e $a) {
522 $share = '_';
523 $archive = basename($a);
524 } elsif ($a =~ /(.+):(.+)/) {
525 $share = $1;
526 my $file = $2;
527 $archive = basename($file);
528 @ARGV = ($file);
529 unless (-e $file) {
530 die "$prg: $file - no such file or directory\n";
531 }
532 } else {
533 die "$prg: $a - no such file or directory\n";
534 }
535 if ($archive =~ /\.(tar|tgz|zip|7z)$/i) {
536 $container = $1;
537 } elsif ($archive =~ /\.(gz|bz2?|rar|avi|mp\d|m4v)$/i) {
538 $container = 'tar';
539 } else {
540 $container = 'tgz';
541 }
542 } else {
543 if ($a =~ /(.+):(.+)/) {
544 $share = $1;
545 $archive = $2;
546 shift @ARGV;
547 } else {
548 $share = '_';
549 if (-e $a) {
550 $archive = inputline("archive name: ") or exit;
551 $archive =~ s/[^\w.+-]/_/g;
552 } else {
553 $archive = $a;
554 shift @ARGV;
555 }
556 }
557 if ($archive =~ s/\.(tar|tgz|zip|7z)$//) {
558 $container = $1;
559 } else {
560 $container = 'tgz';
561 }
562 }
563 if ($archive =~ /^_IO_/) {
564 die "$prg: archive name is reserved for STDIN/STDOUT\n";
565 }
566 } else {
567 # from pipe
568 if (scalar(@ARGV) > 1) {
569 if ($fexid) {
570 die "usage: ... | $prg [SHARE:][NAME]\n";
571 } else {
572 die "usage: ... | $prg SHARE:NAME\n";
573 }
574 }
575 if ($a =~ /(.+):(.+)/) {
576 $share = $1;
577 $archive = $2;
578 } elsif ($a =~ /(.+):$/) {
579 $share = $1;
580 $archive = '_';
581 } else {
582 $share = '_';
583 $archive = $a || '_';
584 }
585 $container = 'gz';
586 }
587
588 $archive =~ s:.*/::;
589 $archive =~ s/[^\w_.+-]/_/g;
590 $archive =~ s/^\./_/;
591
592 if ($share eq '_') {
593 getfid();
594 die "$prg: no owner-user defined\n" unless $owner;
595 } else {
596 die "$prg: no share?!\n" unless $share;
597 ($sshare,$server,$owner,$user,$pkey) = sharedata($share);
598 if ($owner eq $user and not $pkey) {
599 getfid();
600 } else {
601 $ENV{FEXID} = "$server $user PKEY:$pkey";
602 }
603 }
604 $to = "$owner:share=$share";
605
606 if ($opt_C) {
607 $opt_C =~ s/\%u/$hostuser/g;
608 $opt_C =~ s/\%h/$host/g;
609 $opt_C =~ s/\%H/$hostname/g;
610 }
611
612 if (-t STDIN or not -t STDOUT and -e $ARGV[-1]) {
613
614 map { -e or die "$prg: $_ does not exist\n" } @ARGV;
615
616 if (${'opt_.'}) {
617 $xsp = ${'opt_#'}.'#.fex/tmp#.del#.snapshot*';
618 } else {
619 $xsp = ${'opt_#'}.'#.*';
620 }
621
622 $xsp =~ s/^#//;
623 # exclude list wildcard --> regular expression
624 $xrx = $xsp;
625 $xrx =~ s/\./\\./g;
626 $xrx =~ s/(?<!\\)\?/./g;
627 $xrx =~ s/(?<!\\)\*/.*/g;
628 $xrx =~ s/([()+{}\|])/\\$1/g;
629 $xrx =~ s/#/|/g;
630
631 if ($opt_s) {
632 @__ = @ARGV;
633 } else {
634 map { warn "$prg: dereferencing symlink $_\n" if -l } @ARGV;
635 @__ = uniq(map { abs_path($_) } @ARGV);
636 my $cwd = abs_path(getcwd);
637 map { s:^\Q$cwd/:: } @__;
638 }
639
640 if ($opt_T) {
641 $mtime = time();
642 } else {
643 $mtime = 0;
644 nmtime(@__);
645 }
646
647 $dir = '.';
648 if (scalar(@__) > 1) {
649 $dir = dirname($__[0]);
650 foreach (@__) {
651 if (dirname(abs_path($_)) ne $dir) {
652 $dir = '.';
653 last;
654 }
655 }
656 }
657
658 if ($dir ne '.') {
659 chdir $dir or die "$prg: cannot chdir $dir - $!\n";
660 @__ = map { basename($_) } @__;
661 }
662
663 my $version = versiondate($mtime);
664 my @qargv = map { shellquote($_) } @__;
665 $opt_C = '-C '.shellquote($opt_C) if $opt_C;
666
667
668 my $ao = ${'opt_.'} ? '-A' : '-a';
669 grep { /^\..|\/\./ and $ao = '-A' } @ARGV;
670
671 my $xo = '';
672 $xo = '-# '.shellquote(${'opt_#'}) if ${'opt_#'}; # and grep { -d } @__;
673
674 if (($ENV{CALLER}||'') =~ /fexstore/) {
675 my $avc = sprintf "%s_%s.taz",$archive,$version;
676 $cmd = "fexsend $m_opt $opt_C $xo $ao $avc @qargv $to";
677 } elsif (scalar(@ARGV) == 1 and "@ARGV" =~ /^(\S+)\.(tar|tgz|zip|7z)$/) {
678 $archive ||= $1;
679 $container = $2;
680 $archive =~ s/[^\w.+-]/_/g;
681 my $avc = sprintf "%s_%s.%s",$archive,$version,$container;
682 $cmd = "fexsend $m_opt $opt_C -= $avc @qargv $to";
683 } else {
684 my $avc = sprintf "%s_%s.%s",$archive,$version,$container;
685 $cmd = "fexsend $m_opt $opt_C $xo $ao $avc @qargv $to";
686 }
687 $sa = "$share:$archive";
688 warn "$prg: $share:$archive:$version\n";
689 $cmd =~ s/ +/ /g;
690 vsystem($cmd);
691 $status = $?;
692
693 } else {
694
695 # pipe data
696 my $stream = '_IO_'.$archive;
697 $stream =~ s/^(_IO_)_$/$1/;
698 my @d = gmtime(time());
699 my $version .= sprintf("%d%02d%02d_%02d%02d%02d",
700 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
701 my $sv = sprintf "%s_%s.gz",$stream,$version;
702 $opt_C = "-C '$opt_C'" if length($opt_C);
703 $cmd = "gzip | fexsend $m_opt $opt_C -s $sv $to";
704 $cmd =~ s/ +/ /g;
705 $sa = "$share:$stream";
706 warn "$prg: $sa:$version\n";
707 vsystem($cmd);
708 $status = $?;
709 }
710
711 if ($opt_o) {
712 local $ENV{FEXID} = '' if $sa =~ /:/ and $sa !~ /^_:/;
713 vsystem("fexpull -o $sa");
714 }
715 exit $status;
716 }
717
718 if ($prg =~ /pull/) {
719
720 &getfid if $ENV{FEXID} or $opt_i;
721
722 if ($opt_U) {
723 if (scalar(@ARGV)) {
724 die "usage: $prg -U SHARE\n";
725 }
726 ($access,@list) = queryshare($opt_U,'users');
727 if ($access eq 'owner' or $access eq 'manage') {
728 print @list;
729 exit;
730 } else {
731 die "$prg: no manage access right for share $opt_U\n";
732 }
733 }
734
735 if ($opt_o) {
736 my $a = "@ARGV";
737 my ($share,$archive);
738 if ($a =~ /^(\S+):(\S+)$/) {
739 $share = $1;
740 $archive = $2;
741 } elsif ($a =~ /^(\S+)$/) {
742 $share = '_';
743 $archive = $1;
744 } else {
745 die "usage: $prg -o SHARE:ARCHIVE\n";
746 }
747 my ($sshare,$server,$owner,$user,$key) = sharedata($share);
748 $key ||= $id if $owner eq $user;
749
750 unless ($key) {
751 warn "$prg: unknown share $share\n" if $share ne '_';
752 die "usage: $prg -l\n".
753 "usage: $prg -l SHARE[:ARCHIVE[:VERSION]]\n";
754 }
755 ($access,@list) = queryshare($share);
756 if ($access ne 'owner' and $access ne 'manage') {
757 die "$prg: no manage access right for share $share\n";
758 }
759 @list = grep { s/^(\Q$archive\E \S+) .*/$1/ } @list;
760 unless (@list) {
761 die "$prg: no archive $archive found in share $share\n";
762 }
763 exit if scalar(@list) == 1;
764 pop @list;
765 foreach (@list) {
766 if (/\Q$archive\E ($vp)\.(tar|tgz|zip|7z|gz)/) {
767 my $file = sprintf "%s_%s.%s",$archive,$1,$2;
768 my $url = sprintf "%s/fop/%s/%s/%s/%s/%s",
769 $server,$owner,$sshare,$user,md5_hex("$file:$key"),$file;
770 local $no_action = $opt_n;
771 vsystem(qw'fexget -d',$url);
772 sleep 1;
773 }
774 }
775 exit;
776 }
777
778 if ($fexid and not @ARGV) {
779 $share = '_';
780 if (-t STDOUT) {
781 $opt_l = 1;
782 } else {
783 @ARGV = ('_IO_');
784 }
785 }
786
787 if ($opt_l) {
788 my $a = "@ARGV";
789 my $version;
790 if ($fexid) {
791 $usage = "usage: $prg -l [SHARE][:ARCHIVE[:VERSION]]\n".
792 "usage: $prg -l [SHARE:]NUMBER\n";
793 } else {
794 $usage = "usage: $prg -l SHARE[:ARCHIVE[:VERSION]]\n".
795 "usage: $prg -l [SHARE:]NUMBER\n";
796 }
797 if ($a =~ / /) {
798 die $usage;
799 }
800 if ($a =~ /^\d+$/) {
801 $share = readlink "$sharedir/.list" or die "usage: $prg -l SHARE\n";
802 $share =~ s/.list$//;
803 $number = $a;
804 } elsif ($a =~ /^(.+):(\d+)$/) {
805 $share = $1;
806 $number = $2;
807 } elsif ($a =~ /^(.+):(.+):($vp)$/) {
808 $share = $1;
809 $archive = $2;
810 $version = $3;
811 } elsif ($a =~ /^:?(.+):($vp)$/) {
812 $share = '_';
813 $archive = $1;
814 $version = $2;
815 } elsif ($a =~ /(.+):(.+)/) {
816 $share = $1;
817 $archive = $2;
818 } elsif ($a =~ /(.+)/) {
819 $share = $1;
820 } elsif ($fexid) {
821 $share = '_';
822 } else {
823 die $usage;
824 }
825 $share =~ s/:$//;
826
827 unless ($share eq '_' or $ENV{FEXID} or $opt_i or -f "$sharedir/$share.cf") {
828 die "$prg: unknown share $share\n".
829 "usage: $prg -l\n".
830 "usage: $prg -l SHARE[:ARCHIVE[:VERSION]]\n";
831 }
832
833 if ($number or $archive) {
834 my ($file,$sav);
835 my $list = "$sharedir/$share.list";
836 open $list,$list or die "$prg: cannot open $list - $!\n";
837 my $n = 0;
838 while (<$list>) {
839 if (/^(\S+) ($vp)\.(tar|tgz|zip|7z|gz) (\d+) (\d+) (\S+) (".*") (.*)/) {
840 $n++;
841 @A = ($1,$2,$3,$4,$5,$6,$7,$8);
842 if ($number and $number == $n) {
843 $file = sprintf "%s_%s.%s",@A[0..2];
844 $sav = sprintf "%s:%s:%s",$share,@A[0..1];
845 last;
846 } elsif ($archive) {
847 if ($A[0] eq $archive) {
848 if ($version and $version eq $A[1] or not $version) {
849 $file = sprintf "%s_%s.%s",@A[0..2];
850 $sav = sprintf "%s:%s:%s",$share,@A[0..1];
851 }
852 }
853 }
854 }
855 }
856 close $list;
857 if ($file) {
858 warn "$prg: $sav\n";
859 if ($file =~ /\.gz$/) {
860 die "$prg: cannot list $file\n";
861 }
862 my ($sshare,$server,$owner,$user,$key) = sharedata($share);
863 $key ||= $id if $owner eq $user;
864 $url =
865 sprintf "%s/fop/%s/%s/%s/%s/%s",
866 $server,$owner,$sshare,$user,md5_hex("$file:$key"),$file;
867 vsystem(qw'fexget -t',$url);
868 exit $?;
869 } else {
870 die "$prg: not found\n";
871 }
872 }
873
874 my $n = 0;
875 my $header = "\n";
876 if ($opt_L) {
877 $header .=
878 "number upload time size share:archive:version:container uploader comment\n".
879 "-------------------------------------------------------------------------------------\n";
880 if ($share eq '_') {
881 $header =~ s/ uploader//;
882 $header =~ s/---------//;
883 if ($opt_u) {
884 $header =~ s/\n-/ URL\n-/;
885 $header =~ s/\n$/----\n/;
886 }
887 }
888 } else {
889 $header .=
890 "number size share:archive:version\n".
891 "--------------------------------------\n";
892 }
893
894 ($access,@list) = queryshare($share);
895 unless (grep /$vp/,@list) {
896 die "$prg: share $share is empty\n";
897 }
898 print "access=$access\n" if $opt_L;
899 my $list = "$sharedir/$share.list";
900 # if ($share eq '_' and $foreign and $owner) {
901 # $list = "/tmp/$hostuser/.fex/share/$owner/_.list";
902 # }
903 print "$server/$owner\n";
904 open $list,$list or die "$prg: cannot open $list - $!\n";
905 while (<$list>) {
906 if (/(\S+) ($vp)\.(tar|tgz|zip|7z|gz) (\d+) (\d+) (\S+) "(.*)"( http.*)?/) {
907 my $archive = $1;
908 my $version = $2;
909 my $container = $3;
910 my $utime = $4;
911 my $size = $5;
912 my $uploader = $6;
913 my $comment = $7;
914 my $url = $8||'';
915 $url = '' unless $opt_u;
916 $n++;
917 print $header;
918 $header = '';
919 if ($opt_L) {
920 $comment = ' "'.$comment.'"' if $comment;
921 $comment ||= ' ""';
922 $comment = " $uploader$comment" if $share ne '_';
923 my @d = localtime $utime;
924 printf "%3d) %d-%02d-%02d %02d:%02d:%02d %8d MB %s:%s:%s:%s%s%s\n",
925 $n,$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0],int($size/$MB),
926 $share,$archive,$version,$container,$comment,$url;
927 } else {
928 printf "%3d) %8d MB %s:%s:%s\n",
929 $n,int($size/$MB),$share,$archive,$version;
930 }
931 }
932 }
933 close $list;
934 unlink "$sharedir/.list";
935 symlink basename($list),"$sharedir/.list";
936 exit;
937 }
938
939 @files = ();
940
941 unless ($opt_d) {
942 if (scalar(@ARGV) > 1) {
943 @files = @ARGV;
944 (@ARGV) = shift @files;
945 } elsif (@ARGV) {
946 } else {
947 if ($fexid) {
948 die "usage: $prg [-l]\n".
949 "usage: $prg [SHARE:]ARCHIVE[:VERSION]\n".
950 "usage: $prg [SHARE:]NUMBER\n";
951 } else {
952 die "usage: $prg -l\n".
953 "usage: $prg SHARE:ARCHIVE[:VERSION]\n".
954 "usage: $prg [SHARE:]NUMBER\n";
955 }
956 }
957 }
958
959 getfid() if not $owner and $ENV{FEXID} or $opt_i;
960
961 foreach (@ARGV) {
962 $arg = $_;
963 my $a = $_;
964
965 $number = 0;
966 my $share = '';
967 my $list = "$sharedir/.list";
968
969 $a =~ s![^:]+/!!; # remove extraction directory
970 if ($a =~ /^(\d+)$/) {
971 $_ = readlink $list or die "$prg: $list is not a symlink\n";
972 m:(.+)\.list$: or die "$prg: $list is unknown\n";
973 $share = $1;
974 $number = $a;
975 } elsif ($a =~ /^(.+):(\d+)$/) {
976 $share = $1;
977 $number = $2;
978 } elsif ($a =~ /(.+):(.+):($vp)$/) {
979 $share = $1;
980 $archive = "$2:$3";
981 } elsif ($a =~ /(.+):($vp)$/) {
982 $share = '_';
983 $archive = "$1:$2";
984 } elsif ($a =~ /(.+):(.+)/) {
985 $share = $1;
986 $archive = $2;
987 } else {
988 $share = '_';
989 $archive = $a;
990 }
991
992 queryshare($share) unless $number;
993
994 $list = "$sharedir/$share.list";
995 # if ($share eq '_' and $foreign and $owner) {
996 # $list = "/tmp/$hostuser/.fex/share/$owner/_.list";
997 # }
998 open $list,$list or die "$prg: cannot open $list - $!\n";
999 my $access = 'none';
1000 my $n = 0;
1001 my @af;
1002 my %af;
1003 $af[0] = '';
1004
1005 while (<$list>) {
1006 $access = $1 if /^access:(\w+)$/;
1007 if (/(\S+) ($vp)\.(tar|tgz|zip|7z|gz) (\d+) (\d+) (\S+) (".*")/) {
1008 $n++;
1009 if ($opt_d) {
1010 $af[$n] = $af{"$1:$2"} = "$1_$2.$3";
1011 $af{$1} .= "$1_$2.$3 ";
1012 } else {
1013 $af[$n] = $af{$1} = $af{"$1:$2"} = "$1_$2.$3";
1014 my $sav = "$share:$1:$2";
1015 $sav .= " $7" if length($7) > 2;
1016 $sav[$n] = $sav{$1} = $sav{"$1:$2"} = $sav;
1017 }
1018 }
1019 }
1020 close $list;
1021
1022 if ($number) {
1023 $af = $af[$number] or die "$prg: no such archive $number\n";
1024 $sav = $sav[$number];
1025 } elsif ($archive) {
1026 $af = $af{$archive} or die "$prg: no such archive $archive\n";
1027 $sav = $sav{$archive};
1028 } else {
1029 die "$prg: no such archive\n";
1030 }
1031
1032 ($sshare,$server,$owner,$user,$key) = sharedata($share);
1033 $key ||= $id if $owner eq $user;
1034
1035 foreach my $afe (split(' ',$af)) {
1036 $url = sprintf "%s/fop/%s/%s/%s/%s/%s",
1037 $server,$owner,$sshare,$user,md5_hex("$afe:$key"),$afe;
1038 if ($opt_d) {
1039 local $no_action = $opt_n;
1040 vsystem(qw'fexget -d',$url);
1041 # if ($access eq 'owner' or $access eq 'manage') {
1042 # vsystem(qw'fexget -d',$url);
1043 # } else {
1044 # die "$prg: no manage access right for share $share\n";
1045 # }
1046 sleep 1;
1047 }
1048 }
1049
1050 }
1051 exit if $opt_d;
1052
1053 warn "$prg: $sav\n";
1054
1055 my $xdir = '';
1056
1057 # extra extraction directory
1058 if ($arg =~ s!([^:]+)/!!) {
1059 $xdir = $1;
1060 chdir $xdir or die "$prg: cannot chdir $1 - $!\n";
1061 }
1062
1063 $cwd = getcwd();
1064 -w $cwd or die "$prg: $cwd is not writable\n";
1065
1066 # STDOUT archive
1067 if ($url =~ /\.gz$/) {
1068 if (-t STDOUT and "@files" ne '-') {
1069 close STDIN;
1070 vsystem("fexget $m_opt $url");
1071 } else {
1072 vsystem("fexget -q $m_opt -s- $url | gunzip");
1073 }
1074 exit $?;
1075 }
1076
1077 if ("@files" eq '-') {
1078 if ($url =~ /\.tgz$/) {
1079 vsystem("fexget $m_opt -s- $url | gunzip");
1080 } else {
1081 vsystem("fexget $m_opt -s- $url");
1082 }
1083 exit $?;
1084 }
1085
1086 if (-t STDIN and -t STDOUT) {
1087 unless ($xdir) {
1088 print "get it? [Yn] ";
1089 $_ = <STDIN>;
1090 exit if /n/i;
1091 }
1092 }
1093
1094 if ($number and $url =~ /.*\/(.+)_($vp)/ or
1095 not $xdir and ($arg =~ /([^:]+):($vp)/ or $arg =~ /([^:]+)()$/))
1096 {
1097 $xdir = sprintf "%s_%s",$1,$2;
1098 if ($xdir =~ s/_$//) {
1099 if (lstat $xdir and not -d $xdir) {
1100 die "$prg: $xdir does already exist and is not a directory\n";
1101 }
1102 unless (-d $xdir) {
1103 mkdir $xdir or die "$prg: cannot mkdir $xdir - $!\n";
1104 }
1105 $xdir = '.';
1106 } else {
1107 $vdir = $xdir; # version directory
1108 mkdir $xdir or die "$prg: cannot mkdir $xdir - $!\n";
1109 chdir $xdir or die "$prg: cannot cd $xdir - $!\n";
1110 }
1111 } elsif ($url =~ /.*\/(.+)_($vp)/) {
1112 my $file = $1;
1113 my $dt = $2;
1114 unless (-l $file) {
1115 if ($xdir and -d $file and not searchpath('del')) {
1116 die "$prg: local file $file is a directory, cannot overwrite\n";
1117 }
1118 # check time stamp of local regular file
1119 # (does not work with non-base64 filename)
1120 my @s = stat $file;
1121 if (-d $file) {
1122 my $qfile = quotemeta $file;
1123 my @files = sort `find $qfile -printf '%T@ %p\n'`;
1124 @s = stat $1 if $files[-1] =~ / (.+)/;
1125 }
1126 if (@s) {
1127 my @d = gmtime($s[9]);
1128 my $ldt = sprintf("%d%02d%02d_%02d%02d%02d",
1129 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
1130 if ($dt le $ldt) {
1131 die "$prg: local '$file' is newer or same age ($ldt)\n"
1132 }
1133 }
1134 if (-f $file and searchpath('vv')) {
1135 # backup of single file
1136 vsystem(qw'vv -s',$file);
1137 }
1138 if (-d $file and searchpath('del')) {
1139 # save delete of directory
1140 vsystem(qw'del -f',$file);
1141 }
1142 }
1143 }
1144
1145 close STDIN if $url !~ /7z$/;
1146 vsystem("fexget $m_opt -oX $url");
1147 exit $? if $?;
1148 $archive = $url;
1149 $archive =~ s:.*/::;
1150 if ($zip = $archive =~ /\.zip$/) {
1151 # vsystem(qw'unzip -l',$archive,@files);
1152 } elsif ($z7 = $archive =~ /\.7z$/) {
1153 # vsystem(qw'7z l',$archive,@files);
1154 } elsif ($tar = $archive =~ /\.tar$/) {
1155 # vsystem(qw'tar -tvf',$archive,@files);
1156 } else {
1157 # vsystem(qw'tar -tvzf',$archive,@files);
1158 }
1159 my @qfiles = map { shellquote($_) } @files;
1160 if ($zip) {
1161 $cmd = "unzip -o -u $archive @qfiles";
1162 $cmd = "unzip $archive @qfiles";
1163 } elsif ($z7) {
1164 $cmd = "7z x $archive @qfiles";
1165 } elsif ($tar) {
1166 $cmd = "tar --keep-newer-files -xvf $archive @qfiles";
1167 } else {
1168 $cmd = "tar --keep-newer-files -xvzf $archive @qfiles";
1169 }
1170 warn "\$ $cmd\n" if $opt_v;
1171 if ($zip or $z7) {
1172 if (abs_path($xdir) eq abs_path('.')) {
1173 $xdir = '';
1174 } else {
1175 print "extracting in ./$xdir/ :\n";
1176 }
1177 system "$cmd </dev/tty";
1178 if ($xdir and `du` =~ /(\d+)/) {
1179 printf "./%s: %d MB\n",$xdir,$1/1024;
1180 }
1181 } else {
1182 open $cmd,'-|',$cmd or die "$prg: $!\n";
1183 print "extracting:\n";
1184 while (<$cmd>) {
1185 next if /^Archive: .*\.zip$/;
1186 s/^\s+[a-z]+: //;
1187 s:^\Q$1/:: if $vdir and $vdir =~ /(.+)_$vp/;
1188 print "$xdir/$_";
1189 }
1190 }
1191 close $cmd;
1192 unlink $archive if $? == 0;
1193
1194 # version directory with just one subdirectory?
1195 if ($vdir and opendir $vdir,'.') {
1196 my @files = ();
1197 while (defined(my $f = readdir($vdir))) {
1198 next if $f eq '.';
1199 next if $f eq '..';
1200 push @files,$f;
1201 }
1202 closedir $vdir;
1203 if (scalar(@files) == 1 and -d $files[0] ) {
1204 my $dir = $files[0];
1205 if ($xdir =~ /^\Q$dir\E_$vp$/ and
1206 rename $dir,$vdir and opendir $vdir,$vdir) {
1207 while (defined(my $f = readdir($vdir))) {
1208 next if $f eq '.';
1209 next if $f eq '..';
1210 rename "$vdir/$f",$f;
1211 }
1212 closedir $vdir;
1213 rmdir $vdir;
1214 }
1215 }
1216 }
1217
1218 exit;
1219 }
1220
1221 die "$prg: unknown program name\n";
1222
1223
1224 sub fexstore {
1225 my $a = "@ARGV";
1226 my @list = ();
1227
1228 $ENV{FEXID} = $ENV{FEXXX} if $ENV{FEXXX};
1229 getfid();
1230 unless ($id) {
1231 die qq'$prg: no \$FEXID, run "fexsend -I" or set FEXXX\n';
1232 }
1233 if ($foreign) {
1234 # die "$prg: support for foreign FEXID not yet implemented\n";
1235 }
1236 $ENV{FEXID} = "$url $owner $id";
1237 unless (-t STDIN) {
1238 die "usage: ... | $prg\n" if length($a);
1239 my $version = versiondate(time);
1240 my $comment = "[$hostuser\@$host] <STDINOUT>";
1241 $comment .= " ($opt_C)" if $opt_C;
1242 $comment =~ s/\'//g;
1243 my $cmd = "gzip | fexsend $m_opt -C '$comment' -s __$version.gz .:share=_";
1244 warn "| $cmd\n" if $opt_v;
1245 exec $cmd;
1246 }
1247
1248 unless (@ARGV or $opt_l or $opt_u or $opt_o or $opt_d or $opt_D) {
1249 die $usage;
1250 }
1251
1252 if ($opt_u) {
1253 if ($opt_l) {
1254 @list = grep {
1255 s/_:_:$vp:\w+ \"// and
1256 s/\" (http)/\n $1/;
1257 } vsystem("fexpull -Lu _|");
1258 if (@list) {
1259 print qq'number upload time size [uploader] arguments\n';
1260 print qq'---------------------------------------------------------\n';
1261 print @list;
1262 }
1263 } else {
1264 @list = vsystem("fexpull -u $a |");
1265 print grep /\/__$vp/,@list;
1266 }
1267 die "$prg: share _ is empty\n" unless @list;
1268 exit;
1269 }
1270
1271 if ($opt_l and $a =~ /^\d+$/) {
1272 vsystem(qw'fexpull -l',$a);
1273 exit $?;
1274 }
1275
1276 if ($opt_l or $opt_o or $opt_D or $a eq '-' or not $a) {
1277 @list = vsystem("fexpull -L _ 2>/dev/null|");
1278 unless (grep /_:/,@list) {
1279 die "$prg: share _ is empty\n";
1280 }
1281 @list = grep { s/_:_:(.+?):\w+/$1/ } @list
1282 or die "$prg: no archives found\n";
1283 }
1284
1285 if ($opt_l) {
1286 print
1287 "number upload time size [uploader] arguments\n",
1288 "---------------------------------------------------------\n";
1289 map { s/\"//;s/\"$//;s/$vp //;$_ } @list;
1290 print @list;
1291 exit;
1292 }
1293
1294 if ($opt_d) {
1295 unless ($a) {
1296 die "usage: $prg -d NUMBER(s)\n".
1297 "usage: $prg -d REGEX\n";
1298 }
1299 if ($a =~ /^[\d ]+$/) {
1300 my $list = "$sharedir/_.list";
1301 open $list,$list or die "usage: $prg -l\n";
1302 @list = ('');
1303 while (<$list>) {
1304 if (/^(\S+) ($vp)/) {
1305 if (/^_ ($vp)\.\w+ (\d+) \d+ \S+ (".*")/) {
1306 my @d = localtime $2;
1307 push @list,sprintf "%d-%02d-%02d %02d:%02d:%02d %s",
1308 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0],$3;
1309 }
1310 }
1311 }
1312 foreach my $n (@ARGV) {
1313 if (my $x = $list[$n]) {
1314 warn "$prg: $x\n";
1315 local $no_action = $opt_n;
1316 vsystem(qw'fexpull -d',$n);
1317 } else {
1318 warn "$prg: $n not found\n";
1319 }
1320 }
1321 } else {
1322 my ($access,@list) = queryshare('_');
1323 my $n = 0;
1324 foreach (@list) {
1325 if (/^(\S+) ($vp)/) {
1326 $n++;
1327 if (/^_ ($vp)\.\w+ \d+ \d+ \S+ (".*$a.*")/) {
1328 warn "$prg: $1 $2\n";
1329 local $no_action = $opt_n;
1330 vsystem(qw'fexpull -d',$n);
1331 }
1332 }
1333 }
1334 }
1335 exit;
1336 }
1337
1338 if ($opt_D) {
1339 my $d = "@ARGV"||0;
1340 if ($d !~ /^\d+$/) {
1341 die "usage: $prg -D [DAYS]\n";
1342 }
1343 my $t = time()-$d*24*60*60;
1344 my @d = gmtime($t);
1345 my $dt = sprintf("%d%02d%02d_%02d%02d%02d",
1346 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
1347
1348 foreach (@list) {
1349 if (/^\s*(\d+).* ($vp) /) {
1350 my $n = $1;
1351 my $v = $2;
1352 if ($v lt $dt) {
1353 local $no_action = $opt_n;
1354 vsystem("fexpull -d $n");
1355 }
1356 }
1357 }
1358 exit;
1359 }
1360
1361 if ($opt_o or $opt_d) {
1362 pop @list if $opt_o;
1363 my $n = $opt_d || '\d+';
1364 foreach (@list) {
1365 if (/^ *($n)\)/) {
1366 local $no_action = $opt_n;
1367 vsystem("fexpull -d $1");
1368 }
1369 }
1370 exit;
1371 }
1372
1373 if ($a and $a !~ /^(\d+|-)$/) {
1374 foreach my $file (@ARGV) {
1375 die "$prg: $file does not exist\n" unless -e $file;
1376 die "$prg: $file is not readable\n" unless -r $file;
1377 }
1378 $_ = "fexpush $m_opt -s -T -C";
1379 vsystem(split,"[%u\@%h] @ARGV",'_:_',@ARGV);
1380 exit $?;
1381 }
1382
1383 if ($a =~ /^(\d+|-)$/) {
1384 my @list = ();
1385 my $list = "$sharedir/_.list";
1386 open $list,$list or die "usage: $prg -l\n";
1387 my $n = 0;
1388 while (<$list>) {
1389 if (/^(\S+) ($vp)/) {
1390 $n++;
1391 if (/^_ ($vp)\.\w+ \d+ (\d+) \S+ (".*")/) {
1392 push @list,sprintf "%3d) %8d MB _:_:%s %s\n",$n,int($2/$MB),$1,$3;
1393 }
1394 }
1395 }
1396 $a = scalar(@list) if $a eq '-';
1397 foreach (@list) {
1398 if (/$a\).*? (\d+ MB).*($vp) (".*")/) {
1399 my $size = $1;
1400 my $version = $2;
1401 my $comment = $3;
1402 print "$version $size $comment\n";
1403 if (-t STDIN and -t STDOUT) {
1404 print "pull? [Yn] ";
1405 $_ = <STDIN>;
1406 exit if /n/i;
1407 }
1408 if ($comment =~ / <STDINOUT>/) {
1409 # close STDIN;
1410 vsystem("fexpull $m_opt _:$a -");
1411 } else {
1412 vsystem("fexpull $m_opt _:$a - | tar -xvf -");
1413 }
1414 exit $?;
1415 }
1416 }
1417 die "$prg: archive _:$a not found\n";
1418 } elsif ($a eq '-') {
1419 $_ = $list[-1];
1420 if (/(\d+)\).*? (\d+ MB).*($vp) (".*")/) {
1421 my $n = $1;
1422 my $size = $2;
1423 my $version = $3;
1424 my $comment = $4;
1425 if ($comment =~ / <STDINOUT>\"$/) {
1426 close STDIN;
1427 vsystem("fexpull _:$n -");
1428 exit $?;
1429 } else {
1430 print "$version $size $comment\n";
1431 print "pull? [Yn] ";
1432 $_ = <STDIN>;
1433 exit if /n/i;
1434 vsystem("fexpull $m_opt _:$n - | tar -xvf -");
1435 exit $?;
1436 }
1437 } else {
1438 die "$prg: $_";
1439 }
1440 } else {
1441 die $usage;
1442 }
1443 exit;
1444 }
1445
1446
1447 sub nmtime {
1448 my ($file,$dir);
1449 my @files;
1450 my @s;
1451 local $_;
1452
1453 $nfiles += scalar(@_);
1454 print "Scanning: $nfiles files\r" if -t STDOUT and $nfiles > 1;
1455 if (scalar(@_) == 1 and not -d $_[0]) {
1456 if (@s = stat $_[0]) {
1457 $mtime = $s[9] if $s[9] > $mtime;
1458 }
1459 } else {
1460 foreach $file (@_) {
1461 @files = ();
1462 if (-d $file and not -l $file and opendir $dir,$file) {
1463 print "Scanning $file/\n" if $opt_v;
1464 if (@s = stat $dir) {
1465 $mtime = $s[9] if $s[9] > $mtime;
1466 }
1467 while (defined($_ = readdir $dir)) {
1468 next if /^\.\.?$/;
1469 next if /^($xrx)$/;
1470 push @files, "$file/$_";
1471 }
1472 closedir $dir;
1473 nmtime(@files);
1474 } else {
1475 if (@s = lstat $file) {
1476 $mtime = $s[9] if $s[9] > $mtime;
1477 }
1478 }
1479 }
1480 }
1481 }
1482
1483
1484 sub saveconfig {
1485 local $_ = shift;
1486
1487 if (m:(^http.*?)/fas/(.+?)/(.+?)/(.+?)/(\w+):) {
1488 my $surl = $1;
1489 my $owner = $2;
1490 my $share = $3;
1491 my $user = $4;
1492 my $pkey = $5;
1493 print "share:\t$share\n";
1494 print "server:\t$surl\n";
1495 print "owner:\t$owner\n";
1496 print "user:\t$user\n";
1497 mkdir dirname($sharedir);
1498 unless (-d $sharedir) {
1499 mkdir $sharedir or die "$prg: cannot mkdir $sharedir - $!\n";
1500 }
1501 $scf = "$sharedir/$share.cf";
1502 while (-e $scf) {
1503 print "\nConfig file $scf does already exist!\n";
1504 $_ = inputline("Enter an alternate share name or \"!\" to overwrite: ");
1505 last if /^!/;
1506 s/^\.//g;
1507 s/^\s+//g;
1508 s/\s+$//g;
1509 s:[^\w.,=+-]:_:g;
1510 exit unless $_;
1511 $scf = "$sharedir/$_.cf";
1512 }
1513 open $scf,'>',$scf or die "$prg: cannot write $scf - $!\n";
1514 print {$scf} "share=$share\n";
1515 print {$scf} "server=$surl\n";
1516 print {$scf} "owner=$owner\n";
1517 print {$scf} "user=$user\n";
1518 print {$scf} "pkey=$pkey\n";
1519 close $scf;
1520 print "$prg: $scf written\n";
1521 $prg =~ s/push/pull/;
1522 print "usage: $prg -l $share # show available archives\n";
1523 } else {
1524 die "$prg: not a share URL\n";
1525 }
1526 exit;
1527 }
1528
1529
1530 sub delarchives {
1531 my $share = shift;
1532 my $archive = shift;
1533 my ($file,$url);
1534 my ($sshare,$server,$owner,$user,$key) = sharedata($share);
1535
1536 $key ||= $id if $owner eq $user;
1537 return unless $key;
1538
1539 my ($access,@list) = queryshare($share);
1540 unless ($access eq 'owner' or $access eq 'manage') {
1541 warn "$prg: cannot delete archives, because you only have $access right\n";
1542 return;
1543 }
1544 foreach (@list) {
1545 if (/^\Q$archive\E ($vp)\.(tar|tgz|zip|7z|gz)/) {
1546 $file = sprintf "%s_%s.%s",$archive,$1,$2;
1547 $url = sprintf "%s/fop/%s/%s/%s/%s/%s",
1548 $server,$owner,$sshare,$user,md5_hex("$file:$key"),$file;
1549 local $no_action = $opt_n;
1550 vsystem(qw'fexget -d',$url);
1551 sleep 1;
1552 }
1553 }
1554 }
1555
1556
1557 sub getfid {
1558 local $_;
1559 $url = $owner = $id = '';
1560
1561 if (not $opt_i and $_ = $ENV{FEXID}) {
1562 $_ = decode_b64($_) unless / /;
1563 ($url,$owner,$id) = split;
1564 unless ($id) {
1565 die "$prg: illegal \$FEXID\n";
1566 }
1567 } elsif (-s $fid and open $fid,$fid) {
1568 if ($opt_i) {
1569 while (<$fid>) {
1570 if (/^\[$opt_i\]/) {
1571 $url = <$fid>;
1572 $owner = <$fid>;
1573 $id = <$fid>;
1574 last;
1575 }
1576 }
1577 } else {
1578 $url = <$fid>;
1579 $owner = <$fid>;
1580 $id = <$fid>;
1581 if ($prg eq 'fexstore') {
1582 while (<$fid>) {
1583 if (/^\[xx\]/) {
1584 $url = <$fid>;
1585 $owner = <$fid>;
1586 $id = <$fid>;
1587 last;
1588 }
1589 }
1590 }
1591 }
1592 close $fid;
1593 if ($url and $owner and $id) {
1594 $url =~ s/\s//g;
1595 $owner =~ s/\s//g;
1596 $id =~ s/\s//g;
1597 } else {
1598 if ($opt_i) {
1599 die "$prg: no F*EX URL for [$opt_i] in $fid\n";
1600 } else {
1601 die "$prg: no F*EX URL in $fid\n";
1602 }
1603 }
1604 }
1605 $url = "http://$url" if $url !~ /^http/;
1606 $url =~ s:/+$::;
1607
1608 $foreign = 1;
1609 if (open $fid,$fid) {
1610 $_ = <$fid>||'';
1611 $_ = <$fid>||'';
1612 s/\s//g;
1613 $foreign = 0 if lc $_ eq $owner;
1614 close $fid;
1615 }
1616
1617 if ($opt_i) {
1618 die "$prg: no [$opt_i] found in $fid\n" unless $id;
1619 $foreign = 0;
1620 $sharedir = "$HOME/.fex/share/$opt_i";
1621 mkdirp($sharedir);
1622 } elsif ($foreign) {
1623 $sharedir = "/tmp/$hostuser/.fex/share/$owner";
1624 $ENV{FEXTMP} = "/tmp/$hostuser/.fex/tmp";
1625 mkdirp($sharedir);
1626 mkdirp($ENV{FEXTMP});
1627 chmod 0700,"/tmp/$hostuser/.fex";
1628 }
1629
1630 unless ($id) {
1631 die "$prg: no \$FEXID\n";
1632 }
1633
1634 $ENV{FEXID} = "$url $owner $id";
1635 }
1636
1637
1638 sub sharedata {
1639 my $share = shift;
1640 $share =~ s/\.cf$//;
1641 my $scf = "$sharedir/$share.cf";
1642
1643 mkdir dirname($sharedir);
1644 unless (-d $sharedir) {
1645 mkdir $sharedir or die "$prg: cannot mkdir $sharedir - $!\n";
1646 }
1647
1648 if ($share eq '_' and $fexid) {
1649 &getfid;
1650 return ($share,$url,$owner,$owner);
1651 } else {
1652 unless (-f $scf) {
1653 if ($fexid) {
1654 if ($share !~ /^[\w_-]+$/) {
1655 die "$prg: illegal share name $share\n";
1656 }
1657 &getfid;
1658 if ($prg =~ /push/ and $url and $owner) {
1659 warn "$prg: share $share is locally unknown\n";
1660 if (-t STDIN) {
1661 print "create it [yN]? ";
1662 $_ = <STDIN>;
1663 if (/y/i) {
1664 warn "$prg: creating new share $share\n";
1665 &getfid;
1666 open $scf,'>',$scf or die "$prg: cannot write $scf - $!\n";
1667 print {$scf} "share=$share\n";
1668 print {$scf} "server=$url\n";
1669 print {$scf} "owner=$owner\n";
1670 print {$scf} "user=$owner\n";
1671 close $scf;
1672 return ($share,$url,$owner,$owner);
1673 } else {
1674 die "$prg: aborting\n";
1675 }
1676 } else {
1677 die "$prg: unknown share $share\n";
1678 }
1679 }
1680 } else {
1681 die "$prg: unknown share $share\n";
1682 }
1683 }
1684
1685 my $sshare = $share;
1686 my $server = $::server;
1687 my $owner = $::owner;
1688 my $user = $::user;
1689 my $pkey = $::pkey;
1690 my $id;
1691
1692 if (-f $scf) {
1693 open $scf,$scf or die "$prg: cannot open $scf - $!\n";
1694 while (<$scf>) {
1695 $sshare = $1 if /^share=(\S+)/;
1696 $owner = $1 if /^owner=(\S+)/;
1697 $user = $1 if /^user=(\S+)/;
1698 $server = $1 if /^server=(\S+)/;
1699 $pkey = $1 if /^pkey=(\S+)/;
1700 $id = $1 if /^id=(\S+)/;
1701 }
1702 close $scf;
1703 }
1704 unless ($pkey) {
1705 if (not ($owner and $user and $server) or $owner eq $user) {
1706 local $opt_i = $id;
1707 &getfid;
1708 $server = $::url;
1709 $user = $owner = $::owner;
1710 } else {
1711 die "$prg: no pkey in $scf\n";
1712 }
1713 }
1714 $server = "http://$server" if $server !~ /^http/;
1715 $server =~ s:^(http.+//.+)[/!]:$1:;
1716 return ($sshare,$server,$owner,$user,$pkey);
1717 }
1718
1719 }
1720
1721
1722 sub listshares {
1723 my @sp = qw'share server owner user pkey';
1724 my $sp = join('|',@sp);
1725 my @shares = ();
1726 my @sshares = ();
1727 local $_;
1728
1729 die "$prg: no $sharedir\n" unless -d $sharedir;
1730
1731 if ($fexid) {
1732 &getfid;
1733 {
1734 # local $ENV{FEXID} = "$url $owner $id";
1735 foreach (vsystem('fexsend -~ LISTSHARES |')) {
1736 if (m:^server/user:) {
1737 warn $_;
1738 } else {
1739 chomp;
1740 push @sshares,$_;
1741 }
1742 }
1743 }
1744 # store share config only if not foreign FEXID
1745 unless ($foreign) {
1746 foreach my $sshare (@sshares) {
1747 my $scf = "$sharedir/$sshare.cf";
1748 if (not -f $scf and open $scf,'>',$scf) {
1749 print {$scf} "share=$sshare\n";
1750 print {$scf} "server=$url\n";
1751 print {$scf} "owner=$owner\n";
1752 print {$scf} "user=$owner\n";
1753 close $scf;
1754 }
1755 }
1756 }
1757 }
1758
1759 @shares = @sshares;
1760
1761 unless ($foreign) {
1762 # delete local share config if share does not exist any more on server
1763 @shares = grep { s:.*/(.+)\.cf:$1: } glob("$sharedir/*.cf");
1764 foreach my $share (@shares) {
1765 next if $share eq '_';
1766 my $owner = '';
1767 my $user = '';
1768 my $pkey = '';
1769 open $share,"$sharedir/$share.cf" or next;
1770 while (<$share>) {
1771 $owner = $1 if /^owner=(\S+)/;
1772 $user = $1 if /^user=(\S+)/;
1773 $pkey = $1 if /^pkey=(\S+)/;
1774 }
1775 close $share;
1776 if ($owner eq $user and not $pkey) {
1777 unless (grep { $share eq $_ } @sshares) {
1778 unlink "$sharedir/$share.cf";
1779 unlink "$sharedir/$share.list";
1780 }
1781 }
1782 }
1783 }
1784
1785 if ($opt_L) {
1786 foreach my $share (@shares) {
1787 my %sinfo = (
1788 owner => '',
1789 user => '',
1790 );
1791 if (open $share,"$sharedir/$share.cf") {
1792 while (<$share>) {
1793 $sinfo{$1} = $2 if /^($sp)=(.+)/;
1794 }
1795 close $share;
1796 $sinfo{'pkey'} ||= 'id' if $sinfo{'owner'} eq $sinfo{'user'};
1797 my $sinfo = 1;
1798 foreach my $i (@sp) {
1799 $sinfo &&= $sinfo{$i};
1800 }
1801 if ($sinfo) {
1802 print "\n";
1803 print "\$HOME/.fex/share/$share.cf:\n";
1804 foreach my $i (@sp) {
1805 printf "%s=%s\n",$i,$sinfo{$i} if $i ne 'pkey';
1806 }
1807 if ($sinfo{'owner'} eq $sinfo{'user'}) {
1808 print "access=owner\n";
1809 printf "URL=%s/fas?show=share:%s:archives\n",
1810 $sinfo{'server'},$sinfo{'share'};
1811
1812 } else {
1813 my ($access) = queryshare($share);
1814 printf "access=%s\n",$access;
1815 printf "URL=%s/fas/%s/%s/%s/%s\n",
1816 $sinfo{'server'},
1817 $sinfo{'owner'},
1818 $sinfo{'share'},
1819 $sinfo{'user'},
1820 $sinfo{'pkey'};
1821 }
1822 }
1823 }
1824 }
1825 } else {
1826 my $fmt = "%-32s %s\n";
1827 my @list = ();
1828
1829 if (@shares) {
1830 print "\n";
1831 printf($fmt,'share(alias)','owner');
1832 printf($fmt,'------------','-----');
1833 foreach my $sshare (@sshares) {
1834 unless (grep /^\Q$sshare\E$/,@shares) {
1835 push @list,sprintf($fmt,$sshare,$owner);
1836 }
1837 }
1838 if ($foreign) {
1839 # foreign FEXID => list only own shares
1840 foreach my $share (@shares) {
1841 push @list,sprintf($fmt,$share,$owner);
1842 }
1843 } else {
1844 foreach my $share (@shares) {
1845 my %sinfo = (
1846 owner => '',
1847 user => '',
1848 );
1849 if (open $share,"$sharedir/$share.cf") {
1850 while (<$share>) {
1851 $sinfo{$1} = $2 if /^($sp)=(.+)/;
1852 }
1853 close $share;
1854 $sinfo{'pkey'} ||= 'id' if $sinfo{'owner'} eq $sinfo{'user'};
1855 my $sinfo = 1;
1856 foreach my $i (@sp) {
1857 $sinfo &&= $sinfo{$i};
1858 }
1859 if ($sinfo) {
1860 push @list,sprintf($fmt,$share,$sinfo{'owner'});
1861 }
1862 }
1863 }
1864 }
1865 print asort(@list);
1866 # print "\nusage: $prg -l SHARE:\n" if $prg =~ /pull/;
1867 }
1868 }
1869 }
1870
1871
1872 sub queryshare {
1873 my $share = shift;
1874 my $mode = shift||'';
1875 my $sshare = '';
1876 my $access = 'none';
1877 my $cmd;
1878 my $list;
1879 my @list;
1880 my $FEXID;
1881 local $_;
1882
1883 if ($share eq '_') {
1884 $sshare = $share;
1885 } elsif (not -f "$sharedir/$share.cf" and $ENV{FEXXX} or $opt_i) {
1886 # foreign FEXID ==> own shares
1887 $sshare = $share;
1888 &getfid;
1889 $user = $owner;
1890 } else {
1891 ($sshare,$server,$owner,$user,$pkey) = sharedata($share);
1892 if ($server and $sshare and $owner and $owner eq $user) {
1893 # own share
1894 } elsif ($server and $sshare and $owner and $user and $pkey) {
1895 # share user
1896 } else {
1897 warn "$prg: corrupt data in $sharedir/$share\n";
1898 return $access;
1899 }
1900 }
1901
1902 if ($share eq '_' or $opt_i or $owner and $owner eq $user and not $pkey) {
1903 &getfid;
1904 $FEXID = "$url $owner $id";
1905 $server = $url;
1906 $user = $owner;
1907 } else {
1908 $FEXID = "$server $user PKEY:$pkey";
1909 }
1910 $list = "$sharedir/$share.list";
1911 unlink $list;
1912 $ENV{FEXID} = $FEXID;
1913 $cmd = "fexsend -~ LISTSHARE $owner:share=$sshare |";
1914 @list = grep { not m:^server/user: } vsystem($cmd);
1915 if (scalar(@list) > 1) {
1916 open $list,'>',$list or die "$prg: cannot write $list - $!\n";
1917 foreach (@list[0..1]) {
1918 $access = $1 if /^access=(\w+)$/;
1919 }
1920 print {$list} @list;
1921 close $list;
1922 open $list,$list or die "$prg cannot reopen $list - $!\n";
1923 if ($mode eq 'users') {
1924 @list = grep /^http/,<$list>;
1925 } elsif ($mode eq 'archives') {
1926 @list = grep /^$ap$/,<$list>;
1927 } else {
1928 @list = grep /^\S+ $vp/,<$list>;
1929 }
1930 close $list;
1931 }
1932 return $access,@list;
1933 }
1934
1935
1936 sub shellquote {
1937 local $_ = shift;
1938 s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
1939 return $_;
1940 }
1941
1942
1943 sub vsystem {
1944 my @cmd = @_;
1945 my $cmd;
1946 local $ENV{CALLER} = $prg;
1947 local $_;
1948
1949 if ($_=$ENV{FEXID} and ($opt_v or $no_action)) {
1950 my @fid = split;
1951 $fid[-1] =~ s/[a-z0-9]/*/g;
1952 warn "FEXID='@fid'\n";
1953 }
1954
1955 $_ = "@cmd";
1956 if (scalar(@cmd) == 1 and /\s/ and not (/^\w+=/ or /[\'\"\`\\*?&|<>;]/)) {
1957 @cmd = split;
1958 }
1959 if ($opt_v or $no_action) {
1960 if (-t STDIN) { print STDERR '$ ' }
1961 else { print STDERR '| ' }
1962 if (scalar(@cmd) == 1) {
1963 warn "@cmd\n";
1964 } else {
1965 my @w;
1966 foreach (@cmd) {
1967 if (/\'/) {
1968 push @w,shellquote($_);
1969 } elsif (/[^\w\/:=~^%@,.+-]/) {
1970 push @w,"'$_'";
1971 } else {
1972 push @w,$_;
1973 }
1974 }
1975 warn "@w\n";
1976 }
1977 }
1978
1979 return if $no_action;
1980
1981 $cmd = "@cmd";
1982 if (scalar(@cmd) == 1) {
1983 if ($cmd =~ s/\|$//) {
1984 my @a = `$cmd`;
1985 return wantarray ? @a : join('',@a);
1986 } else {
1987 system $cmd;
1988 }
1989 } else {
1990 system @cmd;
1991 }
1992 }
1993
1994
1995 # from MIME::Base64::Perl
1996 sub decode_b64 {
1997 local $_ = shift;
1998 my $uu = '';
1999 my ($i,$l);
2000
2001 tr|A-Za-z0-9+=/||cd;
2002 s/=+$//;
2003 tr|A-Za-z0-9+/| -_|;
2004 return "" unless length;
2005
2006 $l = (length)-60;
2007 for ($i = 0; $i <= $l; $i += 60) {
2008 $uu .= "M" . substr($_,$i,60);
2009 }
2010 $_ = substr($_,$i);
2011 if (length) {
2012 $uu .= chr(32+(length)*3/4) . $_;
2013 }
2014 return unpack("u",$uu);
2015 }
2016
2017
2018 sub urldecode {
2019 local $_ = shift;
2020 s/\%([a-f\d]{2})/chr(hex($1))/ige;
2021 return $_;
2022 }
2023
2024
2025 sub uniq {
2026 my %x;
2027 grep !$x{$_}++,@_;
2028 }
2029
2030
2031 sub asort {
2032 return sort { lc $a cmp lc $b } @_;
2033 }
2034
2035
2036 sub searchpath {
2037 my ($prg,$path,@PATH);
2038
2039 @PATH = split(':',$ENV{PATH});
2040 foreach $prg (@_) {
2041 foreach $path (@PATH) {
2042 return $prg if -x "$path/$prg";
2043 }
2044 }
2045 return '';
2046 }
2047
2048
2049 sub strip {
2050 local $_ = shift;
2051 s/^\s+//;
2052 s/\s+$/\n/;
2053 while (s/^#.*\n//m) {}
2054 return $_;
2055 }
2056
2057
2058 sub inputline {
2059 my $prompt = shift;
2060 my $term = new Term::ReadLine $prg;
2061
2062 $term->ornaments(0) unless $ENV{PERL_RL};
2063 return $term->readline($prompt)||'';
2064 }
2065
2066
2067 sub versiondate {
2068 my @d = gmtime shift;
2069 return sprintf("%d%02d%02d_%02d%02d%02d",
2070 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
2071 }
2072
2073
2074 # emulate mkdir -p
2075 sub mkdirp {
2076 my $dir = shift;
2077 my $pdir;
2078
2079 return if -d $dir;
2080 $dir =~ s://+:/:g;
2081 $dir =~ s:/$::;
2082 die "$prg: cannot mkdir /\n" unless length($dir);
2083 $pdir = $dir;
2084 if ($pdir =~ s:(.+)/.+:$1:) {
2085 mkdirp($pdir) unless -d $pdir;
2086 }
2087 mkdir $dir or die "$prg: mkdir $dir - $!\n";
2088 }