Util.pm: refactor calc_windowmemory for reuse
[girocco.git] / Girocco / Util.pm
blob5cf4173765e81f87094342194cdfcde5c9c2f808
1 package Girocco::Util;
3 use 5.008;
4 use strict;
5 use warnings;
7 use Girocco::Config;
8 use Time::Local;
9 use Encode;
11 BEGIN {
12 use base qw(Exporter);
13 our @EXPORT = qw(get_git scrypt jailed_file sendmail_pipe mailer_pipe
14 lock_file unlock_file valid_tag rand_adjust
15 filedb_atomic_append filedb_atomic_edit filedb_grep
16 filedb_atomic_grep valid_email valid_email_multi
17 valid_repo_url valid_web_url url_base url_path url_server
18 projects_html_list parse_rfc2822_date parse_any_date
19 extract_url_hostname is_dns_hostname is_our_hostname
20 get_cmd online_cpus sys_pagesize sys_memsize
21 calc_windowmemory to_utf8 capture_command human_size
22 has_reserved_suffix noFatalsToBrowser);
25 my $encoder;
26 BEGIN {
27 $encoder = Encode::find_encoding('Windows-1252') ||
28 Encode::find_encoding('ISO-8859-1') or
29 die "failed to load ISO-8859-1 encoder\n";
32 sub to_utf8($;$) {
33 my ($str, $encode) = @_;
34 return undef unless defined $str;
35 my $ans;
36 if (Encode::is_utf8($str) || utf8::decode($str)) {
37 $ans = $str;
38 } else {
39 $ans = $encoder->decode($str, Encode::FB_DEFAULT);
41 utf8::encode($ans) if $encode;
42 return $ans;
45 BEGIN {require "Girocco/extra/capture_command.pl"}
47 # Return the entire output sent to stdout from running a command
48 # Any output the command sends to stderr is discarded
49 # Returns undef if there was an error running the command (see $!)
50 sub get_cmd {
51 my ($status, $result) = capture_command(1, undef, @_);
52 return defined($status) && $status == 0 ? $result : undef;
55 # Same as get_cmd except configured git binary is automatically provided
56 # as the first argument to get_cmd
57 sub get_git {
58 return get_cmd($Girocco::Config::git_bin, @_);
61 sub scrypt {
62 my ($pwd) = @_;
63 crypt($pwd||'', join ('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
66 sub jailed_file {
67 my ($filename) = @_;
68 $filename =~ s,^/,,;
69 $Girocco::Config::chroot."/$filename";
72 sub lock_file {
73 my ($path) = @_;
75 $path .= '.lock';
77 use Errno qw(EEXIST);
78 use Fcntl qw(O_WRONLY O_CREAT O_EXCL);
79 use IO::Handle;
80 my $handle = new IO::Handle;
82 unless (sysopen($handle, $path, O_WRONLY|O_CREAT|O_EXCL)) {
83 my $cnt = 0;
84 while (not sysopen($handle, $path, O_WRONLY|O_CREAT|O_EXCL)) {
85 ($! == EEXIST) or die "$path open failed: $!";
86 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
87 sleep(1);
90 # XXX: filedb-specific
91 chmod 0664, $path or die "$path g+w failed: $!";
93 $handle;
96 sub _is_passwd_file {
97 return defined($_[0]) && $_[0] eq jailed_file('/etc/passwd');
100 sub _run_update_pwd_db {
101 my ($path, $updatearg) = @_;
102 my @cmd = ($Girocco::Config::basedir.'/bin/update-pwd-db', "$path");
103 push(@cmd, $updatearg) if $updatearg;
104 system(@cmd) == 0 or die "update-pwd-db failed: $?";
107 sub unlock_file {
108 my ($path, $noreplace, $updatearg) = @_;
110 if (!$noreplace) {
111 _run_update_pwd_db("$path.lock", $updatearg)
112 if $Girocco::Config::update_pwd_db && _is_passwd_file($path);
113 rename "$path.lock", $path or die "$path unlock failed: $!";
114 } else {
115 unlink "$path.lock" or die "$path unlock failed: $!";
119 sub filedb_atomic_append {
120 my ($file, $line, $updatearg) = @_;
121 my $id = 65536;
123 open my $src, '<', $file or die "$file open for reading failed: $!";
124 my $dst = lock_file($file);
126 while (<$src>) {
127 my $aid = (split /:/)[2];
128 $id = $aid + 1 if ($aid >= $id);
130 print $dst $_ or die "$file(l) write failed: $!";
133 $line =~ s/\\i/$id/g;
134 print $dst "$line\n" or die "$file(l) write failed: $!";
136 close $dst or die "$file(l) close failed: $!";
137 close $src;
139 unlock_file($file, 0, $updatearg);
141 $id;
144 sub filedb_atomic_edit {
145 my ($file, $fn, $updatearg) = @_;
147 open my $src, '<', $file or die "$file open for reading failed: $!";
148 my $dst = lock_file($file);
150 while (<$src>) {
151 print $dst $fn->($_) or die "$file(l) write failed: $!";
154 close $dst or die "$file(l) close failed: $!";
155 close $src;
157 unlock_file($file, 0, $updatearg);
160 sub filedb_atomic_grep {
161 my ($file, $fn) = @_;
162 my @results = ();
164 open my $src, '<', $file or die "$file open for reading failed: $!";
165 my $dst = lock_file($file);
167 while (<$src>) {
168 my $result = $fn->($_);
169 push(@results, $result) if $result;
172 close $dst or die "$file(l) close failed: $!";
173 close $src;
175 unlock_file($file, 1);
176 return @results;
179 sub filedb_grep {
180 my ($file, $fn) = @_;
181 my @results = ();
183 open my $src, '<', $file or die "$file open for reading failed: $!";
185 while (<$src>) {
186 my $result = $fn->($_);
187 push(@results, $result) if $result;
190 close $src;
192 return @results;
195 sub valid_email {
196 my $email = shift;
197 defined($email) or $email = '';
198 return $email =~ /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+$/;
201 sub valid_email_multi {
202 my $email_multi = shift;
203 defined($email_multi) or $email_multi = '';
204 # More relaxed, we just want to avoid too dangerous characters.
205 return $email_multi =~ /^[a-zA-Z0-9+._, @-]+$/;
208 sub valid_web_url {
209 my $url = shift;
210 defined($url) or $url = '';
211 return $url =~
212 /^https?:\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~:?&=;-]*)?(#[a-zA-Z0-9._-]+)?$/;
215 sub valid_repo_url {
216 my $url = shift || '';
217 # Currently neither username nor password is allowed in the URL and IPv6
218 # literal addresses are not accepted either.
219 $Girocco::Config::mirror_svn &&
220 $url =~ /^svn(\+https?)?:\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/os
221 and return 1;
222 $Girocco::Config::mirror_darcs &&
223 $url =~ /^darcs:\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/os
224 and return 1;
225 $Girocco::Config::mirror_bzr &&
226 $url =~ /^bzr:\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/os
227 and return 1;
228 $Girocco::Config::mirror_hg &&
229 $url =~ /^hg\+https?:\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/os
230 and return 1;
231 return $url =~ /^(https?|git):\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/;
234 sub extract_url_hostname {
235 my $url = shift || '';
236 if ($url =~ m,^bzr://,) {
237 $url =~ s,^bzr://,,;
238 return 'launchpad.net' if $url =~ /^lp:/;
240 return undef unless $url =~ m,^[A-Za-z0-9+.-]+://[^/],;
241 $url =~ s,^[A-Za-z0-9+.-]+://,,;
242 $url =~ s,^([^/]+).*$,$1,;
243 $url =~ s/:[0-9]*$//;
244 $url =~ s/^[^@]*[@]//;
245 return $url ? $url : undef;
248 # See these RFCs:
249 # RFC 1034 section 3.5
250 # RFC 1123 section 2.1
251 # RFC 1738 section 3.1
252 # RFC 3986 section 3.2.2
253 sub is_dns_hostname {
254 my $host = shift;
255 defined($host) or $host = '';
256 return 0 if $host eq '' || $host =~ /\s/;
257 # first remove a trailing '.'
258 $host =~ s/\.$//;
259 return 0 if length($host) > 255;
260 my $octet = '(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])';
261 return 0 if $host =~ /^$octet\.$octet\.$octet\.$octet$/o;
262 my @labels = split(/[.]/, $host, -1);
263 return 0 unless @labels && @labels >= $Girocco::Config::min_dns_labels;
264 # now check each label
265 foreach my $label (@labels) {
266 return 0 unless length($label) > 0 && length($label) <= 63;
267 return 0 unless $label =~ /^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/;
269 return 1;
272 sub is_our_hostname {
273 my $test = shift || '';
274 $test =~ s/\.$//;
275 my %names = ();
276 my @urls = (
277 $Girocco::Config::gitweburl,
278 $Girocco::Config::gitwebfiles,
279 $Girocco::Config::webadmurl,
280 $Girocco::Config::bundlesurl,
281 $Girocco::Config::htmlurl,
282 $Girocco::Config::httppullurl,
283 $Girocco::Config::httpbundleurl,
284 $Girocco::Config::httpspushurl,
285 $Girocco::Config::gitpullurl,
286 $Girocco::Config::pushurl
288 foreach my $url (@urls) {
289 if ($url) {
290 my $host = extract_url_hostname($url);
291 if (defined($host)) {
292 $host =~ s/\.$//;
293 $names{lc($host)} = 1;
297 return $names{lc($test)} ? 1 : 0;
300 my %_badtags;
301 BEGIN {
302 %_badtags = (
303 about=>1, after=>1, all=>1, also=>1, an=>1, and=>1, another=>1, any=>1,
304 are=>1, as=>1, at=>1, be=>1, because=>1, been=>1, before=>1, being=>1,
305 between=>1, both=>1, but=>1, by=>1, came=>1, can=>1, come=>1, could=>1,
306 did=>1, do=>1, each=>1, for=>1, from=>1, get=>1, got=>1, had=>1, has=>1,
307 have=>1, he=>1, her=>1, here=>1, him=>1, himself=>1, his=>1, how=>1,
308 if=>1, in=>1, into=>1, is=>1, it=>1, like=>1, make=>1, many=>1, me=>1,
309 might=>1, more=>1, most=>1, much=>1, must=>1, my=>1, never=>1, now=>1,
310 of=>1, on=>1, only=>1, or=>1, other=>1, our=>1, out=>1, over=>1,
311 said=>1, same=>1, see=>1, should=>1, since=>1, some=>1, still=>1,
312 such=>1, take=>1, than=>1, that=>1, the=>1, their=>1, them=>1, then=>1,
313 there=>1, these=>1, they=>1, this=>1, those=>1, through=>1, to=>1,
314 too=>1, under=>1, up=>1, very=>1, was=>1, way=>1, we=>1, well=>1,
315 were=>1, what=>1, where=>1, which=>1, while=>1, who=>1, with=>1,
316 would=>1, you=>1, your=>1
320 # A valid tag must only have [a-zA-Z0-9:.+#_-] characters, must start with a
321 # letter, must not be a noise word and except for 'C' must be more than one
322 # character long and no more than 32 characters long.
323 sub valid_tag {
324 local $_ = $_[0] || '';
325 return 1 if $_ eq 'C'; # Currently only allowed single letter tag
326 return 0 unless /^[a-zA-Z][a-zA-Z0-9:.+#_-]+$/;
327 return 0 if $_badtags{lc($_)};
328 return length($_) <= 32 ? 1 : 0;
331 # If the passed in argument looks like a URL, return only the stuff up through
332 # the host:port part otherwise return the entire argument.
333 sub url_base {
334 my $url = shift || '';
335 # See RFC 3968
336 $url = $1.$2.$3.$4 if $url =~ m,^( [A-Za-z][A-Za-z0-9+.-]*: ) # scheme
337 ( // ) # // separator
338 ((?:[^\@]+\@)?) # optional userinfo
339 ( [^/?#]+ ) # host and port
340 (?:[/?#].*)?$,x; # path and optional query string and/or anchor
341 return $url;
344 # If the passed in argument looks like a URL, return only the stuff following
345 # the host:port part otherwise return the entire argument.
346 # If the optional second argument is true, the returned value will have '/'
347 # appended if it does not already end in '/'.
348 sub url_path {
349 my $url = shift || '';
350 my $add_slash = shift || 0;
351 # See RFC 3968
352 $url = $1 if $url =~ m,^(?: [A-Za-z][A-Za-z0-9+.-]*: ) # scheme
353 (?: // ) # // separator
354 (?: [^\@]+\@ )? # optional userinfo
355 (?: [^/?#]+ ) # host and port
356 ((?:[/?#].*)?)$,x; # path and optional query string and/or anchor
357 $url .= '/' if $add_slash && $url !~ m|/$|;
358 return $url;
361 # If both SERVER_NAME and SERVER_PORT are set pass the argument through url_path
362 # and then prefix it with the appropriate scheme (HTTPS=?on), host and port and
363 # return it. If a something that doesn't look like it could be the start of a
364 # URL path comes back from url_path or SERVER_NAME is a link-local IPv6 address
365 # then just return the argument unchanged.
366 sub url_server {
367 my $url = shift || '';
368 my $path = url_path($url);
369 return $url unless $path eq '' || $path =~ m|^[/?#]|;
370 return $url unless $ENV{'SERVER_NAME'} && $ENV{'SERVER_PORT'} &&
371 $ENV{'SERVER_PORT'} =~ /^[1-9][0-9]{0,4}$/;
372 return $url if $ENV{'SERVER_NAME'} =~ /^[[]?fe80:/i;
373 my $server = $ENV{'SERVER_NAME'};
374 # Deal with Apache bug where IPv6 literal server names do not include
375 # the required surrounding '[' and ']' characters
376 $server = '[' . $server . ']' if $server =~ /:/ && $server !~ /^[[]/;
377 my $ishttps = $ENV{'HTTPS'} && $ENV{'HTTPS'} =~ /^on$/i;
378 my $portnum = 0 + $ENV{'SERVER_PORT'};
379 my $port = '';
380 if (($ishttps && $portnum != 443) || (!$ishttps && $portnum != 80)) {
381 $port = ':' . $portnum;
383 return 'http' . ($ishttps ? 's' : '') . '://' . $server . $port . $path;
386 # Returns the number rounded to the nearest tenths. The ".d" part will be
387 # excluded if it's ".0" unless the optional second argument is true
388 sub _tenths {
389 my $v = shift;
390 my $use0 = shift;
391 $v *= 10;
392 $v += 0.5;
393 $v = int($v);
394 return '' . int($v/10) unless $v % 10 || $use0;
395 return '' . int($v/10) . '.' . ($v%10);
398 # Returns a human-readable size string (e.g. '1.5 MiB') for the value
399 # (in bytes) passed in. Returns '0' for undefined or 0 or not all digits.
400 # Otherwise returns '1 KiB' for < 1024, or else a number rounded to the
401 # nearest tenths of a KiB, MiB or GiB.
402 sub human_size {
403 my $v = shift || 0;
404 return "0" unless $v && $v =~ /^\d+$/;
405 return "1 KiB" unless $v > 1024;
406 $v /= 1024;
407 return _tenths($v) . " KiB" if $v < 1024;
408 $v /= 1024;
409 return _tenths($v) . " MiB" if $v < 1024;
410 $v /= 1024;
411 return _tenths($v) . " GiB";
414 sub _escapeHTML {
415 my $str = shift;
416 $str =~ s/\&/\&amp;/gs;
417 $str =~ s/\</\&lt;/gs;
418 $str =~ s/\>/\&gt;/gs;
419 $str =~ s/\"/\&quot;/gs; #"
420 return $str;
423 # create relative time string from passed in age in seconds
424 sub _rel_age {
425 my $age = shift;
426 my $age_str;
428 if ($age > 60*60*24*365*2) {
429 $age_str = (int $age/60/60/24/365);
430 $age_str .= " years ago";
431 } elsif ($age > 60*60*24*(365/12)*2) {
432 $age_str = int $age/60/60/24/(365/12);
433 $age_str .= " months ago";
434 } elsif ($age > 60*60*24*7*2) {
435 $age_str = int $age/60/60/24/7;
436 $age_str .= " weeks ago";
437 } elsif ($age > 60*60*24*2) {
438 $age_str = int $age/60/60/24;
439 $age_str .= " days ago";
440 } elsif ($age > 60*60*2) {
441 $age_str = int $age/60/60;
442 $age_str .= " hours ago";
443 } elsif ($age > 60*2) {
444 $age_str = int $age/60;
445 $age_str .= " mins ago";
446 } elsif ($age > 2) {
447 $age_str = int $age;
448 $age_str .= " secs ago";
449 } elsif ($age >= 0) {
450 $age_str = "right now";
451 } else {
452 $age_str = "future time";
454 return $age_str;
457 # create relative time string from passed in idle in seconds
458 sub _rel_idle {
459 my $idle_str = _rel_age(shift);
460 $idle_str =~ s/ ago//;
461 $idle_str = "not at all" if $idle_str eq "right now";
462 return $idle_str;
465 sub _strftime {
466 use POSIX qw(strftime);
467 my ($fmt, $secs, $zonesecs) = @_;
468 my ($S,$M,$H,$d,$m,$y) = gmtime($secs + $zonesecs);
469 $zonesecs = int($zonesecs / 60);
470 $fmt =~ s/%z/\$z/g;
471 my $ans = strftime($fmt, $S, $M, $H, $d, $m, $y, -1, -1, -1);
472 my $z;
473 if ($zonesecs < 0) {
474 $z = "-";
475 $zonesecs = -$zonesecs;
476 } else {
477 $z = "+";
479 $z .= sprintf("%02d%02d", int($zonesecs/60), $zonesecs % 60);
480 $ans =~ s/\$z/$z/g;
481 return $ans;
484 # Take a list of project names and produce a nicely formated table that
485 # includes owner links and descriptions. If the list is empty returns ''.
486 # The first argument may be a hash ref that contains options. The following
487 # options are available:
488 # target -- sets the target value of the owner link
489 # emptyok -- if true returns an empty table rather than ''
490 # sizecol -- if true include a human-readable size column
491 # typecol -- if true include type column with hover info
492 # changed -- if true include a changed and idle column
493 sub projects_html_list {
494 my $options = {};
495 if (defined($_[0]) && ref($_[0]) eq 'HASH') {
496 $options = shift;
498 return '' unless @_ || (defined($options->{emptyok}) && $options->{emptyok});
499 require Girocco::Project;
500 my $count = 0;
501 my $target = '';
502 $target = " target=\""._escapeHTML($options->{target})."\""
503 if defined($options->{target});
504 my $withsize = defined($options->{sizecol}) && $options->{sizecol};
505 my $withtype = defined($options->{typecol}) && $options->{typecol};
506 my $withchanged = defined($options->{changed}) && $options->{changed};
507 my $sizehead = '';
508 $sizehead = substr(<<EOT, 0, -1) if $withsize;
509 <th class="sizecol"><span class="hover">Size<span><span class="head">Size</span
510 />Fork size excludes objects borrowed from the parent.</span></span></th
513 my $typehead = '';
514 $typehead = '<th>Type</th>' if $withtype;
515 my $chghead = '';
516 $chghead = substr(<<EOT, 0, -1) if $withchanged;
517 <th><span class="hover">Changed<span><span class="head">Changed</span
518 />The last time a ref change was received by this site.</span></span></th
519 ><th><span class="hover">Idle<span><span class="head">Idle</span
520 />The most recent committer time in <i>refs/heads</i>.</span></span></th
523 my $html = <<EOT;
524 <table class='projectlist'><tr><th>Project</th>$sizehead$typehead$chghead<th class="desc">Description</th></tr>
526 my $trclass = ' class="odd"';
527 foreach (sort({lc($a) cmp lc($b)} @_)) {
528 if (Girocco::Project::does_exist($_, 1)) {
529 my $proj = Girocco::Project->load($_);
530 my $projname = $proj->{name}.".git";
531 my $projdesc = $proj->{desc}||'';
532 utf8::decode($projdesc) if utf8::valid($projdesc);
533 my $sizecol = '';
534 if ($withsize) {
535 my $psize = $proj->{reposizek};
536 $psize = undef unless defined($psize) && $psize =~ /^\d+$/;
537 $psize = 0 if !defined($psize) && $proj->is_empty;
538 if (!defined($psize)) {
539 $psize = 'unknown';
540 } elsif (!$psize) {
541 $psize = 'empty';
542 } else {
543 $psize = human_size($psize * 1024);
544 $psize =~ s/ /\&#160;/g;
546 $sizecol = '<td class="sizecol">'.$psize.'</td>';
548 my $typecol = '';
549 if ($withtype) {
550 if ($proj->{mirror}) {
551 $typecol = substr(<<EOT, 0, -1);
552 <td class="type"><span class="hover">mirror<span class="nowrap">@{[_escapeHTML($proj->{url})]}</span></span></td>
554 } else {
555 my $users = @{$proj->{users}};
556 $users .= ' user';
557 $users .= 's' unless @{$proj->{users}} == 1;
558 my $userlist = join(', ', sort({lc($a) cmp lc($b)} @{$proj->{users}}));
559 my $spncls = length($userlist) > 25 ? '' : ' class="nowrap"';
560 $typecol = $userlist ? substr(<<EOT, 0, -1) : substr(<<EOT, 0, -1);
561 <td class="type"><span class="hover">$users<span$spncls>$userlist</span></span></td>
563 <td class="type">$users</td>
567 my $changecol = '';
568 if ($withchanged) {
569 my $rel = '';
570 my $changetime = $proj->{lastchange};
571 if ($changetime) {
572 $rel = "<span class=\"hover\">" .
573 _rel_age(time - parse_rfc2822_date($changetime)) .
574 "<span class=\"nowrap\">$changetime</span></span>";
575 } else {
576 $rel = "no commits";
578 $changecol = substr(<<EOT, 0, -1);
579 <td class="change">$rel</td>
581 my $idletime = $proj->{lastactivity};
582 my ($idlesecs, $tz);
583 $idlesecs = parse_any_date($idletime, \$tz) if $idletime;
584 if ($idlesecs) {
585 my $idle2822 = _strftime("%a, %d %b %Y %T %z", $idlesecs, $tz);
586 $rel = "<span class=\"hover\">" .
587 _rel_idle(time - $idlesecs) .
588 "<span class=\"nowrap\">$idle2822</span></span>";
589 } else {
590 $rel = "no commits";
592 $changecol .= substr(<<EOT, 0, -1);
593 <td class="idle">$rel</td>
596 $html .= <<EOT;
597 <tr$trclass><td><a href="@{[url_path($Girocco::Config::gitweburl)]}/$projname"$target
598 >@{[_escapeHTML($projname)]}</td>$sizecol$typecol$changecol<td>@{[_escapeHTML($projdesc)]}</td></tr>
600 $trclass = $trclass ? '' : ' class="odd"';
601 ++$count;
604 $html .= <<EOT;
605 </table>
607 return ($count || (defined($options->{emptyok}) && $options->{emptyok})) ? $html : '';
610 my %_month_names;
611 BEGIN {
612 %_month_names = (
613 jan => 0, feb => 1, mar => 2, apr => 3, may => 4, jun => 5,
614 jul => 6, aug => 7, sep => 8, oct => 9, nov => 10, dec => 11
618 # Should be in "date '+%a, %d %b %Y %T %z'" format as saved to lastgc, lastrefresh and lastchange
619 # The leading "%a, " is optional, returns undef if unrecognized date. This is also known as
620 # RFC 2822 date format and git's '%cD', '%aD' and --date=rfc2822 format.
621 # If the second argument is a SCALAR ref, its value will be set to the TZ offset in seconds
622 sub parse_rfc2822_date {
623 my $dstr = shift || '';
624 my $tzoff = shift || '';
625 $dstr = $1 if $dstr =~/^[^\s]+,\s*(.*)$/;
626 return undef unless $dstr =~
627 /^\s*(\d{1,2})\s+([A-Za-z]{3})\s+(\d{4})\s+(\d{1,2}):(\d{2}):(\d{2})\s+([+-]\d{4})\s*$/;
628 my ($d,$b,$Y,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7);
629 my $m = $_month_names{lc($b)};
630 return undef unless defined($m);
631 my $seconds = timegm(0+$S, 0+$M, 0+$H, 0+$d, 0+$m, $Y-1900);
632 my $offset = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
633 $offset = -$offset if substr($z,0,1) eq '-';
634 $$tzoff = $offset if ref($tzoff) eq 'SCALAR';
635 return $seconds - $offset;
638 # Will parse any supported date format. Actually there are three formats
639 # currently supported:
640 # 1. RFC 2822 (uses parse_rfc2822_date)
641 # 2. RFC 3339 / ISO 8601 (T may be ' ' or '_', 'Z' is optional, ':' optional in TZ)
642 # 3. Same as #2 except no colons or hyphens allowed and hours MUST be 2 digits
643 # 4. unix seconds since epoch with optional +/- trailing TZ (may not have a ':')
644 # Returns undef if unsupported date.
645 # If the second argument is a SCALAR ref, its value will be set to the TZ offset in seconds
646 sub parse_any_date {
647 my $dstr = shift || '';
648 my $tzoff = shift || '';
649 if ($dstr =~ /^\s*([-+]?\d+)(?:\s+([-+]\d{4}))?\s*$/) {
650 # Unix timestamp
651 my $ts = 0 + $1;
652 my $off = 0;
653 if ($2) {
654 my $z = $2;
655 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
656 $off = -$off if substr($z,0,1) eq '-';
658 $$tzoff = $off if ref($tzoff) eq 'SCALAR';
659 return $ts;
661 if ($dstr =~ /^\s*(\d{4})-(\d{2})-(\d{2})[Tt _](\d{1,2}):(\d{2}):(\d{2})(?:[ _]?([Zz]|(?:[-+]\d{1,2}:?\d{2})))?\s*$/ ||
662 $dstr =~ /^\s*(\d{4})(\d{2})(\d{2})[Tt _](\d{2})(\d{2})(\d{2})(?:[ _]?([Zz]|(?:[-+]\d{2}\d{2})))?\s*$/) {
663 my ($Y,$m,$d,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7||'');
664 my $seconds = timegm(0+$S, 0+$M, 0+$H, 0+$d, $m-1, $Y-1900);
665 defined($z) && $z ne '' or $z = 'Z';
666 $z =~ s/://;
667 substr($z,1,0) = '0' if length($z) == 4;
668 my $off = 0;
669 if (uc($z) ne 'Z') {
670 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
671 $off = -$off if substr($z,0,1) eq '-';
673 $$tzoff = $off if ref($tzoff) eq 'SCALAR';
674 return $seconds - $off;
676 return parse_rfc2822_date($dstr, $tzoff);
679 # Input is a number such as a minute interval
680 # Return value is a random number between the input and 1.25*input
681 # This can be used to randomize the update and gc operations a bit to avoid
682 # having them all end up all clustered together
683 sub rand_adjust {
684 my $input = shift || 0;
685 return $input unless $input;
686 return $input + int(rand(0.25 * $input));
689 # Open a pipe to a new sendmail process. The '-i' option is always passed to
690 # the new process followed by any addtional arguments passed in. Note that
691 # the sendmail process is only expected to understand the '-i', '-t' and '-f'
692 # options. Using any other options via this function is not guaranteed to work.
693 # A list of recipients may follow the options. Combining a list of recipients
694 # with the '-t' option is not recommended.
695 sub sendmail_pipe {
696 return undef unless @_;
697 die "\$Girocco::Config::sendmail_bin is unset or not executable!\n"
698 unless $Girocco::Config::sendmail_bin && -x $Girocco::Config::sendmail_bin;
699 my $result = open(my $pipe, '|-', $Girocco::Config::sendmail_bin, '-i', @_);
700 return $result ? $pipe : undef;
703 # Open a pipe that works similarly to a mailer such as /usr/bin/mail in that
704 # if the first argument is '-s', a subject line will be automatically added
705 # (using the second argument as the subject). Any remaining arguments are
706 # expected to be recipient addresses that will be added to an explicit To:
707 # line as well as passed on to sendmail_pipe. In addition an
708 # "Auto-Submitted: auto-generated" header is always added as well as a suitable
709 # "From:" header.
710 sub mailer_pipe {
711 my $subject = undef;
712 if (@_ >= 2 && $_[0] eq '-s') {
713 shift;
714 $subject = shift;
716 my $tolist = join(", ", @_);
717 unshift(@_, '-f', $Girocco::Config::sender) if $Girocco::Config::sender;
718 my $pipe = sendmail_pipe(@_);
719 if ($pipe) {
720 print $pipe "From: \"$Girocco::Config::name\" ",
721 "($Girocco::Config::title) ",
722 "<$Girocco::Config::admin>\n";
723 print $pipe "To: $tolist\n";
724 print $pipe "Subject: $subject\n" if defined($subject);
725 print $pipe "MIME-Version: 1.0\n";
726 print $pipe "Content-Type: text/plain; charset=utf-8\n";
727 print $pipe "Content-Transfer-Encoding: 8bit\n";
728 print $pipe "Auto-Submitted: auto-generated\n";
729 print $pipe "\n";
731 return $pipe;
734 sub _goodval {
735 my $val = shift;
736 return undef unless defined($val);
737 $val =~ s/[\r\n]+$//s;
738 return undef unless $val =~ /^\d+$/;
739 $val = 0 + $val;
740 return undef unless $val >= 1;
741 return $val;
744 # Returns the number of "online" cpus or undef if undetermined
745 sub online_cpus {
746 my @confcpus = $^O eq "linux" ?
747 qw(_NPROCESSORS_ONLN NPROCESSORS_ONLN) :
748 qw(NPROCESSORS_ONLN _NPROCESSORS_ONLN) ;
749 my $cpus = _goodval(get_cmd('getconf', $confcpus[0]));
750 return $cpus if $cpus;
751 $cpus = _goodval(get_cmd('getconf', $confcpus[1]));
752 return $cpus if $cpus;
753 if ($^O ne "linux") {
754 my @sysctls = qw(hw.ncpu);
755 unshift(@sysctls, qw(hw.availcpu)) if $^O eq "darwin";
756 foreach my $mib (@sysctls) {
757 $cpus = _goodval(get_cmd('sysctl', '-n', $mib));
758 return $cpus if $cpus;
761 return undef;
764 # Returns the system page size in bytes or undef if undetermined
765 # This should never fail on a POSIX system
766 sub sys_pagesize {
767 use POSIX ":unistd_h";
768 my $pagesize = sysconf(_SC_PAGESIZE);
769 return undef unless defined($pagesize) && $pagesize =~ /^\d+$/;
770 $pagesize = 0 + $pagesize;
771 return undef unless $pagesize >= 256;
772 return $pagesize;
775 # Returns the amount of available physical memory in bytes
776 # This may differ from the actual amount of physical memory installed
777 # Returns undef if this cannot be determined
778 sub sys_memsize {
779 my $pagesize = sys_pagesize;
780 if ($pagesize && $^O eq "linux") {
781 my $pages = _goodval(get_cmd('getconf', '_PHYS_PAGES'));
782 return $pagesize * $pages if $pages;
784 if ($^O ne "linux") {
785 my @sysctls = qw(hw.physmem64);
786 unshift(@sysctls, qw(hw.memsize)) if $^O eq "darwin";
787 foreach my $mib (@sysctls) {
788 my $memsize = _goodval(get_cmd('sysctl', '-n', $mib));
789 return $memsize if $memsize;
791 my $memsize32 = _goodval(get_cmd('sysctl', '-n', 'hw.physmem'));
792 return $memsize32 if $memsize32 && $memsize32 <= 2147483647;
793 if ($pagesize) {
794 my $pages = _goodval(get_cmd('sysctl', '-n', 'hw.availpages'));
795 return $pagesize * $pages if $pages;
797 return 2147483647 + 1 if $memsize32;
799 return undef;
802 sub _get_max_conf_suffixed_size {
803 my $conf = shift;
804 return undef unless defined $conf && $conf =~ /^(\d+)([kKmMgG]?)$/;
805 my ($val, $suffix) = (0+$1, lc($2));
806 $val *= 1024 if $suffix eq 'k';
807 $val *= 1024 * 1024 if $suffix eq 'm';
808 $val *= 1024 * 1024 * 1024 if $suffix eq 'g';
809 return $val;
812 sub _make_suffixed_size {
813 my $size = shift;
814 return $size if $size % 1024;
815 $size /= 1024;
816 return "${size}k" if $size % 1024;
817 $size /= 1024;
818 return "${size}m" if $size % 1024;
819 $size /= 1024;
820 return "${size}g";
823 # Return the value to pass to --window-memory= for git repack
824 # If the system memory or number of CPUs cannot be determined, returns "1g"
825 # Otherwise returns half the available memory divided by the number of CPUs
826 # but never more than 1 gigabyte or max_gc_window_memory_size.
827 sub calc_windowmemory {
828 my $cpus = online_cpus;
829 my $memsize = sys_memsize;
830 my $max = 1024 * 1024 * 1024;
831 if ($cpus && $memsize) {
832 $max = int($memsize / 2 / $cpus);
833 $max = 1024 * 1024 * 1024 if $max >= 1024 * 1024 * 1024;
835 my $maxconf = _get_max_conf_suffixed_size($Girocco::Config::max_gc_window_memory_size);
836 $max = $maxconf if defined($maxconf) && $maxconf && $max > $maxconf;
837 return _make_suffixed_size($max);
840 # $1 => thing to test
841 # $2 => optional directory, if given and -e "$2/$1$3", then return false
842 # $3 => optional, defaults to ''
843 sub has_reserved_suffix {
844 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
845 my ($name, $dir, $ext) = @_;
846 $ext = '' unless defined $ext;
847 return 0 unless defined $name && $name =~ /\.([^.]+)$/;
848 return 0 unless exists $Girocco::Config::reserved_suffixes{lc($1)};
849 return 0 if defined $dir && -e "$dir/$name$ext";
850 return 1;
853 # undoes effect of `use CGI::Carp qw(fatalsToBrowser);`
854 # undoes effect of `use CGI::Carp qw(warningsToBrowser);`
855 sub noFatalsToBrowser {
856 use Carp qw();
857 delete $SIG{__DIE__};
858 delete $SIG{__WARN__};
859 undef *CORE::GLOBAL::die;
860 *CORE::GLOBAL::die = sub {Carp::croak(@_)};
861 undef *CORE::GLOBAL::warn;
862 *CORE::GLOBAL::warn = sub {Carp::carp(@_)};