/usr/share/perl5/CHI/Util.pm is in libchi-perl 0.60-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | package CHI::Util;
$CHI::Util::VERSION = '0.60';
use Carp qw( croak longmess );
use Module::Runtime qw(require_module);
use Data::Dumper;
use Data::UUID;
use Fcntl qw( :DEFAULT );
use File::Spec::Functions qw(catdir catfile);
use JSON::MaybeXS;
use Time::Duration::Parse;
use Try::Tiny;
use strict;
use warnings;
use base qw(Exporter);
our @EXPORT_OK = qw(
can_load
dump_one_line
fast_catdir
fast_catfile
has_moose_class
json_decode
json_encode
parse_duration
parse_memory_size
read_file
read_dir
unique_id
write_file
);
my $Fetch_Flags = O_RDONLY | O_BINARY;
my $Store_Flags = O_WRONLY | O_CREAT | O_BINARY;
sub can_load {
# Load $class_name if possible. Return 1 if successful, 0 if it could not be
# found, and rethrow load error (other than not found).
#
my ($class_name) = @_;
my $result;
try {
require_module($class_name);
$result = 1;
}
catch {
if ( /Can\'t locate .* in \@INC/ && !/Compilation failed/ ) {
$result = 0;
}
else {
die $_;
}
};
return $result;
}
sub dump_one_line {
my ($value) = @_;
return Data::Dumper->new( [$value] )->Indent(0)->Sortkeys(1)->Quotekeys(0)
->Terse(1)->Dump();
}
# Simplified read_dir cribbed from File::Slurp
sub read_dir {
my ($dir) = @_;
## no critic (RequireInitializationForLocalVars)
local *DIRH;
opendir( DIRH, $dir ) or croak "cannot open '$dir': $!";
return grep { $_ ne "." && $_ ne ".." } readdir(DIRH);
}
sub read_file {
my ($file) = @_;
# Fast slurp, adapted from File::Slurp::read, with unnecessary options removed
#
my $buf = "";
my $read_fh;
unless ( sysopen( $read_fh, $file, $Fetch_Flags ) ) {
croak "read_file '$file' - sysopen: $!";
}
my $size_left = -s $read_fh;
while (1) {
my $read_cnt = sysread( $read_fh, $buf, $size_left, length $buf );
if ( defined $read_cnt ) {
last if $read_cnt == 0;
$size_left -= $read_cnt;
last if $size_left <= 0;
}
else {
croak "read_file '$file' - sysread: $!";
}
}
return $buf;
}
sub write_file {
my ( $file, $data, $file_create_mode ) = @_;
$file_create_mode = oct(666) if !defined($file_create_mode);
# Fast spew, adapted from File::Slurp::write, with unnecessary options removed
#
{
my $write_fh;
unless ( sysopen( $write_fh, $file, $Store_Flags, $file_create_mode ) )
{
croak "write_file '$file' - sysopen: $!";
}
my $size_left = length($data);
my $offset = 0;
do {
my $write_cnt = syswrite( $write_fh, $data, $size_left, $offset );
unless ( defined $write_cnt ) {
croak "write_file '$file' - syswrite: $!";
}
$size_left -= $write_cnt;
$offset += $write_cnt;
} while ( $size_left > 0 );
}
}
{
# For efficiency, use Data::UUID to generate an initial unique id, then suffix it to
# generate a series of 0x10000 unique ids. Not to be used for hard-to-guess ids, obviously.
my $uuid;
my $suffix = 0;
sub unique_id {
if ( !$suffix || !defined($uuid) ) {
my $ug = Data::UUID->new();
$uuid = $ug->create_hex();
}
my $hex = sprintf( '%s%04x', $uuid, $suffix );
$suffix = ( $suffix + 1 ) & 0xffff;
return $hex;
}
}
use constant _FILE_SPEC_USING_UNIX =>
( $File::Spec::ISA[0] eq 'File::Spec::Unix' );
sub fast_catdir {
if (_FILE_SPEC_USING_UNIX) {
return join '/', @_;
}
else {
return catdir(@_);
}
}
sub fast_catfile {
if (_FILE_SPEC_USING_UNIX) {
return join '/', @_;
}
else {
return catfile(@_);
}
}
my %memory_size_units = ( 'k' => 1024, 'm' => 1024 * 1024 );
sub parse_memory_size {
my $size = shift;
if ( $size =~ /^\d+b?$/ ) {
return $size;
}
elsif ( my ( $quantity, $unit ) = ( $size =~ /^(\d+)\s*([km])b?$/i ) ) {
return $quantity * $memory_size_units{ lc($unit) };
}
else {
croak "cannot parse memory size '$size'";
}
}
my $json = JSON::MaybeXS->new( utf8 => 1, canonical => 1 );
sub json_decode {
$json->decode( $_[0] );
}
sub json_encode {
$json->encode( $_[0] );
}
1;
__END__
|