--- lib/PXElator/client.pm 2009/08/11 15:55:26 200 +++ lib/PXElator/client.pm 2009/08/15 13:47:37 220 @@ -4,12 +4,37 @@ use strict; use autodie; -use server; use File::Slurp; use Net::Ping; +use Carp qw/confess/; + +use server; +use format; + +our $debug = $server::debug; + +sub mkbasedir { + my $path = shift; + $path =~ s{(^.*)/[^/]+$}{$1}; + mkdir $path unless -d $path; + return $path; +} sub mac_path { $server::conf . '/mac/' . $_[0] } sub ip_path { $server::conf . '/ip/' . join('/', @_) } +sub conf_value { + my $path = shift; + my $value; + if ( -l $path ) { + $value = readlink $path; + $value =~ s{.*/([^/]+)$}{$1}; + } elsif ( -f $path ) { + $value = read_file $path; + } else { + confess "$path not file or symlink"; + } + return $value; +} sub conf { my $ip = shift; @@ -26,34 +51,22 @@ $path .= '/' . $name; if ( defined $value ) { + mkbasedir $path; write_file $path, $value; warn "update $path = $value"; } elsif ( ! -e $path && defined $default ) { + mkbasedir $path; write_file $path, $default; warn "default $path = $default"; $value = $default; - } elsif ( -e $path ) { - if ( -l $path ) { - $value = readlink $path; - $value =~ s{.*/([^/]+)$}{$1}; - } else { - $value = read_file $path; - } + } elsif ( -f $path ) { + $value = read_file $path; + } else { + warn "# $name missing $path\n" if $debug; } return $value; } -sub mac { - my ( $ip, $op ) = @_; - $op ||= 'html'; - my $mac = client::conf( $ip, 'mac' ); - return '' unless $mac; - $mac =~ s{(..)}{$1:}g; - $mac =~ s{:$}{}; - $mac = qq|$mac| if (caller(1))[3] =~ m{^httpd} && $op ne 'clean'; - return uc($mac); -} - sub next_ip($) { my $mac = shift; @@ -74,7 +87,9 @@ mkdir ip_path($ip); - symlink ip_path($ip), mac_path($mac); + my $mac_path = mac_path($mac); + unlink $mac_path if -e $mac_path; # XXX audit? + symlink ip_path($ip), $mac_path; write_file ip_path($ip,'mac'), $mac; return $ip; @@ -98,8 +113,7 @@ symlink ip_path($ip), $mac_path; warn "I: upgrade to mac symlink $mac_path\n"; } elsif ( -l $mac_path ) { - $ip = readlink $mac_path; - $ip =~ s{^.+/([^/]+)$}{$1}; + $ip = conf_value $mac_path; } else { die "$mac_path not file or symlink"; } @@ -109,7 +123,7 @@ sub mac_from_ip($) { my $ip = shift; - return read_file ip_path($ip, 'mac'); + conf_value ip_path($ip, 'mac'); } sub change_ip($$) {