App-MtAws
view release on metacpan or search on metacpan
lib/App/MtAws/Utils.pm view on Meta::CPAN
}x;
1;
}
# TODO: test
sub binary_abs_path
{
my ($path) = @_;
local $SIG{__WARN__}=sub{};
my $orig_id = file_inodev($path, use_filename_encoding => 0);
my $abspath = Cwd::abs_path($path);
return undef unless defined $abspath;
return undef if $abspath eq ''; # workaround RT#47755
# workaround RT#47755 - in case perms problem it tries to return File::Spec->rel2abs
return undef unless -e $abspath && file_inodev($abspath, use_filename_encoding => 0) eq $orig_id;
return $abspath;
}
our $_filename_encoding = 'UTF-8'; # global var
sub set_filename_encoding($) { $_filename_encoding = shift };
sub get_filename_encoding() { $_filename_encoding || confess };
sub binaryfilename(;$)
{
encode(get_filename_encoding, @_ ? shift : $_, Encode::DIE_ON_ERR|Encode::LEAVE_SRC);
}
sub characterfilename(;$)
{
decode(get_filename_encoding, @_ ? shift : $_, Encode::DIE_ON_ERR|Encode::LEAVE_SRC);
}
# TODO: test
sub abs2rel
{
my ($path, $base) = (shift, shift);
confess "too few arguments" unless defined($path) && defined($base);
my (%args) = (use_filename_encoding => 1, @_);
if ($args{use_filename_encoding}) {
$path = binaryfilename $path;
$base = binaryfilename $base;
}
$args{allow_rel_base} or $base =~ m{^/} or confess "relative basedir not allowed";
my $result = File::Spec->abs2rel($path, $base);
$args{use_filename_encoding} ? characterfilename($result) : $result;
}
=head1 open_file(my $f, $filename, %args)
$args{mode} - mode to open, <, > or >>
$args{use_filename_encoding} - (TRUE) - encode to binary string, (FALSE) - don't tocuh (already a binary string). Default TRUE
$args{file_encoding} or $args{binary} - file content encoding or it's a binary file (mutual exclusive)
$args{not_empty} - assert that file is not empty after open
Assertions made (using "confess"):
1) Bad arguments (programmer's error)
2) File is not a plain file
3) File is not a plain file, but after open (race conditions)
4) File is empty and not_empty specified
5) File is empty and not_empty specified, but after open (race conditions)
NOTE: If you want exceptions for (2) and (4) - check it before open_file. And additional checks inside open_file will
prevent race conditions
=cut
sub open_file($$%)
{
(undef, my $filename, my %args) = @_;
%args = (use_filename_encoding => 1, %args);
my $original_filename = $filename;
my %checkargs = %args;
defined $checkargs{$_} && delete $checkargs{$_} for qw/use_filename_encoding mode file_encoding not_empty binary/;
confess "Unknown argument(s) to open_file: ".join(';', keys %checkargs) if %checkargs;
confess 'Argument "mode" is required' unless defined($args{mode});
confess "unknown mode $args{mode}" unless $args{mode} =~ m!^\+?(<|>>?)$!;
my $mode = $args{mode};
confess "not_empty can be used in read mode only"
if ($args{not_empty} && $args{mode} ne '<');
if (defined($args{file_encoding})) {
$mode .= ":encoding($args{file_encoding})";
confess "cannot use binary and file_encoding at same time'" if $args{binary};
} elsif (!$args{binary}) {
confess "there should be file encoding or 'binary'";
}
if ($args{use_filename_encoding}) {
$filename = binaryfilename $filename;
}
confess "File is not a plain file" if -e $filename && (! -f $filename);
confess "File should not be empty" if $args{not_empty} && (! -s $filename);
open ($_[0], $mode, $filename) or return;
my $f = $_[0];
confess unless -f $f; # check for race condition - it was a file when we last checked, but now it's a directory
confess if $args{not_empty} && (! -s $f);
binmode $f if $args{binary};
return $f;
}
sub file_size($%)
{
my $filename = shift;
my (%args) = (use_filename_encoding => 1, @_);
if ($args{use_filename_encoding}) {
$filename = binaryfilename $filename;
}
confess "file not exists" unless -f $filename;
return -s $filename;
}
sub file_exists($%)
{
my $filename = shift;
my (%args) = (use_filename_encoding => 1, @_);
if ($args{use_filename_encoding}) {
$filename = binaryfilename $filename;
}
return -f $filename;
}
sub file_mtime($%)
{
my $filename = shift;
my (%args) = (use_filename_encoding => 1, @_);
if ($args{use_filename_encoding}) {
$filename = binaryfilename $filename;
}
confess "file not exists" unless -f $filename;
return stat($filename)->mtime;
}
# TODO: test
sub file_inodev($%)
{
my $filename = shift;
my (%args) = (use_filename_encoding => 1, @_);
if ($args{use_filename_encoding}) {
$filename = binaryfilename $filename;
}
confess "file not exists" unless -e $filename;
my $s = stat($filename);
$s->dev."-".$s->ino;
}
sub is_wide_string
{
defined($_[0]) && utf8::is_utf8($_[0]) && (bytes::length($_[0]) != length($_[0]))
}
# if we have ASCII-only data, let's drop UTF-8 flag in order to optimize some regexp stuff
# TODO: write also version which does not check is_utf8 - it's faster when utf8 always set
sub try_drop_utf8_flag
{
( run in 2.108 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )