#!/usr/bin/env perl # desymlink --- replace symlinks with copy of file pointed to # Author: Noah Friedman # Created: 1995-09-23 # Public domain # $Id: desymlink,v 1.10 2016/08/20 17:55:43 friedman Exp $ # Commentary: # Code: use strict; use warnings qw(all); use FindBin; use lib "$FindBin::Bin/../lib/perl"; use lib "$ENV{HOME}/lib/perl"; use Getopt::Long; use Pod::Usage; use NF::FileUtil qw(:name :file :symlink); (my $progname = $0) =~ s|.*/||; my %opt; sub errmsg { print STDERR join (": ", $progname, @_), "\n"; return; } sub parse_options { local *ARGV = \@{$_[0]}; # modify our local arglist, not real ARGV. my $help = -1; my $parser = Getopt::Long::Parser->new; $parser->configure (qw(bundling autoabbrev)); my $succ = $parser->getoptions ("h|help|usage+" => \$help, "l|link" => \$opt{hardlink}, "p|preserve" => \$opt{preserve}, "v|verbose+" => \$opt{verbose}, ); pod2usage (-exitstatus => 1, -verbose => 0) unless $succ; pod2usage (-exitstatus => 0, -verbose => $help) if $help >= 0; $NF::FileUtil::errors_fatal = 1; $NF::FileUtil::verbose = $opt{verbose}; } sub main { parse_options (\@_); for my $to (@_) { if (! -l $to) { my $msg = -e $to ? "Not a symbolic link" : $!; errmsg ($to, $msg); next; } my $from = dereference_symlinks ($to); if (-f $from) { if ($opt{hardlink}) { xlink ($from, $to) } else { xunlink ($to); copy_file ($from, $to, $opt{preserve}, 0); } } else { -e $from || return errmsg ($to, "Link does not point to an existing file"); -f $from || return errmsg ($to, "Link does not point to a regular file"); } } } main (@ARGV); __END__ =head1 NAME desymlink - replace symlinks with copy of file pointed to =head1 SYNOPSIS desymlink {options} [symlinks ...] -h, --help You're looking at it. -l, --link Create a hard link to original file; don't copy. -p, --preserve Preserve owner, timestamps on copied files. -v, --verbose Be verbose. =cut