/usr/lib/slimrat/Toolbox.pm is in slimrat-nox 1.0-1.
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 | # slimrat - several frequently-used subroutines
#
# Copyright (c) 2008-2009 Přemek Vyhnal
#
# This file is part of slimrat, an open-source Perl scripted
# command line and GUI utility for downloading files from
# several download providers.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# Authors:
# Přemek Vyhnal <premysl.vyhnal gmail com>
#
#
# Configuration
#
# Package name
package Toolbox;
# Packages
use threads;
use File::Spec;
# Custom packages
use Configuration;
# Export functionality
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(indexof timestamp bytes_readable seconds_readable readable2bytes thread_id wait rel2abs);
# Write nicely
use strict;
use warnings;
# Base configuration
my $config = new Configuration;
$config->set_default("skip_waits", 0);
#
# Static functionality
#
# Configure the package
sub configure($) {
my $complement = shift;
$config->merge($complement);
}
# Look for the index of an item in an array (non-numeric contents)
sub indexof {
my ($value, $arrayref) = (shift, shift);
return -1 unless $arrayref;
foreach my $i (0 .. @$arrayref-1) {
return $i if $$arrayref[$i] eq $value;
}
return -1;
}
# Generate a timestamp
sub timestamp {
my ($sec,$min,$hour) = localtime;
sprintf "[%02d:%02d:%02d] ",$hour,$min,$sec;
}
# Convert a raw amount of bytes to a more human-readable form
sub bytes_readable {
my $bytes = shift;
return "unknown" if ($bytes == -1);
my $bytes_hum = "$bytes";
if ($bytes>=2**30) { $bytes_hum = ($bytes / 2**30) . " GB" }
elsif ($bytes>=2**20) { $bytes_hum = ($bytes / 2**20) . " MB" }
elsif ($bytes>=2**10) { $bytes_hum = ($bytes / 2**10) . " KB" }
else { $bytes_hum = $bytes . " B" }
$bytes_hum =~ s/(^\d{1,}\.\d{2})(\d*)(.+$)/$1$3/;
return $bytes_hum;
}
# Return seconds in m:ss format
sub seconds_readable {
my $input = shift || return "0:00";
return "unknown" if ($input == -1);
my $seconds = $input % 60;
$input /= 60;
my $minutes = $input % 60;
$input /= 60;
my $hours = int($input);
if ($hours) {
return sprintf('%dh %d:%02d', $hours, $minutes, $seconds);
} else {
return sprintf('%d:%02d', $minutes, $seconds);
}
}
# Converts human readable size to bytes
# Case insensitive because most sites don't respect
# SI conventions (k=K=2**10, but b=bit != B=byte)
sub readable2bytes {
$_ = shift;
s/\s+//g;
s/(\d+),(\d+)/$1.$2/;
my %mul = (K=>2**10, M=>2**20, G=>2**30);
if (/(\d+(?:\.\d+)?)([KMG])B?/i) { return $1 * $mul{uc($2)} }
elsif (/(\d+(?:\.\d+)?)B?/i) { return $1 }
else {return 0}
}
# Generate a per-thread ID
sub thread_id {
my $thr = threads->self();
my $tid = $thr->tid();
return $tid;
}
# Wait a while
# $1: seconds to wait
# $2: this wait can be skipped if true
sub wait {
my $wait = shift or return;
return if (shift and $config->get("skip_waits"));
require Log;
Log::info(sprintf("Waiting ".seconds_readable($wait)));
#sleep($wait);
# TODO: sleep() postpones SIG_INT till end of sleep
while ($wait) {
$wait -= 1;
sleep(1);
}
}
sub rel2abs {
return File::Spec->rel2abs(@_);
}
# Return
1;
|