#!/usr/bin/perl # Author : Damien KROTKINE (damien@tuxfamily.org) # # Contributors : # # Copyright (C) 2002 Damien KROTKINE (damien@tuxfamily.org) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. package Libconf::System::MDK::Network; use strict; use vars qw(@ISA); use Libconf::Glueconf; use Libconf::Libconf; #use Libconf::Glueconf::XF86Config::Wrapper; @ISA = qw(Libconf); sub new { my ($pkg) = @_; tie my %h_interfaces, 'Libconf::System::MDK::Network::InterfacesWrapper', '/etc/sysconfig/network-scripts', 'ifcfg-*', 'shell'; bless { general => new Libconf::Glueconf::Shell('/etc/sysconfig/network'), interfaces => \%h_interfaces, }, $pkg; } package Libconf::Glueconf::Shell::DirectoryWrapper; sub debug { $ENV{DEBUG} and print @_ } sub deglob { my ($obj, $key) = @_; my $name = $obj->{glob}; $name =~ s/\*/$key/g; $name; } sub TIEHASH { my ($pkg, $directory, $glob, $template) = @_; debug "Wrapper - TIEHASH\n"; if (-e $directory) { -d $directory or die "$directory is not a directory"; } else { mkdir $directory; } bless { directory => $directory, glob => $glob, template => $template, }, $pkg; } sub CLEAR { my ($obj) = @_; debug "Wrapper - CLEAR\n"; unlink(glob($obj->{directory} . '/' . $obj->{glob})); } sub DELETE { my ($obj, $key) = @_; debug "Wrapper - DELETE - key: $key\n"; unlink($obj->{directory} . '/' . deglob($obj, $key)); } sub FIRSTKEY { my ($obj) = @_; debug "Wrapper - FIRSTKEY\n"; (glob($obj->{directory} . '/' . $obj->{glob}))[0] } sub EXISTS { my ($obj, $key) = @_; debug "Wrapper - EXISTS - key : $key\n"; -f $obj->{directory} . "/" . deglob($obj, $key); } sub NEXTKEY { my ($obj, $lastkey) = @_; my ($directory, $glob) = ($obj->{glob}, $obj->{directory}); my @files = glob($directory . '/' . $glob); $lastkey = deglob($obj, $key); foreach (0..@files-1) { if ($files[$_] eq $lastkey) { $files[$_+1] =~ s/^$directory\///; $glob =~ s/\*/(.*)?/; $files[$_+1] =~ /$glob/; return $&; } } undef; } sub STORE { my ($obj, $key, $value) = @_; debug "Wrapper - STORE - key : $key - value : $value\n"; my ($directory, $glob) = ($obj->{glob}, $obj->{directory}); ref($value) eq 'HASH' or die "trying to store a non hash in $key"; my $filename = $directory . '/' . deglob($obj, $key); -e $filename or utime undef, undef, $filename; #touch replacement my $struct = new Libconf::Glueconf::Shell($filename); %$struct = %$value; $struct->{libconf}->writeConf($filename); #FIXME, when should we write to the file? } sub FETCH { my ($obj, $key) = @_; my ($directory, $glob) = ($obj->{glob}, $obj->{directory}); my $filename = $directory . '/' . deglob($obj, $key); new Libconf::Glueconf::Shell($filename); } 1;