Brackup
view release on metacpan or search on metacpan
lib/Brackup/Restore.pm view on Meta::CPAN
$self->{_remote_user_map} ||= { map { split /:/, $_, 2 } split /\s+/, $meta->{UIDMap} };
# try and lookup local uid using remote username
if (my $remote_user = $self->{_remote_user_map}->{$remote_uid}) {
my $local_uid = getpwnam($remote_user);
return $self->{_local_uid_map}->{$remote_uid} = $local_uid
if defined $local_uid;
}
# if remote username missing locally, fallback to $remote_uid
return $self->{_local_uid_map}->{$remote_uid} = $remote_uid;
}
sub _lookup_remote_gid {
my ($self, $remote_gid, $meta) = @_;
return $self->{_local_gid_map}->{$remote_gid}
if defined $self->{_local_gid_map}->{$remote_gid};
# meta remote group map - remote_gid => remote group
$self->{_remote_group_map} ||= { map { split /:/, $_, 2 } split /\s+/, $meta->{GIDMap} };
# try and lookup local gid using remote group
if (my $remote_group = $self->{_remote_group_map}->{$remote_gid}) {
my $local_gid = getgrnam($remote_group);
return $self->{_local_gid_map}->{$remote_gid} = $local_gid
if defined $local_gid;
}
# if remote group missing locally, fallback to $remote_gid
return $self->{_local_gid_map}->{$remote_gid} = $remote_gid;
}
sub _chown {
my ($self, $full, $it, $type, $meta) = @_;
my $uid = $self->_lookup_remote_uid($it->{UID}, $meta) if $it->{UID};
my $gid = $self->_lookup_remote_gid($it->{GID}, $meta) if $it->{GID};
if ($type eq 'l') {
if (! defined $self->{_lchown}) {
no strict 'subs';
$self->{_lchown} = eval { require Lchown } && Lchown::LCHOWN_AVAILABLE;
}
if ($self->{_lchown}) {
Lchown::lchown($uid, -1, $full) if defined $uid;
Lchown::lchown(-1, $gid, $full) if defined $gid;
}
} else {
# ignore errors, but change uid and gid separately to sidestep unprivileged failures
chown $uid, -1, $full if defined $uid;
chown -1, $gid, $full if defined $gid;
}
}
sub _update_statinfo {
my ($self, $full, $it) = @_;
push @{ $self->{_stats_to_run} }, sub {
if (defined $it->{Mode}) {
chmod(oct $it->{Mode}, $full) or
die "Failed to change mode of $full: $!";
}
if ($it->{Mtime} || $it->{Atime}) {
utime($it->{Atime} || $it->{Mtime},
$it->{Mtime} || $it->{Atime},
$full) or
die "Failed to change utime of $full: $!";
}
};
}
sub _exec_statinfo_updates {
my $self = shift;
# change the modes/times in backwards order, going from deep
# files/directories to shallow ones. (so we can reliably change
# all the directory mtimes without kernel doing it for us when we
# modify files deeper)
while (my $sb = pop @{ $self->{_stats_to_run} }) {
$sb->();
}
}
sub _restore_directory {
my ($self, $full, $it) = @_;
unless (-d $full) {
mkdir $full or # FIXME: permissions on directory
die "Failed to make directory: $full ($it->{Path})";
}
$self->_update_statinfo($full, $it);
}
sub _restore_link {
my ($self, $full, $it) = @_;
if (-e $full) {
# TODO: add --conflict={skip,overwrite} option, defaulting to nothing: which dies
die "Link $full ($it->{Path}) already exists. Aborting.";
}
symlink $it->{Link}, $full
or die "Failed to link";
}
sub _restore_fifo {
my ($self, $full, $it) = @_;
if (-e $full) {
die "Named pipe/fifo $full ($it->{Path}) already exists. Aborting.";
}
mkfifo($full, $it->{Mode}) or die "mkfifo failed: $!";
$self->_update_statinfo($full, $it);
}
sub _restore_file {
my ($self, $full, $it) = @_;
( run in 2.758 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )