girocco: support bundle listings
[girocco.git] / Girocco / Util.pm
blob7e2485a9acedf4c77b6c0ccd706a4fb7336d2fc6
1 package Girocco::Util;
3 use 5.008;
4 use strict;
5 use warnings;
7 use Girocco::Config;
8 use Time::Local;
9 use File::Spec;
10 use IPC::Open3;
11 use Encode;
13 BEGIN {
14 use base qw(Exporter);
15 our @EXPORT = qw(get_git scrypt jailed_file sendmail_pipe mailer_pipe
16 lock_file unlock_file valid_tag rand_adjust
17 filedb_atomic_append filedb_atomic_edit filedb_grep
18 filedb_atomic_grep valid_email valid_email_multi
19 valid_repo_url valid_web_url url_base url_path url_server
20 projects_html_list parse_rfc2822_date parse_any_date
21 extract_url_hostname is_dns_hostname is_our_hostname
22 get_cmd online_cpus sys_pagesize sys_memsize
23 calc_windowmemory to_utf8 capture_command);
26 my $encoder;
27 BEGIN {
28 $encoder = Encode::find_encoding('Windows-1252') ||
29 Encode::find_encoding('ISO-8859-1') or
30 die "failed to load ISO-8859-1 encoder\n";
33 sub to_utf8 {
34 my ($str, $encode) = @_;
35 return undef unless defined $str;
36 my $ans;
37 if (Encode::is_utf8($str) || utf8::decode($str)) {
38 $ans = $str;
39 } else {
40 $ans = $encoder->decode($str, Encode::FB_DEFAULT);
42 utf8::encode($ans) if $encode;
43 return $ans;
46 # Return the entire output sent to stdout and/or stderr from running a command.
47 # Input is redirected to /dev/null. Noncaptured output is totally discarded.
48 # Returns ($status, $output) where $status will be undef if there was a problem
49 # running the command otherwise $status will be the full waitpid $? result.
50 # $output will contain the captured output unless $status is undefined.
51 # First argument is:
52 # 0 => discard stdout and stderr, only return command status
53 # 1 => capture stdout, discard stderr, return command status
54 # 2 => capture stderr, discard stdout, return command status
55 # 3 => capture stdout+stderr, return command status
56 # Second argument is undef, '' or string to send to command's stdin
57 # Subsequent arguments are command and arguments for pipe open call
58 sub capture_command {
59 # We avoid using STDIN/STDOUT in order to be compatible with FCGI mode
60 my $flags = shift;
61 my $input = shift;
62 local(*NULL);
63 local(*CHLDIN);
64 local(*CHLDOUT);
65 open(NULL, '+<', File::Spec->devnull) or die "couldn't open devnull: $!";
66 my $pid;
67 if (defined($input) && $input ne '') {
68 my @cmd = @_;
69 unshift @cmd, 'sh', '-c', <<'SCRIPT', 'sh';
70 input="$(cat; printf x)";
71 printf '%s' "${input%?}" | "$@"
72 SCRIPT
73 if (($flags & 0x3) == 0) {
74 $pid = open3(\*CHLDIN, '>&NULL', '>&NULL', @cmd);
75 } elsif (($flags &0x3) == 1) {
76 $pid = open3(\*CHLDIN, \*CHLDOUT, '>&NULL', @cmd);
77 } elsif (($flags &0x3) == 2) {
78 $pid = open3(\*CHLDIN, '>&NULL', \*CHLDOUT, @cmd);
79 } else {
80 $pid = open3(\*CHLDIN, \*CHLDOUT, \*CHLDOUT, @cmd);
82 if ($pid) {
83 local $SIG{'PIPE'} = 'IGNORE';
84 print CHLDIN $input;
85 close(CHLDIN);
87 } else {
88 open(CHLDIN, '<&NULL') or die "couldn't dup NULL: $!";
89 if (($flags & 0x3) == 0) {
90 $pid = open3('<&CHLDIN', '>&NULL', '>&NULL', @_);
91 } elsif (($flags &0x3) == 1) {
92 $pid = open3('<&CHLDIN', \*CHLDOUT, '>&NULL', @_);
93 } elsif (($flags &0x3) == 2) {
94 $pid = open3('<&CHLDIN', '>&NULL', \*CHLDOUT, @_);
95 } else {
96 $pid = open3('<&CHLDIN', \*CHLDOUT, \*CHLDOUT, @_);
99 close(NULL) or die "couldn't close NULL: $!";
100 my $output;
101 if ($pid && ($flags & 0x03)) {
102 local $/;
103 $output = <CHLDOUT>;
105 defined($output) or $output = '';
106 my $status = waitpid($pid, 0);
107 return (undef) unless defined($status) && $status == $pid;
108 $status = $?;
109 return ($status, $output);
112 # Return the entire output sent to stdout from running a command
113 # Any output the command sends to stderr is discarded
114 # Returns undef if there was an error running the command
115 sub get_cmd {
116 my ($status, $result) = capture_command(1, undef, @_);
117 return defined($status) && $status == 0 ? $result : undef;
120 # Same as get_cmd except configured git binary is automatically provided
121 # as the first argument to get_cmd
122 sub get_git {
123 return get_cmd($Girocco::Config::git_bin, @_);
126 sub scrypt {
127 my ($pwd) = @_;
128 crypt($pwd||'', join ('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
131 sub jailed_file {
132 my ($filename) = @_;
133 $filename =~ s,^/,,;
134 $Girocco::Config::chroot."/$filename";
137 sub lock_file {
138 my ($path) = @_;
140 $path .= '.lock';
142 use Errno qw(EEXIST);
143 use Fcntl qw(O_WRONLY O_CREAT O_EXCL);
144 use IO::Handle;
145 my $handle = new IO::Handle;
147 unless (sysopen($handle, $path, O_WRONLY|O_CREAT|O_EXCL)) {
148 my $cnt = 0;
149 while (not sysopen($handle, $path, O_WRONLY|O_CREAT|O_EXCL)) {
150 ($! == EEXIST) or die "$path open failed: $!";
151 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
152 sleep(1);
155 # XXX: filedb-specific
156 chmod 0664, $path or die "$path g+w failed: $!";
158 $handle;
161 sub _is_passwd_file {
162 return defined($_[0]) && $_[0] eq jailed_file('/etc/passwd');
165 sub _run_update_pwd_db {
166 my ($path, $updatearg) = @_;
167 my @cmd = ($Girocco::Config::basedir.'/bin/update-pwd-db', "$path");
168 push(@cmd, $updatearg) if $updatearg;
169 system(@cmd) == 0 or die "update-pwd-db failed: $?";
172 sub unlock_file {
173 my ($path, $noreplace, $updatearg) = @_;
175 if (!$noreplace) {
176 _run_update_pwd_db("$path.lock", $updatearg)
177 if $Girocco::Config::update_pwd_db && _is_passwd_file($path);
178 rename "$path.lock", $path or die "$path unlock failed: $!";
179 } else {
180 unlink "$path.lock" or die "$path unlock failed: $!";
184 sub filedb_atomic_append {
185 my ($file, $line, $updatearg) = @_;
186 my $id = 65536;
188 open my $src, '<', $file or die "$file open for reading failed: $!";
189 my $dst = lock_file($file);
191 while (<$src>) {
192 my $aid = (split /:/)[2];
193 $id = $aid + 1 if ($aid >= $id);
195 print $dst $_ or die "$file(l) write failed: $!";
198 $line =~ s/\\i/$id/g;
199 print $dst "$line\n" or die "$file(l) write failed: $!";
201 close $dst or die "$file(l) close failed: $!";
202 close $src;
204 unlock_file($file, 0, $updatearg);
206 $id;
209 sub filedb_atomic_edit {
210 my ($file, $fn, $updatearg) = @_;
212 open my $src, '<', $file or die "$file open for reading failed: $!";
213 my $dst = lock_file($file);
215 while (<$src>) {
216 print $dst $fn->($_) or die "$file(l) write failed: $!";
219 close $dst or die "$file(l) close failed: $!";
220 close $src;
222 unlock_file($file, 0, $updatearg);
225 sub filedb_atomic_grep {
226 my ($file, $fn) = @_;
227 my @results = ();
229 open my $src, '<', $file or die "$file open for reading failed: $!";
230 my $dst = lock_file($file);
232 while (<$src>) {
233 my $result = $fn->($_);
234 push(@results, $result) if $result;
237 close $dst or die "$file(l) close failed: $!";
238 close $src;
240 unlock_file($file, 1);
241 return @results;
244 sub filedb_grep {
245 my ($file, $fn) = @_;
246 my @results = ();
248 open my $src, '<', $file or die "$file open for reading failed: $!";
250 while (<$src>) {
251 my $result = $fn->($_);
252 push(@results, $result) if $result;
255 close $src;
257 return @results;
260 sub valid_email {
261 my $email = shift;
262 defined($email) or $email = '';
263 return $email =~ /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+$/;
266 sub valid_email_multi {
267 my $email_multi = shift;
268 defined($email_multi) or $email_multi = '';
269 # More relaxed, we just want to avoid too dangerous characters.
270 return $email_multi =~ /^[a-zA-Z0-9+._, @-]+$/;
273 sub valid_web_url {
274 my $url = shift;
275 defined($url) or $url = '';
276 return $url =~
277 /^https?:\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~:?&=;-]*)?(#[a-zA-Z0-9._-]+)?$/;
280 sub valid_repo_url {
281 my $url = shift || '';
282 # Currently neither username nor password is allowed in the URL and IPv6
283 # literal addresses are not accepted either.
284 $Girocco::Config::mirror_svn &&
285 $url =~ /^svn(\+https?)?:\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/os
286 and return 1;
287 $Girocco::Config::mirror_darcs &&
288 $url =~ /^darcs:\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/os
289 and return 1;
290 $Girocco::Config::mirror_bzr &&
291 $url =~ /^bzr:\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/os
292 and return 1;
293 $Girocco::Config::mirror_hg &&
294 $url =~ /^hg\+https?:\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/os
295 and return 1;
296 return $url =~ /^(https?|git):\/\/[a-zA-Z0-9.:-]+(\/[_\%a-zA-Z0-9.\/~-]*)?$/;
299 sub extract_url_hostname {
300 my $url = shift || '';
301 if ($url =~ m,^bzr://,) {
302 $url =~ s,^bzr://,,;
303 return 'launchpad.net' if $url =~ /^lp:/;
305 return undef unless $url =~ m,^[A-Za-z0-9+.-]+://[^/],;
306 $url =~ s,^[A-Za-z0-9+.-]+://,,;
307 $url =~ s,^([^/]+).*$,$1,;
308 $url =~ s/:[0-9]*$//;
309 $url =~ s/^[^@]*[@]//;
310 return $url ? $url : undef;
313 # See these RFCs:
314 # RFC 1034 section 3.5
315 # RFC 1123 section 2.1
316 # RFC 1738 section 3.1
317 # RFC 3986 section 3.2.2
318 sub is_dns_hostname {
319 my $host = shift;
320 defined($host) or $host = '';
321 return 0 if $host eq '' || $host =~ /\s/;
322 # first remove a trailing '.'
323 $host =~ s/\.$//;
324 return 0 if length($host) > 255;
325 my $octet = '(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])';
326 return 0 if $host =~ /^$octet\.$octet\.$octet\.$octet$/o;
327 my @labels = split(/[.]/, $host, -1);
328 return 0 unless @labels && @labels >= $Girocco::Config::min_dns_labels;
329 # now check each label
330 foreach my $label (@labels) {
331 return 0 unless length($label) > 0 && length($label) <= 63;
332 return 0 unless $label =~ /^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/;
334 return 1;
337 sub is_our_hostname {
338 my $test = shift || '';
339 $test =~ s/\.$//;
340 my %names = ();
341 my @urls = (
342 $Girocco::Config::gitweburl,
343 $Girocco::Config::gitwebfiles,
344 $Girocco::Config::webadmurl,
345 $Girocco::Config::bundlesurl,
346 $Girocco::Config::htmlurl,
347 $Girocco::Config::httppullurl,
348 $Girocco::Config::httpbundleurl,
349 $Girocco::Config::httpspushurl,
350 $Girocco::Config::gitpullurl,
351 $Girocco::Config::pushurl
353 foreach my $url (@urls) {
354 if ($url) {
355 my $host = extract_url_hostname($url);
356 if (defined($host)) {
357 $host =~ s/\.$//;
358 $names{lc($host)} = 1;
362 return $names{lc($test)} ? 1 : 0;
365 my %_badtags;
366 BEGIN {
367 %_badtags = (
368 about=>1, after=>1, all=>1, also=>1, an=>1, and=>1, another=>1, any=>1,
369 are=>1, as=>1, at=>1, be=>1, because=>1, been=>1, before=>1, being=>1,
370 between=>1, both=>1, but=>1, by=>1, came=>1, can=>1, come=>1, could=>1,
371 did=>1, do=>1, each=>1, for=>1, from=>1, get=>1, got=>1, had=>1, has=>1,
372 have=>1, he=>1, her=>1, here=>1, him=>1, himself=>1, his=>1, how=>1,
373 if=>1, in=>1, into=>1, is=>1, it=>1, like=>1, make=>1, many=>1, me=>1,
374 might=>1, more=>1, most=>1, much=>1, must=>1, my=>1, never=>1, now=>1,
375 of=>1, on=>1, only=>1, or=>1, other=>1, our=>1, out=>1, over=>1,
376 said=>1, same=>1, see=>1, should=>1, since=>1, some=>1, still=>1,
377 such=>1, take=>1, than=>1, that=>1, the=>1, their=>1, them=>1, then=>1,
378 there=>1, these=>1, they=>1, this=>1, those=>1, through=>1, to=>1,
379 too=>1, under=>1, up=>1, very=>1, was=>1, way=>1, we=>1, well=>1,
380 were=>1, what=>1, where=>1, which=>1, while=>1, who=>1, with=>1,
381 would=>1, you=>1, your=>1
385 # A valid tag must only have [a-zA-Z0-9:.+#_-] characters, must start with a
386 # letter, must not be a noise word and except for 'C' must be more than one
387 # character long and no more than 32 characters long.
388 sub valid_tag {
389 local $_ = $_[0] || '';
390 return 1 if $_ eq 'C'; # Currently only allowed single letter tag
391 return 0 unless /^[a-zA-Z][a-zA-Z0-9:.+#_-]+$/;
392 return 0 if $_badtags{lc($_)};
393 return length($_) <= 32 ? 1 : 0;
396 # If the passed in argument looks like a URL, return only the stuff up through
397 # the host:port part otherwise return the entire argument.
398 sub url_base {
399 my $url = shift || '';
400 # See RFC 3968
401 $url = $1.$2.$3.$4 if $url =~ m,^( [A-Za-z][A-Za-z0-9+.-]*: ) # scheme
402 ( // ) # // separator
403 ((?:[^\@]+\@)?) # optional userinfo
404 ( [^/?#]+ ) # host and port
405 (?:[/?#].*)?$,x; # path and optional query string and/or anchor
406 return $url;
409 # If the passed in argument looks like a URL, return only the stuff following
410 # the host:port part otherwise return the entire argument.
411 # If the optional second argument is true, the returned value will have '/'
412 # appended if it does not already end in '/'.
413 sub url_path {
414 my $url = shift || '';
415 my $add_slash = shift || 0;
416 # See RFC 3968
417 $url = $1 if $url =~ m,^(?: [A-Za-z][A-Za-z0-9+.-]*: ) # scheme
418 (?: // ) # // separator
419 (?: [^\@]+\@ )? # optional userinfo
420 (?: [^/?#]+ ) # host and port
421 ((?:[/?#].*)?)$,x; # path and optional query string and/or anchor
422 $url .= '/' if $add_slash && $url !~ m|/$|;
423 return $url;
426 # If both SERVER_NAME and SERVER_PORT are set pass the argument through url_path
427 # and then prefix it with the appropriate scheme (HTTPS=?on), host and port and
428 # return it. If a something that doesn't look like it could be the start of a
429 # URL path comes back from url_path or SERVER_NAME is a link-local IPv6 address
430 # then just return the argument unchanged.
431 sub url_server {
432 my $url = shift || '';
433 my $path = url_path($url);
434 return $url unless $path eq '' || $path =~ m|^[/?#]|;
435 return $url unless $ENV{'SERVER_NAME'} && $ENV{'SERVER_PORT'} &&
436 $ENV{'SERVER_PORT'} =~ /^[1-9][0-9]{0,4}$/;
437 return $url if $ENV{'SERVER_NAME'} =~ /^[[]?fe80:/i;
438 my $server = $ENV{'SERVER_NAME'};
439 # Deal with Apache bug where IPv6 literal server names do not include
440 # the required surrounding '[' and ']' characters
441 $server = '[' . $server . ']' if $server =~ /:/ && $server !~ /^[[]/;
442 my $ishttps = $ENV{'HTTPS'} && $ENV{'HTTPS'} =~ /^on$/i;
443 my $portnum = 0 + $ENV{'SERVER_PORT'};
444 my $port = '';
445 if (($ishttps && $portnum != 443) || (!$ishttps && $portnum != 80)) {
446 $port = ':' . $portnum;
448 return 'http' . ($ishttps ? 's' : '') . '://' . $server . $port . $path;
451 sub _escapeHTML {
452 my $str = shift;
453 $str =~ s/\&/\&amp;/gs;
454 $str =~ s/\</\&lt;/gs;
455 $str =~ s/\>/\&gt;/gs;
456 $str =~ s/\"/\&quot;/gs; #"
457 return $str;
460 # create relative time string from passed in age in seconds
461 sub _rel_age {
462 my $age = shift;
463 my $age_str;
465 if ($age > 60*60*24*365*2) {
466 $age_str = (int $age/60/60/24/365);
467 $age_str .= " years ago";
468 } elsif ($age > 60*60*24*(365/12)*2) {
469 $age_str = int $age/60/60/24/(365/12);
470 $age_str .= " months ago";
471 } elsif ($age > 60*60*24*7*2) {
472 $age_str = int $age/60/60/24/7;
473 $age_str .= " weeks ago";
474 } elsif ($age > 60*60*24*2) {
475 $age_str = int $age/60/60/24;
476 $age_str .= " days ago";
477 } elsif ($age > 60*60*2) {
478 $age_str = int $age/60/60;
479 $age_str .= " hours ago";
480 } elsif ($age > 60*2) {
481 $age_str = int $age/60;
482 $age_str .= " mins ago";
483 } elsif ($age > 2) {
484 $age_str = int $age;
485 $age_str .= " secs ago";
486 } elsif ($age >= 0) {
487 $age_str = "right now";
488 } else {
489 $age_str = "future time";
491 return $age_str;
494 # create relative time string from passed in idle in seconds
495 sub _rel_idle {
496 my $idle_str = _rel_age(shift);
497 $idle_str =~ s/ ago//;
498 $idle_str = "not at all" if $idle_str eq "right now";
499 return $idle_str;
502 sub _strftime {
503 use POSIX qw(strftime);
504 my ($fmt, $secs, $zonesecs) = @_;
505 my ($S,$M,$H,$d,$m,$y) = gmtime($secs + $zonesecs);
506 $zonesecs = int($zonesecs / 60);
507 $fmt =~ s/%z/\$z/g;
508 my $ans = strftime($fmt, $S, $M, $H, $d, $m, $y, -1, -1, -1);
509 my $z;
510 if ($zonesecs < 0) {
511 $z = "-";
512 $zonesecs = -$zonesecs;
513 } else {
514 $z = "+";
516 $z .= sprintf("%02d%02d", int($zonesecs/60), $zonesecs % 60);
517 $ans =~ s/\$z/$z/g;
518 return $ans;
521 # Take a list of project names and produce a nicely formated table that
522 # includes owner links and descriptions. If the list is empty returns ''.
523 # The first argument may be a hash ref that contains options. The following
524 # options are available:
525 # target -- sets the target value of the owner link
526 # emptyok -- if true returns an empty table rather than ''
527 # typecol -- if true include type column with hover info
528 # changed -- if true include a changed and idle column
529 sub projects_html_list {
530 my $options = {};
531 if (defined($_[0]) && ref($_[0]) eq 'HASH') {
532 $options = shift;
534 return '' unless @_ || (defined($options->{emptyok}) && $options->{emptyok});
535 require Girocco::Project;
536 my $count = 0;
537 my $target = '';
538 $target = " target=\""._escapeHTML($options->{target})."\""
539 if defined($options->{target});
540 my $withtype = defined($options->{typecol}) && $options->{typecol};
541 my $withchanged = defined($options->{changed}) && $options->{changed};
542 my $typehead = '';
543 $typehead = '<th>Type</th>' if $withtype;
544 my $chghead = '';
545 $chghead = substr(<<EOT, 0, -1) if $withchanged;
546 <th><span class="hover">Changed<span><span class="head">Changed</span
547 />The last time a ref change was received by this site.</span></span></th
548 ><th><span class="hover">Idle<span><span class="head">Idle</span
549 />The most recent committer time in <i>refs/heads</i>.</span></span></th
552 my $html = <<EOT;
553 <table class='projectlist'><tr><th>Project</th>$typehead$chghead<th class="desc">Description</th></tr>
555 my $trclass = ' class="odd"';
556 foreach (sort({lc($a) cmp lc($b)} @_)) {
557 if (Girocco::Project::does_exist($_, 1)) {
558 my $proj = Girocco::Project->load($_);
559 my $projname = $proj->{name}.".git";
560 my $projdesc = $proj->{desc}||'';
561 utf8::decode($projdesc) if utf8::valid($projdesc);
562 my $typecol = '';
563 if ($withtype) {
564 if ($proj->{mirror}) {
565 $typecol = substr(<<EOT, 0, -1);
566 <td class="type"><span class="hover">mirror<span class="nowrap">@{[_escapeHTML($proj->{url})]}</span></span></td>
568 } else {
569 my $users = @{$proj->{users}};
570 $users .= ' user';
571 $users .= 's' unless @{$proj->{users}} == 1;
572 my $userlist = join(', ', sort({lc($a) cmp lc($b)} @{$proj->{users}}));
573 my $spncls = length($userlist) > 25 ? '' : ' class="nowrap"';
574 $typecol = $userlist ? substr(<<EOT, 0, -1) : substr(<<EOT, 0, -1);
575 <td class="type"><span class="hover">$users<span$spncls>$userlist</span></span></td>
577 <td class="type">$users</td>
581 my $changecol = '';
582 if ($withchanged) {
583 my $rel = '';
584 my $changetime = $proj->{lastchange};
585 if ($changetime) {
586 $rel = "<span class=\"hover\">" .
587 _rel_age(time - parse_rfc2822_date($changetime)) .
588 "<span class=\"nowrap\">$changetime</span></span>";
589 } else {
590 $rel = "no commits";
592 $changecol = substr(<<EOT, 0, -1);
593 <td class="change">$rel</td>
595 my $idletime = $proj->{lastactivity};
596 my ($idlesecs, $tz);
597 $idlesecs = parse_any_date($idletime, \$tz) if $idletime;
598 if ($idlesecs) {
599 my $idle2822 = _strftime("%a, %d %b %Y %T %z", $idlesecs, $tz);
600 $rel = "<span class=\"hover\">" .
601 _rel_idle(time - $idlesecs) .
602 "<span class=\"nowrap\">$idle2822</span></span>";
603 } else {
604 $rel = "no commits";
606 $changecol .= substr(<<EOT, 0, -1);
607 <td class="idle">$rel</td>
610 $html .= <<EOT;
611 <tr$trclass><td><a href="@{[url_path($Girocco::Config::gitweburl)]}/$projname"$target
612 >@{[_escapeHTML($projname)]}</td>$typecol$changecol<td>@{[_escapeHTML($projdesc)]}</td></tr>
614 $trclass = $trclass ? '' : ' class="odd"';
615 ++$count;
618 $html .= <<EOT;
619 </table>
621 return ($count || (defined($options->{emptyok}) && $options->{emptyok})) ? $html : '';
624 my %_month_names;
625 BEGIN {
626 %_month_names = (
627 jan => 0, feb => 1, mar => 2, apr => 3, may => 4, jun => 5,
628 jul => 6, aug => 7, sep => 8, oct => 9, nov => 10, dec => 11
632 # Should be in "date '+%a, %d %b %Y %T %z'" format as saved to lastgc, lastrefresh and lastchange
633 # The leading "%a, " is optional, returns undef if unrecognized date. This is also known as
634 # RFC 2822 date format and git's '%cD', '%aD' and --date=rfc2822 format.
635 # If the second argument is a SCALAR ref, its value will be set to the TZ offset in seconds
636 sub parse_rfc2822_date {
637 my $dstr = shift || '';
638 my $tzoff = shift || '';
639 $dstr = $1 if $dstr =~/^[^\s]+,\s*(.*)$/;
640 return undef unless $dstr =~
641 /^\s*(\d{1,2})\s+([A-Za-z]{3})\s+(\d{4})\s+(\d{1,2}):(\d{2}):(\d{2})\s+([+-]\d{4})\s*$/;
642 my ($d,$b,$Y,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7);
643 my $m = $_month_names{lc($b)};
644 return undef unless defined($m);
645 my $seconds = timegm(0+$S, 0+$M, 0+$H, 0+$d, 0+$m, $Y-1900);
646 my $offset = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
647 $offset = -$offset if substr($z,0,1) eq '-';
648 $$tzoff = $offset if ref($tzoff) eq 'SCALAR';
649 return $seconds - $offset;
652 # Will parse any supported date format. Actually there are three formats
653 # currently supported:
654 # 1. RFC 2822 (uses parse_rfc2822_date)
655 # 2. RFC 3339 / ISO 8601 (T may be ' ' or '_', 'Z' is optional, ':' optional in TZ)
656 # 3. Same as #2 except no colons or hyphens allowed and hours MUST be 2 digits
657 # 4. unix seconds since epoch with optional +/- trailing TZ (may not have a ':')
658 # Returns undef if unsupported date.
659 # If the second argument is a SCALAR ref, its value will be set to the TZ offset in seconds
660 sub parse_any_date {
661 my $dstr = shift || '';
662 my $tzoff = shift || '';
663 if ($dstr =~ /^\s*([-+]?\d+)(?:\s+([-+]\d{4}))?\s*$/) {
664 # Unix timestamp
665 my $ts = 0 + $1;
666 my $off = 0;
667 if ($2) {
668 my $z = $2;
669 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
670 $off = -$off if substr($z,0,1) eq '-';
672 $$tzoff = $off if ref($tzoff) eq 'SCALAR';
673 return $ts;
675 if ($dstr =~ /^\s*(\d{4})-(\d{2})-(\d{2})[Tt _](\d{1,2}):(\d{2}):(\d{2})(?:[ _]?([Zz]|(?:[-+]\d{1,2}:?\d{2})))?\s*$/ ||
676 $dstr =~ /^\s*(\d{4})(\d{2})(\d{2})[Tt _](\d{2})(\d{2})(\d{2})(?:[ _]?([Zz]|(?:[-+]\d{2}\d{2})))?\s*$/) {
677 my ($Y,$m,$d,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7||'');
678 my $seconds = timegm(0+$S, 0+$M, 0+$H, 0+$d, $m-1, $Y-1900);
679 defined($z) && $z ne '' or $z = 'Z';
680 $z =~ s/://;
681 substr($z,1,0) = '0' if length($z) == 4;
682 my $off = 0;
683 if (uc($z) ne 'Z') {
684 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
685 $off = -$off if substr($z,0,1) eq '-';
687 $$tzoff = $off if ref($tzoff) eq 'SCALAR';
688 return $seconds - $off;
690 return parse_rfc2822_date($dstr, $tzoff);
693 # Input is a number such as a minute interval
694 # Return value is a random number between the input and 1.25*input
695 # This can be used to randomize the update and gc operations a bit to avoid
696 # having them all end up all clustered together
697 sub rand_adjust {
698 my $input = shift || 0;
699 return $input unless $input;
700 return $input + int(rand(0.25 * $input));
703 # Open a pipe to a new sendmail process. The '-i' option is always passed to
704 # the new process followed by any addtional arguments passed in. Note that
705 # the sendmail process is only expected to understand the '-i', '-t' and '-f'
706 # options. Using any other options via this function is not guaranteed to work.
707 # A list of recipients may follow the options. Combining a list of recipients
708 # with the '-t' option is not recommended.
709 sub sendmail_pipe {
710 return undef unless @_;
711 die "\$Girocco::Config::sendmail_bin is unset or not executable!\n"
712 unless $Girocco::Config::sendmail_bin && -x $Girocco::Config::sendmail_bin;
713 my $result = open(my $pipe, '|-', $Girocco::Config::sendmail_bin, '-i', @_);
714 return $result ? $pipe : undef;
717 # Open a pipe that works similarly to a mailer such as /usr/bin/mail in that
718 # if the first argument is '-s', a subject line will be automatically added
719 # (using the second argument as the subject). Any remaining arguments are
720 # expected to be recipient addresses that will be added to an explicit To:
721 # line as well as passed on to sendmail_pipe. In addition an
722 # "Auto-Submitted: auto-generated" header is always added as well as a suitable
723 # "From:" header.
724 sub mailer_pipe {
725 my $subject = undef;
726 if (@_ >= 2 && $_[0] eq '-s') {
727 shift;
728 $subject = shift;
730 my $tolist = join(", ", @_);
731 unshift(@_, '-f', $Girocco::Config::sender) if $Girocco::Config::sender;
732 my $pipe = sendmail_pipe(@_);
733 if ($pipe) {
734 print $pipe "From: \"$Girocco::Config::name\" ",
735 "($Girocco::Config::title) ",
736 "<$Girocco::Config::admin>\n";
737 print $pipe "To: $tolist\n";
738 print $pipe "Subject: $subject\n" if defined($subject);
739 print $pipe "MIME-Version: 1.0\n";
740 print $pipe "Content-Type: text/plain; charset=utf-8\n";
741 print $pipe "Content-Transfer-Encoding: 8bit\n";
742 print $pipe "Auto-Submitted: auto-generated\n";
743 print $pipe "\n";
745 return $pipe;
748 sub _goodval {
749 my $val = shift;
750 return undef unless defined($val);
751 $val =~ s/[\r\n]+$//s;
752 return undef unless $val =~ /^\d+$/;
753 $val = 0 + $val;
754 return undef unless $val >= 1;
755 return $val;
758 # Returns the number of "online" cpus or undef if undetermined
759 sub online_cpus {
760 my @confcpus = $^O eq "linux" ?
761 qw(_NPROCESSORS_ONLN NPROCESSORS_ONLN) :
762 qw(NPROCESSORS_ONLN _NPROCESSORS_ONLN) ;
763 my $cpus = _goodval(get_cmd('getconf', $confcpus[0]));
764 return $cpus if $cpus;
765 $cpus = _goodval(get_cmd('getconf', $confcpus[1]));
766 return $cpus if $cpus;
767 if ($^O ne "linux") {
768 my @sysctls = qw(hw.ncpu);
769 unshift(@sysctls, qw(hw.availcpu)) if $^O eq "darwin";
770 foreach my $mib (@sysctls) {
771 $cpus = _goodval(get_cmd('sysctl', '-n', $mib));
772 return $cpus if $cpus;
775 return undef;
778 # Returns the system page size in bytes or undef if undetermined
779 # This should never fail on a POSIX system
780 sub sys_pagesize {
781 use POSIX ":unistd_h";
782 my $pagesize = sysconf(_SC_PAGESIZE);
783 return undef unless defined($pagesize) && $pagesize =~ /^\d+$/;
784 $pagesize = 0 + $pagesize;
785 return undef unless $pagesize >= 256;
786 return $pagesize;
789 # Returns the amount of available physical memory in bytes
790 # This may differ from the actual amount of physical memory installed
791 # Returns undef if this cannot be determined
792 sub sys_memsize {
793 my $pagesize = sys_pagesize;
794 if ($pagesize && $^O eq "linux") {
795 my $pages = _goodval(get_cmd('getconf', '_PHYS_PAGES'));
796 return $pagesize * $pages if $pages;
798 if ($^O ne "linux") {
799 my @sysctls = qw(hw.physmem64);
800 unshift(@sysctls, qw(hw.memsize)) if $^O eq "darwin";
801 foreach my $mib (@sysctls) {
802 my $memsize = _goodval(get_cmd('sysctl', '-n', $mib));
803 return $memsize if $memsize;
805 my $memsize32 = _goodval(get_cmd('sysctl', '-n', 'hw.physmem'));
806 return $memsize32 if $memsize32 && $memsize32 <= 2147483647;
807 if ($pagesize) {
808 my $pages = _goodval(get_cmd('sysctl', '-n', 'hw.availpages'));
809 return $pagesize * $pages if $pages;
811 return 2147483647 + 1 if $memsize32;
813 return undef;
816 sub _max_conf_window_bytes {
817 return undef unless defined($Girocco::Config::max_gc_window_memory_size);
818 return undef unless
819 $Girocco::Config::max_gc_window_memory_size =~ /^(\d+)([kKmMgG]?)$/;
820 my ($val, $suffix) = (0+$1, lc($2));
821 $val *= 1024 if $suffix eq 'k';
822 $val *= 1024 * 1024 if $suffix eq 'm';
823 $val *= 1024 * 1024 * 1024 if $suffix eq 'g';
824 return $val;
827 # Return the value to pass to --window-memory= for git repack
828 # If the system memory or number of CPUs cannot be determined, returns "1g"
829 # Otherwise returns half the available memory divided by the number of CPUs
830 # but never more than 1 gigabyte or max_gc_window_memory_size.
831 sub calc_windowmemory {
832 my $cpus = online_cpus;
833 my $memsize = sys_memsize;
834 my $max = 1024 * 1024 * 1024;
835 if ($cpus && $memsize) {
836 $max = int($memsize / 2 / $cpus);
837 $max = 1024 * 1024 * 1024 if $max >= 1024 * 1024 * 1024;
839 my $maxconf = _max_conf_window_bytes;
840 $max = $maxconf if defined($maxconf) && $maxconf && $max > $maxconf;
841 return $max if $max % 1024;
842 $max /= 1024;
843 return "${max}k" if $max % 1024;
844 $max /= 1024;
845 return "${max}m" if $max % 1024;
846 $max /= 1024;
847 return "${max}g";