1package File::Copy::Link;
2
3use strict;
4use warnings;
5
6use Carp;
7use File::Copy ();
8
9require Exporter;
10use base qw(Exporter);
11
12our @EXPORT_OK = qw(copylink safecopylink);
13our $VERSION = '0.06';
14
15sub copylink {
16    local $_ = @_ ? shift : $_;                 # default to $_
17    croak "$_ not a link\n" unless -l;
18    open my $fh, '<', $_ or croak "Can't open link $_: $!\n";
19    unlink or croak "Can't unlink link $_: $!\n";
20    my $ok = File::Copy::copy $fh, $_;
21    croak "copy($fh $_) failed: $!\n" unless $ok;
22    return $ok;
23}
24
25sub safecopylink {
26    local $_ = @_ ? shift : $_;                 # default to $_
27    croak "$_ not a link\n" unless -l;
28    require File::Spec::Link;
29    my $orig = File::Spec::Link->linked($_);
30    croak "$_ link problem\n" unless defined $orig;
31    unlink or croak "Can't unlink link $_: $!\n";
32    my $ok = File::Copy::copy $orig, $_;
33    croak "copy($orig $_) failed: $!\n" unless $ok;
34    return $ok;
35}
36
371;
38__END__
39# Below is stub documentation for your module. You'd better edit it!
40
41=head1 NAME
42
43File::Copy::Link - extension for replacing a link by a copy of the linked file
44
45=head1 SYNOPSIS
46
47  use File::Copy::Link;
48  copylink 'file.lnk';
49
50  use File::Copy::Link qw(safecopylink);
51  safecopylink 'file.lnk';
52
53=head1 DESCRIPTION
54
55=over 4
56
57=item C<copylink>
58
59reads the filename linked to by the argument and replaced
60the link with a copy of the file.  It opens a filehandle to read from
61the link, deletes the link, and then copies the filehandle back to the
62link.
63
64=item C<safecopylink>
65
66does the same as C<copylink> but without the open-and-delete
67manouvre.  Instead, it uses C<File::Spec::Link> to find the target of the
68link and copies from there.
69
70=back
71
72This module is mostly a wrapper round C<File::Spec::Link::linked> and
73C<File::Copy::copy>, the functionality is available in a command line
74script F<copylink>.
75
76=head2 EXPORT
77
78Nothing by default, can export C<copylink>, `C<safecopylink>.
79
80=head1 SEE ALSO
81
82copylink(1) File::Copy(3) File::Spec::Link(3)
83
84=head1 AUTHOR
85
86Robin Barker, <RMBarker@cpan.org>
87
88=head1 COPYRIGHT AND LICENSE
89
90Copyright 2003, 2006, 2007, 2011, 2014 by Robin Barker
91
92This library is free software; you can redistribute it and/or modify
93it under the same terms as Perl itself.
94
95=cut
96
97$Id: Link.pm 342 2014-06-23 18:30:53Z robin $
98