mmm
view release on metacpan or search on metacpan
lib/MMM/Utils.pm view on Meta::CPAN
=cut
sub fmt_duration {
my ($second1, $second2) = @_;
my @gmt1 = _get_meantime($second1);
my @gmt2 = _get_meantime($second2 || scalar(time));
my ($D_y,$D_m,$D_d, $Dh,$Dm,$Ds) = Delta_YMDHMS(
$second1 <= $second2
? (@gmt1, @gmt2)
: (@gmt2, @gmt1)
);
return join(', ', grep { $_ } (
$D_y ? sprintf('%d year%s' , $D_y, $D_y > 1 ? 's' : '') : '',
$D_m ? sprintf('%d month%s' , $D_m, $D_m > 1 ? 's' : '') : '',
$D_d ? sprintf('%d day%s' , $D_d, $D_d > 1 ? 's' : '') : '',
sprintf ("%02dh%02dm%02ds", map { $_ || 0 } ($Dh, $Dm, $Ds)),
)
);
}
=head2 duration2m($duration)
Return in minutes a human readable value like 2d or 3h
=cut
sub duration2m {
my ($v) = @_;
if (my ($n, $u) = $v =~ /^(\d+)(\D)?/) {
for (lc($u || 'm')) {
/m/ and return $n;
/h/ and return $n * 60;
/d/ and return $n * 60 * 24;
/w/ and return $n * 60 * 24 * 7;
}
}
return $v;
}
=head2 setid($user, $group)
Change effective user and group to $user and optionnal $group.
Return arrayref containning old uid and gid on success. Return undef and
error message on failure.
=cut
sub setid {
my ($user, $group) = @_;
my ($uid, $gid);
if ($user
&& ($> == 0 || $< == 0)) { # if we're not root, we can only ignore this
if ($user =~ /^\d+$/) {
$uid = $user;
my @uinfo = POSIX::getpwuid($uid);
if (!scalar(@uinfo)) {
return(undef, sprintf('User %s don\'t exists', $uid));
}
$gid = $uinfo[3];
} else {
my @uinfo = POSIX::getpwnam($user);
if (scalar(@uinfo)) {
($uid, $gid) = ($uinfo[2], $uinfo[3]);
$group = $uinfo[3];
} else {
return(undef, sprintf('User %s don\'t exists', $user));
}
}
}
if ($group) {
if ($group =~ /^\d+$/) {
$gid = $group;
} else {
my @ginfo = POSIX::getgrnam($group);
if (scalar(@ginfo)) {
$gid = $ginfo[2];
} else {
return(undef, sprintf('group %s don\'t exists', $group));
}
}
}
my ($ouid, $ogid) = _setid($uid, $gid);
if(!defined($ogid)) {
return (undef, sprintf('Cannot change to group %s', $group));
}
if (!defined($ouid)) {
return (undef, sprintf('Cannot become user %s', $user));
}
return([$ouid, $ogid]);
}
sub _setid {
my ($uid, $gid) = @_;
my ($olduid, $oldgid) = ($>, $));
if (defined($gid) && $) != $gid) {
$) = $gid;
if ($) != $gid) {
$oldgid = undef;
}
}
if (defined($uid) && $> != $uid) {
$> = $uid;
if ($> != $uid) {
$olduid = undef;
}
}
return($olduid, $oldgid);
}
=head1 AUTHOR
( run in 0.724 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )