line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::FileStore::UtilityFunctions; |
2
|
3
|
|
|
3
|
|
47
|
use strict; |
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
96
|
|
3
|
3
|
|
|
3
|
|
11
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
74
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# utility functions for DBIx::FileStore |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
42
|
use base qw(Exporter); |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
1357
|
|
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw( convert_bytes_to_human_size get_date_and_time get_user_homedir ); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
############################################ |
12
|
|
|
|
|
|
|
# converts bytes to human-readable |
13
|
|
|
|
|
|
|
# my $human_size = convert_bytes_to_human_size( 2341414 ); |
14
|
|
|
|
|
|
|
# yeilds ie 2.34M |
15
|
|
|
|
|
|
|
sub convert_bytes_to_human_size { |
16
|
0
|
|
|
0
|
1
|
|
my $bytes = shift; |
17
|
0
|
0
|
|
|
|
|
return unless defined( $bytes ); |
18
|
0
|
|
|
|
|
|
my $precision = 2; # in more advanced versions we let the caller optionally specify this. |
19
|
0
|
|
|
|
|
|
my $k_size = 1024; |
20
|
0
|
0
|
|
|
|
|
return "?" unless ($bytes =~ s/ ^ \s* (\d+( \. \d* )?) $//x ); # negative bytes not allowed |
21
|
0
|
|
|
|
|
|
$bytes = $1; # pull out the cleaned up version |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# this should be factored out |
24
|
0
|
|
|
|
|
|
my @table = ( [ $k_size**5, "P" ], |
25
|
|
|
|
|
|
|
[ $k_size**4, "T" ], |
26
|
|
|
|
|
|
|
[ $k_size**3, "G" ], |
27
|
|
|
|
|
|
|
[ $k_size**2, "M" ], |
28
|
|
|
|
|
|
|
[ $k_size, "K" ] ); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# convert to relevant units if bytecount is |
31
|
|
|
|
|
|
|
# larger than 1 unit of (P, T, G, M, or K) |
32
|
0
|
|
|
|
|
|
for my $row (@table) { |
33
|
0
|
0
|
|
|
|
|
if ($bytes > $row->[0]) { |
34
|
0
|
|
|
|
|
|
my $value = $bytes / $row->[0]; |
35
|
0
|
0
|
|
|
|
|
if ($value =~ /^\d+(\.0*)?$/) { # if it's .000* |
36
|
0
|
|
|
|
|
|
$value = int( $value ); # truncate fraction |
37
|
|
|
|
|
|
|
} else { |
38
|
0
|
|
|
|
|
|
$value = sprintf( "%.${precision}f", $value); # show to desired precision |
39
|
|
|
|
|
|
|
} |
40
|
0
|
|
|
|
|
|
return $value . $row->[1]; # return the value followed by the unit name, like 14.5G or 12M or 12.44K |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
0
|
|
|
|
|
|
return "${bytes}B"; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
############################################ |
47
|
|
|
|
|
|
|
# returns date and time, in the current TMZ and locale, |
48
|
|
|
|
|
|
|
# like '0000-00-00 00:00:00' |
49
|
|
|
|
|
|
|
sub get_date_and_time { |
50
|
0
|
|
0
|
0
|
1
|
|
my $t = shift || time(); |
51
|
0
|
|
|
|
|
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($t); |
52
|
0
|
|
|
|
|
|
return sprintf("%04d-%02d-%02d %02d:%02d:%02d", |
53
|
|
|
|
|
|
|
1900+$year, $mon+1, $mday, $hour, $min, $sec); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
############################################ |
57
|
|
|
|
|
|
|
# my $homedir = get_user_homedir(); |
58
|
|
|
|
|
|
|
# or |
59
|
|
|
|
|
|
|
# my $homedir = get_user_homedir( "username" ); |
60
|
|
|
|
|
|
|
############################################ |
61
|
|
|
|
|
|
|
sub get_user_homedir { |
62
|
0
|
|
0
|
0
|
1
|
|
my $user = shift || $ENV{USER}; |
63
|
0
|
|
|
|
|
|
my ( $name, $passwd, $uid, $gid, $quota, $comment, $gcos, $homedir, $shell, $expire ) = |
64
|
|
|
|
|
|
|
getpwnam( $user ); # get the userid and the homedir |
65
|
0
|
|
|
|
|
|
return $homedir; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=pod |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 NAME |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
DBIx::FileStore::UtilityFunctions -- Utility functions for DBIx::FileStore |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 SYNOPSIS |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# converts from bytes to something prettier like 10.1G. |
80
|
|
|
|
|
|
|
my $size = convert_bytes_to_human_size( $bytecount) |
81
|
|
|
|
|
|
|
print "12345678 bytes is " . convert_bytes_to_human_size(12345678) . "\n"; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# get a pretty string with the date and time, either for now |
84
|
|
|
|
|
|
|
# or the epoch-based time passed: |
85
|
|
|
|
|
|
|
my $date_string = get_date_and_time(); |
86
|
|
|
|
|
|
|
my $once_upon_a_time = get_date_and_time( 1 ); # 1 second into 1970GMT |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# homedir fetching... |
89
|
|
|
|
|
|
|
my $my_homedir = get_user_homedir(); |
90
|
|
|
|
|
|
|
my $bobs_homedir = get_user_homedir( "bob" ); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 DESCRIPTION |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Provides three functions: |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
get_user_homedir(), get_date_and_time(), and convert bytes_to_human_size(). |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 FUNCTIONS |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=over 4 |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=item my $size = convert_bytes_to_human_size( $bytecount ) |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Converts an integer (like 5 or 10100) into a string for display |
106
|
|
|
|
|
|
|
like 5B, 10.1K, 20.7M, or 33G. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item my $date_string = get_date_and_time( $optional_time ); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Returns a string with the date and time, either for now |
111
|
|
|
|
|
|
|
or the epoch-based $time passed. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item my $homedir = get_user_homedir( $optional_username ); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Returns the home directory for the current user, |
116
|
|
|
|
|
|
|
or the one whose name is passed. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=back |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 COPYRIGHT |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Copyright (c) 2010-2015 Josh Rabinowitz, All Rights Reserved. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 AUTHORS |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Josh Rabinowitz |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|