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 calc_bigfilethreshold has_reserved_suffix
23 noFatalsToBrowser calc_redeltathreshold
24 clean_email_multi read_HEAD_symref read_config_file
25 read_config_file_hash is_git_dir git_bool util_path
31 $encoder = Encode
::find_encoding
('Windows-1252') ||
32 Encode
::find_encoding
('ISO-8859-1') or
33 die "failed to load ISO-8859-1 encoder\n";
37 my ($str, $encode) = @_;
38 return undef unless defined $str;
40 if (Encode
::is_utf8
($str) || utf8
::decode
($str)) {
43 $ans = $encoder->decode($str, Encode
::FB_DEFAULT
);
45 utf8
::encode
($ans) if $encode;
49 BEGIN {require "Girocco/extra/capture_command.pl"}
51 # Return the entire output sent to stdout from running a command
52 # Any output the command sends to stderr is discarded
53 # Returns undef if there was an error running the command (see $!)
55 my ($status, $result) = capture_command
(1, undef, @_);
56 return defined($status) && $status == 0 ?
$result : undef;
59 # Same as get_cmd except configured git binary is automatically provided
60 # as the first argument to get_cmd
62 return get_cmd
($Girocco::Config
::git_bin
, @_);
67 crypt($pwd||'', join ('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
73 $Girocco::Config
::chroot."/$filename";
82 use Fcntl
qw(O_WRONLY O_CREAT O_EXCL);
84 my $handle = new IO
::Handle
;
86 unless (sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
88 while (not sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
89 ($! == EEXIST
) or die "$path open failed: $!";
90 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
94 # XXX: filedb-specific
95 chmod 0664, $path or die "$path g+w failed: $!";
100 sub _is_passwd_file
{
101 return defined($_[0]) && $_[0] eq jailed_file
('/etc/passwd');
104 sub _run_update_pwd_db
{
105 my ($path, $updatearg) = @_;
106 my @cmd = ($Girocco::Config
::basedir
.'/bin/update-pwd-db', "$path");
107 push(@cmd, $updatearg) if $updatearg;
108 system(@cmd) == 0 or die "update-pwd-db failed: $?";
112 my ($path, $noreplace, $updatearg) = @_;
115 _run_update_pwd_db
("$path.lock", $updatearg)
116 if $Girocco::Config
::update_pwd_db
&& _is_passwd_file
($path);
117 rename "$path.lock", $path or die "$path unlock failed: $!";
119 unlink "$path.lock" or die "$path unlock failed: $!";
123 sub filedb_atomic_append
{
124 my ($file, $line, $updatearg) = @_;
127 open my $src, '<', $file or die "$file open for reading failed: $!";
128 my $dst = lock_file
($file);
131 my $aid = (split /:/)[2];
132 $id = $aid + 1 if ($aid >= $id);
134 print $dst $_ or die "$file(l) write failed: $!";
137 $line =~ s/\\i/$id/g;
138 print $dst "$line\n" or die "$file(l) write failed: $!";
140 close $dst or die "$file(l) close failed: $!";
143 unlock_file
($file, 0, $updatearg);
148 sub filedb_atomic_edit
{
149 my ($file, $fn, $updatearg) = @_;
151 open my $src, '<', $file or die "$file open for reading failed: $!";
152 my $dst = lock_file
($file);
155 print $dst $fn->($_) or die "$file(l) write failed: $!";
158 close $dst or die "$file(l) close failed: $!";
161 unlock_file
($file, 0, $updatearg);
164 sub filedb_atomic_grep
{
165 my ($file, $fn) = @_;
168 open my $src, '<', $file or die "$file open for reading failed: $!";
169 my $dst = lock_file
($file);
172 my $result = $fn->($_);
173 push(@results, $result) if $result;
176 close $dst or die "$file(l) close failed: $!";
179 unlock_file
($file, 1);
184 my ($file, $fn) = @_;
187 open my $src, '<', $file or die "$file open for reading failed: $!";
190 my $result = $fn->($_);
191 push(@results, $result) if $result;
201 defined($email) or $email = '';
202 return $email =~ /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+$/;
205 sub clean_email_multi
{
207 defined($input) or $input = '';
208 $input =~ s/^\s+//; $input =~ s/\s+$//;
211 foreach (split(/\s*,\s*/, $input)) {
213 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
215 return join(",", @newlist);
218 sub valid_email_multi
{
219 # each email address must be a valid_email but we silently
220 # ignore extra spaces at the beginning/end and around any comma(s)
221 foreach (split(/,/, clean_email_multi
(shift))) {
222 return 0 unless valid_email
($_);
229 defined($url) or $url = '';
231 /^https?:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~:?&=;-]*)?(#[a-zA-Z0-9._-]+)?$/;
235 my $url = shift || '';
236 # Currently neither username nor password is allowed in the URL and IPv6
237 # literal addresses are not accepted either.
238 $Girocco::Config
::mirror_svn
&&
239 $url =~ /^svn(\+https?)?:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/os
241 $Girocco::Config
::mirror_darcs
&&
242 $url =~ /^darcs:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/os
244 $Girocco::Config
::mirror_bzr
&&
245 $url =~ /^bzr:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/os
247 $Girocco::Config
::mirror_hg
&&
248 $url =~ /^hg\+https?:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/os
250 return $url =~ /^(https?|git):\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/;
253 sub extract_url_hostname
{
254 my $url = shift || '';
255 if ($url =~ m
,^bzr
://,) {
257 return 'launchpad.net' if $url =~ /^lp:/;
259 return undef unless $url =~ m
,^[A
-Za
-z0
-9+.-]+://[^/],;
260 $url =~ s
,^[A
-Za
-z0
-9+.-]+://,,;
261 $url =~ s
,^([^/]+).*$,$1,;
262 $url =~ s/:[0-9]*$//;
263 $url =~ s/^[^\@]*[\@]//;
264 return $url ?
$url : undef;
268 # RFC 1034 section 3.5
269 # RFC 1123 section 2.1
270 # RFC 1738 section 3.1
271 # RFC 2606 sections 2 & 3
272 # RFC 3986 section 3.2.2
273 sub is_dns_hostname
{
275 defined($host) or $host = '';
276 return 0 if $host eq '' || $host =~ /\s/;
277 # first remove a trailing '.'
279 return 0 if length($host) > 255;
280 my $octet = '(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])';
281 return 0 if $host =~ /^$octet\.$octet\.$octet\.$octet$/o;
282 my @labels = split(/[.]/, $host, -1);
283 return 0 unless @labels && @labels >= $Girocco::Config
::min_dns_labels
;
284 # now check each label
285 foreach my $label (@labels) {
286 return 0 unless length($label) > 0 && length($label) <= 63;
287 return 0 unless $label =~ /^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/;
289 # disallow RFC 2606 names provided at least two labels are present
291 my $tld = lc($labels[-1]);
297 my $sld = lc($labels[-2]);
298 return 0 if $sld eq 'example' &&
299 ($tld eq 'com' || $tld eq 'net' || $tld eq 'org');
304 sub is_our_hostname
{
305 my $test = shift || '';
309 $Girocco::Config
::gitweburl
,
310 $Girocco::Config
::gitwebfiles
,
311 $Girocco::Config
::webadmurl
,
312 $Girocco::Config
::bundlesurl
,
313 $Girocco::Config
::htmlurl
,
314 $Girocco::Config
::httppullurl
,
315 $Girocco::Config
::httpbundleurl
,
316 $Girocco::Config
::httpspushurl
,
317 $Girocco::Config
::gitpullurl
,
318 $Girocco::Config
::pushurl
320 foreach my $url (@urls) {
322 my $host = extract_url_hostname
($url);
323 if (defined($host)) {
325 $names{lc($host)} = 1;
329 return $names{lc($test)} ?
1 : 0;
332 my (%_oktags, %_badtags, %_canontags, $_canontagscreated, @_whitetags);
334 # These are always okay (a "whitelist") even if they would
335 # otherwise not be allowed
337 .net 2d 3d 6502 68000 68008 68010 68020 68030 68040 68060
338 8086 80286 80386 80486 80586 c cc make www x
340 map({$_oktags{lc($_)}=1} @_whitetags, @Girocco::Config
::allowed_tags
);
341 # entries MUST be all lowercase to be effective
343 # These are "nonsense" or pointless tags
344 about
=>1, after
=>1, all
=>1, also
=>1, an
=>1, and=>1, another
=>1, any
=>1,
345 are
=>1, as
=>1, at
=>1, be
=>1, because
=>1, been
=>1, before
=>1, being
=>1,
346 between
=>1, both
=>1, but
=>1, by
=>1, came
=>1, can
=>1, come
=>1, could
=>1,
347 did
=>1, do=>1, each=>1, for=>1, from
=>1, get
=>1, got
=>1, had
=>1, has
=>1,
348 have
=>1, he
=>1, her
=>1, here
=>1, him
=>1, himself
=>1, his
=>1, how
=>1,
349 if=>1, in=>1, into
=>1, is
=>1, it
=>1, like
=>1, make
=>1, many
=>1, me
=>1,
350 might
=>1, more
=>1, most
=>1, much
=>1, must
=>1, my=>1, never
=>1, now
=>1,
351 of
=>1, oh
=>1, on
=>1, only
=>1, or=>1, other
=>1, our=>1, out
=>1, over
=>1,
352 said
=>1, same
=>1, see
=>1, should
=>1, since
=>1, some
=>1, still
=>1,
353 such
=>1, take
=>1, than
=>1, that
=>1, the
=>1, their
=>1, them
=>1, then
=>1,
354 there
=>1, these
=>1, they
=>1, this
=>1, those
=>1, through
=>1, to
=>1,
355 too
=>1, under
=>1, up
=>1, very
=>1, was
=>1, way
=>1, we
=>1, well
=>1,
356 were
=>1, what
=>1, where
=>1, which
=>1, while=>1, who
=>1, with
=>1,
357 would
=>1, yea
=>1, yeah
=>1, you
=>1, your
=>1, yup
=>1
359 # These are "offensive" tags with at least one letter escaped to
360 # avoid having this file trigger various safe-scan robots
361 $_badtags{"a\x73\x73"} = 1;
362 $_badtags{"a\x73\x73hole"} = 1;
363 $_badtags{"b\x30\x30b"} = 1;
364 $_badtags{"b\x30\x30bs"} = 1;
365 $_badtags{"b\x6f\x6fb"} = 1;
366 $_badtags{"b\x6f\x6fbs"} = 1;
367 $_badtags{"b\x75tt"} = 1;
368 $_badtags{"b\x75ttd\x69\x63k"} = 1;
369 $_badtags{"c\x6f\x63k"} = 1;
370 $_badtags{"c\x75\x6e\x74"} = 1;
371 $_badtags{"d\x69\x63k"} = 1;
372 $_badtags{"d\x69\x63kb\x75tt"} = 1;
373 $_badtags{"f\x75\x63k"} = 1;
374 $_badtags{"in\x63\x65st"} = 1;
375 $_badtags{"ph\x75\x63k"} = 1;
376 $_badtags{"p\x6f\x72n"} = 1;
377 $_badtags{"p\x6f\x72no"} = 1;
378 $_badtags{"p\x6f\x72nographic"} = 1;
379 $_badtags{"p\x72\x30n"} = 1;
380 $_badtags{"p\x72\x6fn"} = 1;
381 $_badtags{"r\x61\x70e"} = 1;
382 $_badtags{"s\x65\x78"} = 1;
383 map({$_badtags{lc($_)}=1} @Girocco::Config
::blocked_tags
);
386 # A valid tag must only have [a-zA-Z0-9:.+#_-] characters, must start with a
387 # letter, must not be a noise word, must be more than one character long,
388 # must not be a repeated letter and must be no more than 32 characters long.
389 # However, anything in %_oktags is explicitly allowed even if it otherwise
390 # would violate the rules (except that none of [,\s\\\/] are allowed in tags).
391 # Returns the canonical name for the tag if the tag is valid otherwise undef.
394 return undef unless defined($_) && $_ ne "" && !/[,\s\/\\]/;
395 my $fold = $Girocco::Config
::foldtags
;
396 if ($fold && !$_canontagscreated) {
399 $_canontags{lc($_)} = $_ foreach sort({$b cmp $a} @_whitetags, @Girocco::Config
::allowed_tags
);
400 $_canontagscreated = 1;
402 return $_canontags{lc($_)} if $fold && exists($_canontags{lc($_)});
403 return ($fold ?
lc($_) : $_) if $_oktags{lc($_)};
404 return undef unless /^[a-zA-Z][a-zA-Z0-9:.+#_-]+$/;
405 return undef if $_badtags{lc($_)};
406 return undef if /^(.)\1+$/;
407 return length($_) <= 32 ?
($fold ?
lc($_) : $_) : undef;
410 # If the passed in argument looks like a URL, return only the stuff up through
411 # the host:port part otherwise return the entire argument.
413 my $url = shift || '';
415 $url = $1.$2.$3.$4 if $url =~ m
,^( [A
-Za
-z
][A
-Za
-z0
-9+.-]*: ) # scheme
416 ( // ) # // separator
417 ((?
:[^\@
]+\@
)?
) # optional userinfo
418 ( [^/?
#]+ ) # host and port
419 (?
:[/?#].*)?$,x; # path and optional query string and/or anchor
423 # If the passed in argument looks like a URL, return only the stuff following
424 # the host:port part otherwise return the entire argument.
425 # If the optional second argument is true, the returned value will have '/'
426 # appended if it does not already end in '/'.
428 my $url = shift || '';
429 my $add_slash = shift || 0;
431 $url = $1 if $url =~ m
,^(?
: [A
-Za
-z
][A
-Za
-z0
-9+.-]*: ) # scheme
432 (?
: // ) # // separator
433 (?
: [^\@
]+\@
)?
# optional userinfo
434 (?
: [^/?
#]+ ) # host and port
435 ((?
:[/?#].*)?)$,x; # path and optional query string and/or anchor
436 $url .= '/' if $add_slash && $url !~ m
|/$|;
440 # If both SERVER_NAME and SERVER_PORT are set pass the argument through url_path
441 # and then prefix it with the appropriate scheme (HTTPS=?on), host and port and
442 # return it. If a something that doesn't look like it could be the start of a
443 # URL path comes back from url_path or SERVER_NAME is a link-local IPv6 address
444 # then just return the argument unchanged.
446 my $url = shift || '';
447 my $path = url_path
($url);
448 return $url unless $path eq '' || $path =~ m
|^[/?
#]|;
449 return $url unless $ENV{'SERVER_NAME'} && $ENV{'SERVER_PORT'} &&
450 $ENV{'SERVER_PORT'} =~ /^[1-9][0-9]{0,4}$/;
451 return $url if $ENV{'SERVER_NAME'} =~ /^[[]?fe80:/i;
452 my $server = $ENV{'SERVER_NAME'};
453 # Deal with Apache bug where IPv6 literal server names do not include
454 # the required surrounding '[' and ']' characters
455 $server = '[' . $server . ']' if $server =~ /:/ && $server !~ /^[[]/;
456 my $ishttps = $ENV{'HTTPS'} && $ENV{'HTTPS'} =~ /^on$/i;
457 my $portnum = 0 + $ENV{'SERVER_PORT'};
459 if (($ishttps && $portnum != 443) || (!$ishttps && $portnum != 80)) {
460 $port = ':' . $portnum;
462 return 'http' . ($ishttps ?
's' : '') . '://' . $server . $port . $path;
465 # Returns the number rounded to the nearest tenths. The ".d" part will be
466 # excluded if it's ".0" unless the optional second argument is true
473 return '' . int($v/10) unless $v % 10 || $use0;
474 return '' . int($v/10) . '.' . ($v%10);
477 # Returns a human-readable size string (e.g. '1.5 MiB') for the value
478 # (in bytes) passed in. Returns '0' for undefined or 0 or not all digits.
479 # Otherwise returns '1 KiB' for < 1024, or else a number rounded to the
480 # nearest tenths of a KiB, MiB or GiB.
483 return "0" unless $v && $v =~ /^\d+$/;
484 return "1 KiB" unless $v > 1024;
486 return _tenths
($v) . " KiB" if $v < 1024;
488 return _tenths
($v) . " MiB" if $v < 1024;
490 return _tenths
($v) . " GiB";
495 $str =~ s/\&/\&/gs;
496 $str =~ s/\</\</gs;
497 $str =~ s/\>/\>/gs;
498 $str =~ s/\"/\"/gs; #"
502 # create relative time string from passed in age in seconds
507 if ($age > 60*60*24*365*2) {
508 $age_str = (int $age/60/60/24/365);
509 $age_str .= " years ago";
510 } elsif ($age > 60*60*24*(365/12)*2) {
511 $age_str = int $age/60/60/24/(365/12);
512 $age_str .= " months ago";
513 } elsif ($age > 60*60*24*7*2) {
514 $age_str = int $age/60/60/24/7;
515 $age_str .= " weeks ago";
516 } elsif ($age > 60*60*24*2) {
517 $age_str = int $age/60/60/24;
518 $age_str .= " days ago";
519 } elsif ($age > 60*60*2) {
520 $age_str = int $age/60/60;
521 $age_str .= " hours ago";
522 } elsif ($age > 60*2) {
523 $age_str = int $age/60;
524 $age_str .= " mins ago";
527 $age_str .= " secs ago";
528 } elsif ($age >= 0) {
529 $age_str = "right now";
531 $age_str = "future time";
536 # create relative time string from passed in idle in seconds
538 my $idle_str = _rel_age
(shift);
539 $idle_str =~ s/ ago//;
540 $idle_str = "not at all" if $idle_str eq "right now";
545 use POSIX
qw(strftime);
546 my ($fmt, $secs, $zonesecs) = @_;
547 my ($S,$M,$H,$d,$m,$y) = gmtime($secs + $zonesecs);
548 $zonesecs = int($zonesecs / 60);
550 my $ans = strftime
($fmt, $S, $M, $H, $d, $m, $y, -1, -1, -1);
554 $zonesecs = -$zonesecs;
558 $z .= sprintf("%02d%02d", int($zonesecs/60), $zonesecs % 60);
563 # Take a list of project names and produce a nicely formated table that
564 # includes owner links and descriptions. If the list is empty returns ''.
565 # The first argument may be a hash ref that contains options. The following
566 # options are available:
567 # target -- sets the target value of the owner link
568 # emptyok -- if true returns an empty table rather than ''
569 # sizecol -- if true include a human-readable size column
570 # typecol -- if true include type column with hover info
571 # changed -- if true include a changed and idle column
572 sub projects_html_list
{
574 if (defined($_[0]) && ref($_[0]) eq 'HASH') {
577 return '' unless @_ || (defined($options->{emptyok
}) && $options->{emptyok
});
578 require Girocco
::Project
;
581 $target = " target=\""._escapeHTML
($options->{target
})."\""
582 if defined($options->{target
});
583 my $withsize = defined($options->{sizecol
}) && $options->{sizecol
};
584 my $withtype = defined($options->{typecol
}) && $options->{typecol
};
585 my $withchanged = defined($options->{changed
}) && $options->{changed
};
587 $sizehead = substr(<<EOT, 0, -1) if $withsize;
588 <th class="sizecol"><span class="hover">Size<span><span class="head" _data="Size"></span
589 /><span class="none" /><br />(</span>Fork size excludes objects borrowed from the parent.<span class="none">)</span></span></span></th
593 $typehead = '<th>Type</th>' if $withtype;
595 $chghead = substr(<<EOT, 0, -1) if $withchanged;
596 <th><span class="hover">Changed<span><span class="head" _data="Changed"></span
597 /><span class="none" /><br />(</span>The last time a ref change was received by this site.<span class="none">)</span></span></span></th
598 ><th><span class="hover">Idle<span><span class="head" _data="Idle"></span
599 /><span class="none" /><br />(</span>The most recent committer time in <i>refs/heads</i>.<span class="none">)</span></span></span></th
603 <table class='projectlist'><tr valign="top" align="left"><th>Project</th>$sizehead$typehead$chghead<th class="desc">Description</th></tr>
605 my $trclass = ' class="odd"';
606 foreach (sort({lc($a) cmp lc($b)} @_)) {
607 if (Girocco
::Project
::does_exist
($_, 1)) {
608 my $proj = Girocco
::Project
->load($_);
609 my $projname = $proj->{name
}.".git";
610 my $projdesc = $proj->{desc
}||'';
611 utf8
::decode
($projdesc) if utf8
::valid
($projdesc);
614 my $psize = $proj->{reposizek
};
615 $psize = undef unless defined($psize) && $psize =~ /^\d+$/;
616 $psize = 0 if !defined($psize) && $proj->is_empty;
617 if (!defined($psize)) {
622 $psize = human_size
($psize * 1024);
623 $psize =~ s/ /\ /g;
625 $sizecol = '<td class="sizecol">'.$psize.'</td>';
629 if ($proj->{mirror
}) {
630 my $url = _escapeHTML
($proj->{url
});
631 $typecol = substr(<<EOT, 0, -1);
632 <td class="type"><span class="hover">mirror<span class="nowrap"><span class="before" _data="$url"><span class="none"> <a href="$url" rel="nofollow">(URL)</a></span></span></span></span></td>
635 my $users = @
{$proj->{users
}};
637 $users .= 's' unless @
{$proj->{users
}} == 1;
638 my $userlist = join(', ', sort({lc($a) cmp lc($b)} @
{$proj->{users
}}));
639 my $spncls = length($userlist) > 25 ?
'' : ' class="nowrap"';
640 $typecol = $userlist ?
substr(<<EOT, 0, -1) : substr(<<EOT, 0, -1);
641 <td
class="type"><span
class="hover">$users<span
$spncls><br
class="none" />$userlist</span
></span></td
>
643 <td
class="type">$users</td
>
650 my $changetime = $proj->{lastchange
};
653 $ts = parse_rfc2822_date
($changetime, \
$tz);
654 my $ct = _strftime
("%Y-%m-%d %T %z", $ts, $tz);
655 $rel = "<span class=\"hover\">" .
656 _rel_age
(time - $ts) .
657 "<span class=\"nowrap\"><span class=\"before\" _data=\"$changetime\"></span><span class=\"none\"><br />$ct</span></span></span>";
661 $changecol = substr(<<EOT, 0, -1);
662 <td class="change">$rel</td>
664 my $idletime = $proj->{lastactivity
};
666 $idlesecs = parse_any_date
($idletime, \
$tz) if $idletime;
668 my $idle2822 = _strftime
("%a, %d %b %Y %T %z", $idlesecs, $tz);
669 my $ct = _strftime
("%Y-%m-%d %T %z", $idlesecs, $tz);
670 $rel = "<span class=\"hover\">" .
671 _rel_idle
(time - $idlesecs) .
672 "<span class=\"nowrap\"><span class=\"before\" _data=\"$idle2822\"></span><span class=\"none\"><br />$ct</span></span></span>";
676 $changecol .= substr(<<EOT, 0, -1);
677 <td class="idle">$rel</td>
681 <tr valign="top"$trclass><td><a href="@{[url_path($Girocco::Config::gitweburl)]}/$projname"$target
682 >@{[_escapeHTML($projname)]}</td>$sizecol$typecol$changecol<td>@{[_escapeHTML($projdesc)]}</td></tr>
684 $trclass = $trclass ?
'' : ' class="odd"';
691 return ($count || (defined($options->{emptyok
}) && $options->{emptyok
})) ?
$html : '';
697 jan
=> 0, feb
=> 1, mar
=> 2, apr
=> 3, may
=> 4, jun
=> 5,
698 jul
=> 6, aug
=> 7, sep
=> 8, oct => 9, nov
=> 10, dec
=> 11
702 # Should be in "date '+%a, %d %b %Y %T %z'" format as saved to lastgc, lastrefresh and lastchange
703 # The leading "%a, " is optional, returns undef if unrecognized date. This is also known as
704 # RFC 2822 date format and git's '%cD', '%aD' and --date=rfc2822 format.
705 # If the second argument is a SCALAR ref, its value will be set to the TZ offset in seconds
706 sub parse_rfc2822_date
{
707 my $dstr = shift || '';
708 my $tzoff = shift || '';
709 $dstr = $1 if $dstr =~/^[^\s]+,\s*(.*)$/;
710 return undef unless $dstr =~
711 /^\s*(\d{1,2})\s+([A-Za-z]{3})\s+(\d{4})\s+(\d{1,2}):(\d{2}):(\d{2})\s+([+-]\d{4})\s*$/;
712 my ($d,$b,$Y,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7);
713 my $m = $_month_names{lc($b)};
714 return undef unless defined($m);
715 my $seconds = timegm
(0+$S, 0+$M, 0+$H, 0+$d, 0+$m, $Y-1900);
716 my $offset = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
717 $offset = -$offset if substr($z,0,1) eq '-';
718 $$tzoff = $offset if ref($tzoff) eq 'SCALAR';
719 return $seconds - $offset;
722 # Will parse any supported date format. Actually there are three formats
723 # currently supported:
724 # 1. RFC 2822 (uses parse_rfc2822_date)
725 # 2. RFC 3339 / ISO 8601 (T may be ' ' or '_', 'Z' is optional or may be 'UTC', ':' optional in TZ)
726 # 3. Same as #2 except no colons or hyphens allowed and hours MUST be 2 digits
727 # 4. unix seconds since epoch with optional +/- trailing TZ (may not have a ':')
728 # Returns undef if unsupported date.
729 # If the second argument is a SCALAR ref, its value will be set to the TZ offset in seconds
731 my $dstr = shift || '';
732 my $tzoff = shift || '';
733 if ($dstr =~ /^\s*([-+]?\d+)(?:\s+([-+]\d{4}))?\s*$/) {
739 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
740 $off = -$off if substr($z,0,1) eq '-';
742 $$tzoff = $off if ref($tzoff) eq 'SCALAR';
745 if ($dstr =~ /^\s*(\d{4})-(\d{2})-(\d{2})[Tt _](\d{1,2}):(\d{2}):(\d{2})(?:[ _]?([Zz]|[Uu][Tt][Cc]|(?:[-+]\d{1,2}:?\d{2})))?\s*$/ ||
746 $dstr =~ /^\s*(\d{4})(\d{2})(\d{2})[Tt _](\d{2})(\d{2})(\d{2})(?:[ _]?([Zz]|[Uu][Tt][Cc]|(?:[-+]\d{2}\d{2})))?\s*$/) {
747 my ($Y,$m,$d,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7||'');
748 my $seconds = timegm
(0+$S, 0+$M, 0+$H, 0+$d, $m-1, $Y-1900);
749 defined($z) && $z ne '' or $z = 'Z';
752 substr($z,1,0) = '0' if length($z) == 4;
754 if ($z ne 'Z' && $z ne 'UTC') {
755 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
756 $off = -$off if substr($z,0,1) eq '-';
758 $$tzoff = $off if ref($tzoff) eq 'SCALAR';
759 return $seconds - $off;
761 return parse_rfc2822_date
($dstr, $tzoff);
764 # Input is a number such as a minute interval
765 # Return value is a random number between the input and 1.25*input
766 # This can be used to randomize the update and gc operations a bit to avoid
767 # having them all end up all clustered together
769 my $input = shift || 0;
770 return $input unless $input;
771 return $input + int(rand(0.25 * $input));
774 # Open a pipe to a new sendmail process. The '-i' option is always passed to
775 # the new process followed by any addtional arguments passed in. Note that
776 # the sendmail process is only expected to understand the '-i', '-t' and '-f'
777 # options. Using any other options via this function is not guaranteed to work.
778 # A list of recipients may follow the options. Combining a list of recipients
779 # with the '-t' option is not recommended.
781 return undef unless @_;
782 die "\$Girocco::Config::sendmail_bin is unset or not executable!\n"
783 unless $Girocco::Config
::sendmail_bin
&& -x
$Girocco::Config
::sendmail_bin
;
784 my $result = open(my $pipe, '|-', $Girocco::Config
::sendmail_bin
, '-i', @_);
785 return $result ?
$pipe : undef;
788 # Open a pipe that works similarly to a mailer such as /usr/bin/mail in that
789 # if the first argument is '-s', a subject line will be automatically added
790 # (using the second argument as the subject). Any remaining arguments are
791 # expected to be recipient addresses that will be added to an explicit To:
792 # line as well as passed on to sendmail_pipe. In addition an
793 # "Auto-Submitted: auto-generated" header is always added as well as a suitable
797 if (@_ >= 2 && $_[0] eq '-s') {
801 my $tolist = join(", ", @_);
802 unshift(@_, '-f', $Girocco::Config
::sender
) if $Girocco::Config
::sender
;
803 my $pipe = sendmail_pipe
(@_);
805 print $pipe "From: \"$Girocco::Config::name\" ",
806 "($Girocco::Config::title) ",
807 "<$Girocco::Config::admin>\n";
808 print $pipe "To: $tolist\n";
809 print $pipe "Subject: $subject\n" if defined($subject);
810 print $pipe "MIME-Version: 1.0\n";
811 print $pipe "Content-Type: text/plain; charset=utf-8\n";
812 print $pipe "Content-Transfer-Encoding: 8bit\n";
813 print $pipe "X-Girocco: $Girocco::Config::gitweburl\n"
814 unless $Girocco::Config
::suppress_x_girocco
;
815 print $pipe "Auto-Submitted: auto-generated\n";
823 return undef unless defined($val);
824 $val =~ s/[\r\n]+$//s;
825 return undef unless $val =~ /^\d+$/;
827 return undef unless $val >= 1;
831 # Returns the number of "online" cpus or undef if undetermined
833 my @confcpus = $^O
eq "linux" ?
834 qw(_NPROCESSORS_ONLN NPROCESSORS_ONLN) :
835 qw(NPROCESSORS_ONLN _NPROCESSORS_ONLN) ;
836 my $cpus = _goodval
(get_cmd
('getconf', $confcpus[0]));
837 return $cpus if $cpus;
838 $cpus = _goodval
(get_cmd
('getconf', $confcpus[1]));
839 return $cpus if $cpus;
840 if ($^O
ne "linux") {
841 my @sysctls = qw(hw.ncpu);
842 unshift(@sysctls, qw(hw.availcpu)) if $^O
eq "darwin";
843 foreach my $mib (@sysctls) {
844 $cpus = _goodval
(get_cmd
('sysctl', '-n', $mib));
845 return $cpus if $cpus;
851 # Returns the system page size in bytes or undef if undetermined
852 # This should never fail on a POSIX system
854 use POSIX
":unistd_h";
855 my $pagesize = sysconf
(_SC_PAGESIZE
);
856 return undef unless defined($pagesize) && $pagesize =~ /^\d+$/;
857 $pagesize = 0 + $pagesize;
858 return undef unless $pagesize >= 256;
862 # Returns the amount of available physical memory in bytes
863 # This may differ from the actual amount of physical memory installed
864 # Returns undef if this cannot be determined
866 my $pagesize = sys_pagesize
;
867 if ($pagesize && $^O
eq "linux") {
868 my $pages = _goodval
(get_cmd
('getconf', '_PHYS_PAGES'));
869 return $pagesize * $pages if $pages;
871 if ($^O
ne "linux") {
872 my @sysctls = qw(hw.physmem64);
873 unshift(@sysctls, qw(hw.memsize)) if $^O
eq "darwin";
874 foreach my $mib (@sysctls) {
875 my $memsize = _goodval
(get_cmd
('sysctl', '-n', $mib));
876 return $memsize if $memsize;
878 my $memsize32 = _goodval
(get_cmd
('sysctl', '-n', 'hw.physmem'));
879 return $memsize32 if $memsize32 && $memsize32 <= 2147483647;
881 my $pages = _goodval
(get_cmd
('sysctl', '-n', 'hw.availpages'));
882 return $pagesize * $pages if $pages;
884 return 2147483647 + 1 if $memsize32;
889 sub _get_max_conf_suffixed_size
{
891 return undef unless defined $conf && $conf =~ /^(\d+)([kKmMgG]?)$/;
892 my ($val, $suffix) = (0+$1, lc($2));
893 $val *= 1024 if $suffix eq 'k';
894 $val *= 1024 * 1024 if $suffix eq 'm';
895 $val *= 1024 * 1024 * 1024 if $suffix eq 'g';
899 sub _make_suffixed_size
{
901 return $size if $size % 1024;
903 return "${size}k" if $size % 1024;
905 return "${size}m" if $size % 1024;
910 # Return the value to pass to --window-memory= for git repack
911 # If the system memory or number of CPUs cannot be determined, returns "1g"
912 # Otherwise returns one third the available memory divided by the number of CPUs
913 # but never more than 1 gigabyte or max_gc_window_memory_size.
914 sub calc_windowmemory
{
915 my $cpus = online_cpus
;
916 my $memsize = sys_memsize
;
917 my $max = 1024 * 1024 * 1024;
918 if ($cpus && $memsize) {
919 $max = int($memsize / 3 / $cpus);
920 $max = 1024 * 1024 * 1024 if $max >= 1024 * 1024 * 1024;
922 my $maxconf = _get_max_conf_suffixed_size
($Girocco::Config
::max_gc_window_memory_size
);
923 $max = $maxconf if defined($maxconf) && $maxconf && $max > $maxconf;
924 return _make_suffixed_size
($max);
927 # Return the value to set as core.bigFileThreshold for git repack
928 # If the system memory cannot be determined, returns "256m"
929 # Otherwise returns the available memory divided by 16
930 # but never more than 512 megabytes or max_gc_big_file_threshold_size.
931 sub calc_bigfilethreshold
{
932 my $memsize = sys_memsize
;
933 my $max = 256 * 1024 * 1024;
935 $max = int($memsize / 16);
936 $max = 512 * 1024 * 1024 if $max >= 512 * 1024 * 1024;
938 my $maxconf = _get_max_conf_suffixed_size
($Girocco::Config
::max_gc_big_file_threshold_size
);
939 $max = $maxconf if defined($maxconf) && $maxconf && $max > $maxconf;
940 return _make_suffixed_size
($max);
943 # Return the value to use when deciding whether or not to re-calculate object deltas
944 # If there are no more than this many objects then deltas will be recomputed in
945 # order to create more efficient pack files. The new_delta_threshold value
946 # is constrained to be at least 1000 * cpu cores and no more than 100000.
947 # The default is sys_memsize rounded up to the nearest multiple of 256 MB and
948 # then 5000 per 256 MB or 50000 if we cannot determine memory size but never
949 # more than 100000 or less than 1000 * cpu cores.
950 sub calc_redeltathreshold
{
951 my $cpus = online_cpus
|| 1;
952 if (defined($Girocco::Config
::new_delta_threshold
) &&
953 $Girocco::Config
::new_delta_threshold
=~ /^\d+/) {
954 my $ndt = 0 + $Girocco::Config
::new_delta_threshold
;
955 if ($ndt >= $cpus * 1000) {
956 return $ndt <= 100000 ?
$ndt : 100000;
960 my $memsize = sys_memsize
;
962 my $quantum = 256 * 1024 * 1024;
963 $calcval = 5000 * int(($memsize + ($quantum - 1)) / $quantum);
964 $calcval = 1000 * $cpus if $calcval < 1000 * $cpus;
965 $calcval = 100000 if $calcval > 100000;
970 # $1 => thing to test
971 # $2 => optional directory, if given and -e "$2/$1$3", then return false
972 # $3 => optional, defaults to ''
973 sub has_reserved_suffix
{
974 no warnings
; # avoid silly 'unsuccessful stat on filename with \n' warning
975 my ($name, $dir, $ext) = @_;
976 $ext = '' unless defined $ext;
977 return 0 unless defined $name && $name =~ /\.([^.]+)$/;
978 return 0 unless exists $Girocco::Config
::reserved_suffixes
{lc($1)};
979 return 0 if defined $dir && -e
"$dir/$name$ext";
983 # mostly undoes effect of `use CGI::Carp qw(fatalsToBrowser);`
984 # mostly undoes effect of `use CGI::Carp qw(warningsToBrowser);`
985 sub noFatalsToBrowser
{
986 delete $SIG{__DIE__
};
987 delete $SIG{__WARN__
};
988 undef *CORE
::GLOBAL
::die;
989 *CORE
::GLOBAL
::die = sub {
991 my $ec = $! || ($?
>> 8) || 255;
992 my (undef, $fn, $li) = caller(0);
993 my $loc = " at " . $fn . " line " . $li . ".\n";
995 $msg = join("", @_) if @_;
996 $msg = "Died" if $msg eq "";
997 $msg .= $loc unless $msg =~ /\n$/;
999 printf STDERR
"%s", $msg;
1002 undef *CORE
::GLOBAL
::warn;
1003 *CORE
::GLOBAL
::warn = sub {
1005 my (undef, $fn, $li) = caller(0);
1006 my $loc = " at " . $fn . " line " . $li . ".\n";
1008 $msg = join("", @_) if @_;
1009 $msg = "Warning: something's wrong" if $msg eq "";
1010 $msg .= $loc unless $msg =~ /\n$/;
1011 printf STDERR
"%s", $msg;
1015 # mimics Git's symref reading but only for HEAD
1016 # returns undef on failure or if HEAD is not a symbolic ref
1017 sub read_HEAD_symref
{
1018 my $headpath = $_[0] . "/HEAD";
1020 my $rl = readlink($headpath);
1021 return defined($rl) && $rl =~ m
,^refs
/., ?
$rl : undef;
1023 open my $fd, '<', $headpath or return undef;
1030 defined($hv) or return undef;
1032 return $hv =~ m
,^ref:\s
*(refs
/.+)$, ?
$1 : undef;
1045 $_[0] =~ s/\\([btn\042\\])/$escvals{$1}/g;
1050 # mimics Git's config.c git_parse_source function behavior
1051 # returns array of arrayref of key and value
1052 # except that valueless booleans have a value of undef
1053 sub read_config_file
{
1055 my ($fn, $warn) = @_;
1059 open my $fh, '<', $fn or
1060 $warn && warn("could not open \"$fn\": $!\n"), return(undef);
1064 warn "bad config line $li in file $fn\n" if $warn;
1071 s/^\x{feff}// if $li == 1;
1074 if (/\G([.a-zA-Z0-9-]+)\]/gc) {
1075 $section = lc($1) . ".";
1076 } elsif (/\G([.a-zA-Z0-9-]*)\s+"((?:[^\042\\\n]|\\.)*)"\]/gc) {
1077 $section = lc($1) . "." .
1078 &{sub{my $x=shift; $x =~ s/\\(.)/$1/g; $x}}($2) . ".";
1084 next if /\G(?:[;#]|$)/;
1085 if (/\G([a-zA-Z][a-zA-Z0-9-]*)[ \t]*/gc) {
1086 my $k = $section . lc($1);
1090 } elsif (/\G=\s*/gc) {
1098 if (!$qt && /\G((?:[^"\\\n;#]|\\[btn"\\])+)/gc) {
1105 $v .= &$cf_unesc($a);
1106 } elsif ($qt && /\G((?:[^"\\\n]|\\[btn"\\])+)/gc) {
1108 $v .= &$cf_unesc($a);
1109 } elsif (/\G\042/gc) {
1111 } elsif (!$qt && /\G[;#]/gc) {
1118 $_ = to_utf8
($_, 1);
1119 /^\s+/gc unless $v ne "" || $qt;
1131 push(@vals, [$k, $v]);
1140 # Same as read_config_file except that a hashref is returned and
1141 # subsequent same-key-name values replace earlier ones.
1142 # Also valueless booleans are given the value 1
1143 sub read_config_file_hash
{
1144 my $result = read_config_file
(@_);
1145 return undef unless defined($result);
1146 my %config = map {($$_[0], defined($$_[1])?
$$_[1]:1)} @
$result;
1150 # similar to Git's test except that GIT_OBJECT_DIRECTORY is ignored
1153 defined($gd) && $gd ne "" && -d
$gd or return undef;
1154 -d
"$gd/objects" && -x
"$gd/objects" or return 0;
1155 -d
"$gd/refs" && -x
"$gd/refs" or return 0;
1156 if (-l
"$gd/HEAD") {
1157 my $rl = readlink("$gd/HEAD");
1158 defined($rl) && $rl =~ m
,^refs
/., or return 0;
1159 -e
"$gd/HEAD" or return 1;
1161 open my $fd, '<', "$gd/HEAD" or return 0;
1168 defined $hv or return 0;
1170 $hv =~ m
,^ref:\s
*refs
/., and return 1;
1171 return $hv =~ /^[0-9a-f]{40}/;
1174 # Returns 0 for false, 1 for true, undef for unrecognized or undef
1175 # Unless the optional second argument is true in which case undef returns 1
1177 defined($_[0]) or return $_[1] ?
1 : undef;
1179 return 0 if $v eq 'false' || $v eq 'off' || $v eq 'no' || $v eq '' || $v =~ /^[-+]?0+$/;
1180 return 1 if $v eq 'true' || $v eq 'on' || $v eq 'yes' || $v =~ /^[-+]?0*[1-9][0-9]*$/;
1184 # Returns a PATH properly prefixed which guarantees that Git is found and the
1185 # basedir/bin utilities are found as intended. $ENV{PATH} is LEFT UNCHANGED!
1186 # Caller is responsible for assigning result to $ENV{PATH} or otherwise
1187 # arranging for it to be used. If $ENV{PATH} already has the proper prefix
1188 # then it's returned as-is (making this function idempotent).
1189 # Will die if it cannot determine a suitable full PATH.
1190 # Result is cached so all calls after the first are practically free.
1191 my $var_git_exec_path;
1193 if (!defined($var_git_exec_path)) {
1194 defined($Girocco::Config
::basedir
) && $Girocco::Config
::basedir
ne "" &&
1195 -d
$Girocco::Config
::basedir
&& -r _
&& -x _
or
1196 die "invalid \$Girocco::Config::basedir setting: $Girocco::Config::basedir\n";
1197 my $varsfile = $Girocco::Config
::basedir
. "/shlib_vars.sh";
1198 if (-f
$varsfile && -r _
) {
1200 if (open $vars, '<', $varsfile) {
1201 # last value for var_git_exec_path wins
1204 substr($_, 0, 19) eq "var_git_exec_path=\"" or next;
1205 substr($_, -1, 1) eq "\"" or next;
1206 my $xd = substr($_, 19, -1);
1207 $var_git_exec_path = $xd if -d
$xd && -r _
&& -x _
;
1212 if (!defined($var_git_exec_path)) {
1213 my $xd = get_git
("--exec-path");
1214 $var_git_exec_path = $xd if defined($xd) &&
1215 (chomp $xd, $xd) ne "" && -d
$xd && -r _
&& -x _
;
1217 defined($var_git_exec_path) or
1218 die "could not determine \$(git --exec-path) value\n"
1220 my $prefix = "$var_git_exec_path:$Girocco::Config::basedir/bin:";
1221 if (substr($ENV{PATH
}, 0, length($prefix)) eq $prefix) {
1224 return $prefix . $ENV{PATH
};
1228 # Note that Perl performs a "shellish" test in the Perl_do_exec3 function from doio.c,
1229 # but it has slightly different semantics in that whitespace does not automatically
1230 # make something "shellish". The semantics used here more closely match Git's
1231 # semantics so that Girocco will provide an interpretation more similar to Git's.
1233 return unless defined(local $_ = shift);
1234 return 1 if m
#[][\$&*(){}'";:=\\|?<>~`\#\s]#; # contains metacharacters
1235 return 0; # probably not shellish