From eecdd9ae520972e7acadda374894059521daaebb Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Wed, 27 Apr 2011 18:02:32 -0700 Subject: Remove repo rename, add hashed shortnames for repos with too-long names. --- libpiny/lib/Piny/Repo.pm | 92 ++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 38 deletions(-) diff --git a/libpiny/lib/Piny/Repo.pm b/libpiny/lib/Piny/Repo.pm index 335843e..dc4e5c1 100644 --- a/libpiny/lib/Piny/Repo.pm +++ b/libpiny/lib/Piny/Repo.pm @@ -10,9 +10,11 @@ use Moose; use Moose::Util::TypeConstraints; use MooseX::StrictConstructor; +use Digest::SHA qw( sha256_hex ); use File::Find qw( find ); use File::Temp qw( ); use IO::Dir qw( ); +use Math::GMP qw( ); use IkiWiki::FakeSetup qw( readSetup writeSetup ); @@ -39,12 +41,18 @@ subtype 'SimpleText' # Attributes has 'name' => - ( is => 'rw' + ( is => 'ro' , isa => 'Reponame' - , trigger => \&_rename_repo , required => 1 ); +has 'shortname' => + ( is => 'ro' + , isa => 'Str' + , lazy_build => 1 + , init_arg => undef + ); + has 'group' => ( is => 'ro' , isa => 'Piny::Group' @@ -203,18 +211,18 @@ sub has_access { sub rebuild { my ( $s ) = @_; - unless( getgrnam("git-" . $s->name ) ) { - system( "/usr/sbin/addgroup", "--quiet", "git-" . $s->name ) and die "Could not create repo group!"; - system( "/usr/sbin/adduser", "--quiet", $s->owner->name, "git-" . $s->name ) and die "Could not add you to the repo group!"; - system( "/usr/sbin/adduser", "--quiet", "iki-" . $s->name, "git-" . $s->name ) and print "...But that's probably okay.\n"; + unless( getgrnam("git-" . $s->shortname ) ) { + system( "/usr/sbin/addgroup", "--quiet", "git-" . $s->shortname ) and die "Could not create repo group!"; + system( "/usr/sbin/adduser", "--quiet", $s->owner->name, "git-" . $s->shortname ) and die "Could not add you to the repo group!"; + system( "/usr/sbin/adduser", "--quiet", "iki-" . $s->shortname, "git-" . $s->shortname ) and print "...But that's probably okay.\n"; }; - unless( getpwnam("iki-" . $s->name ) ) { - system( "/usr/sbin/adduser", "--quiet", "--system", "--group", "--gecos", $s->name, "iki-" . $s->name ) and die "Could not create ikiwiki user!"; - system( "/usr/sbin/adduser", "--quiet", "iki-" . $s->name, "git-" . $s->name ) and die "Could not add ikiwiki user to the repo group!"; + unless( getpwnam("iki-" . $s->shortname ) ) { + system( "/usr/sbin/adduser", "--quiet", "--system", "--group", "--gecos", $s->shortname, "iki-" . $s->shortname ) and die "Could not create ikiwiki user!"; + system( "/usr/sbin/adduser", "--quiet", "iki-" . $s->shortname, "git-" . $s->shortname ) and die "Could not add ikiwiki user to the repo group!"; }; - my $ikiuser = Piny::User::IkiWiki->new( "name" => "iki-" . $s->name ); + my $ikiuser = Piny::User::IkiWiki->new( "name" => "iki-" . $s->shortname ); foreach( "git-daemon-export-ok", "packed-refs" ) { open( TOUCH, ">", $s->path . "/" . $_ ) or die "Could not touch $_ for repo: $!"; @@ -324,30 +332,6 @@ sub destroy { # Triggers -sub _rename_repo { - my ( $s, $new_name, $old_name ) = @_; - - return unless defined $old_name; - - my $olddir = "/srv/git/$old_name.git"; - my $newdir = "/srv/git/$new_name.git"; - - rename( $olddir, $newdir ) or die "Couldn't rename $olddir to $newdir: $!"; - - warn "XXX Not renaming all the ikiwiki stuff!"; - - $s->clear_path; - $s->clear_ikiwiki_setup; - $s->clear_ikiwiki_destdir; - $s->clear_ikiwiki_srcdir; - $s->clear_ikiwiki_url; - $s->clear_ikiwiki_cgiurl; - $s->clear_ikiwiki_historyurl; - $s->clear_ikiwiki_cgipath; - $s->clear_secure_path; - $s->clear_apache_config; -}; - sub _set_description { my ( $s, $new_description, $old_description ) = @_; @@ -392,13 +376,13 @@ sub create { mkdir( $repo->path ) or die "The repo $name appears to already exist! ($!)"; - system( "/usr/sbin/adduser", "--quiet", "--system", "--group", "--gecos", $name, "iki-$name" ) and die "Could not create ikiwiki user!"; + system( "/usr/sbin/adduser", "--quiet", "--system", "--group", "--gecos", $name, "iki-$repo->shortname" ) and die "Could not create ikiwiki user!"; - my $ikiuser = Piny::User::IkiWiki->new( "name" => "iki-$name" ); + my $ikiuser = Piny::User::IkiWiki->new( "name" => "iki-$repo->shortname" ); - system( "/usr/sbin/addgroup", "--quiet", "git-$name" ) and die "Could not create repo group!"; + system( "/usr/sbin/addgroup", "--quiet", "git-$repo->shortname" ) and die "Could not create repo group!"; - my $group = Piny::Group->new( "name" => "git-$name" ); + my $group = Piny::Group->new( "name" => "git-$repo->shortname" ); system( "/usr/sbin/adduser", "--quiet", $user->name, $group->name ) and die "Could not add you to the repo group!"; system( "/usr/sbin/adduser", "--quiet", $ikiuser->name, $group->name ) and die "Could not add ikiwiki user to the repo group!"; @@ -486,6 +470,38 @@ around BUILDARGS => sub { }; }; +my @letter1 = ( 'a' .. 'z', 'A' .. 'Z' ); # First digit is base 52. +my @lettern = ( '0' .. '9', 'a' .. 'z', 'A' .. 'Z', '-', '_' ); # Remaining digits are base 64. + +sub _build_shortname { + my ( $s ) = @_; + + my $str = $s->name; + + # Short inputs don't need to be hashed. + return $str if length $str <= 28; + + # We have to use Math::GMP here instead of Math::BigInt or pragma bignum + # support because we actually require infinite precision integer arithmetic, + # and bignum tends to only give us 40 decimal digits of precision during + # division because the Perl maintainers are jackasses. + my $n = Math::GMP->new( "0x" . sha256_hex( $str ) ); + + my ( $m ); + + # First digit. + ( $n, $m ) = $n->bdiv( scalar @letter1 ); + my $out = $letter1[$m]; + + # Remaining digits. + while ( length $out < 28 and $n > 0 ) { + ( $n, $m ) = $n->bdiv( scalar @lettern ); + $out .= $lettern[$m]; + }; + + return $out; +}; + sub _build_group { my ( $s ) = @_; -- cgit v1.2.3