summaryrefslogtreecommitdiff
path: root/usr/src/libpiny/lib/Piny/Repo.pm
blob: 26d936a06a0db194586d6dda756d761a1b4d797c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Copyright © 2010 Julian Blake Kongslie <jblake@omgwallhack.org>
# Licensed under the BSD 3-clause license.

package Piny::Repo;

use Moose;
use Moose::Util::TypeConstraints;

use File::Find qw( find );

use Piny::Group;
use Piny::User;

# Types

subtype 'Reponame'
  => as 'Str'
  => where { $_ =~ /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/ }
  => message { 'That name is not in the correct format for a piny repo.' }
  ;

subtype 'SimpleText'
  => as 'Str'
  => where { $_ =~ /^[\x{0020}-\x{FDCF}\x{FDF0}-\x{FFFD}]{1,80}$/ }
  => message { 'That description is not in the correct format for a piny repo.' }
  ;

# Attributes

has 'name' =>
  ( is        => 'rw'
  , isa       => 'Reponame'
  , trigger   => \&_rename_repo
  , required  => 1
  );

has 'group' =>
  ( is          => 'ro'
  , isa         => 'Piny::Group'
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'path' =>
  ( is          => 'ro'
  , isa         => 'Str'
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'description' =>
  ( is          => 'rw'
  , isa         => 'SimpleText'
  , trigger     => \&_set_description
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'repostat' =>
  ( is          => 'ro'
  , isa         => 'ArrayRef'
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'owner' =>
  ( is          => 'rw'
  , isa         => 'Piny::User'
  , trigger     => \&_change_owner
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'globally_readable' =>
  ( is          => 'ro'
  , isa         => 'Bool'
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'globally_writable' =>
  ( is          => 'ro'
  , isa         => 'Bool'
  , lazy_build  => 1
  , init_arg    => undef
  );

# Public methods

sub add_access {
  my ( $s, @users ) = @_;

  $s->group( )->add_member( @users );
};

sub remove_access {
  my ( $s, @users ) = @_;

  $s->group( )->remove_member( @users );
};

sub has_access {
  my ( $s, $user ) = @_;

  return $s->owner( )->uid( ) == $user->uid( ) or $user->has_group( $s->group( ) );
};

# 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: $!";

  $s->clear_path( );
};

sub _set_description {
  my ( $s, $new_description, $old_description ) = @_;

  return unless defined $old_description;

  open( my $fd, ">", $s->path( ) . "/description" ) or die "Unable to open " . $s->path( ) . "/description for writing: $!";
  print $fd $new_description;
  close( $fd ) or die "Error when closing " . $s->path( ) . "/description: $!";
};

sub _change_owner {
  my ( $s, $new_owner, $old_owner ) = @_;

  return unless defined $old_owner;

  find( { wanted => sub { chown( $new_owner->uid( ), -1, $_ ) or die "Couldn't chown $_: $!"; }, no_chdir => 1 }, $s->path( ) );
};

# Class methods

sub all_repos {
  my ( $class, $dir ) = @_;

  $dir = "/srv/git" unless defined $dir;

  my @ret;

  find( { wanted => sub { print "$_\n"; if ( /^[^.].*\.git$/ ) { $File::Find::prune = 1; push( @ret, $File::Find::name ); }; } }, $dir );

  @ret = map { print "$_ "; s/^\Q$dir\E\/?//; s/\.git$//; print "$_\n"; $class->new( name => $_ ); } @ret;

  return @ret;
};

# Builder methods

# If constructed with just one argument, then treat it as a repo name.
around BUILDARGS => sub {
  my ( $orig, $class ) = ( shift, shift );

  if ( @_ == 1 && ! ref $_[0] ) {
    return $class->$orig( name => $_[0] );
  } else {
    return $class->$orig( @_ );
  };
};

sub _build_group {
  my ( $s ) = @_;

  return Piny::Group->new( groupname => "git-" . $s->name( ) );
};

sub _build_path {
  my ( $s ) = @_;

  my $dir = "/srv/git/" . $s->name( ) . ".git";

  if ( -d $dir ) {
    return $dir;
  } else {
    die "Expected repo $dir does not exist!";
  };
};

sub _build_description {
  my ( $s ) = @_;

  open( my $d, "<", $s->path( ) . "/description" ) or die "Unable to open " . $s->path( ) . "/description: $!";
  my $desc;
  {
    local $/ = undef;
    $desc = <$d>;
  };
  close( $d );

  return $desc;
};

sub _build_repostat {
  my ( $s ) = @_;

  my @res = stat( $s->path( ) );
  die "stat( " . $s->path( ) . " ) failed: $!" unless @res;
  return \@res;
};

sub _build_owner {
  my ( $s ) = @_;

  my ( $uid ) = $s->repostat( )->[4];

  return Piny::User->new( uid => $uid );
};

sub _build_globally_readable {
  my ( $s ) = @_;

  return ( $s->repostat( )->[2] & 0444 ) == 0444;
};

sub _build_globally_writable {
  my ( $s ) = @_;

  return ( $s->repostat( )->[2] & 0111 ) == 0111;
};

# Moose boilerplate

__PACKAGE__->meta->make_immutable;

1;