equal
deleted
inserted
replaced
|
1 #!/usr/bin/perl |
|
2 |
|
3 use strict; |
|
4 use Getopt::Long; |
|
5 |
|
6 my $link; |
|
7 my $target; |
|
8 my $help; |
|
9 my $force = 0; |
|
10 |
|
11 sub usage($); |
|
12 sub help(); |
|
13 sub usage_error(); |
|
14 |
|
15 my %optmap = ( 'link' => \$link, |
|
16 'target' => \$target, |
|
17 'force' => \$force, |
|
18 'help' => \$help); |
|
19 |
|
20 GetOptions(\%optmap, |
|
21 'link=s', |
|
22 'target=s', |
|
23 'force!', |
|
24 'help!') |
|
25 or usage_error(); |
|
26 |
|
27 if ($help) { |
|
28 help(); |
|
29 } |
|
30 |
|
31 usage_error(), unless (defined($link) && defined($target)); |
|
32 |
|
33 my $junction_help = `junction /?`; |
|
34 die("Need command \"junction\". Not found\n"), if ($junction_help =~ /is not recognised/); |
|
35 die("Directory \"$target\" not found\n"), unless -d "$target"; |
|
36 |
|
37 if ( -e "$link") { |
|
38 print("\"$link\" already exists. "); |
|
39 if (!$force) { |
|
40 my $choice; |
|
41 while($choice ne 'y' and $choice ne 'n') { |
|
42 print "Delete? (y/n)? "; |
|
43 $choice = <STDIN>; |
|
44 chomp($choice); |
|
45 $choice = lc($choice); |
|
46 } |
|
47 $force = $choice eq 'y'; |
|
48 } |
|
49 if ($force) { |
|
50 system("junction -d \"$link\" > nul"); |
|
51 if ($?) { |
|
52 die("Cannot delete \"$link\": $!\n"); |
|
53 } |
|
54 else { |
|
55 print "Deleted \"$link\"\n"; |
|
56 } |
|
57 } |
|
58 } |
|
59 |
|
60 system("junction \"$link\" \"$target\" > nul"); |
|
61 if ($?) { |
|
62 die("Cannot cteate junction \"$link\" -> \"$target\": $!\n"); |
|
63 } |
|
64 else { |
|
65 print("Created junction \"$link\" -> \"$target\"\n"); |
|
66 } |
|
67 exit 0; |
|
68 |
|
69 sub usage($) |
|
70 { |
|
71 my $error = shift; |
|
72 my $fh = $error == 0 ? *STDOUT : *STDERR; |
|
73 print $fh "make_junction.pl\n" . |
|
74 "Create a Windows junction (a.k.a symbolic link)\n" . |
|
75 "usage:\n" . |
|
76 " make_junction.pl --help\n" . |
|
77 " make_junction.pl --link=LINKDIR --target=TARGDIR\n " . |
|
78 "options:\n" . |
|
79 " --help Display this help and exit\n" . |
|
80 " --link=LINKDIR LINKDIR specifies the junction to be created. Last component is the junction. The rest must exist\n" . |
|
81 " --target=TARGDIR TARGDIR is directory to which the junction will point.\n"; |
|
82 exit $error; |
|
83 } |
|
84 |
|
85 sub help() |
|
86 { |
|
87 usage(0); |
|
88 } |
|
89 |
|
90 sub usage_error() |
|
91 { |
|
92 usage(1); |
|
93 } |
|
94 |
|
95 # EOF |
|
96 |