#!/usr/bin/perl use strict; use warnings; # objekt orientiert my @dirs=map{ directory->new($_) }qw( /home/shared /home/topeg /home/test /var/log); # erste zeile print ';'.join(';', map{$_->path()}@dirs )."\n"; # alle user finden my %users; for my $dir (@dirs) { for($dir->user_list()) { $users{$_->name()}=$_; } } # alle user Ausgeben for(sort(keys(%users))) { my $user=$users{$_}; my @list=map{ $user->has_dir($_) }@dirs; print $user->name().";".join(';',@list)."\n"; } ######################################################################## ######################################################################## ######################################################################## package directory; my $gids; sub new { my $class=shift(); my $dir=shift; $gids=gids->new() unless($gids); return undef unless(-d $dir); bless({dir=>$dir},$class); } sub path { shift()->{dir}; } sub gid { my $self=shift; return (stat($self->path()))[5] if(-d $self->path()); return undef; } sub user_list { my $self=shift; return $gids->user_list($self->gid()); } sub has_user { my $self=shift; my $user=shift; return 0 unless($user); return 0 unless($user->isa('user')); for($self->user_list()) { return 1 if($_->name() eq $user->name()); } return 0; } ######################################################################## package user; sub new { my $class=shift; my $name=shift; bless({name=>$name},$class); } sub name { shift()->{name}; } sub has_dir { my $self=shift; my $dir=shift; return 0 unless $dir->isa('directory'); my $path=$dir->path(); unless(exists($self->{dirs}->{$path})) { $self->{dirs}->{$path}=$dir->has_user($self); } return $self->{dirs}->{$path}; } ######################################################################## package gids; sub new { my $class=shift(); my $gfile=shift() || '/etc/group'; my $ufile=shift() || '/etc/passwd'; return bless({ufile=>$ufile, gfile=>$gfile, user=>{}},$class); } sub user_list { my $self=shift; my $gid=shift; $self->_read_groups(); return () unless(defined($gid)); return () unless(exists($self->{gids}->{$gid})); return @{$self->{gids}->{$gid}}; } sub user { my $self=shift; my $name=shift; unless($self->{user}->{$name}) { $self->{user}->{$name}=user->new($name); } return $self->{user}->{$name}; } # alle user zu einer gid sub _read_groups { my $self=shift; # update wenn nötig if(!$self->{update_g} || !$self->{update_u} || !$self->{gids} || $self->{update_g} < (stat($self->{gfile}))[9] || $self->{update_u} < (stat($self->{ufile}))[9] || ref($self->{gids}) ne 'HASH') { my %gid_user; if(-e $self->{ufile} && open(my $fh, '<', $self->{ufile})) { while(my $line=<$fh>) { chomp($line); my ($user,undef,$uid,$gid)=split(/:/,$line); $user=$self->user($user); push(@{$gid_user{$gid}},$user); } close($fh); $self->{update_u} = (stat($self->{ufile}))[9] } if(-e $self->{gfile} && open(my $fh, '<', $self->{gfile})) { while(my $line=<$fh>) { chomp($line); my ($gname,undef,$gid,$users)=split(/:/,$line); for my $name (split(/,/,$users)) { my $user=$self->user($name); push(@{$gid_user{$gid}}, $user); } } close($fh); $self->{update_g} = (stat($self->{gfile}))[9]; } $self->{gids}=\%gid_user; } }